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