Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ReferenceExtractor | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getReferences | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
extractReferences | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace Flow\Parsoid; |
4 | |
5 | use DOMXPath; |
6 | use Flow\Conversion\Utils; |
7 | use Flow\Exception\InvalidReferenceException; |
8 | use Flow\Model\Reference; |
9 | use Flow\Model\UUID; |
10 | use Flow\Model\Workflow; |
11 | use LogicException; |
12 | use RuntimeException; |
13 | |
14 | /** |
15 | * Extracts references to templates, files and pages (in the form of links) |
16 | * from Parsoid HTML. |
17 | */ |
18 | class ReferenceExtractor { |
19 | /** |
20 | * @var Extractor[][] Map from revision type (AbstractRevision::getRevisionType()) |
21 | * to list of Extractor objects to use. |
22 | */ |
23 | protected $extractors; |
24 | |
25 | /** |
26 | * @param Extractor[][] $extractors Map from revision type (AbstractRevision::getRevisionType()) |
27 | * to a list of extractors to use. |
28 | */ |
29 | public function __construct( array $extractors ) { |
30 | $this->extractors = $extractors; |
31 | } |
32 | |
33 | /** |
34 | * @param Workflow $workflow |
35 | * @param string $objectType |
36 | * @param UUID $objectId |
37 | * @param string $text |
38 | * @return array |
39 | */ |
40 | public function getReferences( Workflow $workflow, $objectType, UUID $objectId, $text ) { |
41 | if ( isset( $this->extractors[$objectType] ) ) { |
42 | return $this->extractReferences( |
43 | new ReferenceFactory( $workflow, $objectType, $objectId ), |
44 | $this->extractors[$objectType], |
45 | $text |
46 | ); |
47 | } else { |
48 | throw new LogicException( "No extractors available for $objectType" ); |
49 | } |
50 | } |
51 | |
52 | /** |
53 | * @param ReferenceFactory $factory |
54 | * @param Extractor[] $extractors |
55 | * @param string $text |
56 | * @return Reference[] |
57 | */ |
58 | protected function extractReferences( ReferenceFactory $factory, array $extractors, $text ) { |
59 | $dom = Utils::createDOM( $text ); |
60 | |
61 | $output = []; |
62 | |
63 | $xpath = new DOMXPath( $dom ); |
64 | |
65 | foreach ( $extractors as $extractor ) { |
66 | $elements = $xpath->query( $extractor->getXPath() ); |
67 | |
68 | if ( !$elements ) { |
69 | $class = get_class( $extractor ); |
70 | throw new RuntimeException( "Malformed xpath from $class: " . $extractor->getXPath() ); |
71 | } |
72 | |
73 | foreach ( $elements as $element ) { |
74 | try { |
75 | $ref = $extractor->perform( $factory, $element ); |
76 | } catch ( InvalidReferenceException $e ) { |
77 | wfDebugLog( 'Flow', 'Invalid reference detected, skipping element' ); |
78 | $ref = null; |
79 | } |
80 | // no reference was generated |
81 | if ( $ref === null ) { |
82 | continue; |
83 | } |
84 | // reference points to a special page |
85 | if ( $ref->getSrcTitle()->isSpecialPage() ) { |
86 | continue; |
87 | } |
88 | |
89 | $output[] = $ref; |
90 | } |
91 | } |
92 | |
93 | return $output; |
94 | } |
95 | } |