basic script to find undocumented / deprecated hooks.
authorAntoine Musso <hashar@users.mediawiki.org>
Tue, 2 May 2006 20:59:56 +0000 (20:59 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Tue, 2 May 2006 20:59:56 +0000 (20:59 +0000)
RELEASE-NOTES
docs/hooks.txt
maintenance/findhooks.php [new file with mode: 0644]

index ea9ae28..530a361 100644 (file)
@@ -188,6 +188,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * Sanitizer: now handles nested <li> in <ul> or <ol>
 * (bug 5796) We require MySQL >=4.0.14
 * Add 'EmailConfirmed' hook
+* New findhooks.php script to find undocumented hooks.
 
 == Compatibility ==
 
index 50aa84a..a8bfd5b 100644 (file)
@@ -434,4 +434,5 @@ $catpage: CategoryPage instance
 $content_actions: The array of content actions
 
 
-More hooks might be available but undocumented.
+More hooks might be available but undocumented, you can execute
+./maintenance/findhooks.php to find hidden one.
diff --git a/maintenance/findhooks.php b/maintenance/findhooks.php
new file mode 100644 (file)
index 0000000..4f446f2
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+/**
+ * Simple script that try to find documented hook and hooks actually
+ * in the code and show what's missing.
+ * 
+ * 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.
+ *
+ * @package MediaWiki
+ * @subpackage Maintenance
+ *
+ * @author Ashar Voultoiz <hashar@altern.org>
+ * @copyright Copyright © Ashar voultoiz
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
+ */
+
+/** This is a command line script*/
+include('commandLine.inc');
+
+
+# GLOBALS
+
+$doc = $IP . '/docs/hooks.txt';
+$pathinc = $IP . '/includes/';
+
+
+# FUNCTIONS
+
+/**
+ * @return array of documented hooks
+ */
+function getHooksFromDoc() {
+       global $doc;
+       $content = file_get_contents( $doc );
+       preg_match_all( "/\n'(.*?)'/", $content, $m);
+       return $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 );
+       preg_match_all( "/wfRunHooks\(\s*\'(.*?)\'/", $content, $m);
+       return $m[1];
+}
+
+/**
+ * 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) );
+                       }
+               }
+               closedir($dh);
+       }
+       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) print "$msg: $v\n";
+}
+
+
+# MAIN
+
+$documented = getHooksFromDoc($doc);
+$potential = getHooksFromPath($pathinc);
+
+$todo = array_diff($potential, $documented);
+$deprecated = array_diff($documented, $potential);
+
+// let's show the results:
+printArray('undocumented', $todo );
+printArray('not found', $deprecated );
+
+?>