Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[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 require_once __DIR__ . '/Maintenance.php';
37
38 /**
39 * Maintenance script that builds doxygen documentation.
40 * @ingroup Maintenance
41 */
42 class MWDocGen extends Maintenance {
43
44 /**
45 * Prepare Maintenance class
46 */
47 public function __construct() {
48 parent::__construct();
49 $this->addDescription( 'Build doxygen documentation' );
50
51 $this->addOption( 'doxygen',
52 'Path to doxygen',
53 false, true );
54 $this->addOption( 'version',
55 'Pass a MediaWiki version',
56 false, true );
57 $this->addOption( 'generate-man',
58 'Whether to generate man files' );
59 $this->addOption( 'file',
60 "Only process given file or directory. Multiple values " .
61 "accepted with comma separation. Path relative to \$IP.",
62 false, true );
63 $this->addOption( 'output',
64 'Path to write doc to',
65 false, true );
66 $this->addOption( 'no-extensions',
67 'Ignore extensions' );
68 }
69
70 public function getDbType() {
71 return Maintenance::DB_NONE;
72 }
73
74 protected function init() {
75 global $wgPhpCli, $IP;
76
77 $this->doxygen = $this->getOption( 'doxygen', 'doxygen' );
78 $this->mwVersion = $this->getOption( 'version', 'master' );
79
80 $this->input = '';
81 $inputs = explode( ',', $this->getOption( 'file', '' ) );
82 foreach ( $inputs as $input ) {
83 # Doxygen inputs are space separted and double quoted
84 $this->input .= " \"$IP/$input\"";
85 }
86
87 $this->output = $this->getOption( 'output', "$IP/docs" );
88
89 // Do not use wfShellWikiCmd, because mwdoc-filter.php is not
90 // a Maintenance script.
91 $this->inputFilter = wfEscapeShellArg( [
92 $wgPhpCli,
93 $IP . '/maintenance/mwdoc-filter.php'
94 ] );
95
96 $this->template = $IP . '/maintenance/Doxyfile';
97 $this->excludes = [
98 'vendor',
99 'node_modules',
100 'images',
101 'static',
102 ];
103 $this->excludePatterns = [];
104 if ( $this->hasOption( 'no-extensions' ) ) {
105 $this->excludePatterns[] = 'extensions';
106 }
107
108 $this->doDot = shell_exec( 'which dot' );
109 $this->doMan = $this->hasOption( 'generate-man' );
110 }
111
112 public function execute() {
113 global $IP;
114
115 $this->init();
116
117 # Build out directories we want to exclude
118 $exclude = '';
119 foreach ( $this->excludes as $item ) {
120 $exclude .= " $IP/$item";
121 }
122
123 $excludePatterns = implode( ' ', $this->excludePatterns );
124
125 $conf = strtr( file_get_contents( $this->template ),
126 [
127 '{{OUTPUT_DIRECTORY}}' => $this->output,
128 '{{STRIP_FROM_PATH}}' => $IP,
129 '{{CURRENT_VERSION}}' => $this->mwVersion,
130 '{{INPUT}}' => $this->input,
131 '{{EXCLUDE}}' => $exclude,
132 '{{EXCLUDE_PATTERNS}}' => $excludePatterns,
133 '{{HAVE_DOT}}' => $this->doDot ? 'YES' : 'NO',
134 '{{GENERATE_MAN}}' => $this->doMan ? 'YES' : 'NO',
135 '{{INPUT_FILTER}}' => $this->inputFilter,
136 ]
137 );
138
139 $tmpFile = tempnam( wfTempDir(), 'MWDocGen-' );
140 if ( file_put_contents( $tmpFile, $conf ) === false ) {
141 $this->fatalError( "Could not write doxygen configuration to file $tmpFile\n" );
142 }
143
144 $command = $this->doxygen . ' ' . $tmpFile;
145 $this->output( "Executing command:\n$command\n" );
146
147 $exitcode = 1;
148 system( $command, $exitcode );
149
150 $this->output( <<<TEXT
151 ---------------------------------------------------
152 Doxygen execution finished.
153 Check above for possible errors.
154
155 You might want to delete the temporary file:
156 $tmpFile
157 ---------------------------------------------------
158
159 TEXT
160 );
161
162 if ( $exitcode !== 0 ) {
163 $this->fatalError( "Something went wrong (exit: $exitcode)\n", $exitcode );
164 }
165 }
166 }
167
168 $maintClass = MWDocGen::class;
169 require_once RUN_MAINTENANCE_IF_MAIN;