Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add special page to import an entity #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
},
"config": {
"WBImportSourceApi": "https://www.wikidata.org/w/api.php",
"WBImportSourceURL": "https://www.wikidata.org",
"WBImportSourceName": "Wikidata",
"WBImportQueryUrl": "https://query.wikidata.org/bigdata/namespace/wdq/sparql",
"WBImportQueryPrefixes": {
"wikibase": "http://wikiba.se/ontology#",
Expand All @@ -36,6 +38,7 @@
]
},
"SpecialPages": {
"ImportEntity": "Wikibase\\Import\\Specials\\SpecialImportEntity::newFromGlobalState"
},
"manifest_version": 1
}
11 changes: 10 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@
"@metadata": {
"authors": []
},
"wikibaseimport-desc": "Allows importing data from another Wikibase instance"
"wikibaseimport-desc": "Allows importing data from another Wikibase instance",
"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.",
"wikibaseimport-importentity-desc": "Import an entity",
"wikibaseimport-importentity-form-section": "Import entity",
"wikibaseimport-importentity-form-submit-label": "Import",
"wikibaseimport-importentity-form-entityid-label": "Entity ID on $1",
"wikibaseimport-importentity-form-entityid-placeholder": "Qxx or Pxx",
"wikibaseimport-importentity-form-importstatements-label": "Import statements (not supported yet)",
"wikibaseimport-importentity-error-no-local-id-title": "Redirect failed",
"wikibaseimport-importentity-error-no-local-id-message": "No local entity ID for this remote entity ID found after import; does the remote entity exist?"
}
11 changes: 10 additions & 1 deletion i18n/qqq.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"@metadata": {
},
"wikibaseimport-desc": "{{desc|name=WikibaseImport|url=https://www.mediawiki.org/wiki/Extension:WikibaseImport}}"
"wikibaseimport-desc": "{{desc|name=WikibaseImport|url=https://www.mediawiki.org/wiki/Extension:WikibaseImport}}",
"wikibaseimport-importentity-desc": "{{doc-special|ImportEntity}}",
"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.",
"wikibaseimport-importentity-form-section": "Header of the section of the entity ID form.",
"wikibaseimport-importentity-form-submit-label": "Label for the button that starts the import.",
"wikibaseimport-importentity-form-entityid-label": "Label for the entity ID input field.",
"wikibaseimport-importentity-form-entityid-placeholder": "Entity ID placeholder for the input field.",
"wikibaseimport-importentity-form-importstatements-label": "Label for the “import statements” checkbox. This feature is not supported yet, so the checkbox is disabled.",
"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.",
"wikibaseimport-importentity-error-no-local-id-message": "Error message for when the local entity ID to redirect to cannot be found."
}
6 changes: 3 additions & 3 deletions src/EntityImporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private function newStatementsImporter() {
);
}

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

Expand Down Expand Up @@ -129,5 +129,5 @@ private function newSerializerFactory() {
}
}

$maintClass = "Wikibase\Import\Maintenance\ImportEntities";
require_once RUN_MAINTENANCE_IF_MAIN;
//$maintClass = "Wikibase\Import\Maintenance\ImportEntities";
//require_once RUN_MAINTENANCE_IF_MAIN;
177 changes: 177 additions & 0 deletions src/Specials/SpecialImportEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace Wikibase\Import\Specials;

use ErrorPageError;
use Html;
use HTMLForm;
use MediaWiki\MediaWikiServices;
use SpecialPage;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\Import\EntityImporter;
use Wikibase\Import\EntityImporterFactory;
use Wikibase\Import\LoggerFactory;
use Wikibase\Import\Store\ImportedEntityMappingStore;
use Wikibase\Lib\Store\EntityTitleLookup;
use Wikibase\Repo\WikibaseRepo;

class SpecialImportEntity extends SpecialPage {

/**
* @var EntityImporter
*/
private $entityIdImporter;

/**
* @var ImportedEntityMappingStore
*/
private $entityMappingStore;

/**
* @var EntityIdParser
*/
private $idParser;

/**
* @var EntityTitleLookup
*/
private $entityTitleLookup;

public static function newFromGlobalState() {
$repo = WikibaseRepo::getDefaultInstance();
$logger = LoggerFactory::newLogger( 'wikibase-import', /* quiet */ true );
$entityImporterFactory = new EntityImporterFactory(
$repo->getStore()->getEntityStore(),
wfGetLB(),
$logger,
MediaWikiServices::getInstance()->getMainConfig()->get( 'WBImportSourceApi' )
);
return new self(
$entityImporterFactory->newEntityImporter(),
$entityImporterFactory->getImportedEntityMappingStore(),
$repo->getEntityIdParser(),
$repo->getEntityTitleLookup()
);
}

/**
* @param EntityImporter $entityIdImporter
*/
public function __construct(
EntityImporter $entityIdImporter,
ImportedEntityMappingStore $entityMappingStore,
EntityIdParser $idParser,
EntityTitleLookup $entityTitleLookup
) {
parent::__construct( 'ImportEntity' );
$this->entityIdImporter = $entityIdImporter;
$this->entityMappingStore = $entityMappingStore;
$this->idParser = $idParser;
$this->entityTitleLookup = $entityTitleLookup;
}

/**
* @see SpecialPage::getDescription
*
* @return string
*/
public function getDescription() {
return $this->msg( 'wikibaseimport-importentity-desc' )->escaped();
}

/**
* @see SpecialPage::execute
*
* @param string|null $subPage
*/
public function execute( $subPage ) {
if ( $this->getContext()->getRequest()->wasPosted() ) {
$this->doImport();
} else {
$this->showPage( $subPage );
}
}

private function doImport() {
$entityId = $this->getContext()->getRequest()->getText( 'wpEntityId' );
$importStatements = $this->getContext()->getRequest()->getCheck( 'wpImportStatements' );
if ( $importStatements ) {
// TODO fix bug with importStatements and then remove this if clause and enable the checkbox
throw new \MWException( 'Importing statements is not yet supported!' );
}
$this->entityIdImporter->importEntities( [ $entityId ], $importStatements );
// redirect to imported entity
$imported = $this->entityMappingStore->getLocalId( $this->idParser->parse( $entityId ) );
if ( $imported ) {
$this->getOutput()->redirect(
$this->entityTitleLookup->getTitleForId( $imported )->getLocalUrl()
);
} else {
throw new ErrorPageError(
"wikibaseimport-importentity-error-no-local-id-title",
"wikibaseimport-importentity-error-no-local-id-message"
);
}
}

/**
* Show the special page with explanation and form.
*
* @param string|null $subPage
*/
private function showPage( $subPage ) {
$this->setHeaders();
// show explanation
$this->getOutput()->addHTML(
Html::rawElement(
'div',
[ 'class' => 'wikibaseimport-importentity-explanation' ],
$this->msg( 'wikibaseimport-importentity-explanation' )->rawParams(
Html::element(
'a',
[ 'href' => $this->getConfig()->get( 'WBImportSourceURL' ) ],
$this->getConfig()->get( 'WBImportSourceName' )
)
)->escaped()
)
);
// show entity ID form
$formDescription = [];
$formDescription['EntityId'] = [
'type' => 'text',
'section' => 'section',
'label-message' => [
// message ID
'wikibaseimport-importentity-form-entityid-label',
// message parameters
$this->getConfig()->get( 'WBImportSourceName' )
],
'placeholder' => $this->msg( 'wikibaseimport-importentity-form-entityid-placeholder' )->escaped(),
];
if ( is_string( $subPage ) && preg_match( '/(P|Q)[0-9]+/', $subPage ) ) {
$formDescription['EntityId']['default'] = $subPage;
}
$formDescription['ImportStatements'] = [
'type' => 'check',
'section' => 'section',
'label-message' => 'wikibaseimport-importentity-form-importstatements-label',
'disabled' => true, // TODO remove this once importing statements works
];
HTMLForm::factory(
'ooui',
$formDescription,
$this->getContext(),
'wikibaseimport-importentity-form'
)
->setSubmitText( $this->msg( 'wikibaseimport-importentity-form-submit-label' )->escaped() )
->setSubmitCallback(
function() {
return false;
}
)
->setMethod( 'post' )
->prepareForm()
->displayForm( false );
}

}