3 * Simple script that try to find documented hook and hooks actually
4 * in the code and show what's missing.
6 * This script assumes that:
7 * - hooks names in hooks.txt are at the beginning of a line and single quoted.
8 * - hooks names in code are the first parameter of wfRunHooks.
10 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
12 * @addtogroup Maintenance
14 * @author Ashar Voultoiz <hashar@altern.org>
15 * @copyright Copyright © Ashar voultoiz
16 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
19 /** This is a command line script*/
20 include('commandLine.inc');
25 $doc = $IP . '/docs/hooks.txt';
26 $pathinc = $IP . '/includes/';
32 * @return array of documented hooks
34 function getHooksFromDoc() {
36 $content = file_get_contents( $doc );
38 preg_match_all( "/\n'(.*?)'/", $content, $m);
43 * Get hooks from a PHP file
44 * @param $file Full filename to the PHP file.
45 * @return array of hooks found.
47 function getHooksFromFile( $file ) {
48 $content = file_get_contents( $file );
50 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
55 * Get hooks from the source code.
56 * @param $path Directory where the include files can be found
57 * @return array of hooks found.
59 function getHooksFromPath( $path ) {
61 if( $dh = opendir($path) ) {
62 while(($file = readdir($dh)) !== false) {
63 if( filetype($path.$file) == 'file' ) {
64 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
73 * Get bad hooks (where the hook name could not be determined) from a PHP file
74 * @param $file Full filename to the PHP file.
75 * @return array of bad wfRunHooks() lines
77 function getBadHooksFromFile( $file ) {
78 $content = file_get_contents( $file );
80 # We want to skip the "function wfRunHooks()" one. :)
81 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
86 * Get bad hooks from the source code.
87 * @param $path Directory where the include files can be found
88 * @return array of bad wfRunHooks() lines
90 function getBadHooksFromPath( $path ) {
92 if( $dh = opendir($path) ) {
93 while(($file = readdir($dh)) !== false) {
94 if( filetype($path.$file) == 'file' ) {
95 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
104 * Nicely output the array
105 * @param $msg A message to show before the value
106 * @param $arr An array
107 * @param $sort Boolean : wheter to sort the array (Default: true)
109 function printArray( $msg, $arr, $sort = true ) {
110 if($sort) asort($arr);
111 foreach($arr as $v) print "$msg: $v\n";
117 $documented = getHooksFromDoc($doc);
118 $potential = getHooksFromPath($pathinc);
119 $bad = getBadHooksFromPath($pathinc);
121 $todo = array_diff($potential, $documented);
122 $deprecated = array_diff($documented, $potential);
124 // let's show the results:
125 printArray('undocumented', $todo );
126 printArray('not found', $deprecated );
127 printArray('unclear hook calls', $bad );