fix doc, this is not an array
[lhc/web/wiklou.git] / maintenance / mwdocgen.php
1 <?php
2 /**
3 * Generate class and file reference documentation for MediaWiki using doxygen.
4 *
5 * If the dot DOT language processor is available, attempt call graph
6 * generation.
7 *
8 * Usage:
9 * php mwdocgen.php
10 *
11 * KNOWN BUGS:
12 *
13 * - pass_thru seems to always use buffering (even with ob_implicit_flush()),
14 * that make output slow when doxygen parses language files.
15 * - the menu doesnt work, got disabled at revision 13740. Need to code it.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * http://www.gnu.org/copyleft/gpl.html
31 *
32 * @file
33 * @todo document
34 * @ingroup Maintenance
35 *
36 * @author Antoine Musso <hashar at free dot fr>
37 * @author Brion Vibber
38 * @author Alexandre Emsenhuber
39 * @version first release
40 */
41
42 #
43 # Variables / Configuration
44 #
45
46 if ( php_sapi_name() != 'cli' ) {
47 echo 'Run "' . __FILE__ . '" from the command line.';
48 die( -1 );
49 }
50
51 /** Figure out the base directory for MediaWiki location */
52 $mwPath = dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR;
53
54 /** doxygen binary script */
55 $doxygenBin = 'doxygen';
56
57 /** doxygen configuration template for mediawiki */
58 $doxygenTemplate = $mwPath . 'maintenance/Doxyfile';
59
60 /** svnstat command, used to get the version of each file */
61 $svnstat = $mwPath . 'bin/svnstat';
62
63 /** where Phpdoc should output documentation */
64 $doxyOutput = $mwPath . 'docs' . DIRECTORY_SEPARATOR ;
65
66 /** MediaWiki subpaths */
67 $mwPathI = $mwPath . 'includes/';
68 $mwPathL = $mwPath . 'languages/';
69 $mwPathM = $mwPath . 'maintenance/';
70 $mwPathS = $mwPath . 'skins/';
71
72 /** Ignored paths relative to $mwPath */
73 $mwExcludePaths = array(
74 'images',
75 'static',
76 );
77
78 /** Variable to get user input */
79 $input = '';
80 $exclude_patterns = '';
81 /** Whether to generates man pages: */
82 $wgDoxyGenerateMan = false;
83
84 #
85 # Functions
86 #
87
88 define( 'MEDIAWIKI', true );
89 require_once( "$mwPath/includes/GlobalFunctions.php" );
90
91 /**
92 * Read a line from the shell
93 * @param $prompt String
94 * @return string
95 */
96 function readaline( $prompt = '' ) {
97 print $prompt;
98 $fp = fopen( "php://stdin", "r" );
99 $resp = trim( fgets( $fp, 1024 ) );
100 fclose( $fp );
101 return $resp;
102 }
103
104 /**
105 * Copied from SpecialVersion::getSvnRevision()
106 * @param $dir String
107 * @return Mixed: string or false
108 */
109 function getSvnRevision( $dir ) {
110 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
111 $entries = $dir . '/.svn/entries';
112
113 if ( !file_exists( $entries ) ) {
114 return false;
115 }
116
117 $content = file( $entries );
118
119 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
120 if ( preg_match( '/^<\?xml/', $content[0] ) ) {
121 // subversion is release <= 1.3
122 if ( !function_exists( 'simplexml_load_file' ) ) {
123 // We could fall back to expat... YUCK
124 return false;
125 }
126
127 $xml = simplexml_load_file( $entries );
128
129 if ( $xml ) {
130 foreach ( $xml->entry as $entry ) {
131 if ( $xml->entry[0]['name'] == '' ) {
132 // The directory entry should always have a revision marker.
133 if ( $entry['revision'] ) {
134 return intval( $entry['revision'] );
135 }
136 }
137 }
138 }
139 return false;
140 } else {
141 // subversion is release 1.4
142 return intval( $content[3] );
143 }
144 }
145
146 /**
147 * Generate a configuration file given user parameters and return the temporary filename.
148 * @param $doxygenTemplate String: full path for the template.
149 * @param $outputDirectory String: directory where the stuff will be output.
150 * @param $stripFromPath String: path that should be stripped out (usually mediawiki base path).
151 * @param $currentVersion String: Version number of the software
152 * @param $svnstat String: path to the svnstat file
153 * @param $input String: Path to analyze.
154 * @param $exclude String: Additionals path regex to exclude
155 * @param $exclude_patterns String: Additionals path regex to exclude
156 * (LocalSettings.php, AdminSettings.php, .svn and .git directories are always excluded)
157 * @return string
158 */
159 function generateConfigFile( $doxygenTemplate, $outputDirectory, $stripFromPath, $currentVersion, $svnstat, $input, $exclude, $exclude_patterns ) {
160
161 global $wgDoxyGenerateMan;
162
163 $template = file_get_contents( $doxygenTemplate );
164
165 // Replace template placeholders by correct values.
166 $replacements = array(
167 '{{OUTPUT_DIRECTORY}}' => $outputDirectory,
168 '{{STRIP_FROM_PATH}}' => $stripFromPath,
169 '{{CURRENT_VERSION}}' => $currentVersion,
170 '{{SVNSTAT}}' => $svnstat,
171 '{{INPUT}}' => $input,
172 '{{EXCLUDE}}' => $exclude,
173 '{{EXCLUDE_PATTERNS}}' => $exclude_patterns,
174 '{{HAVE_DOT}}' => `which dot` ? 'YES' : 'NO',
175 '{{GENERATE_MAN}}' => $wgDoxyGenerateMan ? 'YES' : 'NO',
176 );
177 $tmpCfg = str_replace( array_keys( $replacements ), array_values( $replacements ), $template );
178 $tmpFileName = tempnam( wfTempDir(), 'mwdocgen-' );
179 file_put_contents( $tmpFileName , $tmpCfg ) or die( "Could not write doxygen configuration to file $tmpFileName\n" );
180
181 return $tmpFileName;
182 }
183
184 #
185 # Main !
186 #
187
188 unset( $file );
189
190 if ( is_array( $argv ) ) {
191 for ($i = 0; $i < count($argv); $i++ ) {
192 switch( $argv[$i] ) {
193 case '--all': $input = 0; break;
194 case '--includes': $input = 1; break;
195 case '--languages': $input = 2; break;
196 case '--maintenance': $input = 3; break;
197 case '--skins': $input = 4; break;
198 case '--file':
199 $input = 5;
200 $i++;
201 if ( isset( $argv[$i] ) ) {
202 $file = $argv[$i];
203 }
204 break;
205 case '--no-extensions': $input = 6; break;
206 case '--output':
207 $i++;
208 if ( isset( $argv[$i] ) ) {
209 $doxyOutput = realpath( $argv[$i] );
210 }
211 break;
212 case '--generate-man':
213 $wgDoxyGenerateMan = true;
214 break;
215 case '--help':
216 print <<<END
217 Usage: php mwdocgen.php [<command>] [<options>]
218
219 Commands:
220 --all Process entire codebase
221 --includes Process only files in includes/ dir
222 --languages Process only files in languages/ dir
223 --maintenance Process only files in maintenance/ dir
224 --skins Process only files in skins/ dir
225 --file <file> Process only the given file
226 --no-extensions Process everything but extensions directorys
227
228 If no command is given, you will be prompted.
229
230 Other options:
231 --output <dir> Set output directory (default $doxyOutput)
232 --generate-man Generates man page documentation
233 --help Show this help and exit.
234
235
236 END;
237 exit(0);
238 break;
239 }
240 }
241 }
242
243 // TODO : generate a list of paths ))
244
245 if ( $input === '' ) {
246 echo <<<OPTIONS
247 Several documentation possibilities:
248 0 : whole documentation (1 + 2 + 3 + 4)
249 1 : only includes
250 2 : only languages
251 3 : only maintenance
252 4 : only skins
253 5 : only a given file
254 6 : all but the extensions directory
255 OPTIONS;
256 while ( !is_numeric( $input ) )
257 {
258 $input = readaline( "\nEnter your choice [0]:" );
259 if ( $input == '' ) {
260 $input = 0;
261 }
262 }
263 }
264
265 switch ( $input ) {
266 case 0: $input = $mwPath; break;
267 case 1: $input = $mwPathI; break;
268 case 2: $input = $mwPathL; break;
269 case 3: $input = $mwPathM; break;
270 case 4: $input = $mwPathS; break;
271 case 5:
272 if ( !isset( $file ) ) {
273 $file = readaline( "Enter file name $mwPath" );
274 }
275 $input = $mwPath . $file;
276 case 6:
277 $input = $mwPath;
278 $exclude_patterns = 'extensions';
279 }
280
281 $versionNumber = getSvnRevision( $input );
282 if ( $versionNumber === false ) { # Not using subversion ?
283 $svnstat = ''; # Not really useful if subversion not available
284 # @todo FIXME
285 $version = 'trunk';
286 } else {
287 $version = "trunk (r$versionNumber)";
288 }
289
290 // Generate path exclusions
291 $excludedPaths = $mwPath . join( " $mwPath", $mwExcludePaths );
292 print "EXCLUDE: $excludedPaths\n\n";
293
294 $generatedConf = generateConfigFile( $doxygenTemplate, $doxyOutput, $mwPath, $version, $svnstat, $input, $excludedPaths, $exclude_patterns );
295 $command = $doxygenBin . ' ' . $generatedConf;
296
297 echo <<<TEXT
298 ---------------------------------------------------
299 Launching the command:
300
301 $command
302
303 ---------------------------------------------------
304
305 TEXT;
306
307 passthru( $command );
308
309 echo <<<TEXT
310 ---------------------------------------------------
311 Doxygen execution finished.
312 Check above for possible errors.
313
314 You might want to delete the temporary file $generatedConf
315
316 TEXT;