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