Fix SQLite patch-(page|template)links-fix-pk.sql column order
[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 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 * @todo document
28 * @ingroup Maintenance
29 *
30 * @author Antoine Musso <hashar at free dot fr>
31 * @author Brion Vibber
32 * @author Alexandre Emsenhuber
33 * @version first release
34 */
35
36 use MediaWiki\Shell\Shell;
37
38 require_once __DIR__ . '/Maintenance.php';
39
40 /**
41 * Maintenance script that builds doxygen documentation.
42 * @ingroup Maintenance
43 */
44 class MWDocGen extends Maintenance {
45
46 /**
47 * Prepare Maintenance class
48 */
49 public function __construct() {
50 parent::__construct();
51 $this->addDescription( 'Build doxygen documentation' );
52
53 $this->addOption( 'doxygen',
54 'Path to doxygen',
55 false, true );
56 $this->addOption( 'version',
57 'Pass a MediaWiki version',
58 false, true );
59 $this->addOption( 'generate-man',
60 'Whether to generate man files' );
61 $this->addOption( 'file',
62 "Only process given file or directory. Multiple values " .
63 "accepted with comma separation. Path relative to \$IP.",
64 false, true );
65 $this->addOption( 'output',
66 'Path to write doc to',
67 false, true );
68 $this->addOption( 'no-extensions',
69 'Ignore extensions' );
70 }
71
72 public function getDbType() {
73 return Maintenance::DB_NONE;
74 }
75
76 protected function init() {
77 global $wgPhpCli, $IP;
78
79 $this->doxygen = $this->getOption( 'doxygen', 'doxygen' );
80 $this->mwVersion = $this->getOption( 'version', 'master' );
81
82 $this->input = '';
83 $inputs = explode( ',', $this->getOption( 'file', '' ) );
84 foreach ( $inputs as $input ) {
85 # Doxygen inputs are space separted and double quoted
86 $this->input .= " \"$IP/$input\"";
87 }
88
89 $this->output = $this->getOption( 'output', "$IP/docs" );
90
91 // Do not use wfShellWikiCmd, because mwdoc-filter.php is not
92 // a Maintenance script.
93 $this->inputFilter = Shell::escape( [
94 $wgPhpCli,
95 $IP . '/maintenance/mwdoc-filter.php'
96 ] );
97
98 $this->template = $IP . '/maintenance/Doxyfile';
99 $this->excludes = [
100 'vendor',
101 'node_modules',
102 'images',
103 'static',
104 ];
105 $this->excludePatterns = [];
106 if ( $this->hasOption( 'no-extensions' ) ) {
107 $this->excludePatterns[] = 'extensions';
108 }
109
110 $this->doDot = shell_exec( 'which dot' );
111 $this->doMan = $this->hasOption( 'generate-man' );
112 }
113
114 public function execute() {
115 global $IP;
116
117 $this->init();
118
119 # Build out directories we want to exclude
120 $exclude = '';
121 foreach ( $this->excludes as $item ) {
122 $exclude .= " $IP/$item";
123 }
124
125 $excludePatterns = implode( ' ', $this->excludePatterns );
126
127 $conf = strtr( file_get_contents( $this->template ),
128 [
129 '{{OUTPUT_DIRECTORY}}' => $this->output,
130 '{{STRIP_FROM_PATH}}' => $IP,
131 '{{CURRENT_VERSION}}' => $this->mwVersion,
132 '{{INPUT}}' => $this->input,
133 '{{EXCLUDE}}' => $exclude,
134 '{{EXCLUDE_PATTERNS}}' => $excludePatterns,
135 '{{HAVE_DOT}}' => $this->doDot ? 'YES' : 'NO',
136 '{{GENERATE_MAN}}' => $this->doMan ? 'YES' : 'NO',
137 '{{INPUT_FILTER}}' => $this->inputFilter,
138 ]
139 );
140
141 $tmpFile = tempnam( wfTempDir(), 'MWDocGen-' );
142 if ( file_put_contents( $tmpFile, $conf ) === false ) {
143 $this->fatalError( "Could not write doxygen configuration to file $tmpFile\n" );
144 }
145
146 $command = $this->doxygen . ' ' . $tmpFile;
147 $this->output( "Executing command:\n$command\n" );
148
149 $exitcode = 1;
150 system( $command, $exitcode );
151
152 $this->output( <<<TEXT
153 ---------------------------------------------------
154 Doxygen execution finished.
155 Check above for possible errors.
156
157 You might want to delete the temporary file:
158 $tmpFile
159 ---------------------------------------------------
160
161 TEXT
162 );
163
164 if ( $exitcode !== 0 ) {
165 $this->fatalError( "Something went wrong (exit: $exitcode)\n", $exitcode );
166 }
167 }
168 }
169
170 $maintClass = MWDocGen::class;
171 require_once RUN_MAINTENANCE_IF_MAIN;