Inject dependencies into some ImportTitleFactory classes
NaiveImportTitleFactory NamespaceImportTitleFactory SubpageImportTitleFactory Change-Id: I0ab83d1c6361d5a6f06b0c765c0e997d16e3bdb5
This commit is contained in:
parent
bb1b2bff4f
commit
54cd9e478e
7 changed files with 127 additions and 42 deletions
|
|
@ -115,7 +115,13 @@ class WikiImporter {
|
|||
$this->setLogItemCallback( [ $this, 'importLogItem' ] );
|
||||
$this->setPageOutCallback( [ $this, 'finishImportPage' ] );
|
||||
|
||||
$this->importTitleFactory = new NaiveImportTitleFactory();
|
||||
// TODO inject
|
||||
$services = MediaWikiServices::getInstance();
|
||||
$this->importTitleFactory = new NaiveImportTitleFactory(
|
||||
$services->getContentLanguage(),
|
||||
$services->getNamespaceInfo(),
|
||||
$services->getTitleFactory()
|
||||
);
|
||||
$this->externalUserNames = new ExternalUserNames( 'imported', false );
|
||||
}
|
||||
|
||||
|
|
@ -285,16 +291,29 @@ class WikiImporter {
|
|||
* @return bool
|
||||
*/
|
||||
public function setTargetNamespace( $namespace ) {
|
||||
$services = MediaWikiServices::getInstance();
|
||||
if ( $namespace === null ) {
|
||||
// Don't override namespaces
|
||||
$this->setImportTitleFactory( new NaiveImportTitleFactory() );
|
||||
$this->setImportTitleFactory(
|
||||
new NaiveImportTitleFactory(
|
||||
$services->getContentLanguage(),
|
||||
$services->getNamespaceInfo(),
|
||||
$services->getTitleFactory()
|
||||
)
|
||||
);
|
||||
return true;
|
||||
} elseif (
|
||||
$namespace >= 0 &&
|
||||
MediaWikiServices::getInstance()->getNamespaceInfo()->exists( intval( $namespace ) )
|
||||
$services->getNamespaceInfo()->exists( intval( $namespace ) )
|
||||
) {
|
||||
$namespace = intval( $namespace );
|
||||
$this->setImportTitleFactory( new NamespaceImportTitleFactory( $namespace ) );
|
||||
$this->setImportTitleFactory(
|
||||
new NamespaceImportTitleFactory(
|
||||
$services->getNamespaceInfo(),
|
||||
$services->getTitleFactory(),
|
||||
$namespace
|
||||
)
|
||||
);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
@ -308,28 +327,38 @@ class WikiImporter {
|
|||
*/
|
||||
public function setTargetRootPage( $rootpage ) {
|
||||
$status = Status::newGood();
|
||||
$services = MediaWikiServices::getInstance();
|
||||
$nsInfo = $services->getNamespaceInfo();
|
||||
if ( $rootpage === null ) {
|
||||
// No rootpage
|
||||
$this->setImportTitleFactory( new NaiveImportTitleFactory() );
|
||||
$this->setImportTitleFactory(
|
||||
new NaiveImportTitleFactory(
|
||||
$services->getContentLanguage(),
|
||||
$nsInfo,
|
||||
$services->getTitleFactory()
|
||||
)
|
||||
);
|
||||
} elseif ( $rootpage !== '' ) {
|
||||
$rootpage = rtrim( $rootpage, '/' ); // avoid double slashes
|
||||
$title = Title::newFromText( $rootpage );
|
||||
|
||||
if ( !$title || $title->isExternal() ) {
|
||||
$status->fatal( 'import-rootpage-invalid' );
|
||||
} elseif (
|
||||
!MediaWikiServices::getInstance()->getNamespaceInfo()->
|
||||
hasSubpages( $title->getNamespace() )
|
||||
) {
|
||||
} elseif ( !$nsInfo->hasSubpages( $title->getNamespace() ) ) {
|
||||
$displayNSText = $title->getNamespace() === NS_MAIN
|
||||
? wfMessage( 'blanknamespace' )->text()
|
||||
: MediaWikiServices::getInstance()->getContentLanguage()->
|
||||
getNsText( $title->getNamespace() );
|
||||
: $services->getContentLanguage()->getNsText( $title->getNamespace() );
|
||||
$status->fatal( 'import-rootpage-nosubpage', $displayNSText );
|
||||
} else {
|
||||
// set namespace to 'all', so the namespace check in processTitle() can pass
|
||||
$this->setTargetNamespace( null );
|
||||
$this->setImportTitleFactory( new SubpageImportTitleFactory( $title ) );
|
||||
$this->setImportTitleFactory(
|
||||
new SubpageImportTitleFactory(
|
||||
$nsInfo,
|
||||
$services->getTitleFactory(),
|
||||
$title
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
return $status;
|
||||
|
|
|
|||
|
|
@ -18,8 +18,6 @@
|
|||
* @file
|
||||
*/
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
/**
|
||||
* A class to convert page titles on a foreign wiki (ForeignTitle objects) into
|
||||
* page titles on the local wiki (Title objects), using a default namespace
|
||||
|
|
@ -32,6 +30,30 @@ use MediaWiki\MediaWikiServices;
|
|||
* main namespace as a last resort.
|
||||
*/
|
||||
class NaiveImportTitleFactory implements ImportTitleFactory {
|
||||
/** @var Language */
|
||||
private $contentLanguage;
|
||||
|
||||
/** @var NamespaceInfo */
|
||||
private $namespaceInfo;
|
||||
|
||||
/** @var TitleFactory */
|
||||
private $titleFactory;
|
||||
|
||||
/**
|
||||
* @param Language $contentLanguage
|
||||
* @param NamespaceInfo $namespaceInfo
|
||||
* @param TitleFactory $titleFactory
|
||||
*/
|
||||
public function __construct(
|
||||
Language $contentLanguage,
|
||||
NamespaceInfo $namespaceInfo,
|
||||
TitleFactory $titleFactory
|
||||
) {
|
||||
$this->contentLanguage = $contentLanguage;
|
||||
$this->namespaceInfo = $namespaceInfo;
|
||||
$this->titleFactory = $titleFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines which local title best corresponds to the given foreign title.
|
||||
* If such a title can't be found or would be locally invalid, null is
|
||||
|
|
@ -48,21 +70,20 @@ class NaiveImportTitleFactory implements ImportTitleFactory {
|
|||
// the same namespace ID
|
||||
if (
|
||||
$foreignNs < 100 &&
|
||||
MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $foreignNs )
|
||||
$this->namespaceInfo->exists( $foreignNs )
|
||||
) {
|
||||
return Title::makeTitleSafe( $foreignNs, $foreignTitle->getText() );
|
||||
return $this->titleFactory->makeTitleSafe( $foreignNs, $foreignTitle->getText() );
|
||||
}
|
||||
}
|
||||
|
||||
// Do we have a local namespace by the same name as the foreign
|
||||
// namespace?
|
||||
$targetNs = MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex(
|
||||
$foreignTitle->getNamespaceName() );
|
||||
$targetNs = $this->contentLanguage->getNsIndex( $foreignTitle->getNamespaceName() );
|
||||
if ( $targetNs !== false ) {
|
||||
return Title::makeTitleSafe( $targetNs, $foreignTitle->getText() );
|
||||
return $this->titleFactory->makeTitleSafe( $targetNs, $foreignTitle->getText() );
|
||||
}
|
||||
|
||||
// Otherwise, just fall back to main namespace
|
||||
return Title::makeTitleSafe( 0, $foreignTitle->getFullText() );
|
||||
return $this->titleFactory->makeTitleSafe( 0, $foreignTitle->getFullText() );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,24 +18,32 @@
|
|||
* @file
|
||||
*/
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
/**
|
||||
* A class to convert page titles on a foreign wiki (ForeignTitle objects) into
|
||||
* page titles on the local wiki (Title objects), placing all pages in a fixed
|
||||
* local namespace.
|
||||
*/
|
||||
class NamespaceImportTitleFactory implements ImportTitleFactory {
|
||||
/** @var TitleFactory */
|
||||
private $titleFactory;
|
||||
|
||||
/** @var int */
|
||||
protected $ns;
|
||||
private $ns;
|
||||
|
||||
/**
|
||||
* @param NamespaceInfo $namespaceInfo
|
||||
* @param TitleFactory $titleFactory
|
||||
* @param int $ns The namespace to use for all pages
|
||||
*/
|
||||
public function __construct( $ns ) {
|
||||
if ( !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $ns ) ) {
|
||||
public function __construct(
|
||||
NamespaceInfo $namespaceInfo,
|
||||
TitleFactory $titleFactory,
|
||||
int $ns
|
||||
) {
|
||||
if ( !$namespaceInfo->exists( $ns ) ) {
|
||||
throw new MWException( "Namespace $ns doesn't exist on this wiki" );
|
||||
}
|
||||
$this->titleFactory = $titleFactory;
|
||||
$this->ns = $ns;
|
||||
}
|
||||
|
||||
|
|
@ -48,6 +56,6 @@ class NamespaceImportTitleFactory implements ImportTitleFactory {
|
|||
* @return Title|null
|
||||
*/
|
||||
public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
|
||||
return Title::makeTitleSafe( $this->ns, $foreignTitle->getText() );
|
||||
return $this->titleFactory->makeTitleSafe( $this->ns, $foreignTitle->getText() );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,29 +18,33 @@
|
|||
* @file
|
||||
*/
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
/**
|
||||
* A class to convert page titles on a foreign wiki (ForeignTitle objects) into
|
||||
* page titles on the local wiki (Title objects), placing all pages as subpages
|
||||
* of a given root page.
|
||||
*/
|
||||
class SubpageImportTitleFactory implements ImportTitleFactory {
|
||||
/** @var TitleFactory */
|
||||
private $titleFactory;
|
||||
|
||||
/** @var Title */
|
||||
protected $rootPage;
|
||||
private $rootPage;
|
||||
|
||||
/**
|
||||
* @param Title $rootPage The root page under which all pages should be
|
||||
* created
|
||||
* @param NamespaceInfo $namespaceInfo
|
||||
* @param TitleFactory $titleFactory
|
||||
* @param Title $rootPage The root page under which all pages should be created
|
||||
*/
|
||||
public function __construct( Title $rootPage ) {
|
||||
if (
|
||||
!MediaWikiServices::getInstance()->getNamespaceInfo()->
|
||||
hasSubpages( $rootPage->getNamespace() )
|
||||
) {
|
||||
public function __construct(
|
||||
NamespaceInfo $namespaceInfo,
|
||||
TitleFactory $titleFactory,
|
||||
Title $rootPage
|
||||
) {
|
||||
if ( !$namespaceInfo->hasSubpages( $rootPage->getNamespace() ) ) {
|
||||
throw new MWException( "The root page you specified, $rootPage, is in a " .
|
||||
"namespace where subpages are not allowed" );
|
||||
}
|
||||
$this->titleFactory = $titleFactory;
|
||||
$this->rootPage = $rootPage;
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +57,8 @@ class SubpageImportTitleFactory implements ImportTitleFactory {
|
|||
* @return Title|null
|
||||
*/
|
||||
public function createTitleFromForeignTitle( ForeignTitle $foreignTitle ) {
|
||||
return Title::newFromText( $this->rootPage->getPrefixedDBkey() . '/' .
|
||||
$foreignTitle->getFullText() );
|
||||
return $this->titleFactory->newFromText(
|
||||
$this->rootPage->getPrefixedDBkey() . '/' . $foreignTitle->getFullText()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@
|
|||
* @covers NaiveImportTitleFactory
|
||||
*
|
||||
* @group Title
|
||||
*
|
||||
* TODO convert to unit tests
|
||||
*/
|
||||
class NaiveImportTitleFactoryTest extends MediaWikiIntegrationTestCase {
|
||||
|
||||
|
|
@ -80,7 +82,11 @@ class NaiveImportTitleFactoryTest extends MediaWikiIntegrationTestCase {
|
|||
* @dataProvider basicProvider
|
||||
*/
|
||||
public function testBasic( ForeignTitle $foreignTitle, $titleText ) {
|
||||
$factory = new NaiveImportTitleFactory();
|
||||
$factory = new NaiveImportTitleFactory(
|
||||
$this->getServiceContainer()->getContentLanguage(),
|
||||
$this->getServiceContainer()->getNamespaceInfo(),
|
||||
$this->getServiceContainer()->getTitleFactory()
|
||||
);
|
||||
$testTitle = $factory->createTitleFromForeignTitle( $foreignTitle );
|
||||
$title = Title::newFromText( $titleText );
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@
|
|||
* @covers NamespaceImportTitleFactory
|
||||
*
|
||||
* @group Title
|
||||
*
|
||||
* TODO convert to unit tests
|
||||
*/
|
||||
class NamespaceImportTitleFactoryTest extends MediaWikiIntegrationTestCase {
|
||||
|
||||
|
|
@ -66,7 +68,11 @@ class NamespaceImportTitleFactoryTest extends MediaWikiIntegrationTestCase {
|
|||
* @dataProvider basicProvider
|
||||
*/
|
||||
public function testBasic( ForeignTitle $foreignTitle, $ns, $titleText ) {
|
||||
$factory = new NamespaceImportTitleFactory( $ns );
|
||||
$factory = new NamespaceImportTitleFactory(
|
||||
$this->getServiceContainer()->getNamespaceInfo(),
|
||||
$this->getServiceContainer()->getTitleFactory(),
|
||||
$ns
|
||||
);
|
||||
$testTitle = $factory->createTitleFromForeignTitle( $foreignTitle );
|
||||
$title = Title::newFromText( $titleText );
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@
|
|||
* @covers SubpageImportTitleFactory
|
||||
*
|
||||
* @group Title
|
||||
*
|
||||
* TODO convert to Unit tests
|
||||
*/
|
||||
class SubpageImportTitleFactoryTest extends MediaWikiIntegrationTestCase {
|
||||
|
||||
|
|
@ -35,6 +37,14 @@ class SubpageImportTitleFactoryTest extends MediaWikiIntegrationTestCase {
|
|||
] );
|
||||
}
|
||||
|
||||
private function newSubpageImportTitleFactory( Title $rootPage ) {
|
||||
return new SubpageImportTitleFactory(
|
||||
$this->getServiceContainer()->getNamespaceInfo(),
|
||||
$this->getServiceContainer()->getTitleFactory(),
|
||||
$rootPage
|
||||
);
|
||||
}
|
||||
|
||||
public function basicProvider() {
|
||||
return [
|
||||
[
|
||||
|
|
@ -61,7 +71,7 @@ class SubpageImportTitleFactoryTest extends MediaWikiIntegrationTestCase {
|
|||
public function testBasic( ForeignTitle $foreignTitle, Title $rootPage,
|
||||
Title $title
|
||||
) {
|
||||
$factory = new SubpageImportTitleFactory( $rootPage );
|
||||
$factory = $this->newSubpageImportTitleFactory( $rootPage );
|
||||
$testTitle = $factory->createTitleFromForeignTitle( $foreignTitle );
|
||||
|
||||
$this->assertTrue( $testTitle->equals( $title ) );
|
||||
|
|
@ -80,6 +90,6 @@ class SubpageImportTitleFactoryTest extends MediaWikiIntegrationTestCase {
|
|||
*/
|
||||
public function testFailures( Title $rootPage ) {
|
||||
$this->expectException( MWException::class );
|
||||
new SubpageImportTitleFactory( $rootPage );
|
||||
$this->newSubpageImportTitleFactory( $rootPage );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue