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