1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace ModernTimeline; |
6
|
|
|
|
7
|
|
|
use ModernTimeline\ResultFacade\PropertyValueCollection; |
8
|
|
|
use ModernTimeline\ResultFacade\Subject; |
9
|
|
|
use ModernTimeline\ResultFacade\SubjectCollection; |
10
|
|
|
use RepoGroup; |
11
|
|
|
use SMW\DIWikiPage; |
12
|
|
|
use SMWDITime; |
13
|
|
|
|
14
|
|
|
class EventExtractor { |
15
|
|
|
|
16
|
|
|
private $parameters; |
17
|
|
|
|
18
|
7 |
|
public function __construct( array $parameters ) { |
19
|
7 |
|
$this->parameters = $parameters; |
20
|
|
|
} |
21
|
7 |
|
|
22
|
5 |
|
/** |
23
|
|
|
* @param SubjectCollection $pages |
24
|
5 |
|
* @return Event[] |
25
|
4 |
|
*/ |
26
|
|
|
public function extractEvents( SubjectCollection $pages ): array { |
27
|
|
|
$events = []; |
28
|
|
|
|
29
|
7 |
|
foreach ( $pages->getSubjects() as $subject ) { |
30
|
|
|
[ $startDate, $endDate ] = $this->getDates( $subject ); |
|
|
|
|
31
|
|
|
|
32
|
5 |
|
if ( $startDate !== null ) { |
33
|
5 |
|
$event = new Event( $subject, $startDate, $endDate ); |
34
|
5 |
|
|
35
|
|
|
foreach ( $subject->getPropertyValueCollections() as $propertyValues ) { |
36
|
5 |
|
if ( $propertyValues->getPrintRequest()->getText( null ) === $this->parameters['image property'] ) { |
37
|
4 |
|
foreach ( $propertyValues->getDataItems() as $dataItem ) { |
38
|
|
|
if ( $this->isImageValue( $dataItem ) ) { |
39
|
4 |
|
$event->setImageUrl( $this->getUrlForFileTitle( $dataItem->getTitle() ) ); |
40
|
4 |
|
} |
41
|
4 |
|
} |
42
|
|
|
} |
43
|
1 |
|
} |
44
|
1 |
|
|
45
|
1 |
|
$events[] = $event; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $events; |
50
|
5 |
|
} |
51
|
|
|
|
52
|
|
|
private function isImageValue( \SMWDataItem $dataItem ) { |
53
|
|
|
return $dataItem instanceof DIWikiPage |
|
|
|
|
54
|
|
|
&& $dataItem->getTitle() instanceof \Title |
|
|
|
|
55
|
|
|
&& $dataItem->getTitle()->getNamespace() === NS_FILE |
56
|
5 |
|
&& $dataItem->getTitle()->exists(); |
57
|
5 |
|
} |
58
|
5 |
|
|
59
|
5 |
|
public function getUrlForFileTitle( \Title $existingTitle ): string { |
60
|
4 |
|
return RepoGroup::singleton()->findFile( $existingTitle )->getURL(); |
61
|
4 |
|
} |
62
|
5 |
|
|
63
|
|
|
private function getDates( Subject $subject ): array { |
64
|
|
|
$startDate = null; |
65
|
|
|
$endDate = null; |
66
|
|
|
|
67
|
|
|
foreach ( $this->getPropertyValueCollectionsWithDates( $subject ) as $propertyValues ) { |
68
|
|
|
$dataItem = $propertyValues->getDataItems()[0]; |
69
|
|
|
|
70
|
|
|
if ( $dataItem instanceof SMWDITime ) { |
|
|
|
|
71
|
|
|
if ( $startDate === null ) { |
72
|
|
|
$startDate = $dataItem; |
73
|
|
|
} |
74
|
|
|
else if ( $endDate === null ) { |
75
|
|
|
$endDate = $dataItem; |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return [ $startDate, $endDate ]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return PropertyValueCollection[] |
86
|
|
|
*/ |
87
|
|
|
private function getPropertyValueCollectionsWithDates( Subject $subject ) { |
88
|
|
|
return array_filter( |
89
|
|
|
$subject->getPropertyValueCollections()->toArray(), |
90
|
|
|
function( PropertyValueCollection $pvc ) { |
91
|
|
|
return $pvc->getPrintRequest()->getTypeID() === '_dat' |
92
|
|
|
&& $pvc->getDataItems() !== []; |
93
|
|
|
} |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.