Allows findhooks to find these hooks:
[lhc/web/wiklou.git] / maintenance / findhooks.php
1 <?php
2 /**
3 * Simple script that try to find documented hook and hooks actually
4 * in the code and show what's missing.
5 *
6 * This script assumes that:
7 * - hooks names in hooks.txt are at the beginning of a line and single quoted.
8 * - hooks names in code are the first parameter of wfRunHooks.
9 *
10 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
11 *
12 * @addtogroup Maintenance
13 *
14 * @author Ashar Voultoiz <hashar@altern.org>
15 * @copyright Copyright © Ashar voultoiz
16 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
17 */
18
19 /** This is a command line script*/
20 include('commandLine.inc');
21
22
23 # GLOBALS
24
25 $doc = $IP . '/docs/hooks.txt';
26 $pathinc = $IP . '/includes/';
27 $pathfile = $IP . '/includes/filerepo/LocalFile.php';
28 $pathlang = $IP . '/languages/Language.php';
29 $pathskin = $IP . '/skins/MonoBook.php';
30
31
32 # FUNCTIONS
33
34 /**
35 * @return array of documented hooks
36 */
37 function getHooksFromDoc() {
38 global $doc;
39 $content = file_get_contents( $doc );
40 $m = array();
41 preg_match_all( "/\n'(.*?)'/", $content, $m);
42 return $m[1];
43 }
44
45 /**
46 * Get hooks from a PHP file
47 * @param $file Full filename to the PHP file.
48 * @return array of hooks found.
49 */
50 function getHooksFromFile( $file ) {
51 $content = file_get_contents( $file );
52 $m = array();
53 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
54 return $m[2];
55 }
56
57 /**
58 * Get hooks from the source code.
59 * @param $path Directory where the include files can be found
60 * @return array of hooks found.
61 */
62 function getHooksFromPath( $path ) {
63 $hooks = array();
64 if( $dh = opendir($path) ) {
65 while(($file = readdir($dh)) !== false) {
66 if( filetype($path.$file) == 'file' ) {
67 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
68 }
69 }
70 closedir($dh);
71 }
72 return $hooks;
73 }
74
75 /**
76 * Get bad hooks (where the hook name could not be determined) from a PHP file
77 * @param $file Full filename to the PHP file.
78 * @return array of bad wfRunHooks() lines
79 */
80 function getBadHooksFromFile( $file ) {
81 $content = file_get_contents( $file );
82 $m = array();
83 # We want to skip the "function wfRunHooks()" one. :)
84 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
85 return $m[0];
86 }
87
88 /**
89 * Get bad hooks from the source code.
90 * @param $path Directory where the include files can be found
91 * @return array of bad wfRunHooks() lines
92 */
93 function getBadHooksFromPath( $path ) {
94 $hooks = array();
95 if( $dh = opendir($path) ) {
96 while(($file = readdir($dh)) !== false) {
97 if( filetype($path.$file) == 'file' ) {
98 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
99 }
100 }
101 closedir($dh);
102 }
103 return $hooks;
104 }
105
106 /**
107 * Nicely output the array
108 * @param $msg A message to show before the value
109 * @param $arr An array
110 * @param $sort Boolean : wheter to sort the array (Default: true)
111 */
112 function printArray( $msg, $arr, $sort = true ) {
113 if($sort) asort($arr);
114 foreach($arr as $v) print "$msg: $v\n";
115 }
116
117
118 # MAIN
119
120 $documented = getHooksFromDoc($doc);
121
122 $potenial_inc = getHooksFromPath($pathinc);
123 $potential_file = getHooksFromFile($pathfile);
124 $potential_lang = getHooksFromFile($pathlang);
125 $potential_skin = getHooksFromFile($pathskin);
126
127 $bad_inc = getBadHooksFromPath($pathinc);
128 $bad_file = getBadHooksFromFile($pathfile);
129 $bad_lang = getBadHooksFromFile($pathlang);
130 $bad_skin = getBadHooksFromFile($pathskin);
131
132 $potential = array_merge($potenial_inc, $potential_file, $potential_lang, $potential_skin);
133 $bad = array_merge($bad_inc, $bad_file, $bad_lang, $bad_skin);
134
135 $todo = array_diff($potential, $documented);
136 $deprecated = array_diff($documented, $potential);
137
138 // let's show the results:
139 printArray('undocumented', $todo );
140 printArray('not found', $deprecated );
141 printArray('unclear hook calls', $bad );
142
143