1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\ImageCaption; |
4
|
|
|
|
5
|
|
|
use SMW\Store; |
6
|
|
|
use SMW\DIWikiPage; |
7
|
|
|
use SMW\DIProperty; |
8
|
|
|
use SMW\DataValues\MonolingualTextValue; |
9
|
|
|
use SMW\DataValueFactory; |
10
|
|
|
use SMWDIBlob as DIBlob; |
11
|
|
|
use SMW\Utils\Normalizer; |
12
|
|
|
use SMW\RequestOptions; |
13
|
|
|
use Title; |
14
|
|
|
use File; |
15
|
|
|
use Html; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @license GNU GPL v2+ |
19
|
|
|
* @since 1.0 |
20
|
|
|
* |
21
|
|
|
* @author mwjames |
22
|
|
|
*/ |
23
|
|
|
class ImageCaption { |
24
|
|
|
|
25
|
|
|
const SCHEMA_TYPE = 'IMAGECAPTION_RULE_SCHEMA'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Store |
29
|
|
|
*/ |
30
|
|
|
private $store; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RuleFinder |
34
|
|
|
*/ |
35
|
|
|
private $ruleFinder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Rule |
39
|
|
|
*/ |
40
|
|
|
private $rule; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @since 1.0 |
44
|
|
|
* |
45
|
|
|
* @param Store $store |
46
|
|
|
* @param RuleFinder $ruleFinder |
47
|
|
|
*/ |
48
|
|
|
public function __construct( Store $store, RuleFinder $ruleFinder ) { |
49
|
|
|
$this->store = $store; |
50
|
|
|
$this->ruleFinder = $ruleFinder; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @since 1.0 |
55
|
|
|
* |
56
|
|
|
* @param Title $target |
57
|
|
|
* @param File|false $file |
58
|
|
|
* @param string &$caption |
59
|
|
|
* @param string $languageCode |
60
|
|
|
*/ |
61
|
|
|
public function modifyCaption( Title $target, $file, string &$caption, string $languageCode ) { |
62
|
|
|
|
63
|
|
|
if ( !$file instanceof File ) { |
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Track count of embedded images |
68
|
|
|
if ( isset( $target->semanticimagecaptioncount ) ) { |
69
|
|
|
$target->semanticimagecaptioncount++; |
70
|
|
|
} else { |
71
|
|
|
$target->semanticimagecaptioncount = 1; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$subject = DIWikiPage::newFromTitle( |
75
|
|
|
$file->getTitle() |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$text = $this->findText( $subject, $target, $caption, $languageCode ); |
79
|
|
|
|
80
|
|
|
if ( $text !== '' ) { |
81
|
|
|
$caption = $text; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function findText( DIWikiPage $subject, Title $target, string $caption, string $languageCode ) : string { |
86
|
|
|
|
87
|
|
|
$requestOptions = new RequestOptions(); |
88
|
|
|
$requestOptions->setCaller( __METHOD__ ); |
89
|
|
|
|
90
|
|
|
$dataItems = $this->store->getPropertyValues( |
91
|
|
|
$subject, |
92
|
|
|
new DIProperty( '_INST' ), |
93
|
|
|
$requestOptions |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$categories = []; |
97
|
|
|
|
98
|
|
|
foreach ( $dataItems as $dataItem ) { |
99
|
|
|
$categories[] = $dataItem->getDBKey(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->rule = $this->ruleFinder->findRule( $categories ); |
103
|
|
|
|
104
|
|
|
if ( $this->rule->isEmpty() ) { |
105
|
|
|
return ''; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ( $caption !== '' && !$this->get( 'allow_caption_override', false ) ) { |
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ( ( $property = $this->get( 'caption_property', '' ) ) === '' ) { |
113
|
|
|
return ''; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$property = DIProperty::newFromUserLabel( $property ); |
117
|
|
|
|
118
|
|
|
$text = ''; |
119
|
|
|
$maxLength = $this->get( 'max_length', 200 ); |
120
|
|
|
|
121
|
|
|
if ( $property->findPropertyValueType() === MonolingualTextValue::TYPE_ID ) { |
122
|
|
|
$text .= $this->fetchTextByLanguageCode( $subject, $property, $languageCode ); |
123
|
|
|
} else { |
124
|
|
|
$dataItems = $this->store->getPropertyValues( |
125
|
|
|
$subject, |
126
|
|
|
$property, |
127
|
|
|
$requestOptions |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
if ( $dataItems === [] ) { |
131
|
|
|
return ''; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
foreach ( $dataItems as $dataItem ) { |
135
|
|
|
|
136
|
|
|
if ( !$dataItem instanceof DIBlob ) { |
137
|
|
|
continue; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$text .= $dataItem->getString(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$length = mb_strlen( $text ); |
145
|
|
|
|
146
|
|
|
// Reduces the length and finish it with a whole word |
147
|
|
|
if ( $maxLength > 0 && $length >= $maxLength ) { |
148
|
|
|
$text = Normalizer::reduceLengthTo( $text, $maxLength ) . ' …'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ( $this->get( 'add_figures_reference', false ) ) { |
152
|
|
|
$text = wfMessage( 'semantic-imagecaption-figures', $target->semanticimagecaptioncount )->parse() . " $text"; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $text; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function fetchTextByLanguageCode( $subject, $property, $languageCode ) { |
159
|
|
|
|
160
|
|
|
try { |
161
|
|
|
$monolingualTextLookup = $this->store->service( 'MonolingualTextLookup' ); |
162
|
|
|
} catch( ServiceNotFoundException $e ) { |
|
|
|
|
163
|
|
|
return ''; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ( $monolingualTextLookup === null ) { |
167
|
|
|
return ''; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$monolingualTextLookup->setCaller( __METHOD__ ); |
171
|
|
|
|
172
|
|
|
$dataValue = $monolingualTextLookup->newDataValue( |
173
|
|
|
$subject, |
174
|
|
|
$property, |
175
|
|
|
$languageCode |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
if ( $dataValue === null ) { |
179
|
|
|
return ''; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$dv = $dataValue->getTextValueByLanguageCode( |
183
|
|
|
$languageCode |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
return $dv->getShortWikiText(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function get( $key, $default ) { |
190
|
|
|
|
191
|
|
|
if ( $this->rule->has( "then.$key" ) ) { |
192
|
|
|
return $this->rule->then( $key, $default ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ( $this->rule->has( "$key" ) ) { |
196
|
|
|
return $this->rule->get( $key, $default ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $default; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|