diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2010-07-28 11:52:48 +0200 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2010-07-28 11:52:48 +0200 |
commit | 222b01f5169f1c7e69762e0e8904c24f78f71882 (patch) | |
tree | 8e932e12546bb991357ec48eb1638d1770be7a35 /maintenance/findhooks.php | |
parent | 00ab76a6b686e98a914afc1975812d2b1aaa7016 (diff) |
update to MediaWiki 1.16.0
Diffstat (limited to 'maintenance/findhooks.php')
-rw-r--r-- | maintenance/findhooks.php | 285 |
1 files changed, 167 insertions, 118 deletions
diff --git a/maintenance/findhooks.php b/maintenance/findhooks.php index 64fe9a54..13236b6b 100644 --- a/maintenance/findhooks.php +++ b/maintenance/findhooks.php @@ -12,7 +12,21 @@ * * Any instance of wfRunHooks that doesn't meet these parameters will be noted. * - * @file + * 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 + * * @ingroup Maintenance * * @author Ashar Voultoiz <hashar@altern.org> @@ -20,139 +34,174 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later */ -/** This is a command line script*/ -require('commandLine.inc'); -# GLOBALS +require_once( dirname(__FILE__) . '/Maintenance.php' ); -$doc = $IP . '/docs/hooks.txt'; -$pathinc = array( - $IP.'/', - $IP.'/includes/', - $IP.'/includes/api/', - $IP.'/includes/db/', - $IP.'/includes/diff/', - $IP.'/includes/filerepo/', - $IP.'/includes/parser/', - $IP.'/includes/specials/', - $IP.'/languages/', - $IP.'/maintenance/', - $IP.'/skins/', -); - -# FUNCTIONS +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' ); + } -/** - * @return array of documented hooks - */ -function getHooksFromDoc() { - global $doc, $options; - $m = array(); - if( isset( $options['online'] ) ){ - $content = Http::get( 'http://www.mediawiki.org/w/index.php?title=Manual:Hooks&action=raw' ); - preg_match_all( '/\[\[\/([a-zA-Z0-9-_:]+)\|/', $content, $m ); - } else { - $content = file_get_contents( $doc ); - preg_match_all( "/\n'(.*?)'/", $content, $m ); + public function getDbType() { + return Maintenance::DB_NONE; } - return array_unique( $m[1] ); -} -/** - * Get hooks from a PHP file - * @param $file Full filename to the PHP file. - * @return array of hooks found. - */ -function getHooksFromFile( $file ) { - $content = file_get_contents( $file ); - $m = array(); - preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m); - return $m[2]; -} + public function execute() { + global $IP; -/** - * Get hooks from the source code. - * @param $path Directory where the include files can be found - * @return array of hooks found. - */ -function getHooksFromPath( $path ) { - $hooks = array(); - if( $dh = opendir($path) ) { - while(($file = readdir($dh)) !== false) { - if( filetype($path.$file) == 'file' ) { - $hooks = array_merge( $hooks, getHooksFromFile($path.$file) ); + $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' ); + $potential = array(); + $bad = array(); + $pathinc = array( + $IP.'/', + $IP.'/includes/', + $IP.'/includes/api/', + $IP.'/includes/db/', + $IP.'/includes/diff/', + $IP.'/includes/filerepo/', + $IP.'/includes/parser/', + $IP.'/includes/search/', + $IP.'/includes/specials/', + $IP.'/includes/upload/', + $IP.'/languages/', + $IP.'/maintenance/', + $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' ) ){ + // 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 ); + } else { + $m = array(); + $content = file_get_contents( $doc ); + preg_match_all( "/\n'(.*?)'/", $content, $m ); + return array_unique( $m[1] ); } - closedir($dh); } - return $hooks; -} -/** - * 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 - */ -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 . ")"; + /** + * 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(); + preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m); + return $m[2]; } - 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 - */ -function getBadHooksFromPath( $path ) { - $hooks = array(); - if( $dh = opendir($path) ) { - 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, getBadHooksFromFile($path.$file) ); + /** + * 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(); + if( $dh = opendir($path) ) { + while(($file = readdir($dh)) !== false) { + if( filetype($path.$file) == 'file' ) { + $hooks = array_merge( $hooks, $this->getHooksFromFile($path.$file) ); + } } + closedir($dh); } - closedir($dh); + return $hooks; } - return $hooks; -} -/** - * Nicely output the array - * @param $msg A message to show before the value - * @param $arr An array - * @param $sort Boolean : wheter to sort the array (Default: true) - */ -function printArray( $msg, $arr, $sort = true ) { - if($sort) asort($arr); - foreach($arr as $v) echo "$msg: $v\n"; -} + /** + * 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. :) + preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m); + $list = array(); + foreach( $m[0] as $match ){ + $list[] = $match . "(" . $file . ")"; + } + return $list; + } -# MAIN + /** + * 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(); + if( $dh = opendir($path) ) { + 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; + } -$documented = getHooksFromDoc($doc); -$potential = array(); -$bad = array(); -foreach( $pathinc as $dir ) { - $potential = array_merge( $potential, getHooksFromPath( $dir ) ); - $bad = array_merge( $bad, getBadHooksFromPath( $dir ) ); + /** + * Nicely output the array + * @param $msg A message to show before the value + * @param $arr An array + * @param $sort Boolean : wheter 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" ); + } } -$potential = array_unique( $potential ); -$bad = array_unique( $bad ); -$todo = array_diff( $potential, $documented ); -$deprecated = array_diff( $documented, $potential ); - -// let's show the results: -printArray('undocumented', $todo ); -printArray('not found', $deprecated ); -printArray('unclear hook calls', $bad ); - -if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 ) - echo "Looks good!\n"; +$maintClass = "FindHooks"; +require_once( DO_MAINTENANCE ); |