3 * Simple script that try to find documented hook and hooks actually
4 * in the code and show what's missing.
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.
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
13 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
15 * Copyright © Antoine Musso
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.
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.
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
33 * @ingroup Maintenance
34 * @author Antoine Musso <hashar at free dot fr>
37 require_once __DIR__
. '/Maintenance.php';
40 * Maintenance script that compares documented and actually present mismatches.
42 * @ingroup Maintenance
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' );
51 public function getDbType() {
52 return Maintenance
::DB_NONE
;
55 public function execute() {
58 $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
64 $IP . '/includes/actions/',
65 $IP . '/includes/api/',
66 $IP . '/includes/cache/',
67 $IP . '/includes/changes/',
68 $IP . '/includes/clientpool/',
69 $IP . '/includes/content/',
70 $IP . '/includes/context/',
71 $IP . '/includes/dao/',
72 $IP . '/includes/db/',
73 $IP . '/includes/debug/',
74 $IP . '/includes/deferred/',
75 $IP . '/includes/diff/',
76 $IP . '/includes/externalstore/',
77 $IP . '/includes/filebackend/',
78 $IP . '/includes/filerepo/',
79 $IP . '/includes/filerepo/file/',
80 $IP . '/includes/gallery/',
81 $IP . '/includes/htmlform/',
82 $IP . '/includes/installer/',
83 $IP . '/includes/interwiki/',
84 $IP . '/includes/job/',
85 $IP . '/includes/json/',
86 $IP . '/includes/logging/',
87 $IP . '/includes/media/',
88 $IP . '/includes/parser/',
89 $IP . '/includes/rcfeed/',
90 $IP . '/includes/resourceloader/',
91 $IP . '/includes/revisiondelete/',
92 $IP . '/includes/search/',
93 $IP . '/includes/site/',
94 $IP . '/includes/specialpage/',
95 $IP . '/includes/specials/',
96 $IP . '/includes/upload/',
98 $IP . '/maintenance/',
99 $IP . '/maintenance/language/',
101 $IP . '/tests/parser/',
102 $IP . '/tests/phpunit/suites/',
106 foreach ( $pathinc as $dir ) {
107 $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
108 $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
111 $potential = array_unique( $potential );
112 $bad = array_unique( $bad );
113 $todo = array_diff( $potential, $documented );
114 $deprecated = array_diff( $documented, $potential );
116 // let's show the results:
117 $this->printArray( 'Undocumented', $todo );
118 $this->printArray( 'Documented and not found', $deprecated );
119 $this->printArray( 'Unclear hook calls', $bad );
121 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 ) {
122 $this->output( "Looks good!\n" );
127 * Get the hook documentation, either locally or from MediaWiki.org
128 * @return array of documented hooks
130 private function getHooksFromDoc( $doc ) {
131 if ( $this->hasOption( 'online' ) ) {
132 return $this->getHooksFromOnlineDoc();
134 return $this->getHooksFromLocalDoc( $doc );
139 * Get hooks from a local file (for example docs/hooks.txt)
140 * @param $doc string: filename to look in
141 * @return array of documented hooks
143 private function getHooksFromLocalDoc( $doc ) {
145 $content = file_get_contents( $doc );
146 preg_match_all( "/\n'(.*?)'/", $content, $m );
147 return array_unique( $m[1] );
151 * Get hooks from www.mediawiki.org using the API
152 * @return array of documented hooks
154 private function getHooksFromOnlineDoc() {
156 $allhookdata = Http
::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' );
157 $allhookdata = unserialize( $allhookdata );
159 foreach ( $allhookdata['query']['categorymembers'] as $page ) {
160 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
162 $hook = str_replace( ' ', '_', $matches[1] );
167 $oldhookdata = Http
::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' );
168 $oldhookdata = unserialize( $oldhookdata );
170 foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
171 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
173 $hook = str_replace( ' ', '_', $matches[1] );
177 return array_diff( $allhooks, $removed );
181 * Get hooks from a PHP file
182 * @param $file string Full filename to the PHP file.
183 * @return array of hooks found.
185 private function getHooksFromFile( $file ) {
186 $content = file_get_contents( $file );
188 preg_match_all( '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/', $content, $m );
193 * Get hooks from the source code.
194 * @param $path Directory where the include files can be found
195 * @return array of hooks found.
197 private function getHooksFromPath( $path ) {
199 $dh = opendir( $path );
201 while ( ( $file = readdir( $dh ) ) !== false ) {
202 if ( filetype( $path . $file ) == 'file' ) {
203 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
212 * Get bad hooks (where the hook name could not be determined) from a PHP file
213 * @param $file string Full filename to the PHP file.
214 * @return array of bad wfRunHooks() lines
216 private function getBadHooksFromFile( $file ) {
217 $content = file_get_contents( $file );
219 # We want to skip the "function wfRunHooks()" one. :)
220 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
222 foreach ( $m[0] as $match ) {
223 $list[] = $match . "(" . $file . ")";
229 * Get bad hooks from the source code.
230 * @param $path Directory where the include files can be found
231 * @return array of bad wfRunHooks() lines
233 private function getBadHooksFromPath( $path ) {
235 $dh = opendir( $path );
237 while ( ( $file = readdir( $dh ) ) !== false ) {
238 # We don't want to read this file as it contains bad calls to wfRunHooks()
239 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__
) {
240 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
249 * Nicely output the array
250 * @param $msg String: a message to show before the value
251 * @param $arr Array: an array
252 * @param $sort Boolean: whether to sort the array (Default: true)
254 private function printArray( $msg, $arr, $sort = true ) {
258 foreach ( $arr as $v ) {
259 $this->output( "$msg: $v\n" );
264 $maintClass = 'FindHooks';
265 require_once RUN_MAINTENANCE_IF_MAIN
;