Show tags on deleted edits through the API
[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 $this->output( "Looks good!\n" );
108 }
109 }
110
111 /**
112 * Get the hook documentation, either locally or from MediaWiki.org
113 * @return array of documented hooks
114 */
115 private function getHooksFromDoc( $doc ) {
116 if ( $this->hasOption( 'online' ) ) {
117 return $this->getHooksFromOnlineDoc();
118 } else {
119 return $this->getHooksFromLocalDoc( $doc );
120 }
121 }
122
123 /**
124 * Get hooks from a local file (for example docs/hooks.txt)
125 * @param $doc string: filename to look in
126 * @return array of documented hooks
127 */
128 private function getHooksFromLocalDoc( $doc ) {
129 $m = array();
130 $content = file_get_contents( $doc );
131 preg_match_all( "/\n'(.*?)'/", $content, $m );
132 return array_unique( $m[1] );
133 }
134
135 /**
136 * Get hooks from www.mediawiki.org using the API
137 * @return array of documented hooks
138 */
139 private function getHooksFromOnlineDoc() {
140 // All hooks
141 $allhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' );
142 $allhookdata = unserialize( $allhookdata );
143 $allhooks = array();
144 foreach ( $allhookdata['query']['categorymembers'] as $page ) {
145 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
146 if ( $found ) {
147 $hook = str_replace( ' ', '_', $matches[1] );
148 $allhooks[] = $hook;
149 }
150 }
151 // Removed hooks
152 $oldhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' );
153 $oldhookdata = unserialize( $oldhookdata );
154 $removed = array();
155 foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
156 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
157 if ( $found ) {
158 $hook = str_replace( ' ', '_', $matches[1] );
159 $removed[] = $hook;
160 }
161 }
162 return array_diff( $allhooks, $removed );
163 }
164
165 /**
166 * Get hooks from a PHP file
167 * @param $file string Full filename to the PHP file.
168 * @return array of hooks found.
169 */
170 private function getHooksFromFile( $file ) {
171 $content = file_get_contents( $file );
172 $m = array();
173 preg_match_all( '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/', $content, $m );
174 return $m[2];
175 }
176
177 /**
178 * Get hooks from the source code.
179 * @param $path Directory where the include files can be found
180 * @return array of hooks found.
181 */
182 private function getHooksFromPath( $path ) {
183 $hooks = array();
184 $dh = opendir( $path );
185 if ( $dh ) {
186 while ( ( $file = readdir( $dh ) ) !== false ) {
187 if ( filetype( $path . $file ) == 'file' ) {
188 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
189 }
190 }
191 closedir( $dh );
192 }
193 return $hooks;
194 }
195
196 /**
197 * Get bad hooks (where the hook name could not be determined) from a PHP file
198 * @param $file string Full filename to the PHP file.
199 * @return array of bad wfRunHooks() lines
200 */
201 private function getBadHooksFromFile( $file ) {
202 $content = file_get_contents( $file );
203 $m = array();
204 # We want to skip the "function wfRunHooks()" one. :)
205 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
206 $list = array();
207 foreach ( $m[0] as $match ) {
208 $list[] = $match . "(" . $file . ")";
209 }
210 return $list;
211 }
212
213 /**
214 * Get bad hooks from the source code.
215 * @param $path Directory where the include files can be found
216 * @return array of bad wfRunHooks() lines
217 */
218 private function getBadHooksFromPath( $path ) {
219 $hooks = array();
220 $dh = opendir( $path );
221 if ( $dh ) {
222 while ( ( $file = readdir( $dh ) ) !== false ) {
223 # We don't want to read this file as it contains bad calls to wfRunHooks()
224 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
225 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
226 }
227 }
228 closedir( $dh );
229 }
230 return $hooks;
231 }
232
233 /**
234 * Nicely output the array
235 * @param $msg String: a message to show before the value
236 * @param $arr Array: an array
237 * @param $sort Boolean: whether to sort the array (Default: true)
238 */
239 private function printArray( $msg, $arr, $sort = true ) {
240 if ( $sort ) {
241 asort( $arr );
242 }
243 foreach ( $arr as $v ) {
244 $this->output( "$msg: $v\n" );
245 }
246 }
247 }
248
249 $maintClass = 'FindHooks';
250 require_once RUN_MAINTENANCE_IF_MAIN;