| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use CloneDatabase; |
| 6 |
use DumpBackup; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWikiIntegrationTestCase; |
| 9 |
use WikiExporter; |
| 10 |
use Wikimedia\Rdbms\IDatabase; |
| 11 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 12 |
use XmlDumpWriter; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class BackupDumperPageTest extends DumpTestCase { |
| 23 |
|
| 24 |
use PageDumpTestDataTrait; |
| 25 |
|
| 26 |
|
| 27 |
private $dbClone; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private $sinkDb; |
| 33 |
|
| 34 |
public function addDBData() { |
| 35 |
parent::addDBData(); |
| 36 |
|
| 37 |
$this->addTestPages( $this->getTestSysop()->getUser() ); |
| 38 |
} |
| 39 |
|
| 40 |
protected function tearDown(): void { |
| 41 |
if ( $this->dbClone ) { |
| 42 |
$this->dbClone->destroy(); |
| 43 |
} |
| 44 |
if ( $this->sinkDb ) { |
| 45 |
$this->sinkDb->close( __METHOD__ ); |
| 46 |
} |
| 47 |
|
| 48 |
parent::tearDown(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private function newSinkDbConnection() { |
| 58 |
if ( !$this->sinkDb ) { |
| 59 |
|
| 60 |
$sinkLb = $this->getServiceContainer()->getDBLoadBalancerFactory()->newMainLB(); |
| 61 |
$this->sinkDb = $sinkLb->getConnectionInternal( DB_PRIMARY ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
$this->dbClone = MediaWikiIntegrationTestCase::setupDatabaseWithTestPrefix( $this->sinkDb ); |
| 66 |
|
| 67 |
|
| 68 |
$this->copyTestData( $this->db, $this->sinkDb ); |
| 69 |
|
| 70 |
return $this->sinkDb; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private function newDumpBackup( $argv, $startId, $endId ) { |
| 81 |
$dumper = new DumpBackup( $argv ); |
| 82 |
$dumper->startId = $startId; |
| 83 |
$dumper->endId = $endId; |
| 84 |
$dumper->reporting = false; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if ( $this->db->getType() === 'sqlite' ) { |
| 90 |
$dumper->setDB( $this->db ); |
| 91 |
} else { |
| 92 |
$dumper->setDB( $this->newSinkDbConnection() ); |
| 93 |
} |
| 94 |
|
| 95 |
return $dumper; |
| 96 |
} |
| 97 |
|
| 98 |
public function schemaVersionProvider() { |
| 99 |
foreach ( XmlDumpWriter::$supportedSchemas as $schemaVersion ) { |
| 100 |
yield [ $schemaVersion ]; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testFullTextPlain( $schemaVersion ) { |
| 108 |
|
| 109 |
$tmpFile = $this->getNewTempFile(); |
| 110 |
|
| 111 |
$dumper = $this->newDumpBackup( |
| 112 |
[ '--full', '--quiet', '--output', 'file:' . $tmpFile, '--schema-version', $schemaVersion ], |
| 113 |
$this->pageId1, |
| 114 |
$this->pageId5 + 1 |
| 115 |
); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false ); |
| 120 |
$dumper->execute(); |
| 121 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true ); |
| 122 |
|
| 123 |
|
| 124 |
$this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) ); |
| 125 |
|
| 126 |
|
| 127 |
$asserter = $this->getDumpAsserter( $schemaVersion ); |
| 128 |
$this->setSiteVarMappings( $asserter ); |
| 129 |
$this->setAllRevisionsVarMappings( $asserter ); |
| 130 |
|
| 131 |
$siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion ); |
| 132 |
$pagesTemplate = $this->getDumpTemplatePath( 'AllText', $schemaVersion ); |
| 133 |
|
| 134 |
$asserter->open( $tmpFile ); |
| 135 |
$asserter->assertDumpHead( $siteInfoTemplate ); |
| 136 |
|
| 137 |
|
| 138 |
$asserter->assertDOM( $pagesTemplate ); |
| 139 |
$asserter->assertDumpEnd(); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function testFullStubPlain( $schemaVersion ) { |
| 146 |
|
| 147 |
$tmpFile = $this->getNewTempFile(); |
| 148 |
|
| 149 |
$dumper = $this->newDumpBackup( |
| 150 |
[ |
| 151 |
'--full', |
| 152 |
'--quiet', |
| 153 |
'--output', |
| 154 |
'file:' . $tmpFile, |
| 155 |
'--stub', |
| 156 |
'--schema-version', $schemaVersion, |
| 157 |
], |
| 158 |
$this->pageId1, |
| 159 |
$this->pageId5 + 1 |
| 160 |
); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false ); |
| 165 |
$dumper->execute(); |
| 166 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true ); |
| 167 |
|
| 168 |
|
| 169 |
$this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) ); |
| 170 |
|
| 171 |
$asserter = $this->getDumpAsserter( $schemaVersion ); |
| 172 |
$this->setSiteVarMappings( $asserter ); |
| 173 |
$this->setAllRevisionsVarMappings( $asserter ); |
| 174 |
|
| 175 |
$siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion ); |
| 176 |
$pagesTemplate = $this->getDumpTemplatePath( 'AllStubs', $schemaVersion ); |
| 177 |
|
| 178 |
$asserter->open( $tmpFile ); |
| 179 |
$asserter->assertDumpHead( $siteInfoTemplate ); |
| 180 |
|
| 181 |
|
| 182 |
$asserter->assertDOM( $pagesTemplate ); |
| 183 |
$asserter->assertDumpEnd(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function testCurrentStubPlain( $schemaVersion ) { |
| 190 |
|
| 191 |
$tmpFile = $this->getNewTempFile(); |
| 192 |
|
| 193 |
$dumper = $this->newDumpBackup( |
| 194 |
[ '--output', 'file:' . $tmpFile, '--schema-version', $schemaVersion ], |
| 195 |
$this->pageId1, |
| 196 |
$this->pageId5 + 1 |
| 197 |
); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false ); |
| 202 |
$dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB ); |
| 203 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true ); |
| 204 |
|
| 205 |
|
| 206 |
$this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $schemaVersion ) ); |
| 207 |
|
| 208 |
$asserter = $this->getDumpAsserter( $schemaVersion ); |
| 209 |
$this->setSiteVarMappings( $asserter ); |
| 210 |
$this->setAllRevisionsVarMappings( $asserter ); |
| 211 |
|
| 212 |
$siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion ); |
| 213 |
$pagesTemplate = $this->getDumpTemplatePath( 'CurrentStubs', $schemaVersion ); |
| 214 |
|
| 215 |
$asserter->open( $tmpFile ); |
| 216 |
$asserter->assertDumpHead( $siteInfoTemplate ); |
| 217 |
|
| 218 |
|
| 219 |
$asserter->assertDOM( $pagesTemplate ); |
| 220 |
$asserter->assertDumpEnd(); |
| 221 |
} |
| 222 |
|
| 223 |
public function testCurrentStubGzip() { |
| 224 |
global $wgXmlDumpSchemaVersion; |
| 225 |
|
| 226 |
$this->checkHasGzip(); |
| 227 |
|
| 228 |
|
| 229 |
$tmpFile = $this->getNewTempFile(); |
| 230 |
|
| 231 |
$dumper = $this->newDumpBackup( |
| 232 |
[ '--output', 'gzip:' . $tmpFile ], |
| 233 |
$this->pageId1, |
| 234 |
$this->pageId5 + 1 |
| 235 |
); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false ); |
| 240 |
$dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB ); |
| 241 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true ); |
| 242 |
|
| 243 |
|
| 244 |
$this->gunzip( $tmpFile ); |
| 245 |
|
| 246 |
$this->assertDumpSchema( $tmpFile, $this->getXmlSchemaPath( $wgXmlDumpSchemaVersion ) ); |
| 247 |
|
| 248 |
$asserter = $this->getDumpAsserter( $wgXmlDumpSchemaVersion ); |
| 249 |
$this->setSiteVarMappings( $asserter ); |
| 250 |
$this->setAllRevisionsVarMappings( $asserter ); |
| 251 |
|
| 252 |
$siteInfoTemplate = $this->getDumpTemplatePath( 'SiteInfo', $wgXmlDumpSchemaVersion ); |
| 253 |
$pagesTemplate = $this->getDumpTemplatePath( 'CurrentStubs', $wgXmlDumpSchemaVersion ); |
| 254 |
|
| 255 |
$asserter->open( $tmpFile ); |
| 256 |
$asserter->assertDumpHead( $siteInfoTemplate ); |
| 257 |
|
| 258 |
|
| 259 |
$asserter->assertDOM( $pagesTemplate ); |
| 260 |
$asserter->assertDumpEnd(); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public function testXmlDumpsBackupUseCase( $schemaVersion ) { |
| 277 |
$this->checkHasGzip(); |
| 278 |
|
| 279 |
$fnameMetaHistory = $this->getNewTempFile(); |
| 280 |
$fnameMetaCurrent = $this->getNewTempFile(); |
| 281 |
$fnameArticles = $this->getNewTempFile(); |
| 282 |
|
| 283 |
$expSiteInfo = $this->getDumpTemplatePath( 'SiteInfo', $schemaVersion ); |
| 284 |
$expMetaHistory = $this->getDumpTemplatePath( 'AllStubs', $schemaVersion ); |
| 285 |
$expMetaCurrent = $this->getDumpTemplatePath( 'CurrentStubs', $schemaVersion ); |
| 286 |
$expArticles = $this->getDumpTemplatePath( 'CurrentArticleStubs', $schemaVersion ); |
| 287 |
|
| 288 |
$dumper = $this->newDumpBackup( |
| 289 |
[ "--quiet", "--full", "--stub", "--output=gzip:" . $fnameMetaHistory, |
| 290 |
"--output=gzip:" . $fnameMetaCurrent, "--filter=latest", |
| 291 |
"--output=gzip:" . $fnameArticles, "--filter=latest", |
| 292 |
"--filter=notalk", "--filter=namespace:!NS_USER", |
| 293 |
"--reporting=1000", '--schema-version', $schemaVersion |
| 294 |
], |
| 295 |
$this->pageId1, |
| 296 |
$this->pageId5 + 1 |
| 297 |
); |
| 298 |
$dumper->reporting = true; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
$fnameReport = $this->getNewTempFile(); |
| 305 |
$dumper->stderr = fopen( $fnameReport, 'a' ); |
| 306 |
if ( $dumper->stderr === false ) { |
| 307 |
$this->fail( "Could not open stream for stderr" ); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false ); |
| 313 |
$dumper->dump( WikiExporter::FULL, WikiExporter::STUB ); |
| 314 |
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true ); |
| 315 |
|
| 316 |
$this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" ); |
| 317 |
$this->assertNotEmpty( file_get_contents( $fnameReport ) ); |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
$this->gunzip( $fnameMetaHistory ); |
| 322 |
|
| 323 |
$asserter = $this->getDumpAsserter( $schemaVersion ); |
| 324 |
$this->setSiteVarMappings( $asserter ); |
| 325 |
$this->setAllRevisionsVarMappings( $asserter ); |
| 326 |
|
| 327 |
$asserter->open( $fnameMetaHistory ); |
| 328 |
$asserter->assertDumpHead( $expSiteInfo ); |
| 329 |
$asserter->assertDOM( $expMetaHistory ); |
| 330 |
$asserter->assertDumpEnd(); |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
$this->gunzip( $fnameMetaCurrent ); |
| 335 |
$this->assertDumpSchema( $fnameMetaCurrent, $this->getXmlSchemaPath( $schemaVersion ) ); |
| 336 |
|
| 337 |
$asserter = $this->getDumpAsserter( $schemaVersion ); |
| 338 |
$this->setSiteVarMappings( $asserter ); |
| 339 |
$this->setCurrentRevisionsVarMappings( $asserter ); |
| 340 |
|
| 341 |
$asserter->open( $fnameMetaCurrent ); |
| 342 |
$asserter->assertDumpHead( $expSiteInfo ); |
| 343 |
$asserter->assertDOM( $expMetaCurrent ); |
| 344 |
$asserter->assertDumpEnd(); |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
$this->gunzip( $fnameArticles ); |
| 349 |
$this->assertDumpSchema( $fnameArticles, $this->getXmlSchemaPath( $schemaVersion ) ); |
| 350 |
|
| 351 |
$asserter = $this->getDumpAsserter( $schemaVersion ); |
| 352 |
$this->setSiteVarMappings( $asserter ); |
| 353 |
$this->setCurrentRevisionsVarMappings( $asserter ); |
| 354 |
|
| 355 |
$asserter->open( $fnameArticles ); |
| 356 |
$asserter->assertDumpHead( $expSiteInfo ); |
| 357 |
$asserter->assertDOM( $expArticles ); |
| 358 |
$asserter->assertDumpEnd(); |
| 359 |
} |
| 360 |
} |