Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
WikitextLinksExtractor | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
getLinksToNamespace | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | namespace ProofreadPage\Index; |
4 | |
5 | use MediaWiki\Title\MalformedTitleException; |
6 | use MediaWiki\Title\Title; |
7 | use ProofreadPage\Link; |
8 | |
9 | /** |
10 | * @license GPL-2.0-or-later |
11 | * |
12 | * Utility class to extract links for wikitext pages |
13 | */ |
14 | class WikitextLinksExtractor { |
15 | |
16 | /** |
17 | * @param string $wikitext unparsed wikitext |
18 | * @param int $namespace namespace of the page from which the wikitext was passed |
19 | * @return Link[] array of links in the text |
20 | */ |
21 | public function getLinksToNamespace( $wikitext, $namespace ) { |
22 | preg_match_all( '/\[\[(.*?)(\|(.*?)|)\]\]/i', $wikitext, $textLinks, PREG_PATTERN_ORDER ); |
23 | $links = []; |
24 | $textLinksCount = count( $textLinks[1] ); |
25 | for ( $i = 0; $i < $textLinksCount; $i++ ) { |
26 | try { |
27 | $title = Title::newFromTextThrow( $textLinks[1][$i] ); |
28 | if ( $title->inNamespace( $namespace ) ) { |
29 | if ( $textLinks[3][$i] === '' ) { |
30 | $links[] = new Link( $title, $title->getSubpageText() ); |
31 | } else { |
32 | $links[] = new Link( $title, $textLinks[3][$i] ); |
33 | } |
34 | } |
35 | } catch ( MalformedTitleException $e ) { |
36 | // We ignore invalid links |
37 | } |
38 | } |
39 | return $links; |
40 | } |
41 | } |