| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Permissions\UltimateAuthority; |
| 5 |
use MediaWiki\Title\ForeignTitle; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
use MediaWiki\User\User; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
> |
| 16 |
|
| 17 |
class ImportTest extends MediaWikiLangTestCase { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function testUnknownXMLTags( $xml, $text, $title ) { |
| 27 |
$source = new ImportStringSource( $xml ); |
| 28 |
$services = $this->getServiceContainer(); |
| 29 |
|
| 30 |
$importer = $services->getWikiImporterFactory()->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); |
| 31 |
|
| 32 |
$importer->doImport(); |
| 33 |
$title = Title::newFromText( $title ); |
| 34 |
$this->assertTrue( $title->exists() ); |
| 35 |
|
| 36 |
$this->assertEquals( |
| 37 |
$services->getWikiPageFactory()->newFromTitle( $title )->getContent()->getText(), |
| 38 |
$text |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
public function getUnknownTagsXML() { |
| 43 |
return [ |
| 44 |
[ |
| 45 |
<<< EOF |
| 46 |
<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"> |
| 47 |
<page unknown="123" dontknow="533"> |
| 48 |
<title>TestImportPage</title> |
| 49 |
<unknowntag>Should be ignored</unknowntag> |
| 50 |
<ns>0</ns> |
| 51 |
<id unknown="123" dontknow="533">14</id> |
| 52 |
<revision> |
| 53 |
<id unknown="123" dontknow="533">15</id> |
| 54 |
<unknowntag>Should be ignored</unknowntag> |
| 55 |
<timestamp>2016-01-03T11:18:43Z</timestamp> |
| 56 |
<contributor> |
| 57 |
<unknowntag>Should be ignored</unknowntag> |
| 58 |
<username unknown="123" dontknow="533">Admin</username> |
| 59 |
<id>1</id> |
| 60 |
</contributor> |
| 61 |
<model>wikitext</model> |
| 62 |
<format>text/x-wiki</format> |
| 63 |
<text xml:space="preserve" bytes="0">noitazinagro tseb eht si ikiWaideM</text> |
| 64 |
<sha1>phoiac9h4m842xq45sp7s6u21eteeq1</sha1> |
| 65 |
<unknowntag>Should be ignored</unknowntag> |
| 66 |
</revision> |
| 67 |
</page> |
| 68 |
<unknowntag>Should be ignored</unknowntag> |
| 69 |
</mediawiki> |
| 70 |
EOF |
| 71 |
, |
| 72 |
'noitazinagro tseb eht si ikiWaideM', |
| 73 |
'TestImportPage' |
| 74 |
] |
| 75 |
]; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function testHandlePageContainsRedirect( $xml, $redirectTitle ) { |
| 86 |
$source = new ImportStringSource( $xml ); |
| 87 |
|
| 88 |
$redirect = null; |
| 89 |
$callback = static function ( Title $title, ForeignTitle $foreignTitle, $revCount, |
| 90 |
$sRevCount, $pageInfo ) use ( &$redirect ) { |
| 91 |
if ( array_key_exists( 'redirect', $pageInfo ) ) { |
| 92 |
$redirect = $pageInfo['redirect']; |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
$importer = $this->getServiceContainer() |
| 97 |
->getWikiImporterFactory() |
| 98 |
->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); |
| 99 |
$importer->setPageOutCallback( $callback ); |
| 100 |
$importer->doImport(); |
| 101 |
|
| 102 |
$this->assertEquals( $redirectTitle, $redirect ); |
| 103 |
} |
| 104 |
|
| 105 |
public function getRedirectXML() { |
| 106 |
return [ |
| 107 |
[ |
| 108 |
<<< EOF |
| 109 |
<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"> |
| 110 |
<page> |
| 111 |
<title>Test</title> |
| 112 |
<ns>0</ns> |
| 113 |
<id>21</id> |
| 114 |
<redirect title="Test22"/> |
| 115 |
<revision> |
| 116 |
<id>20</id> |
| 117 |
<timestamp>2014-05-27T10:00:00Z</timestamp> |
| 118 |
<contributor> |
| 119 |
<username>Admin</username> |
| 120 |
<id>10</id> |
| 121 |
</contributor> |
| 122 |
<comment>Admin moved page [[Test]] to [[Test22]]</comment> |
| 123 |
<model>wikitext</model> |
| 124 |
<format>text/x-wiki</format> |
| 125 |
<text xml:space="preserve" bytes="20">#REDIRECT [[Test22]]</text> |
| 126 |
<sha1>tq456o9x3abm7r9ozi6km8yrbbc56o6</sha1> |
| 127 |
</revision> |
| 128 |
</page> |
| 129 |
</mediawiki> |
| 130 |
EOF |
| 131 |
, |
| 132 |
'Test22' |
| 133 |
], |
| 134 |
[ |
| 135 |
<<< EOF |
| 136 |
<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"> |
| 137 |
<page> |
| 138 |
<title>Test</title> |
| 139 |
<ns>0</ns> |
| 140 |
<id>42</id> |
| 141 |
<revision> |
| 142 |
<id>421</id> |
| 143 |
<timestamp>2014-05-27T11:00:00Z</timestamp> |
| 144 |
<contributor> |
| 145 |
<username>Admin</username> |
| 146 |
<id>10</id> |
| 147 |
</contributor> |
| 148 |
<text xml:space="preserve" bytes="4">Abcd</text> |
| 149 |
<sha1>n7uomjq96szt60fy5w3x7ahf7q8m8rh</sha1> |
| 150 |
<model>wikitext</model> |
| 151 |
<format>text/x-wiki</format> |
| 152 |
</revision> |
| 153 |
</page> |
| 154 |
</mediawiki> |
| 155 |
EOF |
| 156 |
, |
| 157 |
null |
| 158 |
], |
| 159 |
]; |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function testSiteInfoContainsNamespaces( $xml, $namespaces ) { |
| 170 |
$source = new ImportStringSource( $xml ); |
| 171 |
|
| 172 |
$importNamespaces = null; |
| 173 |
$callback = static function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) { |
| 174 |
$importNamespaces = $siteinfo['_namespaces']; |
| 175 |
}; |
| 176 |
|
| 177 |
$importer = $this->getServiceContainer() |
| 178 |
->getWikiImporterFactory() |
| 179 |
->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); |
| 180 |
$importer->setSiteInfoCallback( $callback ); |
| 181 |
$importer->doImport(); |
| 182 |
|
| 183 |
$this->assertEquals( $importNamespaces, $namespaces ); |
| 184 |
} |
| 185 |
|
| 186 |
public function getSiteInfoXML() { |
| 187 |
return [ |
| 188 |
[ |
| 189 |
<<< EOF |
| 190 |
<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"> |
| 191 |
<siteinfo> |
| 192 |
<namespaces> |
| 193 |
<namespace key="-2" case="first-letter">Media</namespace> |
| 194 |
<namespace key="-1" case="first-letter">Special</namespace> |
| 195 |
<namespace key="0" case="first-letter" /> |
| 196 |
<namespace key="1" case="first-letter">Talk</namespace> |
| 197 |
<namespace key="2" case="first-letter">User</namespace> |
| 198 |
<namespace key="3" case="first-letter">User talk</namespace> |
| 199 |
<namespace key="100" case="first-letter">Portal</namespace> |
| 200 |
<namespace key="101" case="first-letter">Portal talk</namespace> |
| 201 |
</namespaces> |
| 202 |
</siteinfo> |
| 203 |
</mediawiki> |
| 204 |
EOF |
| 205 |
, |
| 206 |
[ |
| 207 |
'-2' => 'Media', |
| 208 |
'-1' => 'Special', |
| 209 |
'0' => '', |
| 210 |
'1' => 'Talk', |
| 211 |
'2' => 'User', |
| 212 |
'3' => 'User talk', |
| 213 |
'100' => 'Portal', |
| 214 |
'101' => 'Portal talk', |
| 215 |
] |
| 216 |
], |
| 217 |
]; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function testUnknownUserHandling( $assign, $create ) { |
| 230 |
$hookId = -99; |
| 231 |
$this->setTemporaryHook( |
| 232 |
'ImportHandleUnknownUser', |
| 233 |
function ( $name ) use ( $assign, $create, &$hookId ) { |
| 234 |
if ( !$assign ) { |
| 235 |
$this->fail( 'ImportHandleUnknownUser was called unexpectedly' ); |
| 236 |
} |
| 237 |
|
| 238 |
$this->assertEquals( 'UserDoesNotExist', $name ); |
| 239 |
if ( $create ) { |
| 240 |
$user = User::createNew( $name ); |
| 241 |
$this->assertNotNull( $user ); |
| 242 |
$hookId = $user->getId(); |
| 243 |
return false; |
| 244 |
} |
| 245 |
return true; |
| 246 |
} |
| 247 |
); |
| 248 |
|
| 249 |
$user = $this->getTestUser()->getUser(); |
| 250 |
|
| 251 |
$n = ( $assign ? 1 : 0 ) + ( $create ? 2 : 0 ); |
| 252 |
$source = new ImportStringSource( <<<EOF |
| 253 |
<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"> |
| 254 |
<page> |
| 255 |
<title>TestImportPage</title> |
| 256 |
<ns>0</ns> |
| 257 |
<id>14</id> |
| 258 |
<revision> |
| 259 |
<id>15</id> |
| 260 |
<timestamp>2016-01-01T0$n:00:00Z</timestamp> |
| 261 |
<contributor> |
| 262 |
<username>UserDoesNotExist</username> |
| 263 |
<id>1</id> |
| 264 |
</contributor> |
| 265 |
<model>wikitext</model> |
| 266 |
<format>text/x-wiki</format> |
| 267 |
<text xml:space="preserve" bytes="3">foo</text> |
| 268 |
<sha1>1e6gpc3ehk0mu2jqu8cg42g009s796b</sha1> |
| 269 |
</revision> |
| 270 |
<revision> |
| 271 |
<id>16</id> |
| 272 |
<timestamp>2016-01-01T0$n:00:01Z</timestamp> |
| 273 |
<contributor> |
| 274 |
<username>{$user->getName()}</username> |
| 275 |
<id>{$user->getId()}</id> |
| 276 |
</contributor> |
| 277 |
<model>wikitext</model> |
| 278 |
<format>text/x-wiki</format> |
| 279 |
<text xml:space="preserve" bytes="3">bar</text> |
| 280 |
<sha1>bjhlo6dxh5wivnszm93u4b78fheiy4t</sha1> |
| 281 |
</revision> |
| 282 |
</page> |
| 283 |
</mediawiki> |
| 284 |
EOF |
| 285 |
); |
| 286 |
|
| 287 |
|
| 288 |
$services = $this->getServiceContainer(); |
| 289 |
$importer = $services->getWikiImporterFactory()->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); |
| 290 |
|
| 291 |
$importer->setUsernamePrefix( 'Xxx', $assign ); |
| 292 |
$importer->doImport(); |
| 293 |
|
| 294 |
$db = $this->getDb(); |
| 295 |
$row = $services->getRevisionStore()->newSelectQueryBuilder( $db ) |
| 296 |
->where( [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0000" ) ] ) |
| 297 |
->caller( __METHOD__ )->fetchRow(); |
| 298 |
$this->assertSame( |
| 299 |
$assign && $create ? 'UserDoesNotExist' : 'Xxx>UserDoesNotExist', |
| 300 |
$row->rev_user_text |
| 301 |
); |
| 302 |
$this->assertSame( $assign && $create ? $hookId : 0, (int)$row->rev_user ); |
| 303 |
|
| 304 |
$row = $services->getRevisionStore()->newSelectQueryBuilder( $db ) |
| 305 |
->where( [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0001" ) ] ) |
| 306 |
->caller( __METHOD__ )->fetchRow(); |
| 307 |
$this->assertSame( ( $assign ? '' : 'Xxx>' ) . $user->getName(), $row->rev_user_text ); |
| 308 |
$this->assertSame( $assign ? $user->getId() : 0, (int)$row->rev_user ); |
| 309 |
} |
| 310 |
|
| 311 |
public static function provideUnknownUserHandling() { |
| 312 |
return [ |
| 313 |
'no assign' => [ false, false ], |
| 314 |
'assign, no create' => [ true, false ], |
| 315 |
'assign, create' => [ true, true ], |
| 316 |
]; |
| 317 |
} |
| 318 |
|
| 319 |
private function prepareImportLogEntries( $performer ) { |
| 320 |
$source = new ImportStringSource( <<<EOF |
| 321 |
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.11/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.11/ http://www.mediawiki.org/xml/export-0.11.xsd" version="0.11" xml:lang="en"> |
| 322 |
|
| 323 |
<logitem> |
| 324 |
<id>1</id> |
| 325 |
<timestamp>2008-10-23T00:00:02Z</timestamp> |
| 326 |
<contributor> |
| 327 |
<username>Wikimedian</username> |
| 328 |
<id>12345</id> |
| 329 |
</contributor> |
| 330 |
<comment>content was: 'I think this was a silly edit'</comment> |
| 331 |
<type>test</type> |
| 332 |
<action>test</action> |
| 333 |
<logtitle>Silly page name</logtitle> |
| 334 |
<params>a:1:{s:3:"foo";s:3:"bar";}</params> |
| 335 |
</logitem> |
| 336 |
|
| 337 |
<logitem> |
| 338 |
<id>2</id> |
| 339 |
<timestamp>2008-10-23T00:00:01Z</timestamp> |
| 340 |
<contributor> |
| 341 |
<username>Wikimedian</username> |
| 342 |
<id>12345</id> |
| 343 |
</contributor> |
| 344 |
<comment>content was: 'I think this was a silly edit'</comment> |
| 345 |
<type>test</type> |
| 346 |
<action>test</action> |
| 347 |
<logtitle>Silly page name</logtitle> |
| 348 |
<params>a:1:{s:3:"foo";O:8:"stdClass":0:{}}</params> |
| 349 |
</logitem> |
| 350 |
|
| 351 |
</mediawiki> |
| 352 |
EOF |
| 353 |
); |
| 354 |
|
| 355 |
$services = $this->getServiceContainer(); |
| 356 |
$importer = $services->getWikiImporterFactory()->getWikiImporter( $source, $performer ); |
| 357 |
$importer->setUsernamePrefix( 'Xxx', true ); |
| 358 |
return $importer; |
| 359 |
} |
| 360 |
|
| 361 |
public function testLogEntryImportNoRightsChecksUnsafe() { |
| 362 |
$importer = $this->prepareImportLogEntries( new UltimateAuthority( $this->getTestUser()->getUserIdentity() ) ); |
| 363 |
$importer->doImport(); |
| 364 |
|
| 365 |
$result = $this->getDb()->newSelectQueryBuilder() |
| 366 |
->from( 'logging' ) |
| 367 |
->where( [ |
| 368 |
'log_type' => 'test', |
| 369 |
'log_action' => 'test', |
| 370 |
] ) |
| 371 |
->fetchRowCount(); |
| 372 |
$this->assertSame( 1, $result, "Only one, safe log entry imported" ); |
| 373 |
} |
| 374 |
|
| 375 |
public function testLogEntryImportNoRightsChecksSafe() { |
| 376 |
$scope = ExtensionRegistry::getInstance()->setAttributeForTest( 'LogParamsAllowedClasses', [ |
| 377 |
'test/test' => [ stdClass::class ], |
| 378 |
] ); |
| 379 |
|
| 380 |
$importer = $this->prepareImportLogEntries( new UltimateAuthority( $this->getTestUser()->getUserIdentity() ) ); |
| 381 |
$importer->doImport(); |
| 382 |
|
| 383 |
$result = $this->getDb()->newSelectQueryBuilder() |
| 384 |
->from( 'logging' ) |
| 385 |
->where( [ |
| 386 |
'log_type' => 'test', |
| 387 |
'log_action' => 'test', |
| 388 |
] ) |
| 389 |
->fetchRowCount(); |
| 390 |
$this->assertSame( 2, $result, "Both log entries imported" ); |
| 391 |
} |
| 392 |
|
| 393 |
public function testLogEntryImportUserNotAllowed() { |
| 394 |
RequestContext::getMain()->setOutput( $this->createMock( OutputPage::class ) ); |
| 395 |
|
| 396 |
$importer = $this->prepareImportLogEntries( $this->getTestSysop()->getAuthority() ); |
| 397 |
$importer->doImport(); |
| 398 |
|
| 399 |
$result = $this->getDb()->newSelectQueryBuilder() |
| 400 |
->from( 'logging' ) |
| 401 |
->where( [ |
| 402 |
'log_type' => 'test', |
| 403 |
'log_action' => 'test', |
| 404 |
] ) |
| 405 |
->fetchRowCount(); |
| 406 |
$this->assertSame( 0, $result, "No log entries imported" ); |
| 407 |
} |
| 408 |
|
| 409 |
public function testLogEntryImportUserAllowed() { |
| 410 |
$this->overrideConfigValue( MainConfigNames::GroupPermissions, [ |
| 411 |
'logentryimport' => [ 'logentryimport' => true ], |
| 412 |
] ); |
| 413 |
$performer = $this->getTestUser( 'logentryimport' ); |
| 414 |
|
| 415 |
RequestContext::getMain()->setOutput( $this->createMock( OutputPage::class ) ); |
| 416 |
RequestContext::getMain()->setUser( $performer->getUser() ); |
| 417 |
|
| 418 |
$importer = $this->prepareImportLogEntries( $performer->getAuthority() ); |
| 419 |
$importer->doImport(); |
| 420 |
|
| 421 |
$result = $this->getDb()->newSelectQueryBuilder() |
| 422 |
->from( 'logging' ) |
| 423 |
->where( [ |
| 424 |
'log_type' => 'test', |
| 425 |
'log_action' => 'test', |
| 426 |
] ) |
| 427 |
->fetchRowCount(); |
| 428 |
$this->assertSame( 1, $result, "Only one, safe log entry imported" ); |
| 429 |
} |
| 430 |
|
| 431 |
} |