wiki.techinc.nl/tests/phpunit/includes/import/ImportTest.php

334 lines
9.5 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\MediaWikiServices;
/**
* Test class for Import methods.
*
* @group Database
*
* @author Sebastian Brückner < sebastian.brueckner@student.hpi.uni-potsdam.de >
*/
class ImportTest extends MediaWikiLangTestCase {
/**
* @covers WikiImporter
* @dataProvider getUnknownTagsXML
* @param string $xml
* @param string $text
* @param string $title
*/
public function testUnknownXMLTags( $xml, $text, $title ) {
$source = new ImportStringSource( $xml );
$services = MediaWikiServices::getInstance();
$importer = new WikiImporter(
$source,
$services->getMainConfig()
);
$importer->doImport();
$title = Title::newFromText( $title );
$this->assertTrue( $title->exists() );
$this->assertEquals(
$services->getWikiPageFactory()->newFromTitle( $title )->getContent()->getText(),
$text
);
}
public function getUnknownTagsXML() {
// phpcs:disable Generic.Files.LineLength
return [
[
<<< EOF
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
<page unknown="123" dontknow="533">
<title>TestImportPage</title>
<unknowntag>Should be ignored</unknowntag>
<ns>0</ns>
<id unknown="123" dontknow="533">14</id>
<revision>
<id unknown="123" dontknow="533">15</id>
<unknowntag>Should be ignored</unknowntag>
<timestamp>2016-01-03T11:18:43Z</timestamp>
<contributor>
<unknowntag>Should be ignored</unknowntag>
<username unknown="123" dontknow="533">Admin</username>
<id>1</id>
</contributor>
<model>wikitext</model>
<format>text/x-wiki</format>
<text xml:space="preserve" bytes="0">noitazinagro tseb eht si ikiWaideM</text>
<sha1>phoiac9h4m842xq45sp7s6u21eteeq1</sha1>
<unknowntag>Should be ignored</unknowntag>
</revision>
</page>
<unknowntag>Should be ignored</unknowntag>
</mediawiki>
EOF
,
'noitazinagro tseb eht si ikiWaideM',
'TestImportPage'
]
];
// phpcs:enable
}
/**
* @covers WikiImporter::handlePage
* @dataProvider getRedirectXML
* @param string $xml
* @param string|null $redirectTitle
*/
public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
$source = new ImportStringSource( $xml );
$redirect = null;
$callback = static function ( Title $title, ForeignTitle $foreignTitle, $revCount,
$sRevCount, $pageInfo ) use ( &$redirect ) {
if ( array_key_exists( 'redirect', $pageInfo ) ) {
$redirect = $pageInfo['redirect'];
}
};
$importer = new WikiImporter(
$source,
MediaWikiServices::getInstance()->getMainConfig()
);
$importer->setPageOutCallback( $callback );
$importer->doImport();
$this->assertEquals( $redirectTitle, $redirect );
}
public function getRedirectXML() {
// phpcs:disable Generic.Files.LineLength
return [
[
<<< EOF
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
<page>
<title>Test</title>
<ns>0</ns>
<id>21</id>
<redirect title="Test22"/>
<revision>
<id>20</id>
<timestamp>2014-05-27T10:00:00Z</timestamp>
<contributor>
<username>Admin</username>
<id>10</id>
</contributor>
<comment>Admin moved page [[Test]] to [[Test22]]</comment>
<model>wikitext</model>
<format>text/x-wiki</format>
<text xml:space="preserve" bytes="20">#REDIRECT [[Test22]]</text>
<sha1>tq456o9x3abm7r9ozi6km8yrbbc56o6</sha1>
</revision>
</page>
</mediawiki>
EOF
,
'Test22'
],
[
<<< EOF
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.9/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.9/ http://www.mediawiki.org/xml/export-0.9.xsd" version="0.9" xml:lang="en">
<page>
<title>Test</title>
<ns>0</ns>
<id>42</id>
<revision>
<id>421</id>
<timestamp>2014-05-27T11:00:00Z</timestamp>
<contributor>
<username>Admin</username>
<id>10</id>
</contributor>
<text xml:space="preserve" bytes="4">Abcd</text>
<sha1>n7uomjq96szt60fy5w3x7ahf7q8m8rh</sha1>
<model>wikitext</model>
<format>text/x-wiki</format>
</revision>
</page>
</mediawiki>
EOF
,
null
],
];
// phpcs:enable
}
/**
* @covers WikiImporter::handleSiteInfo
* @dataProvider getSiteInfoXML
* @param string $xml
* @param array|null $namespaces
*/
public function testSiteInfoContainsNamespaces( $xml, $namespaces ) {
$source = new ImportStringSource( $xml );
$importNamespaces = null;
$callback = static function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) {
$importNamespaces = $siteinfo['_namespaces'];
};
$importer = new WikiImporter(
$source,
MediaWikiServices::getInstance()->getMainConfig()
);
$importer->setSiteInfoCallback( $callback );
$importer->doImport();
$this->assertEquals( $importNamespaces, $namespaces );
}
public function getSiteInfoXML() {
// phpcs:disable Generic.Files.LineLength
return [
[
<<< EOF
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
<siteinfo>
<namespaces>
<namespace key="-2" case="first-letter">Media</namespace>
<namespace key="-1" case="first-letter">Special</namespace>
<namespace key="0" case="first-letter" />
<namespace key="1" case="first-letter">Talk</namespace>
<namespace key="2" case="first-letter">User</namespace>
<namespace key="3" case="first-letter">User talk</namespace>
<namespace key="100" case="first-letter">Portal</namespace>
<namespace key="101" case="first-letter">Portal talk</namespace>
</namespaces>
</siteinfo>
</mediawiki>
EOF
,
[
'-2' => 'Media',
'-1' => 'Special',
'0' => '',
'1' => 'Talk',
'2' => 'User',
'3' => 'User talk',
'100' => 'Portal',
'101' => 'Portal talk',
]
],
];
// phpcs:enable
}
Avoid DB rows with usable names but ID = 0 by introducing "interwiki" usernames Importing revisions in MediaWiki has long been weird: if the username on the imported revision exists locally it's automatically attributed to the local user, while if the name does not exist locally we wind up with revision table rows with rev_user = 0 and rev_user_text being a valid name that someone might later create. "Global" blocks too create rows with ipb_by = 0 an ipb_by_text being a valid name. The upcoming actor table change, as things currently stand, would regularize that a bit by automatically attributing those imported revisions to the newly-created user. But that's not necessarily what we actually want to happen. And it would certainly confuse CentralAuth's attempt to detect its own global blocks. Thus, this patch introduces "interwiki" usernames that aren't valid for local use, of the format "iw>Example".[1] Linker will interpret these names and generate an appropriate interwiki link in history pages and the like, as if from wikitext like `[[iw:User:Example]]`. Imports for non-existant local users (and optionally for existing local users too) will credit the edit to such an interwiki name. There is also a new hook, 'ImportHandleUnknownUser', to allow extension such as CentralAuth to create local users as their edits are imported. Block will no longer accept usable-but-nonexistent names for 'byText' or ->setBlocker(). CentralAuth's global blocks will be submitted with an interwiki username (see Ieae5d24f9). Wikis that have imported edits or CentralAuth global blocks should run the new maintenance/cleanupUsersWithNoId.php maintenance script. This isn't done by update.php because (1) it needs an interwiki prefix to use and (2) the updater can't know whether to pass the `--assign` flag. [1]: '>' was used instead of the more usual ':' because WMF wikis have many existing usernames containing colons. Bug: T9240 Bug: T20209 Bug: T111605 Change-Id: I5401941c06102e8faa813910519d55482dff36cb Depends-On: Ieae5d24f9098c1977447c50a8d4e2cab58a24d9f
2017-10-25 19:26:53 +00:00
/**
* @dataProvider provideUnknownUserHandling
* @covers WikiImporter::setUsernamePrefix
* @covers ExternalUserNames::addPrefix
* @covers ExternalUserNames::applyPrefix
Avoid DB rows with usable names but ID = 0 by introducing "interwiki" usernames Importing revisions in MediaWiki has long been weird: if the username on the imported revision exists locally it's automatically attributed to the local user, while if the name does not exist locally we wind up with revision table rows with rev_user = 0 and rev_user_text being a valid name that someone might later create. "Global" blocks too create rows with ipb_by = 0 an ipb_by_text being a valid name. The upcoming actor table change, as things currently stand, would regularize that a bit by automatically attributing those imported revisions to the newly-created user. But that's not necessarily what we actually want to happen. And it would certainly confuse CentralAuth's attempt to detect its own global blocks. Thus, this patch introduces "interwiki" usernames that aren't valid for local use, of the format "iw>Example".[1] Linker will interpret these names and generate an appropriate interwiki link in history pages and the like, as if from wikitext like `[[iw:User:Example]]`. Imports for non-existant local users (and optionally for existing local users too) will credit the edit to such an interwiki name. There is also a new hook, 'ImportHandleUnknownUser', to allow extension such as CentralAuth to create local users as their edits are imported. Block will no longer accept usable-but-nonexistent names for 'byText' or ->setBlocker(). CentralAuth's global blocks will be submitted with an interwiki username (see Ieae5d24f9). Wikis that have imported edits or CentralAuth global blocks should run the new maintenance/cleanupUsersWithNoId.php maintenance script. This isn't done by update.php because (1) it needs an interwiki prefix to use and (2) the updater can't know whether to pass the `--assign` flag. [1]: '>' was used instead of the more usual ':' because WMF wikis have many existing usernames containing colons. Bug: T9240 Bug: T20209 Bug: T111605 Change-Id: I5401941c06102e8faa813910519d55482dff36cb Depends-On: Ieae5d24f9098c1977447c50a8d4e2cab58a24d9f
2017-10-25 19:26:53 +00:00
* @param bool $assign
* @param bool $create
*/
public function testUnknownUserHandling( $assign, $create ) {
$hookId = -99;
$this->setMwGlobals( 'wgHooks', [
'ImportHandleUnknownUser' => [ function ( $name ) use ( $assign, $create, &$hookId ) {
if ( !$assign ) {
$this->fail( 'ImportHandleUnknownUser was called unexpectedly' );
}
$this->assertEquals( 'UserDoesNotExist', $name );
if ( $create ) {
$user = User::createNew( $name );
$this->assertNotNull( $user );
$hookId = $user->getId();
return false;
}
return true;
} ]
] );
$user = $this->getTestUser()->getUser();
$n = ( $assign ? 1 : 0 ) + ( $create ? 2 : 0 );
// phpcs:disable Generic.Files.LineLength
$source = new ImportStringSource( <<<EOF
Avoid DB rows with usable names but ID = 0 by introducing "interwiki" usernames Importing revisions in MediaWiki has long been weird: if the username on the imported revision exists locally it's automatically attributed to the local user, while if the name does not exist locally we wind up with revision table rows with rev_user = 0 and rev_user_text being a valid name that someone might later create. "Global" blocks too create rows with ipb_by = 0 an ipb_by_text being a valid name. The upcoming actor table change, as things currently stand, would regularize that a bit by automatically attributing those imported revisions to the newly-created user. But that's not necessarily what we actually want to happen. And it would certainly confuse CentralAuth's attempt to detect its own global blocks. Thus, this patch introduces "interwiki" usernames that aren't valid for local use, of the format "iw>Example".[1] Linker will interpret these names and generate an appropriate interwiki link in history pages and the like, as if from wikitext like `[[iw:User:Example]]`. Imports for non-existant local users (and optionally for existing local users too) will credit the edit to such an interwiki name. There is also a new hook, 'ImportHandleUnknownUser', to allow extension such as CentralAuth to create local users as their edits are imported. Block will no longer accept usable-but-nonexistent names for 'byText' or ->setBlocker(). CentralAuth's global blocks will be submitted with an interwiki username (see Ieae5d24f9). Wikis that have imported edits or CentralAuth global blocks should run the new maintenance/cleanupUsersWithNoId.php maintenance script. This isn't done by update.php because (1) it needs an interwiki prefix to use and (2) the updater can't know whether to pass the `--assign` flag. [1]: '>' was used instead of the more usual ':' because WMF wikis have many existing usernames containing colons. Bug: T9240 Bug: T20209 Bug: T111605 Change-Id: I5401941c06102e8faa813910519d55482dff36cb Depends-On: Ieae5d24f9098c1977447c50a8d4e2cab58a24d9f
2017-10-25 19:26:53 +00:00
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
<page>
<title>TestImportPage</title>
<ns>0</ns>
<id>14</id>
<revision>
<id>15</id>
<timestamp>2016-01-01T0$n:00:00Z</timestamp>
<contributor>
<username>UserDoesNotExist</username>
<id>1</id>
</contributor>
<model>wikitext</model>
<format>text/x-wiki</format>
<text xml:space="preserve" bytes="3">foo</text>
<sha1>1e6gpc3ehk0mu2jqu8cg42g009s796b</sha1>
</revision>
<revision>
<id>16</id>
<timestamp>2016-01-01T0$n:00:01Z</timestamp>
<contributor>
<username>{$user->getName()}</username>
<id>{$user->getId()}</id>
</contributor>
<model>wikitext</model>
<format>text/x-wiki</format>
<text xml:space="preserve" bytes="3">bar</text>
<sha1>bjhlo6dxh5wivnszm93u4b78fheiy4t</sha1>
</revision>
</page>
</mediawiki>
EOF
);
// phpcs:enable
Avoid DB rows with usable names but ID = 0 by introducing "interwiki" usernames Importing revisions in MediaWiki has long been weird: if the username on the imported revision exists locally it's automatically attributed to the local user, while if the name does not exist locally we wind up with revision table rows with rev_user = 0 and rev_user_text being a valid name that someone might later create. "Global" blocks too create rows with ipb_by = 0 an ipb_by_text being a valid name. The upcoming actor table change, as things currently stand, would regularize that a bit by automatically attributing those imported revisions to the newly-created user. But that's not necessarily what we actually want to happen. And it would certainly confuse CentralAuth's attempt to detect its own global blocks. Thus, this patch introduces "interwiki" usernames that aren't valid for local use, of the format "iw>Example".[1] Linker will interpret these names and generate an appropriate interwiki link in history pages and the like, as if from wikitext like `[[iw:User:Example]]`. Imports for non-existant local users (and optionally for existing local users too) will credit the edit to such an interwiki name. There is also a new hook, 'ImportHandleUnknownUser', to allow extension such as CentralAuth to create local users as their edits are imported. Block will no longer accept usable-but-nonexistent names for 'byText' or ->setBlocker(). CentralAuth's global blocks will be submitted with an interwiki username (see Ieae5d24f9). Wikis that have imported edits or CentralAuth global blocks should run the new maintenance/cleanupUsersWithNoId.php maintenance script. This isn't done by update.php because (1) it needs an interwiki prefix to use and (2) the updater can't know whether to pass the `--assign` flag. [1]: '>' was used instead of the more usual ':' because WMF wikis have many existing usernames containing colons. Bug: T9240 Bug: T20209 Bug: T111605 Change-Id: I5401941c06102e8faa813910519d55482dff36cb Depends-On: Ieae5d24f9098c1977447c50a8d4e2cab58a24d9f
2017-10-25 19:26:53 +00:00
$importer = new WikiImporter( $source, MediaWikiServices::getInstance()->getMainConfig() );
$importer->setUsernamePrefix( 'Xxx', $assign );
$importer->doImport();
$db = wfGetDB( DB_PRIMARY );
$revQuery = MediaWikiServices::getInstance()->getRevisionStore()->getQueryInfo();
Avoid DB rows with usable names but ID = 0 by introducing "interwiki" usernames Importing revisions in MediaWiki has long been weird: if the username on the imported revision exists locally it's automatically attributed to the local user, while if the name does not exist locally we wind up with revision table rows with rev_user = 0 and rev_user_text being a valid name that someone might later create. "Global" blocks too create rows with ipb_by = 0 an ipb_by_text being a valid name. The upcoming actor table change, as things currently stand, would regularize that a bit by automatically attributing those imported revisions to the newly-created user. But that's not necessarily what we actually want to happen. And it would certainly confuse CentralAuth's attempt to detect its own global blocks. Thus, this patch introduces "interwiki" usernames that aren't valid for local use, of the format "iw>Example".[1] Linker will interpret these names and generate an appropriate interwiki link in history pages and the like, as if from wikitext like `[[iw:User:Example]]`. Imports for non-existant local users (and optionally for existing local users too) will credit the edit to such an interwiki name. There is also a new hook, 'ImportHandleUnknownUser', to allow extension such as CentralAuth to create local users as their edits are imported. Block will no longer accept usable-but-nonexistent names for 'byText' or ->setBlocker(). CentralAuth's global blocks will be submitted with an interwiki username (see Ieae5d24f9). Wikis that have imported edits or CentralAuth global blocks should run the new maintenance/cleanupUsersWithNoId.php maintenance script. This isn't done by update.php because (1) it needs an interwiki prefix to use and (2) the updater can't know whether to pass the `--assign` flag. [1]: '>' was used instead of the more usual ':' because WMF wikis have many existing usernames containing colons. Bug: T9240 Bug: T20209 Bug: T111605 Change-Id: I5401941c06102e8faa813910519d55482dff36cb Depends-On: Ieae5d24f9098c1977447c50a8d4e2cab58a24d9f
2017-10-25 19:26:53 +00:00
$row = $db->selectRow(
$revQuery['tables'],
$revQuery['fields'],
[ 'rev_timestamp' => $db->timestamp( "201601010{$n}0000" ) ],
__METHOD__,
[],
$revQuery['joins']
Avoid DB rows with usable names but ID = 0 by introducing "interwiki" usernames Importing revisions in MediaWiki has long been weird: if the username on the imported revision exists locally it's automatically attributed to the local user, while if the name does not exist locally we wind up with revision table rows with rev_user = 0 and rev_user_text being a valid name that someone might later create. "Global" blocks too create rows with ipb_by = 0 an ipb_by_text being a valid name. The upcoming actor table change, as things currently stand, would regularize that a bit by automatically attributing those imported revisions to the newly-created user. But that's not necessarily what we actually want to happen. And it would certainly confuse CentralAuth's attempt to detect its own global blocks. Thus, this patch introduces "interwiki" usernames that aren't valid for local use, of the format "iw>Example".[1] Linker will interpret these names and generate an appropriate interwiki link in history pages and the like, as if from wikitext like `[[iw:User:Example]]`. Imports for non-existant local users (and optionally for existing local users too) will credit the edit to such an interwiki name. There is also a new hook, 'ImportHandleUnknownUser', to allow extension such as CentralAuth to create local users as their edits are imported. Block will no longer accept usable-but-nonexistent names for 'byText' or ->setBlocker(). CentralAuth's global blocks will be submitted with an interwiki username (see Ieae5d24f9). Wikis that have imported edits or CentralAuth global blocks should run the new maintenance/cleanupUsersWithNoId.php maintenance script. This isn't done by update.php because (1) it needs an interwiki prefix to use and (2) the updater can't know whether to pass the `--assign` flag. [1]: '>' was used instead of the more usual ':' because WMF wikis have many existing usernames containing colons. Bug: T9240 Bug: T20209 Bug: T111605 Change-Id: I5401941c06102e8faa813910519d55482dff36cb Depends-On: Ieae5d24f9098c1977447c50a8d4e2cab58a24d9f
2017-10-25 19:26:53 +00:00
);
$this->assertSame(
$assign && $create ? 'UserDoesNotExist' : 'Xxx>UserDoesNotExist',
$row->rev_user_text
);
$this->assertSame( $assign && $create ? $hookId : 0, (int)$row->rev_user );
$row = $db->selectRow(
$revQuery['tables'],
$revQuery['fields'],
[ 'rev_timestamp' => $db->timestamp( "201601010{$n}0001" ) ],
__METHOD__,
[],
$revQuery['joins']
Avoid DB rows with usable names but ID = 0 by introducing "interwiki" usernames Importing revisions in MediaWiki has long been weird: if the username on the imported revision exists locally it's automatically attributed to the local user, while if the name does not exist locally we wind up with revision table rows with rev_user = 0 and rev_user_text being a valid name that someone might later create. "Global" blocks too create rows with ipb_by = 0 an ipb_by_text being a valid name. The upcoming actor table change, as things currently stand, would regularize that a bit by automatically attributing those imported revisions to the newly-created user. But that's not necessarily what we actually want to happen. And it would certainly confuse CentralAuth's attempt to detect its own global blocks. Thus, this patch introduces "interwiki" usernames that aren't valid for local use, of the format "iw>Example".[1] Linker will interpret these names and generate an appropriate interwiki link in history pages and the like, as if from wikitext like `[[iw:User:Example]]`. Imports for non-existant local users (and optionally for existing local users too) will credit the edit to such an interwiki name. There is also a new hook, 'ImportHandleUnknownUser', to allow extension such as CentralAuth to create local users as their edits are imported. Block will no longer accept usable-but-nonexistent names for 'byText' or ->setBlocker(). CentralAuth's global blocks will be submitted with an interwiki username (see Ieae5d24f9). Wikis that have imported edits or CentralAuth global blocks should run the new maintenance/cleanupUsersWithNoId.php maintenance script. This isn't done by update.php because (1) it needs an interwiki prefix to use and (2) the updater can't know whether to pass the `--assign` flag. [1]: '>' was used instead of the more usual ':' because WMF wikis have many existing usernames containing colons. Bug: T9240 Bug: T20209 Bug: T111605 Change-Id: I5401941c06102e8faa813910519d55482dff36cb Depends-On: Ieae5d24f9098c1977447c50a8d4e2cab58a24d9f
2017-10-25 19:26:53 +00:00
);
$this->assertSame( ( $assign ? '' : 'Xxx>' ) . $user->getName(), $row->rev_user_text );
$this->assertSame( $assign ? $user->getId() : 0, (int)$row->rev_user );
}
public static function provideUnknownUserHandling() {
return [
'no assign' => [ false, false ],
'assign, no create' => [ true, false ],
'assign, create' => [ true, true ],
];
}
}