links_filter.module 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * A module to display blocks selected by terms in a taxonomy vocabulary,
  4. * as long as they are newer than a set limit
  5. *
  6. * The module defines one block for each term in the vocabulary.
  7. *
  8. * Settings allows the administrator to choose
  9. * - the vocabulary to be used to select nodes
  10. * - the number of days nodes are considered for display
  11. *
  12. * Blocks without matching nodes do not appear.
  13. *
  14. * This file contains the drupal "glue"
  15. *
  16. * @author Frederic G. MARAND
  17. * @version CVS: $Id: taxonews.module,v 1.10.6.9 2008/06/15 21:19:36 fgm Exp $
  18. * @copyright 2005-2008 Ouest Systèmes Informatiques (OSI)
  19. * @license http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
  20. * @link http://drupal.org/project/taxonews
  21. * @since Version 1.1
  22. * @package taxonews
  23. */
  24. class Links_Filter
  25. {
  26. const PATH_SETTINGS = 'admin/settings/links_filter';
  27. static public function adminSettings()
  28. {
  29. $form =array();
  30. return $form;
  31. }
  32. }
  33. /**
  34. * check requirements
  35. * PHP 6 not yet tested either
  36. */
  37. list($major, $minor, $patch) = explode('.', PHP_VERSION);
  38. if (($major != 5) || ($minor == 0))
  39. {
  40. die('Links_filter requires PHP &ge; 5.1. You are using PHP ' . PHP_VERSION);
  41. }
  42. unset($major, $minor, $patch);
  43. $_links_filterErrorReporting = error_reporting(E_ALL | E_STRICT);
  44. /**
  45. * Until a more generic way to use class methods as callbacks is available
  46. * or included in core
  47. *
  48. * @param string $method The name of the method to be invoked
  49. * @return mixed Returns the result of the invoked method
  50. */
  51. function _links_filter_form_rerouter($method)
  52. {
  53. $args = func_get_args();
  54. $ret = call_user_func_array(array('Links_filter', $method), $args);
  55. return $ret;
  56. }
  57. function _links_filter_process($text)
  58. {
  59. $arBacktrace = debug_backtrace();
  60. $log = NULL;
  61. watchdog('links_filter', var_export($arBacktrace, true), WATCHDOG_NOTICE);
  62. if (count($arBacktrace) >= 7)
  63. {
  64. $bt5 = $arBacktrace[5];
  65. $bt6 = $arBacktrace[6];
  66. if ($bt6['function'] = 'node_build_content') // viewing or indexing a node: log its nid/vid
  67. {
  68. //dsm("Viewing nid " . $bt6['args'][0]->nid . ' vid ' . $bt6['args'][0]->vid);
  69. $log = array(0, 'node', $bt6['args'][0]->nid, $bt6['args'][0]->vid);
  70. }
  71. elseif ($bt5['function'] == 'block_block' && $bt5['args'][0] == 'view')
  72. {
  73. //dsm("Viewing custom block delta " . $bt5['args'][1]);
  74. $log = array(0, 'block', $bt5['args'][1], 0);
  75. }
  76. }
  77. if (!empty($log))
  78. {
  79. $sqCount = 'SELECT count(lfl.lfl_id) '
  80. . 'FROM {lf_links} lfl '
  81. . "WHERE /* id %d */ lfl.module = '%s' AND lfl.key1 = %d AND lfl.key2 = %d "
  82. . " AND lfl.url = '%s' ";
  83. /*
  84. * This is an admin-only module, so we do NO access control
  85. */
  86. $arMatches = array();
  87. // @todo PREG can probably be improved
  88. $matchCount = preg_match_all('@<a\s.*href="(.*)".*>@iU', $text, $arMatches);
  89. if ($matchCount > 0)
  90. {
  91. foreach ($arMatches[1] as $match)
  92. {
  93. $log[4] = $match;
  94. $count = db_result(db_query($sqCount, $log));
  95. if ($count == 0)
  96. {
  97. $newLid = db_next_id('link_filter_lfl_id');
  98. $log[0] = $newLid;
  99. $sqInsert = 'INSERT INTO {lf_links} '
  100. . '(lfl_id, module, key1, key2, url) '
  101. . " VALUES (%d, '%s', %d, %d, '%s') ";
  102. db_query($sqInsert, $log);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * -----------------------------------------------------------------------------
  110. * Drupal hooks below
  111. * -----------------------------------------------------------------------------
  112. */
  113. /**
  114. * Implement hook_help
  115. *
  116. * @param string $section
  117. * @return string HTML
  118. */
  119. function links_filter_help($section)
  120. {
  121. $ret = NULL;
  122. $help = t('<p>Store a list of links present in filtered content.</p>') ;
  123. switch ($section)
  124. {
  125. case 'admin/help#links_filter':
  126. $ret = $help
  127. . t('<p>More help later.</p>');
  128. break;
  129. case 'admin/modules#description':
  130. $ret = $help;
  131. break;
  132. }
  133. return $ret;
  134. }
  135. /**
  136. * Implement hook_block
  137. *
  138. * @param string $op list|view|configure|save
  139. * @param int $delta
  140. * @param array $edit
  141. * @return mixed either void or HTML for blocks/block list
  142. * @see Taxonews::blockList
  143. * @see Taxonews::blockView
  144. */
  145. function links_filter_block($op = 'list', $delta = 0, $edit = array())
  146. {
  147. $ret = NULL;
  148. switch ($op)
  149. {
  150. case 'list': break;
  151. case 'view': break;
  152. case 'configure': break;
  153. case 'save': break;
  154. default: break;
  155. }
  156. return $ret;
  157. }
  158. /**
  159. * Implement hook_menu
  160. *
  161. * @param boolean $may_cache
  162. * @return array
  163. */
  164. function links_filter_menu($may_cache)
  165. {
  166. if ($may_cache)
  167. {
  168. $items[] = array
  169. (
  170. 'title' => t('Links filter'),
  171. 'description' => t('Define the various parameters used by the links_filter module'),
  172. 'path' => Links_filter::PATH_SETTINGS,
  173. 'callback' => 'drupal_get_form',
  174. 'callback arguments' => array('_links_filter_form_rerouter', 'adminSettings'),
  175. 'access' => user_access('administer site configuration'),
  176. );
  177. }
  178. return $items;
  179. }
  180. function links_filter_filter_tips($delta, $format, $long = FALSE)
  181. {
  182. dsm(array("links_filter_filter_tips", 'delta' => $delta, 'format' => $format, 'long' => ($long ? 'True' : 'False')));
  183. $ret = 'Links in content are logged for later analytics. Does not modifiy content display';
  184. return $ret;
  185. }
  186. function links_filter_filter($op, $delta = 0, $format = -1, $text = '')
  187. {
  188. //dsm(array('lf_filter', 'op' => $op, 'delta' => $delta, 'format' => $format, 'text' => check_plain($text)));
  189. switch ($op)
  190. {
  191. case 'list': // provide a list of available filters. Returns an associative array of filter names with numerical keys. These keys are used for subsequent operations and passed back through the $delta parameter.
  192. $ret = array
  193. (
  194. 'Links filter',
  195. );
  196. break;
  197. case 'no cache': // Return true if caching should be disabled for this filter.
  198. $ret = TRUE;
  199. break;
  200. case 'description': // Return a short description of what this filter does.
  201. $ret = 'Catch links in content for analysis';
  202. break;
  203. case 'prepare': // Return the prepared version of the content in $text.
  204. $ret = $text;
  205. break;
  206. case 'process': // Return the processed version of the content in $text.
  207. _links_filter_process($text);
  208. return $text;
  209. break;
  210. case 'settings': // Return HTML form controls for the filter's settings.
  211. // These settings are stored with variable_set() when the form is submitted.
  212. // Remember to use the $format identifier in the variable and control names
  213. // to store settings per input format (e.g. "mymodule_setting_$format").
  214. $form = array();
  215. $form['boo'] = array
  216. (
  217. '#type' => 'markup',
  218. '#value' => '<p>Some markup</p>',
  219. );
  220. $ret = $form;
  221. break;
  222. }
  223. return $ret;
  224. }
  225. error_reporting($_links_filterErrorReporting);
  226. unset($_links_filterErrorReporting);