Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
InstallDocFormatter
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 format
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Installer-specific wikitext formatting.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24namespace MediaWiki\Installer;
25
26class InstallDocFormatter {
27    /** @var string */
28    private $text;
29
30    public static function format( $text ) {
31        return ( new self( $text ) )->execute();
32    }
33
34    protected function __construct( $text ) {
35        $this->text = $text;
36    }
37
38    protected function execute() {
39        $text = $this->text;
40        // Use Unix line endings, escape some wikitext stuff
41        $text = str_replace( [ '<', '{{', '[[', '__', "\r" ],
42            [ '&lt;', '&#123;&#123;', '&#91;&#91;', '&#95;&#95;', '' ], $text );
43        // join word-wrapped lines into one
44        do {
45            $prev = $text;
46            $text = preg_replace( "/\n([\\*#\t])([^\n]*?)\n([^\n#\\*:]+)/", "\n\\1\\2 \\3", $text );
47        } while ( $text != $prev );
48        // Replace tab indents with colons
49        $text = preg_replace( '/^\t\t/m', '::', $text );
50        $text = preg_replace( '/^\t/m', ':', $text );
51
52        $linkStart = '<span class="config-plainlink">[';
53        $linkEnd = ' $0]</span>';
54
55        // turn (Tnnnn) into links
56        $text = preg_replace(
57            '/T\d+/',
58            "{$linkStart}https://phabricator.wikimedia.org/$0{$linkEnd}",
59            $text
60        );
61
62        // turn (bug nnnn) into links
63        $text = preg_replace(
64            '/bug (\d+)/',
65            "{$linkStart}https://bugzilla.wikimedia.org/$1{$linkEnd}",
66            $text
67        );
68
69        // add links to manual to every global variable mentioned
70        return preg_replace(
71            '/\$wg[a-z0-9_]+/i',
72            "{$linkStart}https://www.mediawiki.org/wiki/Manual:$0{$linkEnd}",
73            $text
74        );
75    }
76}