Localisation updates for core messages from Betawiki (2008-05-05 23:21 CEST)
[lhc/web/wiklou.git] / maintenance / findhooks.php
1 <?php
2 /**
3 * Simple script that try to find documented hook and hooks actually
4 * in the code and show what's missing.
5 *
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.
9 *
10 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
11 *
12 * @addtogroup Maintenance
13 *
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
17 */
18
19 /** This is a command line script*/
20 include('commandLine.inc');
21
22 # GLOBALS
23
24 $doc = $IP . '/docs/hooks.txt';
25 $pathinc = array( $IP.'/includes/', $IP.'/includes/api/', $IP.'/includes/filerepo/', $IP.'/languages/', $IP.'/maintenance/', $IP.'/skins/' );
26
27 # FUNCTIONS
28
29 /**
30 * @return array of documented hooks
31 */
32 function getHooksFromDoc() {
33 global $doc;
34 $content = file_get_contents( $doc );
35 $m = array();
36 preg_match_all( "/\n'(.*?)'/", $content, $m);
37 return $m[1];
38 }
39
40 /**
41 * Get hooks from a PHP file
42 * @param $file Full filename to the PHP file.
43 * @return array of hooks found.
44 */
45 function getHooksFromFile( $file ) {
46 $content = file_get_contents( $file );
47 $m = array();
48 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
49 return $m[2];
50 }
51
52 /**
53 * Get hooks from the source code.
54 * @param $path Directory where the include files can be found
55 * @return array of hooks found.
56 */
57 function getHooksFromPath( $path ) {
58 $hooks = array();
59 if( $dh = opendir($path) ) {
60 while(($file = readdir($dh)) !== false) {
61 if( filetype($path.$file) == 'file' ) {
62 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
63 }
64 }
65 closedir($dh);
66 }
67 return $hooks;
68 }
69
70 /**
71 * Get bad hooks (where the hook name could not be determined) from a PHP file
72 * @param $file Full filename to the PHP file.
73 * @return array of bad wfRunHooks() lines
74 */
75 function getBadHooksFromFile( $file ) {
76 $content = file_get_contents( $file );
77 $m = array();
78 # We want to skip the "function wfRunHooks()" one. :)
79 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
80 $list = array();
81 foreach( $m[0] as $match ){
82 $list[] = $match . "(" . $file . ")";
83 }
84 return $list;
85 }
86
87 /**
88 * Get bad hooks from the source code.
89 * @param $path Directory where the include files can be found
90 * @return array of bad wfRunHooks() lines
91 */
92 function getBadHooksFromPath( $path ) {
93 $hooks = array();
94 if( $dh = opendir($path) ) {
95 while(($file = readdir($dh)) !== false) {
96 # We don't want to read this file as it contains bad calls to wfRunHooks()
97 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__ ) {
98 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
99 }
100 }
101 closedir($dh);
102 }
103 return $hooks;
104 }
105
106 /**
107 * Nicely output the array
108 * @param $msg A message to show before the value
109 * @param $arr An array
110 * @param $sort Boolean : wheter to sort the array (Default: true)
111 */
112 function printArray( $msg, $arr, $sort = true ) {
113 if($sort) asort($arr);
114 foreach($arr as $v) echo "$msg: $v\n";
115 }
116
117 # MAIN
118
119 $documented = getHooksFromDoc($doc);
120 $potential = array();
121 $bad = array();
122 foreach( $pathinc as $dir ) {
123 $potential = array_merge( $potential, getHooksFromPath( $dir ) );
124 $bad = array_merge( $bad, getBadHooksFromPath( $dir ) );
125 }
126
127 $potential = array_unique( $potential );
128 $bad = array_unique( $bad );
129 $todo = array_diff( $potential, $documented );
130 $deprecated = array_diff( $documented, $potential );
131
132 // let's show the results:
133 printArray('undocumented', $todo );
134 printArray('not found', $deprecated );
135 printArray('unclear hook calls', $bad );
136
137 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
138 echo "Looks good!\n";