Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PlaceholderExtractor | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| getXPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| perform | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| 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 MediaWiki\Json\FormatJson; |
| 10 | use MediaWiki\MediaWikiServices; |
| 11 | use MediaWiki\Parser\ParserOptions; |
| 12 | use MediaWiki\Parser\ParserOutputLinkTypes; |
| 13 | use MediaWiki\Title\Title; |
| 14 | |
| 15 | /** |
| 16 | * phpcs:disable Generic.Files.LineLength |
| 17 | * Parsoid currently returns images that don't exist like: |
| 18 | * <meta typeof="mw:Placeholder" data-parsoid='{"src":"[[File:Image.png|25px]]","optList":[{"ck":"width","ak":"25px"}],"dsr":[0,23,null,null]}'> |
| 19 | * phpcs:enable Generic.Files.LineLength |
| 20 | * |
| 21 | * Links to those should also be registered, but since they're |
| 22 | * different nodes than what we expect above, we'll have to deal |
| 23 | * with them ourselves. This may change some day, as Parsoids |
| 24 | * codebase has a FIXME "Handle missing images properly!!" |
| 25 | */ |
| 26 | class PlaceholderExtractor implements Extractor { |
| 27 | /** |
| 28 | * @inheritDoc |
| 29 | */ |
| 30 | public function getXPath() { |
| 31 | return '//*[contains(concat(" ", @typeof, " "), " mw:Placeholder" )]'; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public function perform( ReferenceFactory $factory, DOMElement $element ) { |
| 38 | $data = FormatJson::decode( $element->getAttribute( 'data-parsoid' ), true ); |
| 39 | if ( !isset( $data['src'] ) ) { |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Parsoid only gives us the raw source to play with. Run it |
| 45 | * through Parser to make sure we're dealing with an image |
| 46 | * and get the image name. |
| 47 | */ |
| 48 | $output = MediaWikiServices::getInstance()->getParser()->parse( |
| 49 | $data['src'], |
| 50 | Title::newFromText( 'Main Page' ), |
| 51 | ParserOptions::newFromAnon() |
| 52 | ); |
| 53 | |
| 54 | $file = $output->getLinkList( ParserOutputLinkTypes::MEDIA ); |
| 55 | if ( !$file ) { |
| 56 | return null; |
| 57 | } |
| 58 | $image = Title::newFromLinkTarget( $file[0]['link'] ); |
| 59 | |
| 60 | return $factory->createWikiReference( WikiReference::TYPE_FILE, $image->getPrefixedDBkey() ); |
| 61 | } |
| 62 | } |