| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use DOMDocument; |
| 6 |
use ExecutableFinder; |
| 7 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 8 |
use MediaWiki\Content\Content; |
| 9 |
use MediaWiki\MediaWikiServices; |
| 10 |
use MediaWiki\Revision\RevisionAccessException; |
| 11 |
use MediaWiki\Revision\RevisionRecord; |
| 12 |
use MediaWiki\Revision\SlotRecord; |
| 13 |
use MediaWikiLangTestCase; |
| 14 |
use RuntimeException; |
| 15 |
use WikiExporter; |
| 16 |
use WikiPage; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
abstract class DumpTestCase extends MediaWikiLangTestCase { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
protected $exceptionFromAddDBData = null; |
| 35 |
|
| 36 |
|
| 37 |
protected static $hasGzip = null; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
protected function checkHasGzip() { |
| 45 |
if ( self::$hasGzip === null ) { |
| 46 |
self::$hasGzip = ( ExecutableFinder::findInDefaultPaths( 'gzip' ) !== false ); |
| 47 |
} |
| 48 |
|
| 49 |
if ( !self::$hasGzip ) { |
| 50 |
$this->markTestSkipped( "Skip test, requires the gzip utility in PATH" ); |
| 51 |
} |
| 52 |
|
| 53 |
return self::$hasGzip; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected function addRevision( |
| 66 |
WikiPage $page, |
| 67 |
$text, |
| 68 |
$summary, |
| 69 |
$model = CONTENT_MODEL_WIKITEXT |
| 70 |
) { |
| 71 |
$contentHandler = $this->getServiceContainer() |
| 72 |
->getContentHandlerFactory()->getContentHandler( $model ); |
| 73 |
|
| 74 |
$content = $contentHandler->unserializeContent( $text ); |
| 75 |
|
| 76 |
$rev = $this->addMultiSlotRevision( $page, [ SlotRecord::MAIN => $content ], $summary ); |
| 77 |
|
| 78 |
if ( !$rev ) { |
| 79 |
throw new RuntimeException( "Could not create revision" ); |
| 80 |
} |
| 81 |
|
| 82 |
$text_id = $this->getSlotTextId( $rev->getSlot( SlotRecord::MAIN ) ); |
| 83 |
return [ $rev->getId(), $text_id, $rev ]; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected function getSlotText( SlotRecord $slot ) { |
| 92 |
try { |
| 93 |
return $slot->getContent()->serialize(); |
| 94 |
} catch ( RevisionAccessException $ex ) { |
| 95 |
return null; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
protected function getSlotTextId( SlotRecord $slot ) { |
| 105 |
return (int)preg_replace( '/^tt:/', '', $slot->getAddress() ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
protected function getSlotFormat( SlotRecord $slot ) { |
| 114 |
$contentHandler = $this->getServiceContainer() |
| 115 |
->getContentHandlerFactory()->getContentHandler( $slot->getModel() ); |
| 116 |
|
| 117 |
return $contentHandler->getDefaultFormat(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
protected function addMultiSlotRevision( |
| 129 |
WikiPage $page, |
| 130 |
array $slots, |
| 131 |
$summary |
| 132 |
) { |
| 133 |
$slotRoleRegistry = MediaWikiServices::getInstance()->getSlotRoleRegistry(); |
| 134 |
|
| 135 |
$updater = $page->newPageUpdater( $this->getTestUser()->getUser() ); |
| 136 |
|
| 137 |
foreach ( $slots as $role => $content ) { |
| 138 |
if ( !$slotRoleRegistry->isDefinedRole( $role ) ) { |
| 139 |
$slotRoleRegistry->defineRoleWithModel( $role, $content->getModel() ); |
| 140 |
} |
| 141 |
|
| 142 |
$updater->setContent( $role, $content ); |
| 143 |
} |
| 144 |
|
| 145 |
$updater->saveRevision( CommentStoreComment::newUnsavedComment( trim( $summary ) ) ); |
| 146 |
return $updater->getNewRevision(); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
protected function gunzip( $fname ) { |
| 156 |
$gzipped_contents = file_get_contents( $fname ); |
| 157 |
if ( $gzipped_contents === false ) { |
| 158 |
$this->fail( "Could not get contents of $fname" ); |
| 159 |
} |
| 160 |
|
| 161 |
$contents = gzdecode( $gzipped_contents ); |
| 162 |
|
| 163 |
$this->assertEquals( |
| 164 |
strlen( $contents ), |
| 165 |
file_put_contents( $fname, $contents ), |
| 166 |
'# bytes written' |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
public static function setUpBeforeClass(): void { |
| 171 |
parent::setUpBeforeClass(); |
| 172 |
|
| 173 |
if ( !function_exists( 'libxml_set_external_entity_loader' ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
libxml_set_external_entity_loader( |
| 181 |
static function ( $public, $system, $context ) { |
| 182 |
switch ( $system ) { |
| 183 |
|
| 184 |
case 'http://www.w3.org/2001/xml.xsd': |
| 185 |
$file = __DIR__ . '/xml.xsd'; |
| 186 |
break; |
| 187 |
default: |
| 188 |
if ( is_file( $system ) ) { |
| 189 |
$file = $system; |
| 190 |
} else { |
| 191 |
return null; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return $file; |
| 196 |
} |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
protected function setUp(): void { |
| 206 |
parent::setUp(); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if ( $this->exceptionFromAddDBData !== null ) { |
| 211 |
throw $this->exceptionFromAddDBData; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
protected function getXmlSchemaPath( $schemaVersion = null ) { |
| 223 |
global $IP, $wgXmlDumpSchemaVersion; |
| 224 |
|
| 225 |
$schemaVersion = $schemaVersion ?: $wgXmlDumpSchemaVersion; |
| 226 |
|
| 227 |
return "$IP/docs/export-$schemaVersion.xsd"; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
protected function expectETAOutput() { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
$this->expectOutputRegex( '//' ); |
| 242 |
|
| 243 |
|
| 244 |
$lines = explode( "\n", $this->getActualOutput() ); |
| 245 |
$this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" ); |
| 246 |
$this->assertSame( '', array_pop( $lines ), "Output ends in LF" ); |
| 247 |
$timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]"; |
| 248 |
foreach ( $lines as $line ) { |
| 249 |
$this->assertMatchesRegularExpression( |
| 250 |
"/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/", |
| 251 |
$line |
| 252 |
); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
protected function getDumpAsserter( $schemaVersion = null ) { |
| 262 |
$schemaVersion = $schemaVersion ?: WikiExporter::schemaVersion(); |
| 263 |
return new DumpAsserter( $schemaVersion ); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
protected function assertDumpSchema( $fname, $schemaFile ) { |
| 272 |
if ( !function_exists( 'libxml_use_internal_errors' ) ) { |
| 273 |
|
| 274 |
|
| 275 |
$this->markAsRisky(); |
| 276 |
return; |
| 277 |
} |
| 278 |
$xml = new DOMDocument(); |
| 279 |
$this->assertTrue( $xml->load( $fname ), |
| 280 |
"Opening temporary file $fname via DOMDocument failed" ); |
| 281 |
|
| 282 |
|
| 283 |
$oldLibXmlInternalErrors = libxml_use_internal_errors( true ); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if ( !$xml->schemaValidate( $schemaFile ) ) { |
| 290 |
$errorText = ''; |
| 291 |
|
| 292 |
foreach ( libxml_get_errors() as $error ) { |
| 293 |
$errorText .= "\nline {$error->line}: {$error->message}"; |
| 294 |
} |
| 295 |
|
| 296 |
libxml_clear_errors(); |
| 297 |
|
| 298 |
$this->fail( |
| 299 |
"Failed asserting that $fname conforms to the schema in $schemaFile:\n$errorText" |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
libxml_use_internal_errors( $oldLibXmlInternalErrors ); |
| 304 |
} |
| 305 |
|
| 306 |
} |