forked from wikimedia/mediawiki-extensions-WikibaseLexeme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikibaseLexeme.entitytypes.php
294 lines (288 loc) · 10.4 KB
/
WikibaseLexeme.entitytypes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* Definition of the lexeme entity type.
* The array returned by the code below is supposed to be merged into $wgWBRepoEntityTypes.
*
* @note: Keep in sync with Wikibase
*
* @note: This is bootstrap code, it is executed for EVERY request. Avoid instantiating
* objects or loading classes here!
*
* @license GPL-2.0-or-later
* @author Amir Sarabadani <[email protected]>
*/
use MediaWiki\MediaWikiServices;
use Wikibase\DataModel\DeserializerFactory;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\SerializerFactory;
use Wikibase\DataModel\Services\EntityId\EntityIdFormatter;
use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
use Wikibase\LanguageFallbackChain;
use Wikibase\Lexeme\ChangeOp\Deserialization\FormChangeOpDeserializer;
use Wikibase\Lexeme\ChangeOp\Deserialization\LanguageChangeOpDeserializer;
use Wikibase\Lexeme\ChangeOp\Deserialization\LemmaChangeOpDeserializer;
use Wikibase\Lexeme\ChangeOp\Deserialization\LexemeChangeOpDeserializer;
use Wikibase\Lexeme\ChangeOp\Deserialization\LexicalCategoryChangeOpDeserializer;
use Wikibase\Lexeme\Content\LexemeContent;
use Wikibase\Lexeme\Content\LexemeHandler;
use Wikibase\Lexeme\DataModel\FormId;
use Wikibase\Lexeme\DataModel\Lexeme;
use Wikibase\Lexeme\DataModel\LexemeId;
use Wikibase\Lexeme\DataModel\Serialization\ExternalLexemeSerializer;
use Wikibase\Lexeme\DataModel\Serialization\LexemeDeserializer;
use Wikibase\Lexeme\DataModel\Serialization\StorageLexemeSerializer;
use Wikibase\Lexeme\DataModel\Services\Diff\FormDiffer;
use Wikibase\Lexeme\DataModel\Services\Diff\FormPatcher;
use Wikibase\Lexeme\DataModel\Services\Diff\LexemeDiffer;
use Wikibase\Lexeme\DataModel\Services\Diff\LexemePatcher;
use Wikibase\Lexeme\Diff\LexemeDiffVisualizer;
use Wikibase\Lexeme\Diff\ItemReferenceDifferenceVisualizer;
use Wikibase\Lexeme\Rdf\LexemeRdfBuilder;
use Wikibase\Lexeme\Search\LexemeFieldDefinitions;
use Wikibase\Lexeme\Store\FormRevisionLookup;
use Wikibase\Lexeme\Store\FormStore;
use Wikibase\Lexeme\Store\FormTitleStoreLookup;
use Wikibase\Lexeme\Validators\LexemeValidatorFactory;
use Wikibase\Lexeme\View\LexemeViewFactory;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Lib\Store\EntityStore;
use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup;
use Wikibase\Rdf\RdfVocabulary;
use Wikibase\Repo\ChangeOp\Deserialization\ClaimsChangeOpDeserializer;
use Wikibase\Repo\ChangeOp\Deserialization\TermChangeOpSerializationValidator;
use Wikibase\Repo\Diff\BasicEntityDiffVisualizer;
use Wikibase\Repo\Diff\ClaimDiffer;
use Wikibase\Repo\Diff\ClaimDifferenceVisualizer;
use Wikibase\Repo\Store\EntityTitleStoreLookup;
use Wikibase\Repo\WikibaseRepo;
use Wikibase\View\EditSectionGenerator;
use Wikibase\View\EntityTermsView;
use Wikimedia\Purtle\RdfWriter;
return [
'lexeme' => [
'serializer-factory-callback' => function ( SerializerFactory $serializerFactory ) {
return new ExternalLexemeSerializer(
new StorageLexemeSerializer(
$serializerFactory->newTermListSerializer(),
$serializerFactory->newStatementListSerializer()
)
);
},
'storage-serializer-factory-callback' => function ( SerializerFactory $serializerFactory ) {
return new StorageLexemeSerializer(
$serializerFactory->newTermListSerializer(),
$serializerFactory->newStatementListSerializer()
);
},
'deserializer-factory-callback' => function ( DeserializerFactory $deserializerFactory ) {
return new LexemeDeserializer(
$deserializerFactory->newEntityIdDeserializer(),
$deserializerFactory->newStatementListDeserializer()
);
},
'view-factory-callback' => function (
$languageCode,
LabelDescriptionLookup $labelDescriptionLookup,
LanguageFallbackChain $fallbackChain,
EditSectionGenerator $editSectionGenerator,
EntityTermsView $entityTermsView
) {
$factory = new LexemeViewFactory(
$languageCode,
$labelDescriptionLookup,
$fallbackChain,
$editSectionGenerator,
$entityTermsView,
WikibaseRepo::getDefaultInstance()->getEntityIdHtmlLinkFormatterFactory()
);
return $factory->newLexemeView();
},
'content-model-id' => LexemeContent::CONTENT_MODEL_ID,
'content-handler-factory-callback' => function () {
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
$config = MediaWikiServices::getInstance()->getMainConfig();
if ( $config->has( 'LexemeLanguageCodePropertyId' ) ) {
$lcID = $config->get( 'LexemeLanguageCodePropertyId' );
} else {
$lcID = null;
}
return new LexemeHandler(
$wikibaseRepo->getStore()->getTermIndex(),
$wikibaseRepo->getEntityContentDataCodec(),
$wikibaseRepo->getEntityConstraintProvider(),
$wikibaseRepo->getValidatorErrorLocalizer(),
$wikibaseRepo->getEntityIdParser(),
$wikibaseRepo->getEntityIdLookup(),
$wikibaseRepo->getEntityLookup(),
$wikibaseRepo->getLanguageFallbackLabelDescriptionLookupFactory(),
new LexemeFieldDefinitions(
$wikibaseRepo->getStatementProviderDefinitions(),
$wikibaseRepo->getEntityLookup(),
$lcID ? $wikibaseRepo->getEntityIdParser()->parse( $lcID ) : null
)
);
},
'entity-id-pattern' => LexemeId::PATTERN,
'entity-id-builder' => function ( $serialization ) {
return new LexemeId( $serialization );
},
'entity-id-composer-callback' => function ( $repositoryName, $uniquePart ) {
return new LexemeId( EntityId::joinSerialization( [
$repositoryName,
'',
'L' . $uniquePart
] ) );
},
'entity-differ-strategy-builder' => function () {
return new LexemeDiffer();
},
'entity-patcher-strategy-builder' => function () {
return new LexemePatcher();
},
'entity-factory-callback' => function () {
return new Lexeme();
},
// Identifier of a resource loader module that, when `require`d, returns a function
// returning a deserializer
'js-deserializer-factory-function' => 'wikibase.lexeme.getDeserializer',
'changeop-deserializer-callback' => function () {
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
$lexemeValidatorFactory = new LexemeValidatorFactory(
1000, // TODO: move to setting, at least change to some reasonable hard-coded value
$wikibaseRepo->getTermValidatorFactory(),
// FIXME: What does belong here?
[]
);
return new LexemeChangeOpDeserializer(
new LemmaChangeOpDeserializer(
// TODO: WikibaseRepo should probably provide this validator?
// TODO: WikibaseRepo::getTermsLanguage is not necessarily the list of language codes
// that should be allowed as "languages" of lemma terms
new TermChangeOpSerializationValidator( $wikibaseRepo->getTermsLanguages() ),
$lexemeValidatorFactory,
$wikibaseRepo->getStringNormalizer()
),
new LexicalCategoryChangeOpDeserializer(
$lexemeValidatorFactory,
$wikibaseRepo->getStringNormalizer()
),
new LanguageChangeOpDeserializer(
$lexemeValidatorFactory,
$wikibaseRepo->getStringNormalizer()
),
new ClaimsChangeOpDeserializer(
$wikibaseRepo->getExternalFormatStatementDeserializer(),
$wikibaseRepo->getChangeOpFactoryProvider()->getStatementChangeOpFactory()
),
new FormChangeOpDeserializer()
);
},
'rdf-builder-factory-callback' => function (
$flavorFlags,
RdfVocabulary $vocabulary,
RdfWriter $writer,
$mentionedEntityTracker,
$dedupe
) {
return new LexemeRdfBuilder(
$vocabulary,
$writer
);
},
'entity-diff-visualizer-callback' => function (
MessageLocalizer $messageLocalizer,
ClaimDiffer $claimDiffer,
ClaimDifferenceVisualizer $claimDiffView,
SiteLookup $siteLookup,
EntityIdFormatter $entityIdFormatter
) {
$basicEntityDiffVisualizer = new BasicEntityDiffVisualizer(
$messageLocalizer,
$claimDiffer,
$claimDiffView,
$siteLookup,
$entityIdFormatter
);
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
$prefetchingTermLookup = $wikibaseRepo->getPrefetchingTermLookup();
$labelDescriptionLookup = new LanguageFallbackLabelDescriptionLookup(
$prefetchingTermLookup,
$wikibaseRepo->getLanguageFallbackChainFactory()
->newFromLanguage( $wikibaseRepo->getUserLanguage() )
);
$entityIdFormatter = $wikibaseRepo->getEntityIdHtmlLinkFormatterFactory()
->getEntityIdFormatter( $labelDescriptionLookup );
return new LexemeDiffVisualizer(
$messageLocalizer,
$basicEntityDiffVisualizer,
$claimDiffer,
$claimDiffView,
new ItemReferenceDifferenceVisualizer(
$entityIdFormatter
)
);
},
'entity-search-callback' => function ( WebRequest $request ) {
$repo = WikibaseRepo::getDefaultInstance();
return new \Wikibase\Lexeme\Search\LexemeSearchEntity(
$repo->getEntityIdParser(),
$request,
$repo->getUserLanguage(),
$repo->getLanguageFallbackChainFactory(),
$repo->getPrefetchingTermLookup()
);
},
],
'form' => [
'entity-store-factory-callback' => function (
EntityStore $defaultStore,
EntityRevisionLookup $lookup
) {
return new FormStore( $defaultStore, $lookup );
},
'entity-revision-lookup-factory-callback' => function (
EntityRevisionLookup $defaultLookup
) {
return new FormRevisionLookup( $defaultLookup );
},
'entity-title-store-lookup-factory-callback' => function (
EntityTitleStoreLookup $defaultLookup
) {
return new FormTitleStoreLookup( $defaultLookup );
},
'entity-id-pattern' => FormId::PATTERN,
'entity-id-builder' => function ( $serialization ) {
return new FormId( $serialization );
},
'entity-differ-strategy-builder' => function () {
return new FormDiffer();
},
'entity-patcher-strategy-builder' => function () {
return new FormPatcher();
},
'content-handler-factory-callback' => function () {
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
$config = MediaWikiServices::getInstance()->getMainConfig();
if ( $config->has( 'LexemeLanguageCodePropertyId' ) ) {
$lcID = $config->get( 'LexemeLanguageCodePropertyId' );
} else {
$lcID = null;
}
return new LexemeHandler(
$wikibaseRepo->getStore()->getTermIndex(),
$wikibaseRepo->getEntityContentDataCodec(),
$wikibaseRepo->getEntityConstraintProvider(),
$wikibaseRepo->getValidatorErrorLocalizer(),
$wikibaseRepo->getEntityIdParser(),
$wikibaseRepo->getEntityIdLookup(),
$wikibaseRepo->getEntityLookup(),
$wikibaseRepo->getLanguageFallbackLabelDescriptionLookupFactory(),
new LexemeFieldDefinitions(
$wikibaseRepo->getStatementProviderDefinitions(),
$wikibaseRepo->getEntityLookup(),
$lcID ? $wikibaseRepo->getEntityIdParser()->parse( $lcID ) : null
)
);
},
],
];