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