1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SESP; |
4
|
|
|
|
5
|
|
|
use SMW\DIProperty; |
6
|
|
|
use SMW\SemanticData; |
7
|
|
|
use SESP\PropertyAnnotators\NullPropertyAnnotator; |
8
|
|
|
use SESP\PropertyAnnotators\CreatorPropertyAnnotator; |
9
|
|
|
use SESP\PropertyAnnotators\PageViewsPropertyAnnotator; |
10
|
|
|
use SESP\PropertyAnnotators\LocalPropertyAnnotator; |
11
|
|
|
use SESP\PropertyAnnotators\UserRegistrationDatePropertyAnnotator; |
12
|
|
|
use SESP\PropertyAnnotators\UserEditCountPropertyAnnotator; |
13
|
|
|
use SESP\PropertyAnnotators\PageIDPropertyAnnotator; |
14
|
|
|
use SESP\PropertyAnnotators\ShortUrlPropertyAnnotator; |
15
|
|
|
use SESP\PropertyAnnotators\ExifPropertyAnnotator; |
16
|
|
|
use SESP\PropertyAnnotators\RevisionIDPropertyAnnotator; |
17
|
|
|
use SESP\PropertyAnnotators\PageNumRevisionPropertyAnnotator; |
18
|
|
|
use SESP\PropertyAnnotators\TalkPageNumRevisionPropertyAnnotator; |
19
|
|
|
use SESP\PropertyAnnotators\PageContributorsPropertyAnnotator; |
20
|
|
|
use SESP\PropertyAnnotators\SubPagePropertyAnnotator; |
21
|
|
|
use SESP\PropertyAnnotators\PageLengthPropertyAnnotator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @private |
25
|
|
|
* |
26
|
|
|
* @license GNU GPL v2+ |
27
|
|
|
* @since 2.0 |
28
|
|
|
* |
29
|
|
|
* @author mwjames |
30
|
|
|
*/ |
31
|
|
|
class ExtraPropertyAnnotator { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var AppFactory |
35
|
|
|
*/ |
36
|
|
|
private $appFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var PropertyAnnotator[] |
40
|
|
|
*/ |
41
|
|
|
private $propertyAnnotators = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var PropertyAnnotator |
45
|
|
|
*/ |
46
|
|
|
private $localPropertyAnnotator; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private $options; |
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @since 2.0 |
55
|
|
|
* |
56
|
|
|
* @param AppFactory $appFactory |
57
|
|
|
*/ |
58
|
|
|
public function __construct( AppFactory $appFactory ) { |
59
|
|
|
$this->appFactory = $appFactory; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @since 2.0 |
64
|
|
|
* |
65
|
|
|
* @param SemanticData $semanticData |
66
|
|
|
*/ |
67
|
|
|
public function addAnnotation( SemanticData $semanticData ) { |
68
|
|
|
|
69
|
|
|
$time = microtime( true ); |
70
|
|
|
|
71
|
|
|
if ( !$this->canAnnotate( $semanticData->getSubject() ) ) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$propertyDefinitions = $this->appFactory->getPropertyDefinitions(); |
76
|
|
|
|
77
|
|
|
foreach ( $this->appFactory->getOption( 'sespSpecialProperties', array() ) as $key ) { |
|
|
|
|
78
|
|
|
|
79
|
|
|
if ( !$propertyDefinitions->deepHas( $key, 'id' ) ) { |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$property = new DIProperty( |
84
|
|
|
$propertyDefinitions->deepGet( $key, 'id' ) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
if ( $propertyDefinitions->isLocalDef( $key ) ) { |
88
|
|
|
$this->localPropertyAnnotator->addAnnotation( $property, $semanticData ); |
89
|
|
|
} else { |
90
|
|
|
$this->findPropertyAnnotator( $property )->addAnnotation( $property, $semanticData ); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->appFactory->getLogger()->info( |
95
|
|
|
__METHOD__ . ' (procTime in sec: '. ( microtime( true ) - $time ) . ')' |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @since 2.0 |
101
|
|
|
* |
102
|
|
|
* @param DIProperty $property |
103
|
|
|
* |
104
|
|
|
* @return PropertyAnnotator |
105
|
|
|
*/ |
106
|
|
|
public function findPropertyAnnotator( DIProperty $property ) { |
107
|
|
|
|
108
|
|
|
$key = $property->getKey(); |
109
|
|
|
|
110
|
|
|
if ( $this->propertyAnnotators === array() ) { |
111
|
|
|
$this->initDefaultPropertyAnnotators(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ( isset( $this->propertyAnnotators[$key] ) && is_callable( $this->propertyAnnotators[$key] ) ) { |
115
|
|
|
return call_user_func( $this->propertyAnnotators[$key], $this->appFactory ); |
116
|
|
|
} elseif( isset( $this->propertyAnnotators[$key] ) ) { |
117
|
|
|
return $this->propertyAnnotators[$key]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return new NullPropertyAnnotator(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @since 2.0 |
125
|
|
|
* |
126
|
|
|
* @param string $key |
127
|
|
|
* @param Closure $callbak |
|
|
|
|
128
|
|
|
*/ |
129
|
|
|
public function addPropertyAnnotator( $key, Closure $callback ) { |
130
|
|
|
$this->propertyAnnotators[$key] = $callback; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function canAnnotate( $subject ) { |
134
|
|
|
|
135
|
|
|
if ( $subject === null || $subject->getTitle() === null || $subject->getTitle()->isSpecialPage() ) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->initDefaultPropertyAnnotators(); |
140
|
|
|
|
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function initDefaultPropertyAnnotators() { |
145
|
|
|
|
146
|
|
|
$this->localPropertyAnnotator = new LocalPropertyAnnotator( |
147
|
|
|
$this->appFactory |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
// Encapsulate each instance to avoid direct instantiation for unused |
151
|
|
|
// matches |
152
|
|
|
$this->propertyAnnotators = array( |
|
|
|
|
153
|
|
|
|
154
|
|
|
CreatorPropertyAnnotator::PROP_ID => function( $appFactory ) { |
155
|
|
|
return new CreatorPropertyAnnotator( $appFactory ); |
156
|
|
|
}, |
157
|
|
|
|
158
|
|
|
PageViewsPropertyAnnotator::PROP_ID => function( $appFactory ) { |
159
|
|
|
return new PageViewsPropertyAnnotator( $appFactory ); |
160
|
|
|
}, |
161
|
|
|
|
162
|
|
|
UserRegistrationDatePropertyAnnotator::PROP_ID => function( $appFactory ) { |
163
|
|
|
return new UserRegistrationDatePropertyAnnotator( $appFactory ); |
164
|
|
|
}, |
165
|
|
|
|
166
|
|
|
UserEditCountPropertyAnnotator::PROP_ID => function( $appFactory ) { |
167
|
|
|
return new UserEditCountPropertyAnnotator( $appFactory ); |
168
|
|
|
}, |
169
|
|
|
|
170
|
|
|
PageIDPropertyAnnotator::PROP_ID => function( $appFactory ) { |
171
|
|
|
return new PageIDPropertyAnnotator( $appFactory ); |
172
|
|
|
}, |
173
|
|
|
|
174
|
|
|
PageLengthPropertyAnnotator::PROP_ID => function( $appFactory ) { |
175
|
|
|
return new PageLengthPropertyAnnotator( $appFactory ); |
176
|
|
|
}, |
177
|
|
|
|
178
|
|
|
RevisionIDPropertyAnnotator::PROP_ID => function( $appFactory ) { |
179
|
|
|
return new RevisionIDPropertyAnnotator( $appFactory ); |
180
|
|
|
}, |
181
|
|
|
|
182
|
|
|
PageNumRevisionPropertyAnnotator::PROP_ID => function( $appFactory ) { |
183
|
|
|
return new PageNumRevisionPropertyAnnotator( $appFactory ); |
184
|
|
|
}, |
185
|
|
|
|
186
|
|
|
TalkPageNumRevisionPropertyAnnotator::PROP_ID => function( $appFactory ) { |
187
|
|
|
return new TalkPageNumRevisionPropertyAnnotator( $appFactory ); |
188
|
|
|
}, |
189
|
|
|
|
190
|
|
|
PageContributorsPropertyAnnotator::PROP_ID => function( $appFactory ) { |
191
|
|
|
return new PageContributorsPropertyAnnotator( $appFactory ); |
192
|
|
|
}, |
193
|
|
|
|
194
|
|
|
SubPagePropertyAnnotator::PROP_ID => function( $appFactory ) { |
195
|
|
|
return new SubPagePropertyAnnotator( $appFactory ); |
196
|
|
|
}, |
197
|
|
|
|
198
|
|
|
ShortUrlPropertyAnnotator::PROP_ID => function( $appFactory ) { |
199
|
|
|
return new ShortUrlPropertyAnnotator( $appFactory ); |
200
|
|
|
}, |
201
|
|
|
|
202
|
|
|
ExifPropertyAnnotator::PROP_ID => function( $appFactory ) { |
203
|
|
|
return new ExifPropertyAnnotator( $appFactory ); |
204
|
|
|
}, |
205
|
|
|
|
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.