Merge "Less false positives for MEDIATYPE_VIDEO"
[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 * @return array of documented hooks
140 */
141 private function getHooksFromDoc( $doc ) {
142 if ( $this->hasOption( 'online' ) ) {
143 return $this->getHooksFromOnlineDoc();
144 } else {
145 return $this->getHooksFromLocalDoc( $doc );
146 }
147 }
148
149 /**
150 * Get hooks from a local file (for example docs/hooks.txt)
151 * @param string $doc filename to look in
152 * @return array Array of documented hooks
153 */
154 private function getHooksFromLocalDoc( $doc ) {
155 $m = array();
156 $content = file_get_contents( $doc );
157 preg_match_all( "/\n'(.*?)':/", $content, $m );
158
159 return array_unique( $m[1] );
160 }
161
162 /**
163 * Get hooks from www.mediawiki.org using the API
164 * @return array of documented hooks
165 */
166 private function getHooksFromOnlineDoc() {
167 // All hooks
168 $allhookdata = Http::get(
169 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&'
170 . 'cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php'
171 );
172 $allhookdata = unserialize( $allhookdata );
173 $allhooks = array();
174 foreach ( $allhookdata['query']['categorymembers'] as $page ) {
175 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
176 if ( $found ) {
177 $hook = str_replace( ' ', '_', $matches[1] );
178 $allhooks[] = $hook;
179 }
180 }
181 // Removed hooks
182 $oldhookdata = Http::get(
183 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&'
184 . 'cmtitle=Category:Removed_hooks&cmlimit=500&format=php'
185 );
186 $oldhookdata = unserialize( $oldhookdata );
187 $removed = array();
188 foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
189 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
190 if ( $found ) {
191 $hook = str_replace( ' ', '_', $matches[1] );
192 $removed[] = $hook;
193 }
194 }
195
196 return array_diff( $allhooks, $removed );
197 }
198
199 /**
200 * Get hooks from a PHP file
201 * @param string $file Full filename to the PHP file.
202 * @return array of hooks found.
203 */
204 private function getHooksFromFile( $file ) {
205 $content = file_get_contents( $file );
206 $m = array();
207 preg_match_all(
208 '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/',
209 $content,
210 $m
211 );
212
213 return $m[2];
214 }
215
216 /**
217 * Get hooks from the source code.
218 * @param string $path Directory where the include files can be found
219 * @return array Array of hooks found.
220 */
221 private function getHooksFromPath( $path ) {
222 $hooks = array();
223 $dh = opendir( $path );
224 if ( $dh ) {
225 while ( ( $file = readdir( $dh ) ) !== false ) {
226 if ( filetype( $path . $file ) == 'file' ) {
227 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
228 }
229 }
230 closedir( $dh );
231 }
232
233 return $hooks;
234 }
235
236 /**
237 * Get bad hooks (where the hook name could not be determined) from a PHP file
238 * @param string $file Full filename to the PHP file.
239 * @return array Array of bad wfRunHooks() lines
240 */
241 private function getBadHooksFromFile( $file ) {
242 $content = file_get_contents( $file );
243 $m = array();
244 # We want to skip the "function wfRunHooks()" one. :)
245 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
246 $list = array();
247 foreach ( $m[0] as $match ) {
248 $list[] = $match . "(" . $file . ")";
249 }
250
251 return $list;
252 }
253
254 /**
255 * Get bad hooks from the source code.
256 * @param string $path Directory where the include files can be found
257 * @return array Array of bad wfRunHooks() lines
258 */
259 private function getBadHooksFromPath( $path ) {
260 $hooks = array();
261 $dh = opendir( $path );
262 if ( $dh ) {
263 while ( ( $file = readdir( $dh ) ) !== false ) {
264 # We don't want to read this file as it contains bad calls to wfRunHooks()
265 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
266 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
267 }
268 }
269 closedir( $dh );
270 }
271
272 return $hooks;
273 }
274
275 /**
276 * Nicely output the array
277 * @param string $msg A message to show before the value
278 * @param array $arr
279 * @param bool $sort Whether to sort the array (Default: true)
280 */
281 private function printArray( $msg, $arr, $sort = true ) {
282 if ( $sort ) {
283 asort( $arr );
284 }
285
286 foreach ( $arr as $v ) {
287 if ( !in_array( $v, self::$ignore ) ) {
288 $this->output( "$msg: $v\n" );
289 }
290 }
291 }
292 }
293
294 $maintClass = 'FindHooks';
295 require_once RUN_MAINTENANCE_IF_MAIN;