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
{
46 * Hooks that are ignored
48 protected static $ignore = array( 'testRunLegacyHooks' );
50 public function __construct() {
51 parent
::__construct();
52 $this->mDescription
= 'Find hooks that are undocumented, missing, or just plain wrong';
53 $this->addOption( 'online', 'Check against MediaWiki.org hook documentation' );
56 public function getDbType() {
57 return Maintenance
::DB_NONE
;
60 public function execute() {
63 $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
67 // TODO: Don't hardcode the list of directories
71 $IP . '/includes/actions/',
72 $IP . '/includes/api/',
73 $IP . '/includes/cache/',
74 $IP . '/includes/changes/',
75 $IP . '/includes/changetags/',
76 $IP . '/includes/clientpool/',
77 $IP . '/includes/content/',
78 $IP . '/includes/context/',
79 $IP . '/includes/dao/',
80 $IP . '/includes/db/',
81 $IP . '/includes/debug/',
82 $IP . '/includes/deferred/',
83 $IP . '/includes/diff/',
84 $IP . '/includes/exception/',
85 $IP . '/includes/externalstore/',
86 $IP . '/includes/filebackend/',
87 $IP . '/includes/filerepo/',
88 $IP . '/includes/filerepo/file/',
89 $IP . '/includes/gallery/',
90 $IP . '/includes/htmlform/',
91 $IP . '/includes/installer/',
92 $IP . '/includes/interwiki/',
93 $IP . '/includes/jobqueue/',
94 $IP . '/includes/json/',
95 $IP . '/includes/logging/',
96 $IP . '/includes/mail/',
97 $IP . '/includes/media/',
98 $IP . '/includes/page/',
99 $IP . '/includes/parser/',
100 $IP . '/includes/password/',
101 $IP . '/includes/rcfeed/',
102 $IP . '/includes/resourceloader/',
103 $IP . '/includes/revisiondelete/',
104 $IP . '/includes/search/',
105 $IP . '/includes/site/',
106 $IP . '/includes/skins/',
107 $IP . '/includes/specialpage/',
108 $IP . '/includes/specials/',
109 $IP . '/includes/upload/',
110 $IP . '/includes/utils/',
112 $IP . '/maintenance/',
113 $IP . '/maintenance/language/',
115 $IP . '/tests/parser/',
116 $IP . '/tests/phpunit/suites/',
119 foreach ( $pathinc as $dir ) {
120 $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
121 $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
124 $potential = array_unique( $potential );
125 $bad = array_unique( $bad );
126 $todo = array_diff( $potential, $documented );
127 $deprecated = array_diff( $documented, $potential );
129 // let's show the results:
130 $this->printArray( 'Undocumented', $todo );
131 $this->printArray( 'Documented and not found', $deprecated );
132 $this->printArray( 'Unclear hook calls', $bad );
134 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 ) {
135 $this->output( "Looks good!\n" );
140 * Get the hook documentation, either locally or from MediaWiki.org
142 * @return array Array of documented hooks
144 private function getHooksFromDoc( $doc ) {
145 if ( $this->hasOption( 'online' ) ) {
146 return $this->getHooksFromOnlineDoc();
148 return $this->getHooksFromLocalDoc( $doc );
153 * Get hooks from a local file (for example docs/hooks.txt)
154 * @param string $doc Filename to look in
155 * @return array Array of documented hooks
157 private function getHooksFromLocalDoc( $doc ) {
159 $content = file_get_contents( $doc );
160 preg_match_all( "/\n'(.*?)':/", $content, $m );
162 return array_unique( $m[1] );
166 * Get hooks from www.mediawiki.org using the API
167 * @return array Array of documented hooks
169 private function getHooksFromOnlineDoc() {
170 $allhooks = $this->getHooksFromOnlineDocCategory( 'MediaWiki_hooks' );
171 $removed = $this->getHooksFromOnlineDocCategory( 'Removed_hooks' );
172 return array_diff( $allhooks, $removed );
176 * @param string $title
179 private function getHooksFromOnlineDocCategory( $title ) {
182 'list' => 'categorymembers',
183 'cmtitle' => "Category:$title",
191 $json = Http
::get( wfAppendQuery( 'http://www.mediawiki.org/w/api.php', $params ), array(), __METHOD__
);
192 $data = FormatJson
::decode( $json, true );
193 foreach ( $data['query']['categorymembers'] as $page ) {
194 if ( preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $m ) ) {
195 $retval[] = str_replace( ' ', '_', $m[1] );
198 if ( !isset( $data['continue'] ) ) {
201 $params = array_replace( $params, $data['continue'] );
206 * Get hooks from a PHP file
207 * @param string $file Full filename to the PHP file.
208 * @return array Array of hooks found
210 private function getHooksFromFile( $file ) {
211 $content = file_get_contents( $file );
214 '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/',
223 * Get hooks from the source code.
224 * @param string $path Directory where the include files can be found
225 * @return array Array of hooks found
227 private function getHooksFromPath( $path ) {
229 $dh = opendir( $path );
231 while ( ( $file = readdir( $dh ) ) !== false ) {
232 if ( filetype( $path . $file ) == 'file' ) {
233 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
243 * Get bad hooks (where the hook name could not be determined) from a PHP file
244 * @param string $file Full filename to the PHP file.
245 * @return array Array of bad wfRunHooks() lines
247 private function getBadHooksFromFile( $file ) {
248 $content = file_get_contents( $file );
250 # We want to skip the "function wfRunHooks()" one. :)
251 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
253 foreach ( $m[0] as $match ) {
254 $list[] = $match . "(" . $file . ")";
261 * Get bad hooks from the source code.
262 * @param string $path Directory where the include files can be found
263 * @return array Array of bad wfRunHooks() lines
265 private function getBadHooksFromPath( $path ) {
267 $dh = opendir( $path );
269 while ( ( $file = readdir( $dh ) ) !== false ) {
270 # We don't want to read this file as it contains bad calls to wfRunHooks()
271 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__
) {
272 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
282 * Nicely output the array
283 * @param string $msg A message to show before the value
285 * @param bool $sort Whether to sort the array (Default: true)
287 private function printArray( $msg, $arr, $sort = true ) {
292 foreach ( $arr as $v ) {
293 if ( !in_array( $v, self
::$ignore ) ) {
294 $this->output( "$msg: $v\n" );
300 $maintClass = 'FindHooks';
301 require_once RUN_MAINTENANCE_IF_MAIN
;