Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TransclusionExtractor | |
0.00% |
0 / 19 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
| getXPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| perform | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Parsoid\Extractor; |
| 4 | |
| 5 | use DOMElement; |
| 6 | use Flow\Model\WikiReference; |
| 7 | use Flow\Parsoid\Extractor; |
| 8 | use Flow\Parsoid\ReferenceFactory; |
| 9 | use LogicException; |
| 10 | use MediaWiki\Json\FormatJson; |
| 11 | use MediaWiki\Title\Title; |
| 12 | |
| 13 | /** |
| 14 | * Finds and creates References for transclusions in parsoid HTML |
| 15 | */ |
| 16 | class 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 | } |