|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Wikibase\Import\Specials; |
| 4 | + |
| 5 | +use ErrorPageError; |
| 6 | +use Html; |
| 7 | +use HTMLForm; |
| 8 | +use MediaWiki\MediaWikiServices; |
| 9 | +use SpecialPage; |
| 10 | +use Wikibase\DataModel\Entity\EntityIdParser; |
| 11 | +use Wikibase\Import\EntityImporter; |
| 12 | +use Wikibase\Import\EntityImporterFactory; |
| 13 | +use Wikibase\Import\LoggerFactory; |
| 14 | +use Wikibase\Import\Store\ImportedEntityMappingStore; |
| 15 | +use Wikibase\Lib\Store\EntityTitleLookup; |
| 16 | +use Wikibase\Repo\WikibaseRepo; |
| 17 | + |
| 18 | +class SpecialImportEntity extends SpecialPage { |
| 19 | + |
| 20 | + /** |
| 21 | + * @var EntityImporter |
| 22 | + */ |
| 23 | + private $entityIdImporter; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ImportedEntityMappingStore |
| 27 | + */ |
| 28 | + private $entityMappingStore; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var EntityIdParser |
| 32 | + */ |
| 33 | + private $idParser; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var EntityTitleLookup |
| 37 | + */ |
| 38 | + private $entityTitleLookup; |
| 39 | + |
| 40 | + public static function newFromGlobalState() { |
| 41 | + $repo = WikibaseRepo::getDefaultInstance(); |
| 42 | + $logger = LoggerFactory::newLogger( 'wikibase-import', /* quiet */ true ); |
| 43 | + $entityImporterFactory = new EntityImporterFactory( |
| 44 | + $repo->getStore()->getEntityStore(), |
| 45 | + wfGetLB(), |
| 46 | + $logger, |
| 47 | + MediaWikiServices::getInstance()->getMainConfig()->get( 'WBImportSourceApi' ) |
| 48 | + ); |
| 49 | + return new self( |
| 50 | + $entityImporterFactory->newEntityImporter(), |
| 51 | + $entityImporterFactory->getImportedEntityMappingStore(), |
| 52 | + $repo->getEntityIdParser(), |
| 53 | + $repo->getEntityTitleLookup() |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param EntityImporter $entityIdImporter |
| 59 | + */ |
| 60 | + public function __construct( |
| 61 | + EntityImporter $entityIdImporter, |
| 62 | + ImportedEntityMappingStore $entityMappingStore, |
| 63 | + EntityIdParser $idParser, |
| 64 | + EntityTitleLookup $entityTitleLookup |
| 65 | + ) { |
| 66 | + parent::__construct( 'ImportEntity' ); |
| 67 | + $this->entityIdImporter = $entityIdImporter; |
| 68 | + $this->entityMappingStore = $entityMappingStore; |
| 69 | + $this->idParser = $idParser; |
| 70 | + $this->entityTitleLookup = $entityTitleLookup; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @see SpecialPage::getDescription |
| 75 | + * |
| 76 | + * @return string |
| 77 | + */ |
| 78 | + public function getDescription() { |
| 79 | + return $this->msg( 'wikibaseimport-importentity-desc' )->escaped(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @see SpecialPage::execute |
| 84 | + * |
| 85 | + * @param string|null $subPage |
| 86 | + */ |
| 87 | + public function execute( $subPage ) { |
| 88 | + if ( $this->getContext()->getRequest()->wasPosted() ) { |
| 89 | + $this->doImport(); |
| 90 | + } else { |
| 91 | + $this->showPage( $subPage ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private function doImport() { |
| 96 | + $entityId = $this->getContext()->getRequest()->getText( 'wpEntityId' ); |
| 97 | + $importStatements = $this->getContext()->getRequest()->getCheck( 'wpImportStatements' ); |
| 98 | + if ( $importStatements ) { |
| 99 | + // TODO fix bug with importStatements and then remove this if clause and enable the checkbox |
| 100 | + throw new \MWException( 'Importing statements is not yet supported!' ); |
| 101 | + } |
| 102 | + $this->entityIdImporter->importEntities( [ $entityId ], $importStatements ); |
| 103 | + // redirect to imported entity |
| 104 | + $imported = $this->entityMappingStore->getLocalId( $this->idParser->parse( $entityId ) ); |
| 105 | + if ( $imported ) { |
| 106 | + $this->getOutput()->redirect( |
| 107 | + $this->entityTitleLookup->getTitleForId( $imported )->getLocalUrl() |
| 108 | + ); |
| 109 | + } else { |
| 110 | + throw new ErrorPageError( |
| 111 | + "wikibaseimport-importentity-error-no-local-id-title", |
| 112 | + "wikibaseimport-importentity-error-no-local-id-message" |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Show the special page with explanation and form. |
| 119 | + * |
| 120 | + * @param string|null $subPage |
| 121 | + */ |
| 122 | + private function showPage( $subPage ) { |
| 123 | + $this->setHeaders(); |
| 124 | + // show explanation |
| 125 | + $this->getOutput()->addHTML( |
| 126 | + Html::rawElement( |
| 127 | + 'div', |
| 128 | + [ 'class' => 'wikibaseimport-importentity-explanation' ], |
| 129 | + $this->msg( 'wikibaseimport-importentity-explanation' )->rawParams( |
| 130 | + Html::element( |
| 131 | + 'a', |
| 132 | + [ 'href' => $this->getConfig()->get( 'WBImportSourceURL' ) ], |
| 133 | + $this->getConfig()->get( 'WBImportSourceName' ) |
| 134 | + ) |
| 135 | + )->escaped() |
| 136 | + ) |
| 137 | + ); |
| 138 | + // show entity ID form |
| 139 | + $formDescription = []; |
| 140 | + $formDescription['EntityId'] = [ |
| 141 | + 'type' => 'text', |
| 142 | + 'section' => 'section', |
| 143 | + 'label-message' => [ |
| 144 | + // message ID |
| 145 | + 'wikibaseimport-importentity-form-entityid-label', |
| 146 | + // message parameters |
| 147 | + $this->getConfig()->get( 'WBImportSourceName' ) |
| 148 | + ], |
| 149 | + 'placeholder' => $this->msg( 'wikibaseimport-importentity-form-entityid-placeholder' )->escaped(), |
| 150 | + ]; |
| 151 | + if ( is_string( $subPage ) && preg_match( '/(P|Q)[0-9]+/', $subPage ) ) { |
| 152 | + $formDescription['EntityId']['default'] = $subPage; |
| 153 | + } |
| 154 | + $formDescription['ImportStatements'] = [ |
| 155 | + 'type' => 'check', |
| 156 | + 'section' => 'section', |
| 157 | + 'label-message' => 'wikibaseimport-importentity-form-importstatements-label', |
| 158 | + 'disabled' => true, // TODO remove this once importing statements works |
| 159 | + ]; |
| 160 | + HTMLForm::factory( |
| 161 | + 'ooui', |
| 162 | + $formDescription, |
| 163 | + $this->getContext(), |
| 164 | + 'wikibaseimport-importentity-form' |
| 165 | + ) |
| 166 | + ->setSubmitText( $this->msg( 'wikibaseimport-importentity-form-submit-label' )->escaped() ) |
| 167 | + ->setSubmitCallback( |
| 168 | + function() { |
| 169 | + return false; |
| 170 | + } |
| 171 | + ) |
| 172 | + ->setMethod( 'post' ) |
| 173 | + ->prepareForm() |
| 174 | + ->displayForm( false ); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments