Fixes Bug 31865 - Tag <dws> for discarding whitespaces.
[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
82 #
83 # Functions
84 #
85
86 define( 'MEDIAWIKI', true );
87 require_once( "$mwPath/includes/GlobalFunctions.php" );
88
89 /**
90 * Read a line from the shell
91 * @param $prompt String
92 * @return string
93 */
94 function readaline( $prompt = '' ) {
95 print $prompt;
96 $fp = fopen( "php://stdin", "r" );
97 $resp = trim( fgets( $fp, 1024 ) );
98 fclose( $fp );
99 return $resp;
100 }
101
102 /**
103 * Copied from SpecialVersion::getSvnRevision()
104 * @param $dir String
105 * @return Mixed: string or false
106 */
107 function getSvnRevision( $dir ) {
108 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
109 $entries = $dir . '/.svn/entries';
110
111 if ( !file_exists( $entries ) ) {
112 return false;
113 }
114
115 $content = file( $entries );
116
117 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
118 if ( preg_match( '/^<\?xml/', $content[0] ) ) {
119 // subversion is release <= 1.3
120 if ( !function_exists( 'simplexml_load_file' ) ) {
121 // We could fall back to expat... YUCK
122 return false;
123 }
124
125 $xml = simplexml_load_file( $entries );
126
127 if ( $xml ) {
128 foreach ( $xml->entry as $entry ) {
129 if ( $xml->entry[0]['name'] == '' ) {
130 // The directory entry should always have a revision marker.
131 if ( $entry['revision'] ) {
132 return intval( $entry['revision'] );
133 }
134 }
135 }
136 }
137 return false;
138 } else {
139 // subversion is release 1.4
140 return intval( $content[3] );
141 }
142 }
143
144 /**
145 * Generate a configuration file given user parameters and return the temporary filename.
146 * @param $doxygenTemplate String: full path for the template.
147 * @param $outputDirectory String: directory where the stuff will be output.
148 * @param $stripFromPath String: path that should be stripped out (usually mediawiki base path).
149 * @param $currentVersion String: Version number of the software
150 * @param $svnstat String: path to the svnstat file
151 * @param $input String: Path to analyze.
152 * @param $exclude String: Additionals path regex to exclude
153 * @param $exclude_patterns String: Additionals path regex to exclude
154 * (LocalSettings.php, AdminSettings.php, .svn and .git directories are always excluded)
155 * @return string
156 */
157 function generateConfigFile( $doxygenTemplate, $outputDirectory, $stripFromPath, $currentVersion, $svnstat, $input, $exclude, $exclude_patterns ) {
158
159 global $wgDoxyGenerateMan;
160
161 $template = file_get_contents( $doxygenTemplate );
162
163 // Replace template placeholders by correct values.
164 $replacements = array(
165 '{{OUTPUT_DIRECTORY}}' => $outputDirectory,
166 '{{STRIP_FROM_PATH}}' => $stripFromPath,
167 '{{CURRENT_VERSION}}' => $currentVersion,
168 '{{SVNSTAT}}' => $svnstat,
169 '{{INPUT}}' => $input,
170 '{{EXCLUDE}}' => $exclude,
171 '{{EXCLUDE_PATTERNS}}' => $exclude_patterns,
172 '{{HAVE_DOT}}' => `which dot` ? 'YES' : 'NO',
173 '{{GENERATE_MAN}}' => $wgDoxyGenerateMan ? 'YES' : 'NO',
174 );
175 $tmpCfg = str_replace( array_keys( $replacements ), array_values( $replacements ), $template );
176 $tmpFileName = tempnam( wfTempDir(), 'mwdocgen-' );
177 file_put_contents( $tmpFileName , $tmpCfg ) or die( "Could not write doxygen configuration to file $tmpFileName\n" );
178
179 return $tmpFileName;
180 }
181
182 #
183 # Main !
184 #
185
186 unset( $file );
187
188 if ( is_array( $argv ) ) {
189 for ($i = 0; $i < count($argv); $i++ ) {
190 switch( $argv[$i] ) {
191 case '--all': $input = 0; break;
192 case '--includes': $input = 1; break;
193 case '--languages': $input = 2; break;
194 case '--maintenance': $input = 3; break;
195 case '--skins': $input = 4; break;
196 case '--file':
197 $input = 5;
198 $i++;
199 if ( isset( $argv[$i] ) ) {
200 $file = $argv[$i];
201 }
202 break;
203 case '--no-extensions': $input = 6; break;
204 case '--output':
205 $i++;
206 if ( isset( $argv[$i] ) ) {
207 $doxyOutput = realpath( $argv[$i] );
208 }
209 break;
210 case '--generate-man':
211 $wgDoxyGenerateMan = true;
212 break;
213 case '--help':
214 print <<<END
215 Usage: php mwdocgen.php [<command>] [<options>]
216
217 Commands:
218 --all Process entire codebase
219 --includes Process only files in includes/ dir
220 --languages Process only files in languages/ dir
221 --maintenance Process only files in maintenance/ dir
222 --skins Process only files in skins/ dir
223 --file <file> Process only the given file
224
225 If no command is given, you will be prompted.
226
227 Other options:
228 --output <dir> Set output directory (default $doxyOutput)
229 --generate-man Generates man page documentation
230 --help Show this help and exit.
231
232
233 END;
234 exit(0);
235 break;
236 }
237 }
238 }
239
240 // TODO : generate a list of paths ))
241
242 if ( $input === '' ) {
243 echo <<<OPTIONS
244 Several documentation possibilities:
245 0 : whole documentation (1 + 2 + 3 + 4)
246 1 : only includes
247 2 : only languages
248 3 : only maintenance
249 4 : only skins
250 5 : only a given file
251 6 : all but the extensions directory
252 OPTIONS;
253 while ( !is_numeric( $input ) )
254 {
255 $input = readaline( "\nEnter your choice [0]:" );
256 if ( $input == '' ) {
257 $input = 0;
258 }
259 }
260 }
261
262 switch ( $input ) {
263 case 0: $input = $mwPath; break;
264 case 1: $input = $mwPathI; break;
265 case 2: $input = $mwPathL; break;
266 case 3: $input = $mwPathM; break;
267 case 4: $input = $mwPathS; break;
268 case 5:
269 if ( !isset( $file ) ) {
270 $file = readaline( "Enter file name $mwPath" );
271 }
272 $input = $mwPath . $file;
273 case 6:
274 $input = $mwPath;
275 $exclude_patterns = 'extensions';
276 }
277
278 $versionNumber = getSvnRevision( $input );
279 if ( $versionNumber === false ) { # Not using subversion ?
280 $svnstat = ''; # Not really useful if subversion not available
281 # @todo FIXME
282 $version = 'trunk';
283 } else {
284 $version = "trunk (r$versionNumber)";
285 }
286
287 // Generate path exclusions
288 $excludedPaths = $mwPath . join( " $mwPath", $mwExcludePaths );
289 print "EXCLUDE: $excludedPaths\n\n";
290
291 $generatedConf = generateConfigFile( $doxygenTemplate, $doxyOutput, $mwPath, $version, $svnstat, $input, $excludedPaths, $exclude_patterns );
292 $command = $doxygenBin . ' ' . $generatedConf;
293
294 echo <<<TEXT
295 ---------------------------------------------------
296 Launching the command:
297
298 $command
299
300 ---------------------------------------------------
301
302 TEXT;
303
304 passthru( $command );
305
306 echo <<<TEXT
307 ---------------------------------------------------
308 Doxygen execution finished.
309 Check above for possible errors.
310
311 You might want to delete the temporary file $generatedConf
312
313 TEXT;