Skip to content

Commit 3cb9440

Browse files
Add special page to import an entity
Special:ImportEntity imports a single entity and then redirects to the imported entity. Two new configuration options are added to provide a human-readable link to the remote Wikibase installation on the special page, in addition to the API URL that the extension needs to actually import the entity. Importing statements does not work properly, since the page_props table is apparently not updated at all anymore once the first attempt to import statements is made. For now, the checkbox to import statements is disabled, and statements are never imported. This commit is not yet ready for merging, since it currently includes two changes to EntityImporterFactory; the change from private to public might be necessary, but the commented-out toplevel statements probably have some better solution.
1 parent 3c79baa commit 3cb9440

File tree

5 files changed

+203
-5
lines changed

5 files changed

+203
-5
lines changed

extension.json

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
},
2323
"config": {
2424
"WBImportSourceApi": "https://www.wikidata.org/w/api.php",
25+
"WBImportSourceURL": "https://www.wikidata.org",
26+
"WBImportSourceName": "Wikidata",
2527
"WBImportQueryUrl": "https://query.wikidata.org/bigdata/namespace/wdq/sparql",
2628
"WBImportQueryPrefixes": {
2729
"wikibase": "http://wikiba.se/ontology#",
@@ -36,6 +38,7 @@
3638
]
3739
},
3840
"SpecialPages": {
41+
"ImportEntity": "Wikibase\\Import\\Specials\\SpecialImportEntity::newFromGlobalState"
3942
},
4043
"manifest_version": 1
4144
}

i18n/en.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,14 @@
22
"@metadata": {
33
"authors": []
44
},
5-
"wikibaseimport-desc": "Allows importing data from another Wikibase instance"
5+
"wikibaseimport-desc": "Allows importing data from another Wikibase instance",
6+
"wikibaseimport-importentity-explanation": "This special page imports an entity (item or property) from $1. After the import is completed (which can take several seconds), you will be redirected to the local version of the entity. If the entity was already imported, no new entity is created, and you will be redirected to the existing local entity.",
7+
"wikibaseimport-importentity-desc": "Import an entity",
8+
"wikibaseimport-importentity-form-section": "Import entity",
9+
"wikibaseimport-importentity-form-submit-label": "Import",
10+
"wikibaseimport-importentity-form-entityid-label": "Entity ID on $1",
11+
"wikibaseimport-importentity-form-entityid-placeholder": "Qxx or Pxx",
12+
"wikibaseimport-importentity-form-importstatements-label": "Import statements (not supported yet)",
13+
"wikibaseimport-importentity-error-no-local-id-title": "Redirect failed",
14+
"wikibaseimport-importentity-error-no-local-id-message": "No local entity ID for this remote entity ID found after import; does the remote entity exist?"
615
}

i18n/qqq.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
{
22
"@metadata": {
33
},
4-
"wikibaseimport-desc": "{{desc|name=WikibaseImport|url=https://www.mediawiki.org/wiki/Extension:WikibaseImport}}"
4+
"wikibaseimport-desc": "{{desc|name=WikibaseImport|url=https://www.mediawiki.org/wiki/Extension:WikibaseImport}}",
5+
"wikibaseimport-importentity-desc": "{{doc-special|ImportEntity}}",
6+
"wikibaseimport-importentity-explanation": "Explanation of what this special page does. This text is displayed on the special page above the entity ID form. Parameters:\n* $1 contains a link to the remote (source) Wikibase installation from which entities are imported.",
7+
"wikibaseimport-importentity-form-section": "Header of the section of the entity ID form.",
8+
"wikibaseimport-importentity-form-submit-label": "Label for the button that starts the import.",
9+
"wikibaseimport-importentity-form-entityid-label": "Label for the entity ID input field.",
10+
"wikibaseimport-importentity-form-entityid-placeholder": "Entity ID placeholder for the input field.",
11+
"wikibaseimport-importentity-form-importstatements-label": "Label for the “import statements” checkbox. This feature is not supported yet, so the checkbox is disabled.",
12+
"wikibaseimport-importentity-error-no-local-id-title": "Page title of the error page for when the local entity ID to redirect to cannot be found.",
13+
"wikibaseimport-importentity-error-no-local-id-message": "Error message for when the local entity ID to redirect to cannot be found."
514
}

src/EntityImporterFactory.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private function newStatementsImporter() {
101101
);
102102
}
103103

104-
private function getImportedEntityMappingStore() {
104+
public function getImportedEntityMappingStore() {
105105
if ( $this->importedEntityMappingStore === null ) {
106106
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
107107

@@ -129,5 +129,5 @@ private function newSerializerFactory() {
129129
}
130130
}
131131

132-
$maintClass = "Wikibase\Import\Maintenance\ImportEntities";
133-
require_once RUN_MAINTENANCE_IF_MAIN;
132+
//$maintClass = "Wikibase\Import\Maintenance\ImportEntities";
133+
//require_once RUN_MAINTENANCE_IF_MAIN;

src/Specials/SpecialImportEntity.php

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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

Comments
 (0)