2006-05-02 20:59:56 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Simple script that try to find documented hook and hooks actually
|
|
|
|
|
* in the code and show what's missing.
|
2010-12-04 03:20:14 +00:00
|
|
|
*
|
2006-05-02 20:59:56 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
2008-05-09 16:22:33 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
2007-12-04 02:08:45 +00:00
|
|
|
* Any instance of wfRunHooks that doesn't meet these parameters will be noted.
|
|
|
|
|
*
|
2010-09-01 19:36:18 +00:00
|
|
|
* Copyright © Ashar Voultoiz
|
|
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
2010-09-01 19:36:18 +00:00
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2010-09-26 15:52:51 +00:00
|
|
|
* @author Ashar Voultoiz <hashar at free dot fr>
|
2006-05-02 20:59:56 +00:00
|
|
|
*/
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
require_once( dirname( __FILE__ ) . '/Maintenance.php' );
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
class FindHooks extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2011-03-22 20:44:26 +00:00
|
|
|
$this->mDescription = 'Find hooks that are undocumented, missing, or just plain wrong';
|
|
|
|
|
$this->addOption( 'online', 'Check against MediaWiki.org hook documentation' );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2010-03-10 12:59:44 +00:00
|
|
|
public function getDbType() {
|
2009-08-09 13:31:15 +00:00
|
|
|
return Maintenance::DB_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
|
|
|
|
global $IP;
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
|
|
|
|
|
$potential = array();
|
|
|
|
|
$bad = array();
|
|
|
|
|
$pathinc = array(
|
2010-05-22 16:50:39 +00:00
|
|
|
$IP . '/',
|
|
|
|
|
$IP . '/includes/',
|
|
|
|
|
$IP . '/includes/api/',
|
|
|
|
|
$IP . '/includes/db/',
|
|
|
|
|
$IP . '/includes/diff/',
|
|
|
|
|
$IP . '/includes/filerepo/',
|
2010-11-10 20:21:49 +00:00
|
|
|
$IP . '/includes/installer/',
|
2010-05-22 16:50:39 +00:00
|
|
|
$IP . '/includes/parser/',
|
2010-11-10 20:21:49 +00:00
|
|
|
$IP . '/includes/resourceloader/',
|
2011-02-12 17:15:24 +00:00
|
|
|
$IP . '/includes/revisiondelete/',
|
2010-05-22 16:50:39 +00:00
|
|
|
$IP . '/includes/search/',
|
|
|
|
|
$IP . '/includes/specials/',
|
|
|
|
|
$IP . '/includes/upload/',
|
|
|
|
|
$IP . '/languages/',
|
|
|
|
|
$IP . '/maintenance/',
|
2011-02-12 17:15:24 +00:00
|
|
|
$IP . '/tests/',
|
|
|
|
|
$IP . '/tests/parser/',
|
|
|
|
|
$IP . '/tests/phpunit/suites/',
|
2010-05-22 16:50:39 +00:00
|
|
|
$IP . '/skins/',
|
2009-08-02 19:35:17 +00:00
|
|
|
);
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $pathinc as $dir ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
|
|
|
|
|
$bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
|
2009-07-20 04:43:22 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$potential = array_unique( $potential );
|
|
|
|
|
$bad = array_unique( $bad );
|
|
|
|
|
$todo = array_diff( $potential, $documented );
|
|
|
|
|
$deprecated = array_diff( $documented, $potential );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
// let's show the results:
|
2010-05-22 16:50:39 +00:00
|
|
|
$this->printArray( 'Undocumented', $todo );
|
|
|
|
|
$this->printArray( 'Documented and not found', $deprecated );
|
|
|
|
|
$this->printArray( 'Unclear hook calls', $bad );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
|
2011-03-22 20:44:26 +00:00
|
|
|
{
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Looks good!\n" );
|
2011-03-22 20:44:26 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-03-22 20:44:26 +00:00
|
|
|
* Get the hook documentation, either locally or from MediaWiki.org
|
2009-08-02 19:35:17 +00:00
|
|
|
* @return array of documented hooks
|
|
|
|
|
*/
|
|
|
|
|
private function getHooksFromDoc( $doc ) {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->hasOption( 'online' ) ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
// 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();
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $allhookdata['query']['categorymembers'] as $page ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $found ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$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();
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $found ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$hook = str_replace( ' ', '_', $matches[1] );
|
|
|
|
|
$removed[] = $hook;
|
|
|
|
|
}
|
2009-07-20 04:43:22 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
return array_diff( $allhooks, $removed );
|
|
|
|
|
} else {
|
|
|
|
|
$m = array();
|
|
|
|
|
$content = file_get_contents( $doc );
|
|
|
|
|
preg_match_all( "/\n'(.*?)'/", $content, $m );
|
|
|
|
|
return array_unique( $m[1] );
|
2009-07-20 04:43:22 +00:00
|
|
|
}
|
2008-05-09 16:22:33 +00:00
|
|
|
}
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Get hooks from a PHP file
|
|
|
|
|
* @param $file Full filename to the PHP file.
|
|
|
|
|
* @return array of hooks found.
|
|
|
|
|
*/
|
|
|
|
|
private function getHooksFromFile( $file ) {
|
|
|
|
|
$content = file_get_contents( $file );
|
|
|
|
|
$m = array();
|
2011-03-22 20:44:26 +00:00
|
|
|
preg_match_all( '/(wfRunHooks|Hooks\:\:run)\(\s*([\'"])(.*?)\1/', $content, $m );
|
2009-08-02 19:35:17 +00:00
|
|
|
return $m[2];
|
|
|
|
|
}
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* 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();
|
2010-11-01 00:07:17 +00:00
|
|
|
$dh = opendir( $path );
|
|
|
|
|
if ( $dh ) {
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( ( $file = readdir( $dh ) ) !== false ) {
|
|
|
|
|
if ( filetype( $path . $file ) == 'file' ) {
|
|
|
|
|
$hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2006-05-02 20:59:56 +00:00
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
closedir( $dh );
|
2006-05-02 20:59:56 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
return $hooks;
|
2006-05-02 20:59:56 +00:00
|
|
|
}
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Get bad hooks (where the hook name could not be determined) from a PHP file
|
|
|
|
|
* @param $file 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. :)
|
2010-05-22 16:50:39 +00:00
|
|
|
preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
|
2009-08-02 19:35:17 +00:00
|
|
|
$list = array();
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $m[0] as $match ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$list[] = $match . "(" . $file . ")";
|
|
|
|
|
}
|
|
|
|
|
return $list;
|
2008-02-11 09:37:39 +00:00
|
|
|
}
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* 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();
|
2010-11-01 00:07:17 +00:00
|
|
|
$dh = opendir( $path );
|
|
|
|
|
if ( $dh ) {
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( ( $file = readdir( $dh ) ) !== false ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
# We don't want to read this file as it contains bad calls to wfRunHooks()
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
|
|
|
|
|
$hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2007-12-04 02:08:45 +00:00
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
closedir( $dh );
|
2007-12-04 02:08:45 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
return $hooks;
|
2007-12-04 02:08:45 +00:00
|
|
|
}
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Nicely output the array
|
2011-03-22 20:44:26 +00:00
|
|
|
* @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)
|
2009-08-02 19:35:17 +00:00
|
|
|
*/
|
|
|
|
|
private function printArray( $msg, $arr, $sort = true ) {
|
2011-03-22 20:44:26 +00:00
|
|
|
if ( $sort ) {
|
|
|
|
|
asort( $arr );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $arr as $v ) {
|
|
|
|
|
$this->output( "$msg: $v\n" );
|
|
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2008-02-11 09:37:39 +00:00
|
|
|
}
|
2008-03-03 16:22:40 +00:00
|
|
|
|
2011-03-22 20:44:26 +00:00
|
|
|
$maintClass = 'FindHooks';
|
2011-01-13 22:58:55 +00:00
|
|
|
require_once( RUN_MAINTENANCE_IF_MAIN );
|