1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace ModernTimeline\SlidePresenter; |
6
|
|
|
|
7
|
|
|
use MediaWiki\MediaWikiServices; |
8
|
|
|
use ModernTimeline\ResultFacade\PropertyValueCollection; |
9
|
|
|
use ModernTimeline\ResultFacade\Subject; |
10
|
|
|
use SMW\DataValueFactory; |
11
|
|
|
|
12
|
|
|
class TemplateSlidePresenter implements SlidePresenter { |
13
|
|
|
|
14
|
|
|
private $templateName; |
15
|
|
|
|
16
|
2 |
|
public function __construct( string $templateName ) { |
17
|
2 |
|
$this->templateName = $templateName; |
18
|
2 |
|
} |
19
|
|
|
|
20
|
1 |
|
public function getText( Subject $subject ): string { |
21
|
1 |
|
$parser = $this->getParser(); |
22
|
|
|
|
23
|
1 |
|
return $parser->recursiveTagParseFully( |
24
|
1 |
|
( new TemplateSlidePresenter( $this->templateName ) )->getTemplateText( $subject ) |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
private function getParser(): \Parser { |
29
|
1 |
|
return MediaWikiServices::getInstance()->getParser(); |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
public function getTemplateText( Subject $subject ): string { |
33
|
2 |
|
return '{{' . implode( '|', $this->getTemplateSegments( $subject ) ) . '}}'; |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
private function getTemplateSegments( Subject $subject ): array { |
37
|
2 |
|
return array_merge( |
38
|
|
|
[ |
39
|
2 |
|
$this->templateName, |
40
|
2 |
|
$this->parameter( 'title', $subject->getWikiPage()->getTitle()->getFullText() ) |
41
|
|
|
], |
42
|
2 |
|
array_map( |
43
|
2 |
|
function( PropertyValueCollection $pvc ) { |
44
|
2 |
|
return $this->parameter( |
45
|
2 |
|
$pvc->getPrintRequest()->getText( null ) ?? '', |
46
|
2 |
|
$pvc->getDataItems() === [] ? '' : $this->dataItemToText( $pvc->getDataItems()[0] ) |
47
|
|
|
); |
48
|
2 |
|
}, |
49
|
2 |
|
$subject->getPropertyValueCollections()->toArray() |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
private function dataItemToText( \SMWDataItem $dataItem ): string { |
55
|
2 |
|
return DataValueFactory::getInstance()->newDataValueByItem( $dataItem )->getLongHTMLText(); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
private function parameter( string $name, string $value ): string { |
59
|
2 |
|
return $name . '=' . $value; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|