Skip to content

Commit 9427d03

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. On my local development wiki, I get an error (local entity not found in page_props) when importing with statements, so statements import is currently disabled. Ideally, that would be a checkbox on the form once that bug is fixed (or turns out to only affect my local wiki). 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 a70c166 commit 9427d03

5 files changed

Lines changed: 175 additions & 7 deletions

File tree

extension.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
},
2222
"config": {
2323
"WBImportSourceApi": "https://www.wikidata.org/w/api.php",
24+
"WBImportSourceURL": "https://www.wikidata.org",
25+
"WBImportSourceName": "Wikidata",
2426
"WBImportQueryUrl": "https://query.wikidata.org/bigdata/namespace/wdq/sparql",
2527
"WBImportQueryPrefixes": {
2628
"wikibase": "http://wikiba.se/ontology#",
@@ -35,6 +37,7 @@
3537
]
3638
},
3739
"SpecialPages": {
40+
"ImportEntity": "Wikibase\\Import\\Specials\\SpecialImportEntity::newFromGlobalState"
3841
},
3942
"manifest_version": 1
4043
}

i18n/en.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
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/Pxx",
12+
"wikibaseimport-importentity-error-no-local-id-title": "Redirect failed",
13+
"wikibaseimport-importentity-error-no-local-id-message": "No local entity ID for this remote entity ID found after import; does the remote entity exist?"
614
}

i18n/qqq.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
2-
"@metadata": {
3-
},
4-
"wikibaseimport-desc": "{{desc|name=WikibaseImport|url=https://www.mediawiki.org/wiki/Extension:WikibaseImport}}"
2+
"@metadata": {
3+
},
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-error-no-local-id-title": "Page title of the error page for when the local entity ID to redirect to cannot be found.",
12+
"wikibaseimport-importentity-error-no-local-id-message": "Error message for when the local entity ID to redirect to cannot be found."
513
}

src/EntityImporterFactory.php

Lines changed: 3 additions & 3 deletions
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;
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
$entityId = $this->getContext()->getRequest()->getVal( 'entityid' );
89+
if ( $entityId ) {
90+
// do the import
91+
$this->entityIdImporter->importEntities( [ $entityId ], /* importStatements */ false ); // TODO fix bug with importStatements and then make configurable
92+
// redirect to imported entity
93+
$imported = $this->entityMappingStore->getLocalId( $this->idParser->parse( $entityId ) );
94+
if ( $imported ) {
95+
$this->getOutput()->redirect(
96+
$this->entityTitleLookup->getTitleForId( $imported )->getLocalUrl()
97+
);
98+
} else {
99+
throw new ErrorPageError(
100+
"wikibaseimport-importentity-error-no-local-id-title",
101+
"wikibaseimport-importentity-error-no-local-id-message"
102+
);
103+
}
104+
return;
105+
} else {
106+
$this->setHeaders();
107+
// show explanation
108+
$this->getOutput()->addHTML(
109+
Html::rawElement(
110+
'div',
111+
[ 'class' => 'wikibaseimport-importentity-explanation' ],
112+
$this->msg( 'wikibaseimport-importentity-explanation' )->rawParams(
113+
Html::element(
114+
'a',
115+
[ 'href' => $this->getConfig()->get( 'WBImportSourceURL' ) ],
116+
$this->getConfig()->get( 'WBImportSourceName' )
117+
)
118+
)->escaped()
119+
)
120+
);
121+
// show entity ID form
122+
HTMLForm::factory(
123+
'ooui',
124+
[
125+
'entityid' => [
126+
'class' => 'HTMLTextField',
127+
'section' => 'section',
128+
'name' => 'entityid',
129+
'label-message' => [
130+
// message ID
131+
'wikibaseimport-importentity-form-entityid-label',
132+
// message parameters
133+
$this->getConfig()->get( 'WBImportSourceName' )
134+
],
135+
'cssclass' => 'wikibaseimport-importentity-form-entity-id',
136+
'placeholder' => $this->msg( 'wikibaseimport-importentity-form-entityid-placeholder' )->escaped()
137+
]
138+
],
139+
$this->getContext(),
140+
'wikibaseimport-importentity-form'
141+
)
142+
->setSubmitText( $this->msg( 'wikibaseimport-importentity-form-submit-label' )->escaped() )
143+
->setSubmitCallback( function() { return false; } )
144+
->setMethod( 'post' )
145+
->prepareForm()
146+
->displayForm( false );
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)