diff options
Diffstat (limited to 'maintenance/findHooks.php')
-rw-r--r-- | maintenance/findHooks.php | 133 |
1 files changed, 88 insertions, 45 deletions
diff --git a/maintenance/findHooks.php b/maintenance/findHooks.php index 373170ff..36760d7e 100644 --- a/maintenance/findHooks.php +++ b/maintenance/findHooks.php @@ -42,6 +42,11 @@ require_once __DIR__ . '/Maintenance.php'; * @ingroup Maintenance */ class FindHooks extends Maintenance { + /* + * Hooks that are ignored + */ + protected static $ignore = array( 'testRunLegacyHooks' ); + public function __construct() { parent::__construct(); $this->mDescription = 'Find hooks that are undocumented, missing, or just plain wrong'; @@ -58,34 +63,53 @@ class FindHooks extends Maintenance { $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' ); $potential = array(); $bad = array(); + + // TODO: Don't hardcode the list of directories $pathinc = array( $IP . '/', $IP . '/includes/', $IP . '/includes/actions/', $IP . '/includes/api/', $IP . '/includes/cache/', + $IP . '/includes/changes/', + $IP . '/includes/clientpool/', $IP . '/includes/content/', $IP . '/includes/context/', + $IP . '/includes/dao/', $IP . '/includes/db/', + $IP . '/includes/debug/', + $IP . '/includes/deferred/', $IP . '/includes/diff/', + $IP . '/includes/externalstore/', + $IP . '/includes/filebackend/', $IP . '/includes/filerepo/', $IP . '/includes/filerepo/file/', + $IP . '/includes/gallery/', + $IP . '/includes/htmlform/', $IP . '/includes/installer/', $IP . '/includes/interwiki/', + $IP . '/includes/jobqueue/', + $IP . '/includes/json/', $IP . '/includes/logging/', $IP . '/includes/media/', + $IP . '/includes/page/', $IP . '/includes/parser/', + $IP . '/includes/rcfeed/', $IP . '/includes/resourceloader/', $IP . '/includes/revisiondelete/', $IP . '/includes/search/', + $IP . '/includes/site/', + $IP . '/includes/skins/', + $IP . '/includes/specialpage/', $IP . '/includes/specials/', $IP . '/includes/upload/', + $IP . '/includes/utils/', $IP . '/languages/', $IP . '/maintenance/', + $IP . '/maintenance/language/', $IP . '/tests/', $IP . '/tests/parser/', $IP . '/tests/phpunit/suites/', - $IP . '/skins/', ); foreach ( $pathinc as $dir ) { @@ -103,15 +127,15 @@ class FindHooks extends Maintenance { $this->printArray( 'Documented and not found', $deprecated ); $this->printArray( 'Unclear hook calls', $bad ); - if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 ) - { + 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 + * @param string $doc + * @return array Array of documented hooks */ private function getHooksFromDoc( $doc ) { if ( $this->hasOption( 'online' ) ) { @@ -123,62 +147,75 @@ class FindHooks extends Maintenance { /** * Get hooks from a local file (for example docs/hooks.txt) - * @param $doc string: filename to look in - * @return array of documented hooks + * @param string $doc Filename to look in + * @return array Array of documented hooks */ private function getHooksFromLocalDoc( $doc ) { - $m = array(); - $content = file_get_contents( $doc ); - preg_match_all( "/\n'(.*?)'/", $content, $m ); - return array_unique( $m[1] ); + $m = array(); + $content = file_get_contents( $doc ); + preg_match_all( "/\n'(.*?)':/", $content, $m ); + + return array_unique( $m[1] ); } /** * Get hooks from www.mediawiki.org using the API - * @return array of documented hooks + * @return array Array of documented hooks */ private function getHooksFromOnlineDoc() { - // 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; - } + // 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; - } + } + // 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 ); + } + + return array_diff( $allhooks, $removed ); } /** * Get hooks from a PHP file - * @param $file string Full filename to the PHP file. - * @return array of hooks found. + * @param string $file Full filename to the PHP file. + * @return array Array of hooks found */ private function getHooksFromFile( $file ) { $content = file_get_contents( $file ); $m = array(); - preg_match_all( '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/', $content, $m ); + preg_match_all( + '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/', + $content, + $m + ); + return $m[2]; } /** * Get hooks from the source code. - * @param $path Directory where the include files can be found - * @return array of hooks found. + * @param string $path Directory where the include files can be found + * @return array Array of hooks found */ private function getHooksFromPath( $path ) { $hooks = array(); @@ -191,13 +228,14 @@ class FindHooks extends Maintenance { } closedir( $dh ); } + return $hooks; } /** * Get bad hooks (where the hook name could not be determined) from a PHP file - * @param $file string Full filename to the PHP file. - * @return array of bad wfRunHooks() lines + * @param string $file Full filename to the PHP file. + * @return array Array of bad wfRunHooks() lines */ private function getBadHooksFromFile( $file ) { $content = file_get_contents( $file ); @@ -208,13 +246,14 @@ class FindHooks extends Maintenance { foreach ( $m[0] as $match ) { $list[] = $match . "(" . $file . ")"; } + 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 + * @param string $path Directory where the include files can be found + * @return array Array of bad wfRunHooks() lines */ private function getBadHooksFromPath( $path ) { $hooks = array(); @@ -228,21 +267,25 @@ class FindHooks extends Maintenance { } closedir( $dh ); } + return $hooks; } /** * Nicely output the array - * @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) + * @param string $msg A message to show before the value + * @param array $arr + * @param bool $sort Whether 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" ); + if ( !in_array( $v, self::$ignore ) ) { + $this->output( "$msg: $v\n" ); + } } } } |