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 © Ashar Voultoiz
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 Ashar Voultoiz <hashar at free dot fr>
37 require_once( dirname( __FILE__
) . '/Maintenance.php' );
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' );
46 public function getDbType() {
47 return Maintenance
::DB_NONE
;
50 public function execute() {
53 $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
59 $IP . '/includes/api/',
60 $IP . '/includes/db/',
61 $IP . '/includes/diff/',
62 $IP . '/includes/filerepo/',
63 $IP . '/includes/installer/',
64 $IP . '/includes/parser/',
65 $IP . '/includes/resourceloader/',
66 $IP . '/includes/search/',
67 $IP . '/includes/specials/',
68 $IP . '/includes/upload/',
70 $IP . '/maintenance/',
71 $IP . '/maintenance/tests/',
72 $IP . '/maintenance/tests/parser/',
73 $IP . '/maintenance/tests/phpunit/suites/',
77 foreach ( $pathinc as $dir ) {
78 $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
79 $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
82 $potential = array_unique( $potential );
83 $bad = array_unique( $bad );
84 $todo = array_diff( $potential, $documented );
85 $deprecated = array_diff( $documented, $potential );
87 // let's show the results:
88 $this->printArray( 'Undocumented', $todo );
89 $this->printArray( 'Documented and not found', $deprecated );
90 $this->printArray( 'Unclear hook calls', $bad );
92 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
93 $this->output( "Looks good!\n" );
97 * Get the hook documentation, either locally or from mediawiki.org
98 * @return array of documented hooks
100 private function getHooksFromDoc( $doc ) {
101 if ( $this->hasOption( 'online' ) ) {
103 $allhookdata = Http
::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' );
104 $allhookdata = unserialize( $allhookdata );
106 foreach ( $allhookdata['query']['categorymembers'] as $page ) {
107 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
109 $hook = str_replace( ' ', '_', $matches[1] );
114 $oldhookdata = Http
::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' );
115 $oldhookdata = unserialize( $oldhookdata );
117 foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
118 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
120 $hook = str_replace( ' ', '_', $matches[1] );
124 return array_diff( $allhooks, $removed );
127 $content = file_get_contents( $doc );
128 preg_match_all( "/\n'(.*?)'/", $content, $m );
129 return array_unique( $m[1] );
134 * Get hooks from a PHP file
135 * @param $file Full filename to the PHP file.
136 * @return array of hooks found.
138 private function getHooksFromFile( $file ) {
139 $content = file_get_contents( $file );
141 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m );
146 * Get hooks from the source code.
147 * @param $path Directory where the include files can be found
148 * @return array of hooks found.
150 private function getHooksFromPath( $path ) {
152 $dh = opendir( $path );
154 while ( ( $file = readdir( $dh ) ) !== false ) {
155 if ( filetype( $path . $file ) == 'file' ) {
156 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
165 * Get bad hooks (where the hook name could not be determined) from a PHP file
166 * @param $file Full filename to the PHP file.
167 * @return array of bad wfRunHooks() lines
169 private function getBadHooksFromFile( $file ) {
170 $content = file_get_contents( $file );
172 # We want to skip the "function wfRunHooks()" one. :)
173 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
175 foreach ( $m[0] as $match ) {
176 $list[] = $match . "(" . $file . ")";
182 * Get bad hooks from the source code.
183 * @param $path Directory where the include files can be found
184 * @return array of bad wfRunHooks() lines
186 private function getBadHooksFromPath( $path ) {
188 $dh = opendir( $path );
190 while ( ( $file = readdir( $dh ) ) !== false ) {
191 # We don't want to read this file as it contains bad calls to wfRunHooks()
192 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__
) {
193 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
202 * Nicely output the array
203 * @param $msg A message to show before the value
204 * @param $arr An array
205 * @param $sort Boolean : wheter to sort the array (Default: true)
207 private function printArray( $msg, $arr, $sort = true ) {
208 if ( $sort ) asort( $arr );
209 foreach ( $arr as $v ) $this->output( "$msg: $v\n" );
213 $maintClass = "FindHooks";
214 require_once( DO_MAINTENANCE
);