Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TransclusionExtractor
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 getXPath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 perform
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace Flow\Parsoid\Extractor;
4
5use DOMElement;
6use Flow\Model\WikiReference;
7use Flow\Parsoid\Extractor;
8use Flow\Parsoid\ReferenceFactory;
9use LogicException;
10use MediaWiki\Json\FormatJson;
11use MediaWiki\Title\Title;
12
13/**
14 * Finds and creates References for transclusions in parsoid HTML
15 */
16class TransclusionExtractor implements Extractor {
17    /**
18     * @inheritDoc
19     */
20    public function getXPath() {
21        return '//*[@typeof="mw:Transclusion"]';
22    }
23
24    /**
25     * @inheritDoc
26     */
27    public function perform( ReferenceFactory $factory, DOMElement $element ) {
28        $orig = $element->getAttribute( 'data-mw' );
29        $data = FormatJson::decode( $orig );
30        if ( !isset( $data->parts ) || !is_array( $data->parts ) ) {
31            throw new LogicException( "Missing template target: $orig" );
32        }
33        $target = null;
34        foreach ( $data->parts as $part ) {
35            if ( isset( $part->template->target->wt ) ) {
36                $target = $part->template->target->wt;
37                break;
38            }
39        }
40        if ( $target === null ) {
41            throw new LogicException( "Missing template target: $orig" );
42        }
43        $templateTarget = Title::newFromText( $target, NS_TEMPLATE );
44
45        if ( !$templateTarget ) {
46            return null;
47        }
48
49        return $factory->createWikiReference(
50            WikiReference::TYPE_TEMPLATE,
51            $templateTarget->getPrefixedText()
52        );
53    }
54}