Follow-up 6281b0a: LookupElement is still not in core
[lhc/web/wiklou.git] / maintenance / findHooks.php
index 0925406..60bdf29 100644 (file)
@@ -45,11 +45,11 @@ class FindHooks extends Maintenance {
        /*
         * Hooks that are ignored
         */
-       protected static $ignore = array( 'testRunLegacyHooks' );
+       protected static $ignore = [ 'testRunLegacyHooks' ];
 
        public function __construct() {
                parent::__construct();
-               $this->mDescription = 'Find hooks that are undocumented, missing, or just plain wrong';
+               $this->addDescription( 'Find hooks that are undocumented, missing, or just plain wrong' );
                $this->addOption( 'online', 'Check against MediaWiki.org hook documentation' );
        }
 
@@ -60,12 +60,12 @@ class FindHooks extends Maintenance {
        public function execute() {
                global $IP;
 
-               $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
-               $potential = array();
-               $bad = array();
+               $documentedHooks = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
+               $potentialHooks = [];
+               $bad = [];
 
                // TODO: Don't hardcode the list of directories
-               $pathinc = array(
+               $pathinc = [
                        $IP . '/',
                        $IP . '/includes/',
                        $IP . '/includes/actions/',
@@ -82,12 +82,14 @@ class FindHooks extends Maintenance {
                        $IP . '/includes/deferred/',
                        $IP . '/includes/diff/',
                        $IP . '/includes/exception/',
+                       $IP . '/includes/export/',
                        $IP . '/includes/externalstore/',
                        $IP . '/includes/filebackend/',
                        $IP . '/includes/filerepo/',
                        $IP . '/includes/filerepo/file/',
                        $IP . '/includes/gallery/',
                        $IP . '/includes/htmlform/',
+                       $IP . '/includes/import/',
                        $IP . '/includes/installer/',
                        $IP . '/includes/interwiki/',
                        $IP . '/includes/jobqueue/',
@@ -102,11 +104,13 @@ class FindHooks extends Maintenance {
                        $IP . '/includes/resourceloader/',
                        $IP . '/includes/revisiondelete/',
                        $IP . '/includes/search/',
+                       $IP . '/includes/session/',
                        $IP . '/includes/site/',
                        $IP . '/includes/skins/',
                        $IP . '/includes/specialpage/',
                        $IP . '/includes/specials/',
                        $IP . '/includes/upload/',
+                       $IP . '/includes/user/',
                        $IP . '/includes/utils/',
                        $IP . '/languages/',
                        $IP . '/maintenance/',
@@ -114,24 +118,56 @@ class FindHooks extends Maintenance {
                        $IP . '/tests/',
                        $IP . '/tests/parser/',
                        $IP . '/tests/phpunit/suites/',
-               );
+               ];
 
                foreach ( $pathinc as $dir ) {
-                       $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
+                       $potentialHooks = array_merge( $potentialHooks, $this->getHooksFromPath( $dir ) );
                        $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
                }
 
+               $documented = array_keys( $documentedHooks );
+               $potential = array_keys( $potentialHooks );
                $potential = array_unique( $potential );
                $bad = array_diff( array_unique( $bad ), self::$ignore );
                $todo = array_diff( $potential, $documented, self::$ignore );
                $deprecated = array_diff( $documented, $potential, self::$ignore );
 
+               // Check parameter count and references
+               $badParameterCount = $badParameterReference = [];
+               foreach ( $potentialHooks as $hook => $args ) {
+                       if ( !isset( $documentedHooks[$hook] ) ) {
+                               // Not documented, but that will also be in $todo
+                               continue;
+                       }
+                       $argsDoc = $documentedHooks[$hook];
+                       if ( $args === 'unknown' || $argsDoc === 'unknown' ) {
+                               // Could not get parameter information
+                               continue;
+                       }
+                       if ( count( $argsDoc ) !== count( $args ) ) {
+                               $badParameterCount[] = $hook . ': Doc: ' . count( $argsDoc ) . ' vs. Code: ' . count( $args );
+                       } else {
+                               // Check if & is equal
+                               foreach ( $argsDoc as $index => $argDoc ) {
+                                       $arg = $args[$index];
+                                       if ( ( $arg[0] === '&' ) !== ( $argDoc[0] === '&' ) ) {
+                                               $badParameterReference[] = $hook . ': References different: Doc: ' . $argDoc .
+                                                       ' vs. Code: ' . $arg;
+                                       }
+                               }
+                       }
+               }
+
                // let's show the results:
                $this->printArray( 'Undocumented', $todo );
                $this->printArray( 'Documented and not found', $deprecated );
                $this->printArray( 'Unclear hook calls', $bad );
+               $this->printArray( 'Different parameter count', $badParameterCount );
+               $this->printArray( 'Different parameter reference', $badParameterReference );
 
-               if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 ) {
+               if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0
+                       && count( $badParameterCount ) == 0 && count( $badParameterReference ) == 0
+               ) {
                        $this->output( "Looks good!\n" );
                } else {
                        $this->error( 'The script finished with errors.', 1 );
@@ -141,7 +177,7 @@ class FindHooks extends Maintenance {
        /**
         * Get the hook documentation, either locally or from MediaWiki.org
         * @param string $doc
-        * @return array Array of documented hooks
+        * @return array Array: key => hook name; value => array of arguments or string 'unknown'
         */
        private function getHooksFromDoc( $doc ) {
                if ( $this->hasOption( 'online' ) ) {
@@ -154,24 +190,41 @@ class FindHooks extends Maintenance {
        /**
         * Get hooks from a local file (for example docs/hooks.txt)
         * @param string $doc Filename to look in
-        * @return array Array of documented hooks
+        * @return array Array: key => hook name; value => array of arguments or string 'unknown'
         */
        private function getHooksFromLocalDoc( $doc ) {
-               $m = array();
+               $m = [];
                $content = file_get_contents( $doc );
-               preg_match_all( "/\n'(.*?)':/", $content, $m );
+               preg_match_all(
+                       "/\n'(.*?)':.*((?:\n.+)*)/",
+                       $content,
+                       $m,
+                       PREG_SET_ORDER
+               );
 
-               return array_unique( $m[1] );
+               // Extract the documented parameter
+               $hooks = [];
+               foreach ( $m as $match ) {
+                       $args = [];
+                       if ( isset( $match[2] ) ) {
+                               $n = [];
+                               if ( preg_match_all( "/\n(&?\\$\w+):.+/", $match[2], $n ) ) {
+                                       $args = $n[1];
+                               }
+                       }
+                       $hooks[$match[1]] = $args;
+               }
+               return $hooks;
        }
 
        /**
         * Get hooks from www.mediawiki.org using the API
-        * @return array Array of documented hooks
+        * @return array Array: key => hook name; value => string 'unknown'
         */
        private function getHooksFromOnlineDoc() {
                $allhooks = $this->getHooksFromOnlineDocCategory( 'MediaWiki_hooks' );
                $removed = $this->getHooksFromOnlineDocCategory( 'Removed_hooks' );
-               return array_diff( $allhooks, $removed );
+               return array_diff_key( $allhooks, $removed );
        }
 
        /**
@@ -179,26 +232,27 @@ class FindHooks extends Maintenance {
         * @return array
         */
        private function getHooksFromOnlineDocCategory( $title ) {
-               $params = array(
+               $params = [
                        'action' => 'query',
                        'list' => 'categorymembers',
                        'cmtitle' => "Category:$title",
                        'cmlimit' => 500,
                        'format' => 'json',
                        'continue' => '',
-               );
+               ];
 
-               $retval = array();
+               $retval = [];
                while ( true ) {
                        $json = Http::get(
                                wfAppendQuery( 'http://www.mediawiki.org/w/api.php', $params ),
-                               array(),
+                               [],
                                __METHOD__
                        );
                        $data = FormatJson::decode( $json, true );
                        foreach ( $data['query']['categorymembers'] as $page ) {
                                if ( preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $m ) ) {
-                                       $retval[] = str_replace( ' ', '_', $m[1] );
+                                       // parameters are unknown, because that needs parsing of wikitext
+                                       $retval[str_replace( ' ', '_', $m[1] )] = 'unknown';
                                }
                        }
                        if ( !isset( $data['continue'] ) ) {
@@ -211,27 +265,57 @@ class FindHooks extends Maintenance {
        /**
         * Get hooks from a PHP file
         * @param string $file Full filename to the PHP file.
-        * @return array Array of hooks found
+        * @return array Array: key => hook name; value => array of arguments or string 'unknown'
         */
        private function getHooksFromFile( $file ) {
                $content = file_get_contents( $file );
-               $m = array();
+               $m = [];
                preg_match_all(
-                       '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\(\s*([\'"])(.*?)\1/',
+                       // All functions which runs hooks
+                       '/(?:wfRunHooks|Hooks\:\:run|ContentHandler\:\:runLegacyHooks)\s*\(\s*' .
+                               // First argument is the hook name as string
+                               '([\'"])(.*?)\1' .
+                               // Comma for second argument
+                               '(?:\s*(,))?' .
+                               // Second argument must start with array to be processed
+                               '(?:\s*array\s*\(' .
+                               // Matching inside array - allows one deep of brackets
+                               '((?:[^\(\)]|\([^\(\)]*\))*)' .
+                               // End
+                               '\))?/',
                        $content,
-                       $m
+                       $m,
+                       PREG_SET_ORDER
                );
 
-               return $m[2];
+               // Extract parameter
+               $hooks = [];
+               foreach ( $m as $match ) {
+                       $args = [];
+                       if ( isset( $match[4] ) ) {
+                               $n = [];
+                               if ( preg_match_all( '/((?:[^,\(\)]|\([^\(\)]*\))+)/', $match[4], $n ) ) {
+                                       $args = array_map( 'trim', $n[1] );
+                               }
+                       } elseif ( isset( $match[3] ) ) {
+                               // Found a parameter for Hooks::run,
+                               // but could not extract the hooks argument,
+                               // because there are given by a variable
+                               $args = 'unknown';
+                       }
+                       $hooks[$match[2]] = $args;
+               }
+
+               return $hooks;
        }
 
        /**
         * Get hooks from the source code.
         * @param string $path Directory where the include files can be found
-        * @return array Array of hooks found
+        * @return array Array: key => hook name; value => array of arguments or string 'unknown'
         */
        private function getHooksFromPath( $path ) {
-               $hooks = array();
+               $hooks = [];
                $dh = opendir( $path );
                if ( $dh ) {
                        while ( ( $file = readdir( $dh ) ) !== false ) {
@@ -252,10 +336,10 @@ class FindHooks extends Maintenance {
         */
        private function getBadHooksFromFile( $file ) {
                $content = file_get_contents( $file );
-               $m = array();
+               $m = [];
                # We want to skip the "function wfRunHooks()" one.  :)
                preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
-               $list = array();
+               $list = [];
                foreach ( $m[0] as $match ) {
                        $list[] = $match . "(" . $file . ")";
                }
@@ -269,7 +353,7 @@ class FindHooks extends Maintenance {
         * @return array Array of bad wfRunHooks() lines
         */
        private function getBadHooksFromPath( $path ) {
-               $hooks = array();
+               $hooks = [];
                $dh = opendir( $path );
                if ( $dh ) {
                        while ( ( $file = readdir( $dh ) ) !== false ) {