]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/mwdocgen.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.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 /** Global variable: temporary directory */
41 $tmpPath = '/tmp/';
42
43 /** doxygen binary script */
44 $doxygenBin = 'doxygen';
45
46 /** doxygen configuration template for mediawiki */
47 $doxygenTemplate = $mwPath . 'maintenance/Doxyfile';
48
49 /** svnstat command, used to get the version of each file */
50 $svnstat = $mwPath . 'bin/svnstat';
51
52 /** where Phpdoc should output documentation */
53 #$doxyOutput = '/var/www/mwdoc/';
54 $doxyOutput = $mwPath . 'docs' . DIRECTORY_SEPARATOR ;
55
56 /** MediaWiki subpaths */
57 $mwPathI = $mwPath.'includes/';
58 $mwPathL = $mwPath.'languages/';
59 $mwPathM = $mwPath.'maintenance/';
60 $mwPathS = $mwPath.'skins/';
61
62 /** Variable to get user input */
63 $input = '';
64
65 #
66 # Functions
67 #
68
69 /**
70  * Read a line from the shell
71  * @param $prompt String
72  */
73 function readaline( $prompt = '' ){
74         print $prompt;
75         $fp = fopen( "php://stdin", "r" );
76         $resp = trim( fgets( $fp, 1024 ) );
77         fclose( $fp );
78         return $resp;
79 }
80
81 /**
82  * Copied from SpecialVersion::getSvnRevision()
83  * @param $dir String
84  * @return Mixed: string or false
85  */
86 function getSvnRevision( $dir ) {
87         // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
88         $entries = $dir . '/.svn/entries';
89
90         if( !file_exists( $entries ) ) {
91                 return false;
92         }
93
94         $content = file( $entries );
95
96         // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
97         if( preg_match( '/^<\?xml/', $content[0] ) ) {
98                 // subversion is release <= 1.3
99                 if( !function_exists( 'simplexml_load_file' ) ) {
100                         // We could fall back to expat... YUCK
101                         return false;
102                 }
103
104                 $xml = simplexml_load_file( $entries );
105
106                 if( $xml ) {
107                         foreach( $xml->entry as $entry ) {
108                                 if( $xml->entry[0]['name'] == '' ) {
109                                         // The directory entry should always have a revision marker.
110                                         if( $entry['revision'] ) {
111                                                 return intval( $entry['revision'] );
112                                         }
113                                 }
114                         }
115                 }
116                 return false;
117         } else {
118                 // subversion is release 1.4
119                 return intval( $content[3] );
120         }
121 }
122
123 /**
124  * Generate a configuration file given user parameters and return the temporary filename.
125  * @param $doxygenTemplate String: full path for the template.
126  * @param $outputDirectory String: directory where the stuff will be output.
127  * @param $stripFromPath String: path that should be stripped out (usually mediawiki base path).
128  * @param $currentVersion String: Version number of the software
129  * @param $svnstat String: path to the svnstat file
130  * @param $input String: Path to analyze.
131  */
132 function generateConfigFile( $doxygenTemplate, $outputDirectory, $stripFromPath, $currentVersion, $svnstat, $input ){
133         global $tmpPath;
134
135         $template = file_get_contents( $doxygenTemplate );
136
137         // Replace template placeholders by correct values.     
138         $replacements = array(
139                 '{{OUTPUT_DIRECTORY}}' => $outputDirectory,
140                 '{{STRIP_FROM_PATH}}'  => $stripFromPath,
141                 '{{CURRENT_VERSION}}'  => $currentVersion,
142                 '{{SVNSTAT}}'          => $svnstat,
143                 '{{INPUT}}'            => $input,
144         );
145         $tmpCfg = str_replace( array_keys( $replacements ), array_values( $replacements ), $template );
146         $tmpFileName = $tmpPath . 'mwdocgen'. rand() .'.tmp';
147         file_put_contents( $tmpFileName , $tmpCfg ) or die("Could not write doxygen configuration to file $tmpFileName\n");
148
149         return $tmpFileName;
150 }
151
152 #
153 # Main !
154 #
155
156 unset( $file );
157
158 if( is_array( $argv ) && isset( $argv[1] ) ) {
159         switch( $argv[1] ) {
160         case '--all':         $input = 0; break;
161         case '--includes':    $input = 1; break;
162         case '--languages':   $input = 2; break;
163         case '--maintenance': $input = 3; break;
164         case '--skins':       $input = 4; break;
165         case '--file':
166                 $input = 5;
167                 if( isset( $argv[2] ) ) {
168                         $file = $argv[2];
169                 }
170                 break;
171         }
172 }
173
174 // TODO : generate a list of paths ))
175
176 if( $input === '' ) {
177         echo <<<OPTIONS
178 Several documentation possibilities:
179  0 : whole documentation (1 + 2 + 3 + 4)
180  1 : only includes
181  2 : only languages
182  3 : only maintenance
183  4 : only skins
184  5 : only a given file
185 OPTIONS;
186         while ( !is_numeric($input) )
187         {
188                 $input = readaline( "\nEnter your choice [0]:" );
189                 if($input == '') {
190                         $input = 0;
191                 }
192         }
193 }
194
195 switch ($input) {
196 case 0: $input = $mwPath;  break;
197 case 1: $input = $mwPathI; break;
198 case 2: $input = $mwPathL; break;
199 case 3: $input = $mwPathM; break;
200 case 4: $input = $mwPathS; break;
201 case 5:
202         if( !isset( $file ) ) {
203                 $file = readaline( "Enter file name $mwPath" );
204         }
205         $input = $mwPath.$file;
206 }
207
208 $versionNumber = getSvnRevision( $input );
209 if( $versionNumber === false ){ #Not using subversion ?
210         $svnstat = ''; # Not really useful if subversion not available
211         $version = 'trunk'; # FIXME
212 } else {
213         $version = "trunk (r$versionNumber)";
214 }
215
216 $generatedConf = generateConfigFile( $doxygenTemplate, $doxyOutput, $mwPath, $version, $svnstat, $input );
217 $command = $doxygenBin . ' ' . $generatedConf;
218
219 echo <<<TEXT
220 ---------------------------------------------------
221 Launching the command:
222
223 $command
224
225 ---------------------------------------------------
226
227 TEXT;
228
229 passthru( $command );
230
231 echo <<<TEXT
232 ---------------------------------------------------
233 Doxygen execution finished.
234 Check above for possible errors.
235
236 You might want to deleted the temporary file $generatedConf
237
238 TEXT;