Merge "Split includes/Pager.php"
[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
67 // TODO: Don't hardcode the list of directories
68 $pathinc = array(
69 $IP . '/',
70 $IP . '/includes/',
71 $IP . '/includes/actions/',
72 $IP . '/includes/api/',
73 $IP . '/includes/cache/',
74 $IP . '/includes/changes/',
75 $IP . '/includes/clientpool/',
76 $IP . '/includes/content/',
77 $IP . '/includes/context/',
78 $IP . '/includes/dao/',
79 $IP . '/includes/db/',
80 $IP . '/includes/debug/',
81 $IP . '/includes/deferred/',
82 $IP . '/includes/diff/',
83 $IP . '/includes/externalstore/',
84 $IP . '/includes/filebackend/',
85 $IP . '/includes/filerepo/',
86 $IP . '/includes/filerepo/file/',
87 $IP . '/includes/gallery/',
88 $IP . '/includes/htmlform/',
89 $IP . '/includes/installer/',
90 $IP . '/includes/interwiki/',
91 $IP . '/includes/jobqueue/',
92 $IP . '/includes/json/',
93 $IP . '/includes/logging/',
94 $IP . '/includes/media/',
95 $IP . '/includes/page/',
96 $IP . '/includes/parser/',
97 $IP . '/includes/rcfeed/',
98 $IP . '/includes/resourceloader/',
99 $IP . '/includes/revisiondelete/',
100 $IP . '/includes/search/',
101 $IP . '/includes/site/',
102 $IP . '/includes/specialpage/',
103 $IP . '/includes/specials/',
104 $IP . '/includes/upload/',
105 $IP . '/includes/utils/',
106 $IP . '/languages/',
107 $IP . '/maintenance/',
108 $IP . '/maintenance/language/',
109 $IP . '/tests/',
110 $IP . '/tests/parser/',
111 $IP . '/tests/phpunit/suites/',
112 $IP . '/skins/',
113 $IP . '/skins/MonoBook/',
114 $IP . '/skins/Vector/',
115 );
116
117 foreach ( $pathinc as $dir ) {
118 $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
119 $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
120 }
121
122 $potential = array_unique( $potential );
123 $bad = array_unique( $bad );
124 $todo = array_diff( $potential, $documented );
125 $deprecated = array_diff( $documented, $potential );
126
127 // let's show the results:
128 $this->printArray( 'Undocumented', $todo );
129 $this->printArray( 'Documented and not found', $deprecated );
130 $this->printArray( 'Unclear hook calls', $bad );
131
132 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 ) {
133 $this->output( "Looks good!\n" );
134 }
135 }
136
137 /**
138 * Get the hook documentation, either locally or from MediaWiki.org
139 * @param string $doc
140 * @return array Array of documented hooks
141 */
142 private function getHooksFromDoc( $doc ) {
143 if ( $this->hasOption( 'online' ) ) {
144 return $this->getHooksFromOnlineDoc();
145 } else {
146 return $this->getHooksFromLocalDoc( $doc );
147 }
148 }
149
150 /**
151 * Get hooks from a local file (for example docs/hooks.txt)
152 * @param string $doc Filename to look in
153 * @return array Array of documented hooks
154 */
155 private function getHooksFromLocalDoc( $doc ) {
156 $m = array();
157 $content = file_get_contents( $doc );
158 preg_match_all( "/\n'(.*?)':/", $content, $m );
159
160 return array_unique( $m[1] );
161 }
162
163 /**
164 * Get hooks from www.mediawiki.org using the API
165 * @return array Array of documented hooks
166 */
167 private function getHooksFromOnlineDoc() {
168 // All hooks
169 $allhookdata = Http::get(
170 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&'
171 . 'cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php'
172 );
173 $allhookdata = unserialize( $allhookdata );
174 $allhooks = array();
175 foreach ( $allhookdata['query']['categorymembers'] as $page ) {
176 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
177 if ( $found ) {
178 $hook = str_replace( ' ', '_', $matches[1] );
179 $allhooks[] = $hook;
180 }
181 }
182 // Removed hooks
183 $oldhookdata = Http::get(
184 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&'
185 . 'cmtitle=Category:Removed_hooks&cmlimit=500&format=php'
186 );
187 $oldhookdata = unserialize( $oldhookdata );
188 $removed = array();
189 foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
190 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
191 if ( $found ) {
192 $hook = str_replace( ' ', '_', $matches[1] );
193 $removed[] = $hook;
194 }
195 }
196
197 return array_diff( $allhooks, $removed );
198 }
199
200 /**
201 * Get hooks from a PHP file
202 * @param string $file Full filename to the PHP file.
203 * @return array Array of hooks found
204 */
205 private function getHooksFromFile( $file ) {
206 $content = file_get_contents( $file );
207 $m = array();
208 preg_match_all(
209 '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/',
210 $content,
211 $m
212 );
213
214 return $m[2];
215 }
216
217 /**
218 * Get hooks from the source code.
219 * @param string $path Directory where the include files can be found
220 * @return array Array of hooks found
221 */
222 private function getHooksFromPath( $path ) {
223 $hooks = array();
224 $dh = opendir( $path );
225 if ( $dh ) {
226 while ( ( $file = readdir( $dh ) ) !== false ) {
227 if ( filetype( $path . $file ) == 'file' ) {
228 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
229 }
230 }
231 closedir( $dh );
232 }
233
234 return $hooks;
235 }
236
237 /**
238 * Get bad hooks (where the hook name could not be determined) from a PHP file
239 * @param string $file Full filename to the PHP file.
240 * @return array Array of bad wfRunHooks() lines
241 */
242 private function getBadHooksFromFile( $file ) {
243 $content = file_get_contents( $file );
244 $m = array();
245 # We want to skip the "function wfRunHooks()" one. :)
246 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
247 $list = array();
248 foreach ( $m[0] as $match ) {
249 $list[] = $match . "(" . $file . ")";
250 }
251
252 return $list;
253 }
254
255 /**
256 * Get bad hooks from the source code.
257 * @param string $path Directory where the include files can be found
258 * @return array Array of bad wfRunHooks() lines
259 */
260 private function getBadHooksFromPath( $path ) {
261 $hooks = array();
262 $dh = opendir( $path );
263 if ( $dh ) {
264 while ( ( $file = readdir( $dh ) ) !== false ) {
265 # We don't want to read this file as it contains bad calls to wfRunHooks()
266 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
267 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
268 }
269 }
270 closedir( $dh );
271 }
272
273 return $hooks;
274 }
275
276 /**
277 * Nicely output the array
278 * @param string $msg A message to show before the value
279 * @param array $arr
280 * @param bool $sort Whether to sort the array (Default: true)
281 */
282 private function printArray( $msg, $arr, $sort = true ) {
283 if ( $sort ) {
284 asort( $arr );
285 }
286
287 foreach ( $arr as $v ) {
288 if ( !in_array( $v, self::$ignore ) ) {
289 $this->output( "$msg: $v\n" );
290 }
291 }
292 }
293 }
294
295 $maintClass = 'FindHooks';
296 require_once RUN_MAINTENANCE_IF_MAIN;