script to parse wikitext from CLI
[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 $template = file_get_contents( $doxygenTemplate );
160
161 // Replace template placeholders by correct values.
162 $replacements = array(
163 '{{OUTPUT_DIRECTORY}}' => $outputDirectory,
164 '{{STRIP_FROM_PATH}}' => $stripFromPath,
165 '{{CURRENT_VERSION}}' => $currentVersion,
166 '{{SVNSTAT}}' => $svnstat,
167 '{{INPUT}}' => $input,
168 '{{EXCLUDE}}' => $exclude,
169 '{{EXCLUDE_PATTERNS}}' => $exclude_patterns,
170 '{{HAVE_DOT}}' => `which dot` ? 'YES' : 'NO',
171 );
172 $tmpCfg = str_replace( array_keys( $replacements ), array_values( $replacements ), $template );
173 $tmpFileName = tempnam( wfTempDir(), 'mwdocgen-' );
174 file_put_contents( $tmpFileName , $tmpCfg ) or die( "Could not write doxygen configuration to file $tmpFileName\n" );
175
176 return $tmpFileName;
177 }
178
179 #
180 # Main !
181 #
182
183 unset( $file );
184
185 if ( is_array( $argv ) ) {
186 for ($i = 0; $i < count($argv); $i++ ) {
187 switch( $argv[$i] ) {
188 case '--all': $input = 0; break;
189 case '--includes': $input = 1; break;
190 case '--languages': $input = 2; break;
191 case '--maintenance': $input = 3; break;
192 case '--skins': $input = 4; break;
193 case '--file':
194 $input = 5;
195 $i++;
196 if ( isset( $argv[$i] ) ) {
197 $file = $argv[$i];
198 }
199 break;
200 case '--no-extensions': $input = 6; break;
201 case '--output':
202 $i++;
203 if ( isset( $argv[$i] ) ) {
204 $doxyOutput = realpath( $argv[$i] );
205 }
206 break;
207 case '--help':
208 print <<<END
209 Usage: php mwdocgen.php [<command>] [<options>]
210
211 Commands:
212 --all Process entire codebase
213 --includes Process only files in includes/ dir
214 --languages Process only files in languages/ dir
215 --maintenance Process only files in maintenance/ dir
216 --skins Process only files in skins/ dir
217 --file <file> Process only the given file
218
219 If no command is given, you will be prompted.
220
221 Other options:
222 --output <dir> Set output directory (default $doxyOutput)
223 --help Show this help and exit.
224
225
226 END;
227 exit(0);
228 break;
229 }
230 }
231 }
232
233 // TODO : generate a list of paths ))
234
235 if ( $input === '' ) {
236 echo <<<OPTIONS
237 Several documentation possibilities:
238 0 : whole documentation (1 + 2 + 3 + 4)
239 1 : only includes
240 2 : only languages
241 3 : only maintenance
242 4 : only skins
243 5 : only a given file
244 6 : all but the extensions directory
245 OPTIONS;
246 while ( !is_numeric( $input ) )
247 {
248 $input = readaline( "\nEnter your choice [0]:" );
249 if ( $input == '' ) {
250 $input = 0;
251 }
252 }
253 }
254
255 switch ( $input ) {
256 case 0: $input = $mwPath; break;
257 case 1: $input = $mwPathI; break;
258 case 2: $input = $mwPathL; break;
259 case 3: $input = $mwPathM; break;
260 case 4: $input = $mwPathS; break;
261 case 5:
262 if ( !isset( $file ) ) {
263 $file = readaline( "Enter file name $mwPath" );
264 }
265 $input = $mwPath . $file;
266 case 6:
267 $input = $mwPath;
268 $exclude_patterns = 'extensions';
269 }
270
271 $versionNumber = getSvnRevision( $input );
272 if ( $versionNumber === false ) { # Not using subversion ?
273 $svnstat = ''; # Not really useful if subversion not available
274 # @todo FIXME
275 $version = 'trunk';
276 } else {
277 $version = "trunk (r$versionNumber)";
278 }
279
280 // Generate path exclusions
281 $excludedPaths = $mwPath . join( " $mwPath", $mwExcludePaths );
282 print "EXCLUDE: $excludedPaths\n\n";
283
284 $generatedConf = generateConfigFile( $doxygenTemplate, $doxyOutput, $mwPath, $version, $svnstat, $input, $excludedPaths, $exclude_patterns );
285 $command = $doxygenBin . ' ' . $generatedConf;
286
287 echo <<<TEXT
288 ---------------------------------------------------
289 Launching the command:
290
291 $command
292
293 ---------------------------------------------------
294
295 TEXT;
296
297 passthru( $command );
298
299 echo <<<TEXT
300 ---------------------------------------------------
301 Doxygen execution finished.
302 Check above for possible errors.
303
304 You might want to delete the temporary file $generatedConf
305
306 TEXT;