Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.75% |
15 / 16 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ParserFunctionSpamlistLookup | |
93.75% |
15 / 16 |
|
50.00% |
1 / 2 |
3.00 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fetchTargets | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\MassMessage\Lookup; |
4 | |
5 | use MediaWiki\Content\TextContent; |
6 | use MediaWiki\MassMessage\MassMessageHooks; |
7 | use MediaWiki\MediaWikiServices; |
8 | use MediaWiki\Revision\RevisionRecord; |
9 | use MediaWiki\Title\Title; |
10 | |
11 | class ParserFunctionSpamlistLookup extends SpamlistLookup { |
12 | |
13 | /** |
14 | * @var Title |
15 | */ |
16 | protected $spamlist; |
17 | |
18 | /** |
19 | * @param Title $spamlist |
20 | */ |
21 | public function __construct( Title $spamlist ) { |
22 | $this->spamlist = $spamlist; |
23 | } |
24 | |
25 | /** |
26 | * Get an array of targets via the #target parser function. |
27 | * |
28 | * @return array[] |
29 | */ |
30 | public function fetchTargets() { |
31 | $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $this->spamlist ); |
32 | $content = $page->getContent( RevisionRecord::RAW ); |
33 | /** @var TextContent $content */ |
34 | '@phan-var TextContent $content'; |
35 | $text = $content->getText(); |
36 | |
37 | // Prep the parser |
38 | $parserOptions = $page->makeParserOptions( 'canonical' ); |
39 | $parser = MediaWikiServices::getInstance()->getParserFactory()->create(); |
40 | // Do this so that our initial parser function is added |
41 | $parser->firstCallInit(); |
42 | // Now overwrite it |
43 | $parser->setFunctionHook( |
44 | 'target', |
45 | [ MassMessageHooks::class, 'storeDataParserFunction' ] |
46 | ); |
47 | |
48 | // Parse |
49 | $output = $parser->parse( $text, $this->spamlist, $parserOptions ); |
50 | $data = $output->getExtensionData( 'massmessage-targets' ); |
51 | |
52 | if ( $data ) { |
53 | return $data; |
54 | } else { |
55 | // No parser functions on page |
56 | return []; |
57 | } |
58 | } |
59 | } |