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