Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
WikiLinkParser
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 registerWikiLinkCleaner
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parse
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace FileImporter\Services\Wikitext;
4
5/**
6 * @license GPL-2.0-or-later
7 * @author Thiemo Kreuz
8 */
9class WikiLinkParser {
10
11    /** @var WikiLinkCleaner[] */
12    private array $cleaners = [];
13
14    public function registerWikiLinkCleaner( WikiLinkCleaner $cleaner ): void {
15        $this->cleaners[] = $cleaner;
16    }
17
18    public function parse( string $wikitext ): string {
19        return preg_replace_callback(
20            '/
21                # Look-behind for the opening [[
22                (?<=\[\[)
23                # The extra + at the end of ++ avoids backtracking
24                [^\v\[|\]]++
25                # Look-ahead for | or the closing ]]
26                (?=\||\]\])
27            /xu',
28            function ( array $matches ): string {
29                $link = $matches[0];
30                foreach ( $this->cleaners as $cleaner ) {
31                    $link = $cleaner->process( $link );
32                }
33                return $link;
34            },
35            $wikitext
36        );
37    }
38
39}