Merge "WikiExporter: inject services"

This commit is contained in:
jenkins-bot 2021-09-16 17:04:04 +00:00 committed by Gerrit Code Review
commit 76ba780e41
12 changed files with 182 additions and 18 deletions

View file

@ -855,6 +855,7 @@ $wgAutoloadLocalClasses = [
'MediaWiki\\DAO\\WikiAwareEntity' => __DIR__ . '/includes/dao/WikiAwareEntity.php',
'MediaWiki\\DAO\\WikiAwareEntityTrait' => __DIR__ . '/includes/dao/WikiAwareEntityTrait.php',
'MediaWiki\\Debug\\DeprecatablePropertyArray' => __DIR__ . '/includes/debug/DeprecatablePropertyArray.php',
'MediaWiki\\Export\\WikiExporterFactory' => __DIR__ . '/includes/export/WikiExporterFactory.php',
'MediaWiki\\FileBackend\\FSFile\\TempFSFileFactory' => __DIR__ . '/includes/libs/filebackend/fsfile/TempFSFileFactory.php',
'MediaWiki\\Hook\\AbortEmailNotificationHook' => __DIR__ . '/includes/changes/Hook/AbortEmailNotificationHook.php',
'MediaWiki\\Hook\\AbortTalkPageEmailNotificationHook' => __DIR__ . '/includes/mail/Hook/AbortTalkPageEmailNotificationHook.php',

View file

@ -45,6 +45,7 @@ use MediaWiki\Config\ConfigRepository;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Content\Transform\ContentTransformer;
use MediaWiki\EditPage\SpamChecker;
use MediaWiki\Export\WikiExporterFactory;
use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
use MediaWiki\FileBackend\LockManager\LockManagerGroupFactory;
use MediaWiki\HookContainer\HookContainer;
@ -1746,6 +1747,14 @@ class MediaWikiServices extends ServiceContainer {
return $this->getService( 'WatchlistManager' );
}
/**
* @since 1.37
* @return WikiExporterFactory
*/
public function getWikiExporterFactory(): WikiExporterFactory {
return $this->getService( 'WikiExporterFactory' );
}
/**
* @since 1.37
* @return WikiImporterFactory

View file

@ -68,6 +68,7 @@ use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Content\Transform\ContentTransformer;
use MediaWiki\EditPage\Constraint\EditConstraintFactory;
use MediaWiki\EditPage\SpamChecker;
use MediaWiki\Export\WikiExporterFactory;
use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
use MediaWiki\FileBackend\LockManager\LockManagerGroupFactory;
use MediaWiki\HookContainer\DeprecatedHooks;
@ -1805,6 +1806,14 @@ return [
);
},
'WikiExporterFactory' => static function ( MediaWikiServices $services ): WikiExporterFactory {
return new WikiExporterFactory(
$services->getHookContainer(),
$services->getRevisionStore(),
$services->getTitleParser()
);
},
'WikiImporterFactory' => static function ( MediaWikiServices $services ): WikiImporterFactory {
return new WikiImporterFactory(
$services->getMainConfig(),

View file

@ -789,7 +789,9 @@ class ApiQuery extends ApiBase {
}
}
$exporter = new WikiExporter( $this->getDB() );
$exporter = MediaWikiServices::getInstance()
->getWikiExporterFactory()
->getWikiExporter( $this->getDB() );
$sink = new DumpStringOutput;
$exporter->setOutputSink( $sink );
$exporter->setSchemaVersion( $this->mParams['exportschema'] );

View file

@ -27,8 +27,8 @@
* @defgroup Dump Dump
*/
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Revision\RevisionAccessException;
use MediaWiki\Revision\RevisionRecord;
@ -101,6 +101,9 @@ class WikiExporter {
/**
* @param IDatabase $db
* @param HookContainer $hookContainer
* @param RevisionStore $revisionStore
* @param TitleParser $titleParser
* @param int|array $history One of WikiExporter::FULL, WikiExporter::CURRENT,
* WikiExporter::RANGE or WikiExporter::STABLE, or an associative array:
* - offset: non-inclusive offset at which to start the query
@ -112,20 +115,24 @@ class WikiExporter {
*/
public function __construct(
$db,
HookContainer $hookContainer,
RevisionStore $revisionStore,
TitleParser $titleParser,
$history = self::CURRENT,
$text = self::TEXT,
$limitNamespaces = null
) {
$this->db = $db;
$this->history = $history;
// TODO: add a $hookContainer parameter to XmlDumpWriter so that we can inject
// and then be able to convert the factory test to a unit test
$this->writer = new XmlDumpWriter( $text, self::schemaVersion() );
$this->sink = new DumpOutput();
$this->text = $text;
$this->limitNamespaces = $limitNamespaces;
$services = MediaWikiServices::getInstance();
$this->hookRunner = new HookRunner( $services->getHookContainer() );
$this->revisionStore = $services->getRevisionStore();
$this->titleParser = $services->getTitleParser();
$this->hookRunner = new HookRunner( $hookContainer );
$this->revisionStore = $revisionStore;
$this->titleParser = $titleParser;
}
/**

View file

@ -0,0 +1,84 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Export;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Revision\RevisionStore;
use TitleParser;
use WikiExporter;
use Wikimedia\Rdbms\IDatabase;
/**
* Factory service for WikiExporter instances.
*
* @author Zabe
* @since 1.37
*/
class WikiExporterFactory {
/** @var HookContainer */
private $hookContainer;
/** @var RevisionStore */
private $revisionStore;
/** @var TitleParser */
private $titleParser;
/**
* @param HookContainer $hookContainer
* @param RevisionStore $revisionStore
* @param TitleParser $titleParser
*/
public function __construct(
HookContainer $hookContainer,
RevisionStore $revisionStore,
TitleParser $titleParser
) {
$this->hookContainer = $hookContainer;
$this->revisionStore = $revisionStore;
$this->titleParser = $titleParser;
}
/**
* @param IDatabase $db
* @param int|array $history
* @param int $text
* @param null|array $limitNamespaces
*
* @return WikiExporter
*/
public function getWikiExporter(
IDatabase $db,
$history = WikiExporter::CURRENT,
$text = WikiExporter::TEXT,
$limitNamespaces = null
): WikiExporter {
return new WikiExporter(
$db,
$this->hookContainer,
$this->revisionStore,
$this->titleParser,
$history,
$text,
$limitNamespaces
);
}
}

View file

@ -756,6 +756,7 @@ class SpecialPageFactory {
'class' => \SpecialExport::class,
'services' => [
'DBLoadBalancer',
'WikiExporterFactory',
]
],
'Import' => [

View file

@ -23,6 +23,7 @@
* @ingroup SpecialPage
*/
use MediaWiki\Export\WikiExporterFactory;
use MediaWiki\Logger\LoggerFactory;
use Wikimedia\Rdbms\ILoadBalancer;
@ -37,14 +38,20 @@ class SpecialExport extends SpecialPage {
/** @var ILoadBalancer */
private $loadBalancer;
/** @var WikiExporterFactory */
private $wikiExporterFactory;
/**
* @param ILoadBalancer $loadBalancer
* @param WikiExporterFactory $wikiExporterFactory
*/
public function __construct(
ILoadBalancer $loadBalancer
ILoadBalancer $loadBalancer,
WikiExporterFactory $wikiExporterFactory
) {
parent::__construct( 'Export' );
$this->loadBalancer = $loadBalancer;
$this->wikiExporterFactory = $wikiExporterFactory;
}
public function execute( $par ) {
@ -390,7 +397,7 @@ class SpecialExport extends SpecialPage {
/* Ok, let's get to it... */
$db = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
$exporter = new WikiExporter( $db, $history );
$exporter = $this->wikiExporterFactory->getWikiExporter( $db, $history );
$exporter->list_authors = $list_authors;
$exporter->openStream();

View file

@ -292,7 +292,13 @@ abstract class BackupDumper extends Maintenance {
$this->initProgress( $history );
$db = $this->backupDb();
$exporter = new WikiExporter( $db, $history, $text, $this->limitNamespaces );
$services = MediaWikiServices::getInstance();
$exporter = $services->getWikiExporterFactory()->getWikiExporter(
$db,
$history,
$text,
$this->limitNamespaces
);
$exporter->setSchemaVersion( $this->schemaVersion );
$exporter->dumpUploads = $this->dumpUploads;
$exporter->dumpUploadFileContents = $this->dumpUploadFileContents;

View file

@ -1,7 +1,5 @@
<?php
use MediaWiki\MediaWikiServices;
/**
* Test class for Export methods.
*
@ -24,10 +22,10 @@ class ExportTest extends MediaWikiLangTestCase {
public function testPageByTitle() {
$pageTitle = 'UTPage';
$exporter = new WikiExporter(
$this->db,
WikiExporter::FULL
);
$services = $this->getServiceContainer();
$exporter = $services
->getWikiExporterFactory()
->getWikiExporter( $this->db, WikiExporter::FULL );
$title = Title::newFromText( $pageTitle );
@ -53,8 +51,7 @@ class ExportTest extends MediaWikiLangTestCase {
}
$xmlNamespaces = str_replace( ' ', '_', $xmlNamespaces );
$actualNamespaces = (array)MediaWikiServices::getInstance()->getContentLanguage()->
getNamespaces();
$actualNamespaces = (array)$services->getContentLanguage()->getNamespaces();
$actualNamespaces = array_values( $actualNamespaces );
$this->assertEquals( $actualNamespaces, $xmlNamespaces );

View file

@ -29,7 +29,9 @@ class ImportExportTest extends MediaWikiLangTestCase {
* @return WikiExporter
*/
private function getExporter( string $schemaVersion ) {
$exporter = new WikiExporter( $this->db, WikiExporter::FULL );
$exporter = $this->getServiceContainer()
->getWikiExporterFactory()
->getWikiExporter( $this->db, WikiExporter::FULL );
$exporter->setSchemaVersion( $schemaVersion );
return $exporter;
}

View file

@ -0,0 +1,39 @@
<?php
namespace MediaWiki\Tests\Export;
use FactoryArgTestTrait;
use MediaWiki\Export\WikiExporterFactory;
use MediaWikiIntegrationTestCase;
use WikiExporter;
use XmlDumpWriter;
/**
* @covers MediaWiki\Export\WikiExporterFactory
*/
class WikiExporterFactoryTest extends MediaWikiIntegrationTestCase {
use FactoryArgTestTrait;
protected function setUp(): void {
parent::setUp();
$this->setMwGlobals( [
'XmlDumpSchemaVersion' => XmlDumpWriter::$supportedSchemas[0],
] );
}
protected static function getFactoryClass() {
return WikiExporterFactory::class;
}
protected static function getInstanceClass() {
return WikiExporter::class;
}
protected static function getExtraClassArgCount() {
return 4;
}
protected function getFactoryMethodName() {
return 'getWikiExporter';
}
}