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