Add .php5 entry for mwScriptLoader.
[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/search/',
37 $IP.'/includes/specials/',
38 $IP.'/includes/upload/',
39 $IP.'/languages/',
40 $IP.'/maintenance/',
41 $IP.'/skins/',
42 );
43
44 # FUNCTIONS
45
46 /**
47 * @return array of documented hooks
48 */
49 function getHooksFromDoc() {
50 global $doc, $options;
51 if( isset( $options['online'] ) ){
52 // All hooks
53 $allhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' );
54 $allhookdata = unserialize( $allhookdata );
55 $allhooks = array();
56 foreach( $allhookdata['query']['categorymembers'] as $page ) {
57 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
58 if( $found ) {
59 $hook = str_replace( ' ', '_', $matches[1] );
60 $allhooks[] = $hook;
61 }
62 }
63 // Removed hooks
64 $oldhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' );
65 $oldhookdata = unserialize( $oldhookdata );
66 $removed = array();
67 foreach( $oldhookdata['query']['categorymembers'] as $page ) {
68 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
69 if( $found ) {
70 $hook = str_replace( ' ', '_', $matches[1] );
71 $removed[] = $hook;
72 }
73 }
74 return array_diff( $allhooks, $removed );
75 } else {
76 $m = array();
77 $content = file_get_contents( $doc );
78 preg_match_all( "/\n'(.*?)'/", $content, $m );
79 return array_unique( $m[1] );
80 }
81 }
82
83 /**
84 * Get hooks from a PHP file
85 * @param $file Full filename to the PHP file.
86 * @return array of hooks found.
87 */
88 function getHooksFromFile( $file ) {
89 $content = file_get_contents( $file );
90 $m = array();
91 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
92 return $m[2];
93 }
94
95 /**
96 * Get hooks from the source code.
97 * @param $path Directory where the include files can be found
98 * @return array of hooks found.
99 */
100 function getHooksFromPath( $path ) {
101 $hooks = array();
102 if( $dh = opendir($path) ) {
103 while(($file = readdir($dh)) !== false) {
104 if( filetype($path.$file) == 'file' ) {
105 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
106 }
107 }
108 closedir($dh);
109 }
110 return $hooks;
111 }
112
113 /**
114 * Get bad hooks (where the hook name could not be determined) from a PHP file
115 * @param $file Full filename to the PHP file.
116 * @return array of bad wfRunHooks() lines
117 */
118 function getBadHooksFromFile( $file ) {
119 $content = file_get_contents( $file );
120 $m = array();
121 # We want to skip the "function wfRunHooks()" one. :)
122 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
123 $list = array();
124 foreach( $m[0] as $match ){
125 $list[] = $match . "(" . $file . ")";
126 }
127 return $list;
128 }
129
130 /**
131 * Get bad hooks from the source code.
132 * @param $path Directory where the include files can be found
133 * @return array of bad wfRunHooks() lines
134 */
135 function getBadHooksFromPath( $path ) {
136 $hooks = array();
137 if( $dh = opendir($path) ) {
138 while(($file = readdir($dh)) !== false) {
139 # We don't want to read this file as it contains bad calls to wfRunHooks()
140 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__ ) {
141 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
142 }
143 }
144 closedir($dh);
145 }
146 return $hooks;
147 }
148
149 /**
150 * Nicely output the array
151 * @param $msg A message to show before the value
152 * @param $arr An array
153 * @param $sort Boolean : wheter to sort the array (Default: true)
154 */
155 function printArray( $msg, $arr, $sort = true ) {
156 if($sort) asort($arr);
157 foreach($arr as $v) echo "$msg: $v\n";
158 }
159
160 # MAIN
161
162 $documented = getHooksFromDoc($doc);
163 $potential = array();
164 $bad = array();
165 foreach( $pathinc as $dir ) {
166 $potential = array_merge( $potential, getHooksFromPath( $dir ) );
167 $bad = array_merge( $bad, getBadHooksFromPath( $dir ) );
168 }
169
170 $potential = array_unique( $potential );
171 $bad = array_unique( $bad );
172 $todo = array_diff( $potential, $documented );
173 $deprecated = array_diff( $documented, $potential );
174
175 // let's show the results:
176 printArray('undocumented', $todo );
177 printArray('not found', $deprecated );
178 printArray('unclear hook calls', $bad );
179
180 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
181 echo "Looks good!\n";