X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2FfindHooks.php;h=f9c61c70f734c4d0e18f53a7eb6773bf649fa93e;hb=6bd9ad621a1f9347d2f5dc03f0d14aa6bf1dd2d1;hp=114366bca60cd62e49681324d8a4c3cb4a1d226e;hpb=37f4900008e8ec88e7dd71b883fcc6138f1cff0f;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/findHooks.php b/maintenance/findHooks.php index 114366bca6..f9c61c70f7 100644 --- a/maintenance/findHooks.php +++ b/maintenance/findHooks.php @@ -60,8 +60,8 @@ class FindHooks extends Maintenance { public function execute() { global $IP; - $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' ); - $potential = array(); + $documentedHooks = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' ); + $potentialHooks = array(); $bad = array(); // TODO: Don't hardcode the list of directories @@ -117,21 +117,53 @@ class FindHooks extends Maintenance { ); 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 = array(); + 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 +173,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 +186,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(); $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 = array(); + foreach ( $m as $match ) { + $args = array(); + if ( isset( $match[2] ) ) { + $n = array(); + 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 ); } /** @@ -190,11 +239,16 @@ class FindHooks extends Maintenance { $retval = array(); while ( true ) { - $json = Http::get( wfAppendQuery( 'http://www.mediawiki.org/w/api.php', $params ), array(), __METHOD__ ); + $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'] ) ) { @@ -207,24 +261,54 @@ 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(); 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 = array(); + foreach ( $m as $match ) { + $args = array(); + if ( isset( $match[4] ) ) { + $n = array(); + 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();