Merge "Declare dynamic properties"
[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 /** @var string */
46 private $doxygen;
47 /** @var string */
48 private $mwVersion;
49 /** @var string */
50 private $output;
51 /** @var string */
52 private $input;
53 /** @var string */
54 private $inputFilter;
55 /** @var string */
56 private $template;
57 /** @var string[] */
58 private $excludes;
59 /** @var string[] */
60 private $excludePatterns;
61 /** @var bool */
62 private $doDot;
63 /** @var bool */
64 private $doMan;
65
66 /**
67 * Prepare Maintenance class
68 */
69 public function __construct() {
70 parent::__construct();
71 $this->addDescription( 'Build doxygen documentation' );
72
73 $this->addOption( 'doxygen',
74 'Path to doxygen',
75 false, true );
76 $this->addOption( 'version',
77 'Pass a MediaWiki version',
78 false, true );
79 $this->addOption( 'file',
80 "Only process given file or directory. Multiple values " .
81 "accepted with comma separation. Path relative to \$IP.",
82 false, true );
83 $this->addOption( 'output',
84 'Path to write doc to',
85 false, true );
86 $this->addOption( 'no-extensions',
87 'Ignore extensions' );
88 }
89
90 public function getDbType() {
91 return Maintenance::DB_NONE;
92 }
93
94 protected function init() {
95 global $wgPhpCli, $IP;
96
97 $this->doxygen = $this->getOption( 'doxygen', 'doxygen' );
98 $this->mwVersion = $this->getOption( 'version', 'master' );
99
100 $this->input = '';
101 $inputs = explode( ',', $this->getOption( 'file', '' ) );
102 foreach ( $inputs as $input ) {
103 # Doxygen inputs are space separted and double quoted
104 $this->input .= " \"$IP/$input\"";
105 }
106
107 $this->output = $this->getOption( 'output', "$IP/docs" );
108
109 // Do not use wfShellWikiCmd, because mwdoc-filter.php is not
110 // a Maintenance script.
111 $this->inputFilter = Shell::escape( [
112 $wgPhpCli,
113 $IP . '/maintenance/mwdoc-filter.php'
114 ] );
115
116 $this->template = $IP . '/maintenance/Doxyfile';
117 $this->excludes = [
118 'vendor',
119 'node_modules',
120 'resources/lib',
121 'images',
122 'static',
123 'tests',
124 'includes/libs/Message/README.md',
125 'includes/libs/objectcache/README.md',
126 'includes/libs/ParamValidator/README.md',
127 'maintenance/benchmarks/README.md',
128 'resources/src/mediawiki.ui/styleguide.md',
129 ];
130 $this->excludePatterns = [];
131 if ( $this->hasOption( 'no-extensions' ) ) {
132 $this->excludePatterns[] = 'extensions';
133 $this->excludePatterns[] = 'skins';
134 }
135
136 $this->doDot = shell_exec( 'which dot' );
137 }
138
139 public function execute() {
140 global $IP;
141
142 $this->init();
143
144 # Build out directories we want to exclude
145 $exclude = '';
146 foreach ( $this->excludes as $item ) {
147 $exclude .= " $IP/$item";
148 }
149
150 $excludePatterns = implode( ' ', $this->excludePatterns );
151
152 $conf = strtr( file_get_contents( $this->template ),
153 [
154 '{{OUTPUT_DIRECTORY}}' => $this->output,
155 '{{STRIP_FROM_PATH}}' => $IP,
156 '{{CURRENT_VERSION}}' => $this->mwVersion,
157 '{{INPUT}}' => $this->input,
158 '{{EXCLUDE}}' => $exclude,
159 '{{EXCLUDE_PATTERNS}}' => $excludePatterns,
160 '{{HAVE_DOT}}' => $this->doDot ? 'YES' : 'NO',
161 '{{INPUT_FILTER}}' => $this->inputFilter,
162 ]
163 );
164
165 $tmpFile = tempnam( wfTempDir(), 'MWDocGen-' );
166 if ( file_put_contents( $tmpFile, $conf ) === false ) {
167 $this->fatalError( "Could not write doxygen configuration to file $tmpFile\n" );
168 }
169
170 $command = $this->doxygen . ' ' . $tmpFile;
171 $this->output( "Executing command:\n$command\n" );
172
173 $exitcode = 1;
174 system( $command, $exitcode );
175
176 $this->output( <<<TEXT
177 ---------------------------------------------------
178 Doxygen execution finished.
179 Check above for possible errors.
180
181 You might want to delete the temporary file:
182 $tmpFile
183 ---------------------------------------------------
184
185 TEXT
186 );
187
188 if ( $exitcode !== 0 ) {
189 $this->fatalError( "Something went wrong (exit: $exitcode)\n", $exitcode );
190 }
191 }
192 }
193
194 $maintClass = MWDocGen::class;
195 require_once RUN_MAINTENANCE_IF_MAIN;