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 * @license GPL-2.0-or-later
7 * @file
8 */
9
10namespace MediaWiki\Installer;
11
12class InstallDocFormatter {
13    /** @var string */
14    private $text;
15
16    public static function format( string $text ): string {
17        return ( new self( $text ) )->execute();
18    }
19
20    protected function __construct( string $text ) {
21        $this->text = $text;
22    }
23
24    protected function execute(): string {
25        $text = $this->text;
26        // Use Unix line endings, escape some wikitext stuff
27        $text = str_replace( [ '<', '{{', '[[', '__', "\r" ],
28            [ '&lt;', '&#123;&#123;', '&#91;&#91;', '&#95;&#95;', '' ], $text );
29        // join word-wrapped lines into one
30        do {
31            $prev = $text;
32            $text = preg_replace( "/\n([\\*#\t])([^\n]*?)\n([^\n#\\*:]+)/", "\n\\1\\2 \\3", $text );
33        } while ( $text != $prev );
34        // Replace tab indents with colons
35        $text = preg_replace( '/^\t\t/m', '::', $text );
36        $text = preg_replace( '/^\t/m', ':', $text );
37
38        $linkStart = '<span class="config-plainlink">[';
39        $linkEnd = ' $0]</span>';
40
41        // turn (Tnnnn) into links
42        $text = preg_replace(
43            '/T\d+/',
44            "{$linkStart}https://phabricator.wikimedia.org/$0{$linkEnd}",
45            $text
46        );
47
48        // turn (bug nnnn) into links
49        $text = preg_replace(
50            '/bug (\d+)/',
51            "{$linkStart}https://bugzilla.wikimedia.org/$1{$linkEnd}",
52            $text
53        );
54
55        // add links to manual to every global variable mentioned
56        return preg_replace(
57            '/\$wg[a-z0-9_]+/i',
58            "{$linkStart}https://www.mediawiki.org/wiki/Manual:$0{$linkEnd}",
59            $text
60        );
61    }
62}