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