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