Apparently for certain (API) requests $this->getTitle() doesn't return a valid Title.
[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 * Copyright © Antoine Musso
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * http://www.gnu.org/copyleft/gpl.html
31 *
32 * @file
33 * @ingroup Maintenance
34 * @author Antoine Musso <hashar at free dot fr>
35 */
36
37 require_once __DIR__ . '/Maintenance.php';
38
39 /**
40 * Maintenance script that compares documented and actually present mismatches.
41 *
42 * @ingroup Maintenance
43 */
44 class FindHooks extends Maintenance {
45 /*
46 * Hooks that are ignored
47 */
48 protected static $ignore = array( 'testRunLegacyHooks' );
49
50 public function __construct() {
51 parent::__construct();
52 $this->mDescription = 'Find hooks that are undocumented, missing, or just plain wrong';
53 $this->addOption( 'online', 'Check against MediaWiki.org hook documentation' );
54 }
55
56 public function getDbType() {
57 return Maintenance::DB_NONE;
58 }
59
60 public function execute() {
61 global $IP;
62
63 $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
64 $potential = array();
65 $bad = array();
66 $pathinc = array(
67 $IP . '/',
68 $IP . '/includes/',
69 $IP . '/includes/actions/',
70 $IP . '/includes/api/',
71 $IP . '/includes/cache/',
72 $IP . '/includes/changes/',
73 $IP . '/includes/clientpool/',
74 $IP . '/includes/content/',
75 $IP . '/includes/context/',
76 $IP . '/includes/dao/',
77 $IP . '/includes/db/',
78 $IP . '/includes/debug/',
79 $IP . '/includes/deferred/',
80 $IP . '/includes/diff/',
81 $IP . '/includes/externalstore/',
82 $IP . '/includes/filebackend/',
83 $IP . '/includes/filerepo/',
84 $IP . '/includes/filerepo/file/',
85 $IP . '/includes/gallery/',
86 $IP . '/includes/htmlform/',
87 $IP . '/includes/installer/',
88 $IP . '/includes/interwiki/',
89 $IP . '/includes/jobqueue/',
90 $IP . '/includes/json/',
91 $IP . '/includes/logging/',
92 $IP . '/includes/media/',
93 $IP . '/includes/parser/',
94 $IP . '/includes/rcfeed/',
95 $IP . '/includes/resourceloader/',
96 $IP . '/includes/revisiondelete/',
97 $IP . '/includes/search/',
98 $IP . '/includes/site/',
99 $IP . '/includes/specialpage/',
100 $IP . '/includes/specials/',
101 $IP . '/includes/upload/',
102 $IP . '/languages/',
103 $IP . '/maintenance/',
104 $IP . '/maintenance/language/',
105 $IP . '/tests/',
106 $IP . '/tests/parser/',
107 $IP . '/tests/phpunit/suites/',
108 $IP . '/skins/',
109 );
110
111 foreach ( $pathinc as $dir ) {
112 $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
113 $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
114 }
115
116 $potential = array_unique( $potential );
117 $bad = array_unique( $bad );
118 $todo = array_diff( $potential, $documented );
119 $deprecated = array_diff( $documented, $potential );
120
121 // let's show the results:
122 $this->printArray( 'Undocumented', $todo );
123 $this->printArray( 'Documented and not found', $deprecated );
124 $this->printArray( 'Unclear hook calls', $bad );
125
126 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 ) {
127 $this->output( "Looks good!\n" );
128 }
129 }
130
131 /**
132 * Get the hook documentation, either locally or from MediaWiki.org
133 * @return array of documented hooks
134 */
135 private function getHooksFromDoc( $doc ) {
136 if ( $this->hasOption( 'online' ) ) {
137 return $this->getHooksFromOnlineDoc();
138 } else {
139 return $this->getHooksFromLocalDoc( $doc );
140 }
141 }
142
143 /**
144 * Get hooks from a local file (for example docs/hooks.txt)
145 * @param string $doc filename to look in
146 * @return array Array of documented hooks
147 */
148 private function getHooksFromLocalDoc( $doc ) {
149 $m = array();
150 $content = file_get_contents( $doc );
151 preg_match_all( "/\n'(.*?)':/", $content, $m );
152
153 return array_unique( $m[1] );
154 }
155
156 /**
157 * Get hooks from www.mediawiki.org using the API
158 * @return array of documented hooks
159 */
160 private function getHooksFromOnlineDoc() {
161 // All hooks
162 $allhookdata = Http::get(
163 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&'
164 . 'cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php'
165 );
166 $allhookdata = unserialize( $allhookdata );
167 $allhooks = array();
168 foreach ( $allhookdata['query']['categorymembers'] as $page ) {
169 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
170 if ( $found ) {
171 $hook = str_replace( ' ', '_', $matches[1] );
172 $allhooks[] = $hook;
173 }
174 }
175 // Removed hooks
176 $oldhookdata = Http::get(
177 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&'
178 . 'cmtitle=Category:Removed_hooks&cmlimit=500&format=php'
179 );
180 $oldhookdata = unserialize( $oldhookdata );
181 $removed = array();
182 foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
183 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
184 if ( $found ) {
185 $hook = str_replace( ' ', '_', $matches[1] );
186 $removed[] = $hook;
187 }
188 }
189
190 return array_diff( $allhooks, $removed );
191 }
192
193 /**
194 * Get hooks from a PHP file
195 * @param string $file Full filename to the PHP file.
196 * @return array of hooks found.
197 */
198 private function getHooksFromFile( $file ) {
199 $content = file_get_contents( $file );
200 $m = array();
201 preg_match_all(
202 '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/',
203 $content,
204 $m
205 );
206
207 return $m[2];
208 }
209
210 /**
211 * Get hooks from the source code.
212 * @param string $path Directory where the include files can be found
213 * @return array Array of hooks found.
214 */
215 private function getHooksFromPath( $path ) {
216 $hooks = array();
217 $dh = opendir( $path );
218 if ( $dh ) {
219 while ( ( $file = readdir( $dh ) ) !== false ) {
220 if ( filetype( $path . $file ) == 'file' ) {
221 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
222 }
223 }
224 closedir( $dh );
225 }
226
227 return $hooks;
228 }
229
230 /**
231 * Get bad hooks (where the hook name could not be determined) from a PHP file
232 * @param string $file Full filename to the PHP file.
233 * @return array Array of bad wfRunHooks() lines
234 */
235 private function getBadHooksFromFile( $file ) {
236 $content = file_get_contents( $file );
237 $m = array();
238 # We want to skip the "function wfRunHooks()" one. :)
239 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
240 $list = array();
241 foreach ( $m[0] as $match ) {
242 $list[] = $match . "(" . $file . ")";
243 }
244
245 return $list;
246 }
247
248 /**
249 * Get bad hooks from the source code.
250 * @param string $path Directory where the include files can be found
251 * @return array Array of bad wfRunHooks() lines
252 */
253 private function getBadHooksFromPath( $path ) {
254 $hooks = array();
255 $dh = opendir( $path );
256 if ( $dh ) {
257 while ( ( $file = readdir( $dh ) ) !== false ) {
258 # We don't want to read this file as it contains bad calls to wfRunHooks()
259 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
260 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
261 }
262 }
263 closedir( $dh );
264 }
265
266 return $hooks;
267 }
268
269 /**
270 * Nicely output the array
271 * @param string $msg A message to show before the value
272 * @param array $arr
273 * @param bool $sort Whether to sort the array (Default: true)
274 */
275 private function printArray( $msg, $arr, $sort = true ) {
276 if ( $sort ) {
277 asort( $arr );
278 }
279
280 foreach ( $arr as $v ) {
281 if ( !in_array( $v, self::$ignore ) ) {
282 $this->output( "$msg: $v\n" );
283 }
284 }
285 }
286 }
287
288 $maintClass = 'FindHooks';
289 require_once RUN_MAINTENANCE_IF_MAIN;