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