Merge "Add wfDeprecated to wfMsg* methods from 1.21 on"
[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( __DIR__ ) . DIRECTORY_SEPARATOR;
53
54 /** doxygen binary script */
55 $doxygenBin = 'doxygen';
56
57 /** doxygen configuration template for mediawiki */
58 $doxygenTemplate = $mwPath . 'maintenance/Doxyfile';
59
60 /** doxygen input filter to tweak source file before they are parsed */
61 $doxygenInputFilter = "php {$mwPath}maintenance/mwdoc-filter.php";
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 $excludePatterns = '';
81 /** Whether to generates man pages: */
82 $doxyGenerateMan = false;
83
84 #
85 # Functions
86 #
87
88 define( 'MEDIAWIKI', true );
89 require_once( "$mwPath/includes/GlobalFunctions.php" );
90
91 /**
92 * Read a line from the shell
93 * @param $prompt String
94 * @return string
95 */
96 function readaline( $prompt = '' ) {
97 print $prompt;
98 $fp = fopen( "php://stdin", "r" );
99 $resp = trim( fgets( $fp, 1024 ) );
100 fclose( $fp );
101 return $resp;
102 }
103
104 /**
105 * Generate a configuration file given user parameters and return the temporary filename.
106 * @param $doxygenTemplate String: full path for the template.
107 * @param $outputDirectory String: directory where the stuff will be output.
108 * @param $stripFromPath String: path that should be stripped out (usually mediawiki base path).
109 * @param $currentVersion String: Version number of the software
110 * @param $input String: Path to analyze.
111 * @param $exclude String: Additionals path regex to exclude
112 * @param $excludePatterns String: Additionals path regex to exclude
113 * (LocalSettings.php, AdminSettings.php, .svn and .git directories are always excluded)
114 * @param $doxyGenerateMan Boolean
115 * @return string
116 */
117 function generateConfigFile( $doxygenTemplate, $outputDirectory, $stripFromPath, $currentVersion, $input, $exclude, $excludePatterns, $doxyGenerateMan ) {
118 global $doxygenInputFilter;
119
120 $template = file_get_contents( $doxygenTemplate );
121 // Replace template placeholders by correct values.
122 $replacements = array(
123 '{{OUTPUT_DIRECTORY}}' => $outputDirectory,
124 '{{STRIP_FROM_PATH}}' => $stripFromPath,
125 '{{CURRENT_VERSION}}' => $currentVersion,
126 '{{INPUT}}' => $input,
127 '{{EXCLUDE}}' => $exclude,
128 '{{EXCLUDE_PATTERNS}}' => $excludePatterns,
129 '{{HAVE_DOT}}' => `which dot` ? 'YES' : 'NO',
130 '{{GENERATE_MAN}}' => $doxyGenerateMan ? 'YES' : 'NO',
131 '{{INPUT_FILTER}}' => $doxygenInputFilter,
132 );
133 $tmpCfg = str_replace( array_keys( $replacements ), array_values( $replacements ), $template );
134 $tmpFileName = tempnam( wfTempDir(), 'mwdocgen-' );
135 file_put_contents( $tmpFileName , $tmpCfg ) or die( "Could not write doxygen configuration to file $tmpFileName\n" );
136
137 return $tmpFileName;
138 }
139
140 #
141 # Main !
142 #
143
144 unset( $file );
145
146 if ( is_array( $argv ) ) {
147 for ($i = 0; $i < count($argv); $i++ ) {
148 switch( $argv[$i] ) {
149 case '--all': $input = 0; break;
150 case '--includes': $input = 1; break;
151 case '--languages': $input = 2; break;
152 case '--maintenance': $input = 3; break;
153 case '--skins': $input = 4; break;
154 case '--file':
155 $input = 5;
156 $i++;
157 if ( isset( $argv[$i] ) ) {
158 $file = $argv[$i];
159 }
160 break;
161 case '--no-extensions': $input = 6; break;
162 case '--output':
163 $i++;
164 if ( isset( $argv[$i] ) ) {
165 $doxyOutput = realpath( $argv[$i] );
166 }
167 break;
168 case '--generate-man':
169 $doxyGenerateMan = true;
170 break;
171 case '--help':
172 print <<<END
173 Usage: php mwdocgen.php [<command>] [<options>]
174
175 Commands:
176 --all Process entire codebase
177 --includes Process only files in includes/ dir
178 --languages Process only files in languages/ dir
179 --maintenance Process only files in maintenance/ dir
180 --skins Process only files in skins/ dir
181 --file <file> Process only the given file
182 --no-extensions Process everything but extensions directorys
183
184 If no command is given, you will be prompted.
185
186 Other options:
187 --output <dir> Set output directory (default $doxyOutput)
188 --generate-man Generates man page documentation
189 --help Show this help and exit.
190
191
192 END;
193 exit(0);
194 break;
195 }
196 }
197 }
198
199 // TODO : generate a list of paths ))
200
201 if ( $input === '' ) {
202 echo <<<OPTIONS
203 Several documentation possibilities:
204 0 : whole documentation (1 + 2 + 3 + 4)
205 1 : only includes
206 2 : only languages
207 3 : only maintenance
208 4 : only skins
209 5 : only a given file
210 6 : all but the extensions directory
211 OPTIONS;
212 while ( !is_numeric( $input ) )
213 {
214 $input = readaline( "\nEnter your choice [0]:" );
215 if ( $input == '' ) {
216 $input = 0;
217 }
218 }
219 }
220
221 switch ( $input ) {
222 case 0: $input = $mwPath; break;
223 case 1: $input = $mwPathI; break;
224 case 2: $input = $mwPathL; break;
225 case 3: $input = $mwPathM; break;
226 case 4: $input = $mwPathS; break;
227 case 5:
228 if ( !isset( $file ) ) {
229 $file = readaline( "Enter file name $mwPath" );
230 }
231 $input = $mwPath . $file;
232 break;
233 case 6:
234 $input = $mwPath;
235 $excludePatterns = 'extensions';
236 }
237
238 // @todo FIXME to work on git
239 $version = 'master';
240
241 // Generate path exclusions
242 $excludedPaths = $mwPath . join( " $mwPath", $mwExcludePaths );
243 print "EXCLUDE: $excludedPaths\n\n";
244
245 $generatedConf = generateConfigFile( $doxygenTemplate, $doxyOutput, $mwPath, $version, $input, $excludedPaths, $excludePatterns, $doxyGenerateMan );
246 $command = $doxygenBin . ' ' . $generatedConf;
247
248 echo <<<TEXT
249 ---------------------------------------------------
250 Launching the command:
251
252 $command
253
254 ---------------------------------------------------
255
256 TEXT;
257
258 passthru( $command );
259
260 echo <<<TEXT
261 ---------------------------------------------------
262 Doxygen execution finished.
263 Check above for possible errors.
264
265 You might want to delete the temporary file $generatedConf
266
267 TEXT;