(bug 12301) Allow maintenance/findhooks.php to search hooks in multiple directories...
[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 = array( $IP.'/includes/', $IP.'/includes/api/', $IP.'/includes/filerepo/', $IP.'/languages/', $IP.'/maintenance/', $IP.'/skins/' );
27
28 # FUNCTIONS
29
30 /**
31 * @return array of documented hooks
32 */
33 function getHooksFromDoc() {
34 global $doc;
35 $content = file_get_contents( $doc );
36 $m = array();
37 preg_match_all( "/\n'(.*?)'/", $content, $m);
38 return $m[1];
39 }
40
41 /**
42 * Get hooks from a PHP file
43 * @param $file Full filename to the PHP file.
44 * @return array of hooks found.
45 */
46 function getHooksFromFile( $file ) {
47 $content = file_get_contents( $file );
48 $m = array();
49 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
50 return $m[2];
51 }
52
53 /**
54 * Get hooks from the source code.
55 * @param $path Directory where the include files can be found
56 * @return array of hooks found.
57 */
58 function getHooksFromPath( $path ) {
59 $hooks = array();
60 if( $dh = opendir($path) ) {
61 while(($file = readdir($dh)) !== false) {
62 if( filetype($path.$file) == 'file' ) {
63 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
64 }
65 }
66 closedir($dh);
67 }
68 return $hooks;
69 }
70
71 /**
72 * Get bad hooks (where the hook name could not be determined) from a PHP file
73 * @param $file Full filename to the PHP file.
74 * @return array of bad wfRunHooks() lines
75 */
76 function getBadHooksFromFile( $file ) {
77 $content = file_get_contents( $file );
78 $m = array();
79 # We want to skip the "function wfRunHooks()" one. :)
80 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
81 $list = array();
82 foreach( $m[0] as $match ){
83 $list[] = $match . "(" . $file . ")";
84 }
85 return $list;
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 # We don't want to read this file as it contains bad calls to wfRunHooks()
98 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__ ) {
99 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
100 }
101 }
102 closedir($dh);
103 }
104 return $hooks;
105 }
106
107 /**
108 * Nicely output the array
109 * @param $msg A message to show before the value
110 * @param $arr An array
111 * @param $sort Boolean : wheter to sort the array (Default: true)
112 */
113 function printArray( $msg, $arr, $sort = true ) {
114 if($sort) asort($arr);
115 foreach($arr as $v) echo "$msg: $v\n";
116 }
117
118
119 # MAIN
120
121 $documented = getHooksFromDoc($doc);
122 $potential = array();
123 $bad = array();
124 foreach( $pathinc as $dir ) {
125 $potential = array_merge( $potential, getHooksFromPath( $dir ) );
126 $bad = array_merge( $bad, getBadHooksFromPath( $dir ) );
127 }
128
129 $potential = array_unique( $potential );
130 $bad = array_unique( $bad );
131 $todo = array_diff( $potential, $documented );
132 $deprecated = array_diff( $documented, $potential );
133
134 // let's show the results:
135 printArray('undocumented', $todo );
136 printArray('not found', $deprecated );
137 printArray('unclear hook calls', $bad );
138
139 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
140 echo "Looks good!\n";