Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
PageCollectionHookHandler | |
0.00% |
0 / 15 |
|
0.00% |
0 / 2 |
6 | |
0.00% |
0 / 1 |
onParserFirstCallInit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
parsePageCollection | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types=1 ); |
4 | |
5 | namespace MediaWiki\Extension\WikimediaCampaignEvents\Hooks\Handlers; |
6 | |
7 | use MediaWiki\Hook\ParserFirstCallInitHook; |
8 | use MediaWiki\Parser\Parser; |
9 | |
10 | class PageCollectionHookHandler implements ParserFirstCallInitHook { |
11 | public const PAGE_COLLECTION_EXTENSION_DATA_KEY = 'PageCollection'; |
12 | |
13 | /** |
14 | * Bind the parsePageCollection function to the page-collection magic word |
15 | * @param Parser $parser |
16 | */ |
17 | public function onParserFirstCallInit( $parser ) { |
18 | $parser->setHook( "page-collection", [ $this, "parsePageCollection" ] ); |
19 | } |
20 | |
21 | /** |
22 | * This method parses the custom <page-collection> HTML marker, extracts |
23 | * the page collection metadata that are defined inside its attributes and stores them |
24 | * as extension data, to be used by the "pagecollectionsmetadata" Action API module. |
25 | * |
26 | * It also adds a tracking category to the page that includes the marker, so that |
27 | * we can easily find/query such pages. |
28 | * |
29 | * Finally, it returns an empty string, as we don't want to render anything inside |
30 | * the page, for this marker. |
31 | * |
32 | * Example HTML marker definition: |
33 | * <page-collection |
34 | * name='My Page Collection' |
35 | * description='This is a page collection' |
36 | * end-date='2024-10-20' |
37 | * ></page-collection> |
38 | * |
39 | * @param string|null $label |
40 | * @param array $args |
41 | * @param Parser $parser |
42 | * @return string |
43 | */ |
44 | public function parsePageCollection( ?string $label, array $args, Parser $parser ): string { |
45 | $parserOutput = $parser->getOutput(); |
46 | |
47 | // add a tracking category to the page that includes the marker, so that we can easily |
48 | // find/query such pages |
49 | $parser->addTrackingCategory( "page-collection-tracking-category" ); |
50 | |
51 | // Get the named parameters and merge with defaults. |
52 | $defaultOptions = [ |
53 | "lang" => $parser->getContentLanguage()->getCode(), |
54 | "name" => "", |
55 | "description" => "", |
56 | "end-date" => "", |
57 | ]; |
58 | $pageCollectionDefinition = array_merge( $defaultOptions, $args ); |
59 | |
60 | $parserOutput->setExtensionData( |
61 | self::PAGE_COLLECTION_EXTENSION_DATA_KEY, |
62 | json_encode( $pageCollectionDefinition ) |
63 | ); |
64 | |
65 | // For now, we don't want to display anything inside the page, for these HTML markers. |
66 | return ""; |
67 | } |
68 | } |