Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
34.62% |
72 / 208 |
|
30.77% |
4 / 13 |
CRAP | |
0.00% |
0 / 1 |
| EntitySchemaContentHandler | |
34.62% |
72 / 208 |
|
30.77% |
4 / 13 |
611.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| getContentClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSlotDiffRendererWithOptions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| canBeUsedOn | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getActionOverrides | |
0.00% |
0 / 71 |
|
0.00% |
0 / 1 |
2 | |||
| getActionOverridesEdit | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
30 | |||
| getActionOverridesSubmit | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| supportsDirectApiEditing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUndoContent | |
79.17% |
19 / 24 |
|
0.00% |
0 / 1 |
8.58 | |||
| isParserCacheSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fillParserOutput | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 | |||
| getFieldsForSearchIndex | |
76.47% |
13 / 17 |
|
0.00% |
0 / 1 |
8.83 | |||
| getDataForSearchIndex | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
9.03 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace EntitySchema\MediaWiki\Content; |
| 6 | |
| 7 | use CirrusSearch\CirrusSearch; |
| 8 | use EntitySchema\DataAccess\EntitySchemaEncoder; |
| 9 | use EntitySchema\DataAccess\LabelLookup; |
| 10 | use EntitySchema\MediaWiki\Actions\EntitySchemaEditAction; |
| 11 | use EntitySchema\MediaWiki\Actions\EntitySchemaSubmitAction; |
| 12 | use EntitySchema\MediaWiki\Actions\RestoreSubmitAction; |
| 13 | use EntitySchema\MediaWiki\Actions\RestoreViewAction; |
| 14 | use EntitySchema\MediaWiki\Actions\UndoSubmitAction; |
| 15 | use EntitySchema\MediaWiki\Actions\UndoViewAction; |
| 16 | use EntitySchema\MediaWiki\Actions\ViewEntitySchemaAction; |
| 17 | use EntitySchema\MediaWiki\UndoHandler; |
| 18 | use EntitySchema\Presentation\InputValidator; |
| 19 | use EntitySchema\Services\Converter\EntitySchemaConverter; |
| 20 | use LogicException; |
| 21 | use MediaWiki\Actions\Action; |
| 22 | use MediaWiki\Config\Config; |
| 23 | use MediaWiki\Content\Content; |
| 24 | use MediaWiki\Content\JsonContentHandler; |
| 25 | use MediaWiki\Content\Renderer\ContentParseParams; |
| 26 | use MediaWiki\Context\IContextSource; |
| 27 | use MediaWiki\Language\LanguageNameUtils; |
| 28 | use MediaWiki\Page\Article; |
| 29 | use MediaWiki\Page\WikiPage; |
| 30 | use MediaWiki\Parser\ParserOutput; |
| 31 | use MediaWiki\Parser\Parsoid\ParsoidParserFactory; |
| 32 | use MediaWiki\Permissions\PermissionManager; |
| 33 | use MediaWiki\Revision\RevisionRecord; |
| 34 | use MediaWiki\Revision\RevisionStore; |
| 35 | use MediaWiki\Revision\SlotRecord; |
| 36 | use MediaWiki\Search\SearchEngine; |
| 37 | use MediaWiki\Search\SearchIndexField; |
| 38 | use MediaWiki\Title\Title; |
| 39 | use MediaWiki\Title\TitleFactory; |
| 40 | use MediaWiki\User\Options\UserOptionsLookup; |
| 41 | use MediaWiki\User\TempUser\TempUserConfig; |
| 42 | use Wikibase\Lib\LanguageNameLookupFactory; |
| 43 | use Wikibase\Lib\SettingsArray; |
| 44 | use Wikibase\Search\Elastic\Fields\DescriptionsProviderFieldDefinitions; |
| 45 | use Wikibase\Search\Elastic\Fields\LabelsProviderFieldDefinitions; |
| 46 | use Wikimedia\Rdbms\ReadOnlyMode; |
| 47 | |
| 48 | /** |
| 49 | * Content handler for the EntitySchema content |
| 50 | * |
| 51 | * @license GPL-2.0-or-later |
| 52 | */ |
| 53 | class EntitySchemaContentHandler extends JsonContentHandler { |
| 54 | |
| 55 | /** |
| 56 | * @var LabelsProviderFieldDefinitions|null The search field definitions for labels, |
| 57 | * or null if WikibaseCirrusSearch is not loaded and no search fields are available. |
| 58 | */ |
| 59 | private ?LabelsProviderFieldDefinitions $labelsFieldDefinitions; |
| 60 | |
| 61 | /** |
| 62 | * @var DescriptionsProviderFieldDefinitions|null The search field definitions for descriptions, |
| 63 | * or null if WikibaseCirrusSearch is not loaded and no search fields are available. |
| 64 | */ |
| 65 | private ?DescriptionsProviderFieldDefinitions $descriptionsFieldDefinitions; |
| 66 | |
| 67 | private LanguageNameLookupFactory $languageNameLookupFactory; |
| 68 | |
| 69 | private LabelLookup $labelLookup; |
| 70 | |
| 71 | public function __construct( |
| 72 | string $modelId, |
| 73 | ParsoidParserFactory $parsoidParserFactory, |
| 74 | TitleFactory $titleFactory, |
| 75 | LabelLookup $labelLookup, |
| 76 | LanguageNameLookupFactory $languageNameLookupFactory, |
| 77 | ?LabelsProviderFieldDefinitions $labelsFieldDefinitions, |
| 78 | ?DescriptionsProviderFieldDefinitions $descriptionsFieldDefinitions |
| 79 | ) { |
| 80 | // $modelId is typically EntitySchemaContent::CONTENT_MODEL_ID |
| 81 | parent::__construct( |
| 82 | $modelId, |
| 83 | $parsoidParserFactory, |
| 84 | $titleFactory |
| 85 | ); |
| 86 | $this->labelLookup = $labelLookup; |
| 87 | $this->languageNameLookupFactory = $languageNameLookupFactory; |
| 88 | $this->labelsFieldDefinitions = $labelsFieldDefinitions; |
| 89 | $this->descriptionsFieldDefinitions = $descriptionsFieldDefinitions; |
| 90 | } |
| 91 | |
| 92 | protected function getContentClass(): string { |
| 93 | return EntitySchemaContent::class; |
| 94 | } |
| 95 | |
| 96 | /** @inheritDoc */ |
| 97 | protected function getSlotDiffRendererWithOptions( |
| 98 | IContextSource $context, |
| 99 | $options = [] |
| 100 | ): EntitySchemaSlotDiffRenderer { |
| 101 | return new EntitySchemaSlotDiffRenderer( |
| 102 | $context, |
| 103 | $this->createTextSlotDiffRenderer( $options ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | public function canBeUsedOn( Title $title ): bool { |
| 108 | return $title->inNamespace( NS_ENTITYSCHEMA_JSON ) && parent::canBeUsedOn( $title ); |
| 109 | } |
| 110 | |
| 111 | public function getActionOverrides(): array { |
| 112 | return [ |
| 113 | 'edit' => [ |
| 114 | 'factory' => function ( |
| 115 | Article $article, |
| 116 | IContextSource $context, |
| 117 | RevisionStore $revisionStore, |
| 118 | Config $mainConfig, |
| 119 | LanguageNameUtils $languageNameUtils, |
| 120 | UserOptionsLookup $userOptionsLookup, |
| 121 | SettingsArray $repoSettings, |
| 122 | TempUserConfig $tempUserConfig |
| 123 | ) { |
| 124 | return $this->getActionOverridesEdit( |
| 125 | $article, |
| 126 | $context, |
| 127 | $revisionStore, |
| 128 | $mainConfig, |
| 129 | $languageNameUtils, |
| 130 | $userOptionsLookup, |
| 131 | $repoSettings, |
| 132 | $tempUserConfig |
| 133 | ); |
| 134 | }, |
| 135 | 'services' => [ |
| 136 | 'RevisionStore', |
| 137 | 'MainConfig', |
| 138 | 'LanguageNameUtils', |
| 139 | 'UserOptionsLookup', |
| 140 | 'WikibaseRepo.Settings', |
| 141 | 'TempUserConfig', |
| 142 | ], |
| 143 | ], |
| 144 | 'submit' => [ |
| 145 | 'factory' => function ( |
| 146 | Article $article, |
| 147 | IContextSource $context, |
| 148 | ReadOnlyMode $readOnlyMode, |
| 149 | RevisionStore $revisionStore, |
| 150 | PermissionManager $permissionManager, |
| 151 | Config $mainConfig, |
| 152 | LanguageNameUtils $languageNameUtils, |
| 153 | UserOptionsLookup $userOptionsLookup, |
| 154 | SettingsArray $repoSettings, |
| 155 | TempUserConfig $tempUserConfig |
| 156 | ) { |
| 157 | return $this->getActionOverridesSubmit( |
| 158 | $article, |
| 159 | $context, |
| 160 | $readOnlyMode, |
| 161 | $revisionStore, |
| 162 | $permissionManager, |
| 163 | $mainConfig, |
| 164 | $languageNameUtils, |
| 165 | $userOptionsLookup, |
| 166 | $repoSettings, |
| 167 | $tempUserConfig |
| 168 | ); |
| 169 | }, |
| 170 | 'services' => [ |
| 171 | 'ReadOnlyMode', |
| 172 | 'RevisionStore', |
| 173 | 'PermissionManager', |
| 174 | 'MainConfig', |
| 175 | 'LanguageNameUtils', |
| 176 | 'UserOptionsLookup', |
| 177 | 'WikibaseRepo.Settings', |
| 178 | 'TempUserConfig', |
| 179 | ], |
| 180 | ], |
| 181 | 'view' => ViewEntitySchemaAction::class, |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | private function getActionOverridesEdit( |
| 186 | Article $article, |
| 187 | IContextSource $context, |
| 188 | RevisionStore $revisionStore, |
| 189 | Config $mainConfig, |
| 190 | LanguageNameUtils $languageNameUtils, |
| 191 | UserOptionsLookup $userOptionsLookup, |
| 192 | SettingsArray $repoSettings, |
| 193 | TempUserConfig $tempUserConfig |
| 194 | ): Action { |
| 195 | global $wgEditSubmitButtonLabelPublish; |
| 196 | |
| 197 | if ( $article->getPage()->getRevisionRecord() === null ) { |
| 198 | return Action::factory( 'view', $article, $context ); |
| 199 | } |
| 200 | |
| 201 | $req = $context->getRequest(); |
| 202 | |
| 203 | if ( |
| 204 | $req->getCheck( 'undo' ) |
| 205 | || $req->getCheck( 'undoafter' ) |
| 206 | ) { |
| 207 | return new UndoViewAction( |
| 208 | $article, |
| 209 | $context, |
| 210 | $this->getSlotDiffRendererWithOptions( $context ), |
| 211 | $revisionStore |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | if ( $req->getCheck( 'restore' ) ) { |
| 216 | return new RestoreViewAction( |
| 217 | $article, |
| 218 | $context, |
| 219 | $this->getSlotDiffRendererWithOptions( $context ) |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | // TODo: check redirect? |
| 224 | // !$article->isRedirect() |
| 225 | return new EntitySchemaEditAction( |
| 226 | $article, |
| 227 | $context, |
| 228 | new InputValidator( $context, $mainConfig, $languageNameUtils ), |
| 229 | $wgEditSubmitButtonLabelPublish, |
| 230 | $userOptionsLookup, |
| 231 | $repoSettings->getSetting( 'dataRightsUrl' ), |
| 232 | $repoSettings->getSetting( 'dataRightsText' ), |
| 233 | $tempUserConfig |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | private function getActionOverridesSubmit( |
| 238 | Article $article, |
| 239 | IContextSource $context, |
| 240 | ReadOnlyMode $readOnlyMode, |
| 241 | RevisionStore $revisionStore, |
| 242 | PermissionManager $permissionManager, |
| 243 | Config $mainConfig, |
| 244 | LanguageNameUtils $languageNameUtils, |
| 245 | UserOptionsLookup $userOptionsLookup, |
| 246 | SettingsArray $repoSettings, |
| 247 | TempUserConfig $tempUserConfig |
| 248 | ): Action { |
| 249 | global $wgEditSubmitButtonLabelPublish; |
| 250 | $req = $context->getRequest(); |
| 251 | |
| 252 | if ( |
| 253 | $req->getCheck( 'undo' ) |
| 254 | || $req->getCheck( 'undoafter' ) |
| 255 | ) { |
| 256 | return new UndoSubmitAction( |
| 257 | $article, |
| 258 | $context, |
| 259 | $readOnlyMode, |
| 260 | $permissionManager, |
| 261 | $revisionStore |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | if ( $req->getCheck( 'restore' ) ) { |
| 266 | return new RestoreSubmitAction( $article, $context ); |
| 267 | } |
| 268 | |
| 269 | return new EntitySchemaSubmitAction( |
| 270 | $article, |
| 271 | $context, |
| 272 | new InputValidator( $context, $mainConfig, $languageNameUtils ), |
| 273 | $wgEditSubmitButtonLabelPublish, |
| 274 | $userOptionsLookup, |
| 275 | $repoSettings->getSetting( 'dataRightsUrl' ), |
| 276 | $repoSettings->getSetting( 'dataRightsText' ), |
| 277 | $tempUserConfig |
| 278 | ); |
| 279 | } |
| 280 | |
| 281 | public function supportsDirectApiEditing(): bool { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Get the Content object that needs to be saved in order to undo all revisions |
| 287 | * between $undo and $undoafter. Revisions must belong to the same page, |
| 288 | * must exist and must not be deleted. |
| 289 | * |
| 290 | * @since 1.32 accepts Content objects for all parameters instead of Revision objects. |
| 291 | * Passing Revision objects is deprecated. |
| 292 | * @since 1.37 only accepts Content objects |
| 293 | * |
| 294 | * @param Content $baseContent The current text |
| 295 | * @param Content $undoFromContent The content of the revision to undo |
| 296 | * @param Content $undoToContent Must be from an earlier revision than $undo |
| 297 | * @param bool $undoIsLatest Set true if $undo is from the current revision (since 1.32) |
| 298 | * |
| 299 | * @return Content|false |
| 300 | */ |
| 301 | public function getUndoContent( |
| 302 | Content $baseContent, |
| 303 | Content $undoFromContent, |
| 304 | Content $undoToContent, |
| 305 | $undoIsLatest = false |
| 306 | ) { |
| 307 | if ( $undoIsLatest ) { |
| 308 | return $undoToContent; |
| 309 | } |
| 310 | |
| 311 | // Make sure correct subclass |
| 312 | if ( !$baseContent instanceof EntitySchemaContent || |
| 313 | !$undoFromContent instanceof EntitySchemaContent || |
| 314 | !$undoToContent instanceof EntitySchemaContent |
| 315 | ) { |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | $undoHandler = new UndoHandler(); |
| 320 | try { |
| 321 | $schemaId = $undoHandler->validateContentIds( $undoToContent, $undoFromContent, $baseContent ); |
| 322 | } catch ( LogicException ) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | $diffStatus = $undoHandler->getDiffFromContents( $undoFromContent, $undoToContent ); |
| 327 | if ( !$diffStatus->isOK() ) { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | $patchStatus = $undoHandler->tryPatching( $diffStatus->getValue(), $baseContent ); |
| 332 | if ( !$patchStatus->isOK() ) { |
| 333 | return false; |
| 334 | } |
| 335 | $patchedSchema = $patchStatus->getValue()->data; |
| 336 | |
| 337 | return new EntitySchemaContent( EntitySchemaEncoder::getPersistentRepresentation( |
| 338 | $schemaId, |
| 339 | $patchedSchema['labels'], |
| 340 | $patchedSchema['descriptions'], |
| 341 | $patchedSchema['aliases'], |
| 342 | $patchedSchema['schemaText'] |
| 343 | ) ); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Returns true to indicate that the parser cache can be used for Schemas. |
| 348 | * |
| 349 | * @note The html representation of Schemas depends on the user language, so |
| 350 | * EntitySchemaContent::getParserOutput needs to make sure |
| 351 | * ParserOutput::recordOption( 'userlang' ) is called to split the cache by user language. |
| 352 | * |
| 353 | * @see ContentHandler::isParserCacheSupported |
| 354 | * |
| 355 | * @return bool Always true in this default implementation. |
| 356 | */ |
| 357 | public function isParserCacheSupported(): bool { |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * @inheritDoc |
| 363 | */ |
| 364 | protected function fillParserOutput( |
| 365 | Content $content, |
| 366 | ContentParseParams $cpoParams, |
| 367 | ParserOutput &$parserOutput |
| 368 | ): void { |
| 369 | '@phan-var EntitySchemaContent $content'; |
| 370 | $parserOptions = $cpoParams->getParserOptions(); |
| 371 | $generateHtml = $cpoParams->getGenerateHtml(); |
| 372 | if ( $generateHtml && $content->isValid() ) { |
| 373 | $languageCode = $parserOptions->getUserLang(); |
| 374 | $renderer = new EntitySchemaSlotViewRenderer( |
| 375 | $languageCode, |
| 376 | $this->labelLookup, |
| 377 | $this->languageNameLookupFactory |
| 378 | ); |
| 379 | $renderer->fillParserOutput( |
| 380 | ( new EntitySchemaConverter() ) |
| 381 | ->getFullViewSchemaData( $content->getText() ), |
| 382 | $cpoParams->getPage(), |
| 383 | $parserOutput |
| 384 | ); |
| 385 | } else { |
| 386 | $parserOutput->setContentHolderText( '' ); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * @param SearchEngine $engine |
| 392 | * @return SearchIndexField[] List of fields this content handler can provide. |
| 393 | */ |
| 394 | public function getFieldsForSearchIndex( SearchEngine $engine ): array { |
| 395 | if ( $this->labelsFieldDefinitions === null || $this->descriptionsFieldDefinitions === null ) { |
| 396 | if ( $engine instanceof CirrusSearch ) { |
| 397 | wfLogWarning( |
| 398 | 'Trying to use CirrusSearch but WikibaseCirrusSearch is not loaded. ' . |
| 399 | 'EntitySchema search is not available; consider loading WikibaseCirrusSearch.' |
| 400 | ); |
| 401 | } |
| 402 | return []; |
| 403 | } else { |
| 404 | $fields = []; |
| 405 | foreach ( $this->labelsFieldDefinitions->getFields() as $name => $field ) { |
| 406 | $mappingField = $field->getMappingField( $engine, $name ); |
| 407 | if ( $mappingField !== null ) { |
| 408 | $fields[$name] = $mappingField; |
| 409 | } |
| 410 | } |
| 411 | foreach ( $this->descriptionsFieldDefinitions->getFields() as $name => $field ) { |
| 412 | $mappingField = $field->getMappingField( $engine, $name ); |
| 413 | if ( $mappingField !== null ) { |
| 414 | $fields[$name] = $mappingField; |
| 415 | } |
| 416 | } |
| 417 | return $fields; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | public function getDataForSearchIndex( |
| 422 | WikiPage $page, |
| 423 | ParserOutput $output, |
| 424 | SearchEngine $engine, |
| 425 | ?RevisionRecord $revision = null |
| 426 | ): array { |
| 427 | $fieldsData = parent::getDataForSearchIndex( $page, $output, $engine, $revision ); |
| 428 | if ( $this->labelsFieldDefinitions === null || $this->descriptionsFieldDefinitions === null ) { |
| 429 | return $fieldsData; |
| 430 | } |
| 431 | $content = $revision !== null ? $revision->getContent( SlotRecord::MAIN ) : $page->getContent(); |
| 432 | if ( $content instanceof EntitySchemaContent ) { |
| 433 | $adapter = ( new EntitySchemaConverter() ) |
| 434 | ->getSearchEntitySchemaAdapter( $content->getText() ); |
| 435 | foreach ( $this->labelsFieldDefinitions->getFields() as $name => $field ) { |
| 436 | if ( $field !== null ) { |
| 437 | $fieldsData[$name] = $field->getLabelsIndexedData( $adapter ); |
| 438 | } |
| 439 | } |
| 440 | foreach ( $this->descriptionsFieldDefinitions->getFields() as $name => $field ) { |
| 441 | if ( $field !== null ) { |
| 442 | $fieldsData[$name] = $field->getDescriptionsIndexedData( $adapter ); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | return $fieldsData; |
| 447 | } |
| 448 | |
| 449 | } |