Three small fixes to mediawiki.specials.preferences.js
[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_patterns String: Additionals path regex to exclude
152 * (LocalSettings.php, AdminSettings.php, .svn and .git directories are always excluded)
153 */
154 function generateConfigFile( $doxygenTemplate, $outputDirectory, $stripFromPath, $currentVersion, $svnstat, $input, $exclude_patterns ) {
155
156 $template = file_get_contents( $doxygenTemplate );
157
158 // Generate path exclusions
159 global $mwExcludePaths, $mwPath;
160 $excludedPaths = $mwPath . join( " $mwPath", $mwExcludePaths );
161 print "EXCLUDE: $excludedPaths\n\n";
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}}' => $excludedPaths,
171 '{{EXCLUDE_PATTERNS}}' => $exclude_patterns,
172 '{{HAVE_DOT}}' => `which dot` ? 'YES' : 'NO',
173 );
174 $tmpCfg = str_replace( array_keys( $replacements ), array_values( $replacements ), $template );
175 $tmpFileName = tempnam( wfTempDir(), 'mwdocgen-' );
176 file_put_contents( $tmpFileName , $tmpCfg ) or die( "Could not write doxygen configuration to file $tmpFileName\n" );
177
178 return $tmpFileName;
179 }
180
181 #
182 # Main !
183 #
184
185 unset( $file );
186
187 if ( is_array( $argv ) && isset( $argv[1] ) ) {
188 switch( $argv[1] ) {
189 case '--all': $input = 0; break;
190 case '--includes': $input = 1; break;
191 case '--languages': $input = 2; break;
192 case '--maintenance': $input = 3; break;
193 case '--skins': $input = 4; break;
194 case '--file':
195 $input = 5;
196 if ( isset( $argv[2] ) ) {
197 $file = $argv[2];
198 }
199 break;
200 case '--no-extensions': $input = 6; break;
201 }
202 }
203
204 // TODO : generate a list of paths ))
205
206 if ( $input === '' ) {
207 echo <<<OPTIONS
208 Several documentation possibilities:
209 0 : whole documentation (1 + 2 + 3 + 4)
210 1 : only includes
211 2 : only languages
212 3 : only maintenance
213 4 : only skins
214 5 : only a given file
215 6 : all but the extensions directory
216 OPTIONS;
217 while ( !is_numeric( $input ) )
218 {
219 $input = readaline( "\nEnter your choice [0]:" );
220 if ( $input == '' ) {
221 $input = 0;
222 }
223 }
224 }
225
226 switch ( $input ) {
227 case 0: $input = $mwPath; break;
228 case 1: $input = $mwPathI; break;
229 case 2: $input = $mwPathL; break;
230 case 3: $input = $mwPathM; break;
231 case 4: $input = $mwPathS; break;
232 case 5:
233 if ( !isset( $file ) ) {
234 $file = readaline( "Enter file name $mwPath" );
235 }
236 $input = $mwPath . $file;
237 case 6:
238 $input = $mwPath;
239 $exclude_patterns = 'extensions';
240 }
241
242 $versionNumber = getSvnRevision( $input );
243 if ( $versionNumber === false ) { # Not using subversion ?
244 $svnstat = ''; # Not really useful if subversion not available
245 $version = 'trunk'; # FIXME
246 } else {
247 $version = "trunk (r$versionNumber)";
248 }
249
250 $generatedConf = generateConfigFile( $doxygenTemplate, $doxyOutput, $mwPath, $version, $svnstat, $input, $exclude_patterns );
251 $command = $doxygenBin . ' ' . $generatedConf;
252
253 echo <<<TEXT
254 ---------------------------------------------------
255 Launching the command:
256
257 $command
258
259 ---------------------------------------------------
260
261 TEXT;
262
263 passthru( $command );
264
265 echo <<<TEXT
266 ---------------------------------------------------
267 Doxygen execution finished.
268 Check above for possible errors.
269
270 You might want to delete the temporary file $generatedConf
271
272 TEXT;