add includes/upload/ to findhooks.php's path list
[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 * if --online option is passed, the script will compare the hooks in the code
11 * with the ones at http://www.mediawiki.org/wiki/Manual:Hooks
12 *
13 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
14 *
15 * @file
16 * @ingroup Maintenance
17 *
18 * @author Ashar Voultoiz <hashar@altern.org>
19 * @copyright Copyright © Ashar voultoiz
20 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
21 */
22
23 /** This is a command line script*/
24 require('commandLine.inc');
25 # GLOBALS
26
27 $doc = $IP . '/docs/hooks.txt';
28 $pathinc = array(
29 $IP.'/',
30 $IP.'/includes/',
31 $IP.'/includes/api/',
32 $IP.'/includes/db/',
33 $IP.'/includes/diff/',
34 $IP.'/includes/filerepo/',
35 $IP.'/includes/parser/',
36 $IP.'/includes/specials/',
37 $IP.'/includes/upload/',
38 $IP.'/languages/',
39 $IP.'/maintenance/',
40 $IP.'/skins/',
41 );
42
43 # FUNCTIONS
44
45 /**
46 * @return array of documented hooks
47 */
48 function getHooksFromDoc() {
49 global $doc, $options;
50 $m = array();
51 if( isset( $options['online'] ) ){
52 $content = Http::get( 'http://www.mediawiki.org/w/index.php?title=Manual:Hooks&action=raw' );
53 preg_match_all( '/\[\[\/([a-zA-Z0-9-_:]+)\|/', $content, $m );
54 } else {
55 $content = file_get_contents( $doc );
56 preg_match_all( "/\n'(.*?)'/", $content, $m );
57 }
58 return array_unique( $m[1] );
59 }
60
61 /**
62 * Get hooks from a PHP file
63 * @param $file Full filename to the PHP file.
64 * @return array of hooks found.
65 */
66 function getHooksFromFile( $file ) {
67 $content = file_get_contents( $file );
68 $m = array();
69 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
70 return $m[2];
71 }
72
73 /**
74 * Get hooks from the source code.
75 * @param $path Directory where the include files can be found
76 * @return array of hooks found.
77 */
78 function getHooksFromPath( $path ) {
79 $hooks = array();
80 if( $dh = opendir($path) ) {
81 while(($file = readdir($dh)) !== false) {
82 if( filetype($path.$file) == 'file' ) {
83 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
84 }
85 }
86 closedir($dh);
87 }
88 return $hooks;
89 }
90
91 /**
92 * Get bad hooks (where the hook name could not be determined) from a PHP file
93 * @param $file Full filename to the PHP file.
94 * @return array of bad wfRunHooks() lines
95 */
96 function getBadHooksFromFile( $file ) {
97 $content = file_get_contents( $file );
98 $m = array();
99 # We want to skip the "function wfRunHooks()" one. :)
100 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
101 $list = array();
102 foreach( $m[0] as $match ){
103 $list[] = $match . "(" . $file . ")";
104 }
105 return $list;
106 }
107
108 /**
109 * Get bad hooks from the source code.
110 * @param $path Directory where the include files can be found
111 * @return array of bad wfRunHooks() lines
112 */
113 function getBadHooksFromPath( $path ) {
114 $hooks = array();
115 if( $dh = opendir($path) ) {
116 while(($file = readdir($dh)) !== false) {
117 # We don't want to read this file as it contains bad calls to wfRunHooks()
118 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__ ) {
119 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
120 }
121 }
122 closedir($dh);
123 }
124 return $hooks;
125 }
126
127 /**
128 * Nicely output the array
129 * @param $msg A message to show before the value
130 * @param $arr An array
131 * @param $sort Boolean : wheter to sort the array (Default: true)
132 */
133 function printArray( $msg, $arr, $sort = true ) {
134 if($sort) asort($arr);
135 foreach($arr as $v) echo "$msg: $v\n";
136 }
137
138 # MAIN
139
140 $documented = getHooksFromDoc($doc);
141 $potential = array();
142 $bad = array();
143 foreach( $pathinc as $dir ) {
144 $potential = array_merge( $potential, getHooksFromPath( $dir ) );
145 $bad = array_merge( $bad, getBadHooksFromPath( $dir ) );
146 }
147
148 $potential = array_unique( $potential );
149 $bad = array_unique( $bad );
150 $todo = array_diff( $potential, $documented );
151 $deprecated = array_diff( $documented, $potential );
152
153 // let's show the results:
154 printArray('undocumented', $todo );
155 printArray('not found', $deprecated );
156 printArray('unclear hook calls', $bad );
157
158 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
159 echo "Looks good!\n";