Merge "Use a faster 7z command line by default"
[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/mail/',
95 $IP . '/includes/media/',
96 $IP . '/includes/page/',
97 $IP . '/includes/parser/',
98 $IP . '/includes/rcfeed/',
99 $IP . '/includes/resourceloader/',
100 $IP . '/includes/revisiondelete/',
101 $IP . '/includes/search/',
102 $IP . '/includes/site/',
103 $IP . '/includes/skins/',
104 $IP . '/includes/specialpage/',
105 $IP . '/includes/specials/',
106 $IP . '/includes/upload/',
107 $IP . '/includes/utils/',
108 $IP . '/languages/',
109 $IP . '/maintenance/',
110 $IP . '/maintenance/language/',
111 $IP . '/tests/',
112 $IP . '/tests/parser/',
113 $IP . '/tests/phpunit/suites/',
114 );
115
116 foreach ( $pathinc as $dir ) {
117 $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
118 $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
119 }
120
121 $potential = array_unique( $potential );
122 $bad = array_unique( $bad );
123 $todo = array_diff( $potential, $documented );
124 $deprecated = array_diff( $documented, $potential );
125
126 // let's show the results:
127 $this->printArray( 'Undocumented', $todo );
128 $this->printArray( 'Documented and not found', $deprecated );
129 $this->printArray( 'Unclear hook calls', $bad );
130
131 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 ) {
132 $this->output( "Looks good!\n" );
133 }
134 }
135
136 /**
137 * Get the hook documentation, either locally or from MediaWiki.org
138 * @param string $doc
139 * @return array 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 Array of documented hooks
165 */
166 private function getHooksFromOnlineDoc() {
167 $allhooks = $this->getHooksFromOnlineDocCategory( 'MediaWiki_hooks' );
168 $removed = $this->getHooksFromOnlineDocCategory( 'Removed_hooks' );
169 return array_diff( $allhooks, $removed );
170 }
171
172 /**
173 * @param string $title
174 * @return array
175 */
176 private function getHooksFromOnlineDocCategory( $title ) {
177 $params = array(
178 'action' => 'query',
179 'list' => 'categorymembers',
180 'cmtitle' => "Category:$title",
181 'cmlimit' => 500,
182 'format' => 'json',
183 'continue' => '',
184 );
185
186 $retval = array();
187 while ( true ) {
188 $json = Http::get( wfAppendQuery( 'http://www.mediawiki.org/w/api.php', $params ), array(), __METHOD__ );
189 $data = FormatJson::decode( $json, true );
190 foreach ( $data['query']['categorymembers'] as $page ) {
191 if ( preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $m ) ) {
192 $retval[] = str_replace( ' ', '_', $m[1] );
193 }
194 }
195 if ( !isset( $data['continue'] ) ) {
196 return $retval;
197 }
198 $params = array_replace( $params, $data['continue'] );
199 }
200 }
201
202 /**
203 * Get hooks from a PHP file
204 * @param string $file Full filename to the PHP file.
205 * @return array Array of hooks found
206 */
207 private function getHooksFromFile( $file ) {
208 $content = file_get_contents( $file );
209 $m = array();
210 preg_match_all(
211 '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/',
212 $content,
213 $m
214 );
215
216 return $m[2];
217 }
218
219 /**
220 * Get hooks from the source code.
221 * @param string $path Directory where the include files can be found
222 * @return array Array of hooks found
223 */
224 private function getHooksFromPath( $path ) {
225 $hooks = array();
226 $dh = opendir( $path );
227 if ( $dh ) {
228 while ( ( $file = readdir( $dh ) ) !== false ) {
229 if ( filetype( $path . $file ) == 'file' ) {
230 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
231 }
232 }
233 closedir( $dh );
234 }
235
236 return $hooks;
237 }
238
239 /**
240 * Get bad hooks (where the hook name could not be determined) from a PHP file
241 * @param string $file Full filename to the PHP file.
242 * @return array Array of bad wfRunHooks() lines
243 */
244 private function getBadHooksFromFile( $file ) {
245 $content = file_get_contents( $file );
246 $m = array();
247 # We want to skip the "function wfRunHooks()" one. :)
248 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
249 $list = array();
250 foreach ( $m[0] as $match ) {
251 $list[] = $match . "(" . $file . ")";
252 }
253
254 return $list;
255 }
256
257 /**
258 * Get bad hooks from the source code.
259 * @param string $path Directory where the include files can be found
260 * @return array Array of bad wfRunHooks() lines
261 */
262 private function getBadHooksFromPath( $path ) {
263 $hooks = array();
264 $dh = opendir( $path );
265 if ( $dh ) {
266 while ( ( $file = readdir( $dh ) ) !== false ) {
267 # We don't want to read this file as it contains bad calls to wfRunHooks()
268 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
269 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
270 }
271 }
272 closedir( $dh );
273 }
274
275 return $hooks;
276 }
277
278 /**
279 * Nicely output the array
280 * @param string $msg A message to show before the value
281 * @param array $arr
282 * @param bool $sort Whether to sort the array (Default: true)
283 */
284 private function printArray( $msg, $arr, $sort = true ) {
285 if ( $sort ) {
286 asort( $arr );
287 }
288
289 foreach ( $arr as $v ) {
290 if ( !in_array( $v, self::$ignore ) ) {
291 $this->output( "$msg: $v\n" );
292 }
293 }
294 }
295 }
296
297 $maintClass = 'FindHooks';
298 require_once RUN_MAINTENANCE_IF_MAIN;