Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MWDocGen
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
2
 getDbType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 init
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
 execute
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
30
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 Brooke Vibber
32 * @author Alexandre Emsenhuber
33 * @version first release
34 */
35
36use MediaWiki\Shell\Shell;
37
38require_once __DIR__ . '/Maintenance.php';
39
40/**
41 * Maintenance script that builds doxygen documentation.
42 * @ingroup Maintenance
43 */
44class 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 bool */
60    private $doDot;
61
62    /**
63     * Prepare Maintenance class
64     */
65    public function __construct() {
66        parent::__construct();
67        $this->addDescription( 'Build doxygen documentation' );
68
69        $this->addOption( 'doxygen',
70            'Path to doxygen',
71            false, true );
72        $this->addOption( 'version',
73            'Pass a MediaWiki version',
74            false, true );
75        $this->addOption( 'file',
76            "Only process given file or directory. Multiple values " .
77            "accepted with comma separation. Path relative to MW_INSTALL_PATH.",
78            false, true );
79        $this->addOption( 'output',
80            'Path to write doc to',
81            false, true );
82        $this->addOption( 'extensions',
83            'Process the extensions/ directory as well (ignored if --file is used)' );
84        $this->addOption( 'skins',
85            'Process the skins/ directory as well (ignored if --file is used)' );
86    }
87
88    public function getDbType() {
89        return Maintenance::DB_NONE;
90    }
91
92    protected function init() {
93        global $wgPhpCli;
94
95        $this->doxygen = $this->getOption( 'doxygen', 'doxygen' );
96        $this->mwVersion = $this->getOption( 'version', 'master' );
97
98        $this->output = $this->getOption( 'output', 'docs' );
99
100        // Do not use wfShellWikiCmd, because mwdoc-filter.php is not
101        // a Maintenance script.
102        $this->inputFilter = Shell::escape( [
103            $wgPhpCli,
104            MW_INSTALL_PATH . '/maintenance/mwdoc-filter.php'
105        ] );
106
107        $this->template = MW_INSTALL_PATH . '/maintenance/Doxyfile';
108        $this->excludes = [
109            'cache',
110            'images',
111        ];
112
113        $file = $this->getOption( 'file' );
114        if ( $file !== null ) {
115            $this->input = '';
116            foreach ( explode( ',', $file ) as $input ) {
117                // Doxygen inputs are space separated and double quoted
118                $this->input .= " \"$input\"";
119            }
120        } else {
121            // If no explicit --file filter is set, we're indexing all of MediaWiki core
122            // in MW_INSTALL_PATH, but not extension and skin submodules (T317451).
123            $this->input = '';
124            if ( !$this->hasOption( 'extensions' ) ) {
125                $this->excludes[] = 'extensions';
126            }
127            if ( !$this->hasOption( 'skins' ) ) {
128                $this->excludes[] = 'skins';
129            }
130        }
131
132        $this->doDot = (bool)shell_exec( 'which dot' );
133    }
134
135    public function execute() {
136        $this->init();
137
138        # Build out directories we want to exclude
139        $exclude = '';
140        foreach ( $this->excludes as $item ) {
141            $exclude .= " $item";
142        }
143
144        $conf = strtr( file_get_contents( $this->template ),
145            [
146                '{{OUTPUT_DIRECTORY}}' => $this->output,
147                '{{CURRENT_VERSION}}' => $this->mwVersion,
148                '{{INPUT}}' => $this->input,
149                '{{EXCLUDE}}' => $exclude,
150                '{{HAVE_DOT}}' => $this->doDot ? 'YES' : 'NO',
151                '{{INPUT_FILTER}}' => $this->inputFilter,
152            ]
153        );
154
155        $tmpFile = tempnam( wfTempDir(), 'MWDocGen-' );
156        if ( file_put_contents( $tmpFile, $conf ) === false ) {
157            $this->fatalError( "Could not write doxygen configuration to file $tmpFile\n" );
158        }
159
160        $command = $this->doxygen . ' ' . $tmpFile;
161        $this->output( "Executing command:\n$command\n" );
162
163        $exitcode = 1;
164        system( $command, $exitcode );
165
166        $this->output( <<<TEXT
167---------------------------------------------------
168Doxygen execution finished.
169Check above for possible errors.
170
171You might want to delete the temporary file:
172 $tmpFile
173---------------------------------------------------
174
175TEXT
176        );
177
178        if ( $exitcode !== 0 ) {
179            $this->fatalError( "Something went wrong (exit: $exitcode)\n", $exitcode );
180        }
181    }
182}
183
184$maintClass = MWDocGen::class;
185require_once RUN_MAINTENANCE_IF_MAIN;