forked from filbertkm/WikibaseImport
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpecialImportEntity.php
More file actions
177 lines (163 loc) · 4.74 KB
/
SpecialImportEntity.php
File metadata and controls
177 lines (163 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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 );
}
}