Localisation updates for core and extension messages from translatewiki.net (2010...
[lhc/web/wiklou.git] / maintenance / mwdocgen.php
1 <?php
2 /**
3 * Script to easily generate the mediawiki documentation using doxygen.
4 *
5 * By default it will generate the whole documentation but you will be able to
6 * generate just some parts.
7 *
8 * Usage:
9 * php mwdocgen.php
10 *
11 * Then make a selection from the menu
12 *
13 * KNOWN BUGS:
14 *
15 * - pass_thru seems to always use buffering (even with ob_implicit_flush()),
16 * that make output slow when doxygen parses language files.
17 * - the menu doesnt work, got disabled at revision 13740. Need to code it.
18 *
19 *
20 * @file
21 * @todo document
22 * @ingroup Maintenance
23 *
24 * @author Ashar Voultoiz <thoane@altern.org>
25 * @version first release
26 */
27
28 #
29 # Variables / Configuration
30 #
31
32 if ( php_sapi_name() != 'cli' ) {
33 echo 'Run me from the command line.';
34 die( -1 );
35 }
36
37 /** Figure out the base directory for MediaWiki location */
38 $mwPath = dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR;
39
40 /** doxygen binary script */
41 $doxygenBin = 'doxygen';
42
43 /** doxygen configuration template for mediawiki */
44 $doxygenTemplate = $mwPath . 'maintenance/Doxyfile';
45
46 /** svnstat command, used to get the version of each file */
47 $svnstat = $mwPath . 'bin/svnstat';
48
49 /** where Phpdoc should output documentation */
50 # $doxyOutput = '/var/www/mwdoc/';
51 $doxyOutput = $mwPath . 'docs' . DIRECTORY_SEPARATOR ;
52
53 /** MediaWiki subpaths */
54 $mwPathI = $mwPath . 'includes/';
55 $mwPathL = $mwPath . 'languages/';
56 $mwPathM = $mwPath . 'maintenance/';
57 $mwPathS = $mwPath . 'skins/';
58
59 /** Variable to get user input */
60 $input = '';
61 $exclude = '';
62
63 #
64 # Functions
65 #
66
67 define( 'MEDIAWIKI', true );
68 require_once( "$mwPath/includes/GlobalFunctions.php" );
69
70 /**
71 * Read a line from the shell
72 * @param $prompt String
73 */
74 function readaline( $prompt = '' ) {
75 print $prompt;
76 $fp = fopen( "php://stdin", "r" );
77 $resp = trim( fgets( $fp, 1024 ) );
78 fclose( $fp );
79 return $resp;
80 }
81
82 /**
83 * Copied from SpecialVersion::getSvnRevision()
84 * @param $dir String
85 * @return Mixed: string or false
86 */
87 function getSvnRevision( $dir ) {
88 // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
89 $entries = $dir . '/.svn/entries';
90
91 if ( !file_exists( $entries ) ) {
92 return false;
93 }
94
95 $content = file( $entries );
96
97 // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
98 if ( preg_match( '/^<\?xml/', $content[0] ) ) {
99 // subversion is release <= 1.3
100 if ( !function_exists( 'simplexml_load_file' ) ) {
101 // We could fall back to expat... YUCK
102 return false;
103 }
104
105 $xml = simplexml_load_file( $entries );
106
107 if ( $xml ) {
108 foreach ( $xml->entry as $entry ) {
109 if ( $xml->entry[0]['name'] == '' ) {
110 // The directory entry should always have a revision marker.
111 if ( $entry['revision'] ) {
112 return intval( $entry['revision'] );
113 }
114 }
115 }
116 }
117 return false;
118 } else {
119 // subversion is release 1.4
120 return intval( $content[3] );
121 }
122 }
123
124 /**
125 * Generate a configuration file given user parameters and return the temporary filename.
126 * @param $doxygenTemplate String: full path for the template.
127 * @param $outputDirectory String: directory where the stuff will be output.
128 * @param $stripFromPath String: path that should be stripped out (usually mediawiki base path).
129 * @param $currentVersion String: Version number of the software
130 * @param $svnstat String: path to the svnstat file
131 * @param $input String: Path to analyze.
132 * @param $exclude String: Additionals path regex to exlcude
133 * (LocalSettings.php, AdminSettings.php and .svn directories are always excluded)
134 */
135 function generateConfigFile( $doxygenTemplate, $outputDirectory, $stripFromPath, $currentVersion, $svnstat, $input, $exclude ) {
136
137 $template = file_get_contents( $doxygenTemplate );
138
139 // Replace template placeholders by correct values.
140 $replacements = array(
141 '{{OUTPUT_DIRECTORY}}' => $outputDirectory,
142 '{{STRIP_FROM_PATH}}' => $stripFromPath,
143 '{{CURRENT_VERSION}}' => $currentVersion,
144 '{{SVNSTAT}}' => $svnstat,
145 '{{INPUT}}' => $input,
146 '{{EXCLUDE}}' => $exclude,
147 );
148 $tmpCfg = str_replace( array_keys( $replacements ), array_values( $replacements ), $template );
149 $tmpFileName = tempnam( wfTempDir(), 'mwdocgen-' );
150 file_put_contents( $tmpFileName , $tmpCfg ) or die( "Could not write doxygen configuration to file $tmpFileName\n" );
151
152 return $tmpFileName;
153 }
154
155 #
156 # Main !
157 #
158
159 unset( $file );
160
161 if ( is_array( $argv ) && isset( $argv[1] ) ) {
162 switch( $argv[1] ) {
163 case '--all': $input = 0; break;
164 case '--includes': $input = 1; break;
165 case '--languages': $input = 2; break;
166 case '--maintenance': $input = 3; break;
167 case '--skins': $input = 4; break;
168 case '--file':
169 $input = 5;
170 if ( isset( $argv[2] ) ) {
171 $file = $argv[2];
172 }
173 break;
174 case '--no-extensions': $input = 6; break;
175 }
176 }
177
178 // TODO : generate a list of paths ))
179
180 if ( $input === '' ) {
181 echo <<<OPTIONS
182 Several documentation possibilities:
183 0 : whole documentation (1 + 2 + 3 + 4)
184 1 : only includes
185 2 : only languages
186 3 : only maintenance
187 4 : only skins
188 5 : only a given file
189 6 : all but the extensions directory
190 OPTIONS;
191 while ( !is_numeric( $input ) )
192 {
193 $input = readaline( "\nEnter your choice [0]:" );
194 if ( $input == '' ) {
195 $input = 0;
196 }
197 }
198 }
199
200 switch ( $input ) {
201 case 0: $input = $mwPath; break;
202 case 1: $input = $mwPathI; break;
203 case 2: $input = $mwPathL; break;
204 case 3: $input = $mwPathM; break;
205 case 4: $input = $mwPathS; break;
206 case 5:
207 if ( !isset( $file ) ) {
208 $file = readaline( "Enter file name $mwPath" );
209 }
210 $input = $mwPath . $file;
211 case 6:
212 $input = $mwPath;
213 $exclude = 'extensions';
214 }
215
216 $versionNumber = getSvnRevision( $input );
217 if ( $versionNumber === false ) { # Not using subversion ?
218 $svnstat = ''; # Not really useful if subversion not available
219 $version = 'trunk'; # FIXME
220 } else {
221 $version = "trunk (r$versionNumber)";
222 }
223
224 $generatedConf = generateConfigFile( $doxygenTemplate, $doxyOutput, $mwPath, $version, $svnstat, $input, $exclude );
225 $command = $doxygenBin . ' ' . $generatedConf;
226
227 echo <<<TEXT
228 ---------------------------------------------------
229 Launching the command:
230
231 $command
232
233 ---------------------------------------------------
234
235 TEXT;
236
237 passthru( $command );
238
239 echo <<<TEXT
240 ---------------------------------------------------
241 Doxygen execution finished.
242 Check above for possible errors.
243
244 You might want to delete the temporary file $generatedConf
245
246 TEXT;