1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
<?php
/**
* Simple script that try to find documented hook and hooks actually
* in the code and show what's missing.
*
* This script assumes that:
* - hooks names in hooks.txt are at the beginning of a line and single quoted.
* - hooks names in code are the first parameter of wfRunHooks.
*
* if --online option is passed, the script will compare the hooks in the code
* with the ones at http://www.mediawiki.org/wiki/Manual:Hooks
*
* Any instance of wfRunHooks that doesn't meet these parameters will be noted.
*
* Copyright © Antoine Musso
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Maintenance
* @author Antoine Musso <hashar at free dot fr>
*/
require_once __DIR__ . '/Maintenance.php';
/**
* Maintenance script that compares documented and actually present mismatches.
*
* @ingroup Maintenance
*/
class FindHooks extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = 'Find hooks that are undocumented, missing, or just plain wrong';
$this->addOption( 'online', 'Check against MediaWiki.org hook documentation' );
}
public function getDbType() {
return Maintenance::DB_NONE;
}
public function execute() {
global $IP;
$documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
$potential = array();
$bad = array();
$pathinc = array(
$IP . '/',
$IP . '/includes/',
$IP . '/includes/actions/',
$IP . '/includes/api/',
$IP . '/includes/cache/',
$IP . '/includes/content/',
$IP . '/includes/context/',
$IP . '/includes/db/',
$IP . '/includes/diff/',
$IP . '/includes/filerepo/',
$IP . '/includes/filerepo/file/',
$IP . '/includes/installer/',
$IP . '/includes/interwiki/',
$IP . '/includes/logging/',
$IP . '/includes/media/',
$IP . '/includes/parser/',
$IP . '/includes/resourceloader/',
$IP . '/includes/revisiondelete/',
$IP . '/includes/search/',
$IP . '/includes/specials/',
$IP . '/includes/upload/',
$IP . '/languages/',
$IP . '/maintenance/',
$IP . '/tests/',
$IP . '/tests/parser/',
$IP . '/tests/phpunit/suites/',
$IP . '/skins/',
);
foreach ( $pathinc as $dir ) {
$potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
$bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
}
$potential = array_unique( $potential );
$bad = array_unique( $bad );
$todo = array_diff( $potential, $documented );
$deprecated = array_diff( $documented, $potential );
// let's show the results:
$this->printArray( 'Undocumented', $todo );
$this->printArray( 'Documented and not found', $deprecated );
$this->printArray( 'Unclear hook calls', $bad );
if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
{
$this->output( "Looks good!\n" );
}
}
/**
* Get the hook documentation, either locally or from MediaWiki.org
* @return array of documented hooks
*/
private function getHooksFromDoc( $doc ) {
if ( $this->hasOption( 'online' ) ) {
return $this->getHooksFromOnlineDoc();
} else {
return $this->getHooksFromLocalDoc( $doc );
}
}
/**
* Get hooks from a local file (for example docs/hooks.txt)
* @param $doc string: filename to look in
* @return array of documented hooks
*/
private function getHooksFromLocalDoc( $doc ) {
$m = array();
$content = file_get_contents( $doc );
preg_match_all( "/\n'(.*?)'/", $content, $m );
return array_unique( $m[1] );
}
/**
* Get hooks from www.mediawiki.org using the API
* @return array of documented hooks
*/
private function getHooksFromOnlineDoc() {
// All hooks
$allhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' );
$allhookdata = unserialize( $allhookdata );
$allhooks = array();
foreach ( $allhookdata['query']['categorymembers'] as $page ) {
$found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
if ( $found ) {
$hook = str_replace( ' ', '_', $matches[1] );
$allhooks[] = $hook;
}
}
// Removed hooks
$oldhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' );
$oldhookdata = unserialize( $oldhookdata );
$removed = array();
foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
$found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
if ( $found ) {
$hook = str_replace( ' ', '_', $matches[1] );
$removed[] = $hook;
}
}
return array_diff( $allhooks, $removed );
}
/**
* Get hooks from a PHP file
* @param $file string Full filename to the PHP file.
* @return array of hooks found.
*/
private function getHooksFromFile( $file ) {
$content = file_get_contents( $file );
$m = array();
preg_match_all( '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/', $content, $m );
return $m[2];
}
/**
* Get hooks from the source code.
* @param $path Directory where the include files can be found
* @return array of hooks found.
*/
private function getHooksFromPath( $path ) {
$hooks = array();
$dh = opendir( $path );
if ( $dh ) {
while ( ( $file = readdir( $dh ) ) !== false ) {
if ( filetype( $path . $file ) == 'file' ) {
$hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
}
}
closedir( $dh );
}
return $hooks;
}
/**
* Get bad hooks (where the hook name could not be determined) from a PHP file
* @param $file string Full filename to the PHP file.
* @return array of bad wfRunHooks() lines
*/
private function getBadHooksFromFile( $file ) {
$content = file_get_contents( $file );
$m = array();
# We want to skip the "function wfRunHooks()" one. :)
preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
$list = array();
foreach ( $m[0] as $match ) {
$list[] = $match . "(" . $file . ")";
}
return $list;
}
/**
* Get bad hooks from the source code.
* @param $path Directory where the include files can be found
* @return array of bad wfRunHooks() lines
*/
private function getBadHooksFromPath( $path ) {
$hooks = array();
$dh = opendir( $path );
if ( $dh ) {
while ( ( $file = readdir( $dh ) ) !== false ) {
# We don't want to read this file as it contains bad calls to wfRunHooks()
if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
$hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
}
}
closedir( $dh );
}
return $hooks;
}
/**
* Nicely output the array
* @param $msg String: a message to show before the value
* @param $arr Array: an array
* @param $sort Boolean: whether to sort the array (Default: true)
*/
private function printArray( $msg, $arr, $sort = true ) {
if ( $sort ) {
asort( $arr );
}
foreach ( $arr as $v ) {
$this->output( "$msg: $v\n" );
}
}
}
$maintClass = 'FindHooks';
require_once RUN_MAINTENANCE_IF_MAIN;
|