MediaWiki REL1_33
DumpAsserter.php
Go to the documentation of this file.
1<?php
2
4
6use XMLReader;
7
12
18 protected $xml = null;
19
25 protected $schemaVersion;
26
32 public function __construct( $schemaVersion ) {
33 $this->schemaVersion = $schemaVersion;
34 }
35
44 public function skipToNodeEnd( $name ) {
45 while ( $this->xml->read() ) {
46 if ( $this->xml->nodeType == XMLReader::END_ELEMENT &&
47 $this->xml->name == $name
48 ) {
49 return true;
50 }
51 }
52
53 return false;
54 }
55
66 public function skipPastNodeEnd( $name ) {
67 Assert::assertTrue( $this->skipToNodeEnd( $name ),
68 "Skipping to end of $name" );
69 while ( $this->xml->read() ) {
70 if ( $this->xml->nodeType == XMLReader::ELEMENT ) {
71 return true;
72 }
73 }
74
75 return false;
76 }
77
85 public function assertDumpStart( $fname, $skip_siteinfo = true ) {
86 $this->xml = new XMLReader();
87
88 Assert::assertTrue( $this->xml->open( $fname ),
89 "Opening temporary file $fname via XMLReader failed" );
90 if ( $skip_siteinfo ) {
91 Assert::assertTrue( $this->skipPastNodeEnd( "siteinfo" ),
92 "Skipping past end of siteinfo" );
93 }
94 }
95
103 public function assertDumpEnd( $name = "mediawiki" ) {
104 $this->assertNodeEnd( $name, false );
105 if ( $this->xml->read() ) {
106 $this->skipWhitespace();
107 }
108 Assert::assertEquals( $this->xml->nodeType, XMLReader::NONE,
109 "No proper entity left to parse" );
110 $this->xml->close();
111 }
112
116 public function skipWhitespace() {
117 $cont = true;
118 while ( $cont && ( ( $this->xml->nodeType == XMLReader::WHITESPACE )
119 || ( $this->xml->nodeType == XMLReader::SIGNIFICANT_WHITESPACE ) ) ) {
120 $cont = $this->xml->read();
121 }
122 }
123
132 public function assertNodeStart( $name, $skip = true ) {
133 Assert::assertEquals( $name, $this->xml->name, "Node name" );
134 Assert::assertEquals( XMLReader::ELEMENT, $this->xml->nodeType, "Node type" );
135 if ( $skip ) {
136 Assert::assertTrue( $this->xml->read(), "Skipping past start tag" );
137 }
138 }
139
148 public function assertNodeEnd( $name, $skip = true ) {
149 Assert::assertEquals( $name, $this->xml->name, "Node name" );
150 Assert::assertEquals( XMLReader::END_ELEMENT, $this->xml->nodeType, "Node type" );
151 if ( $skip ) {
152 Assert::assertTrue( $this->xml->read(), "Skipping past end tag" );
153 }
154 }
155
167 public function assertTextNode( $name, $text, $skip_ws = true ) {
168 $this->assertNodeStart( $name );
169
170 if ( $text !== false ) {
171 Assert::assertEquals( $text, $this->xml->value, "Text of node " . $name );
172 }
173 Assert::assertTrue( $this->xml->read(), "Skipping past processed text of " . $name );
174 $this->assertNodeEnd( $name );
175
176 if ( $skip_ws ) {
177 $this->skipWhitespace();
178 }
179 }
180
193 public function assertPageStart( $id, $ns, $name ) {
194 $this->assertNodeStart( "page" );
195 $this->skipWhitespace();
196
197 $this->assertTextNode( "title", $name );
198 $this->assertTextNode( "ns", $ns );
199 $this->assertTextNode( "id", $id );
200 }
201
206 public function assertPageEnd() {
207 $this->assertNodeEnd( "page" );
208 $this->skipWhitespace();
209 }
210
226 public function assertRevision( $id, $summary, $text_id, $text_bytes,
227 $text_sha1, $text = false, $parentid = false,
229 ) {
230 $this->assertNodeStart( "revision" );
231 $this->skipWhitespace();
232
233 $this->assertTextNode( "id", $id );
234 if ( $parentid !== false ) {
235 $this->assertTextNode( "parentid", $parentid );
236 }
237 $this->assertTextNode( "timestamp", false );
238
239 $this->assertNodeStart( "contributor" );
240 $this->skipWhitespace();
241 $this->assertTextNode( "username", false );
242 $this->assertTextNode( "id", false );
243 $this->assertNodeEnd( "contributor" );
244 $this->skipWhitespace();
245
246 $this->assertTextNode( "comment", $summary );
247 $this->skipWhitespace();
248
249 $this->assertTextNode( "model", $model );
250 $this->skipWhitespace();
251
252 $this->assertTextNode( "format", $format );
253 $this->skipWhitespace();
254
255 if ( $this->xml->name == "text" ) {
256 // note: <text> tag may occur here or at the very end.
257 $text_found = true;
258 $this->assertText( $id, $text_id, $text_bytes, $text );
259 } else {
260 $text_found = false;
261 }
262
263 $this->assertTextNode( "sha1", $text_sha1 );
264
265 if ( !$text_found ) {
266 $this->assertText( $id, $text_id, $text_bytes, $text );
267 }
268
269 $this->assertNodeEnd( "revision" );
270 $this->skipWhitespace();
271 }
272
273 public function assertText( $id, $text_id, $text_bytes, $text ) {
274 $this->assertNodeStart( "text", false );
275 if ( $text_bytes !== false ) {
276 Assert::assertEquals( $this->xml->getAttribute( "bytes" ), $text_bytes,
277 "Attribute 'bytes' of revision " . $id );
278 }
279
280 if ( $text === false ) {
281 // Testing for a stub
282 Assert::assertEquals( $this->xml->getAttribute( "id" ), $text_id,
283 "Text id of revision " . $id );
284 Assert::assertFalse( $this->xml->hasValue, "Revision has text" );
285 Assert::assertTrue( $this->xml->read(), "Skipping text start tag" );
286 if ( ( $this->xml->nodeType == XMLReader::END_ELEMENT )
287 && ( $this->xml->name == "text" )
288 ) {
289 $this->xml->read();
290 }
291 $this->skipWhitespace();
292 } else {
293 // Testing for a real dump
294 Assert::assertTrue( $this->xml->read(), "Skipping text start tag" );
295 Assert::assertEquals( $text, $this->xml->value, "Text of revision " . $id );
296 Assert::assertTrue( $this->xml->read(), "Skipping past text" );
297 $this->assertNodeEnd( "text" );
298 $this->skipWhitespace();
299 }
300 }
301
315 public function assertLogItem( $id, $user_name, $user_id, $comment, $type,
316 $subtype, $title, $parameters = []
317 ) {
318 $this->assertNodeStart( "logitem" );
319 $this->skipWhitespace();
320
321 $this->assertTextNode( "id", $id );
322 $this->assertTextNode( "timestamp", false );
323
324 $this->assertNodeStart( "contributor" );
325 $this->skipWhitespace();
326 $this->assertTextNode( "username", $user_name );
327 $this->assertTextNode( "id", $user_id );
328 $this->assertNodeEnd( "contributor" );
329 $this->skipWhitespace();
330
331 if ( $comment !== null ) {
332 $this->assertTextNode( "comment", $comment );
333 }
334 $this->assertTextNode( "type", $type );
335 $this->assertTextNode( "action", $subtype );
336 $this->assertTextNode( "logtitle", $title );
337
338 $this->assertNodeStart( "params" );
339 $parameters_xml = unserialize( $this->xml->value );
340 Assert::assertEquals( $parameters, $parameters_xml );
341 Assert::assertTrue( $this->xml->read(), "Skipping past processed text of params" );
342 $this->assertNodeEnd( "params" );
343 $this->skipWhitespace();
344
345 $this->assertNodeEnd( "logitem" );
346 $this->skipWhitespace();
347 }
348}
unserialize( $serialized)
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:123
Helper for asserting the structure of an XML dump stream.
assertLogItem( $id, $user_name, $user_id, $comment, $type, $subtype, $title, $parameters=[])
asserts that the xml reader is at the beginning of a log entry and skips over it while analyzing it.
assertNodeEnd( $name, $skip=true)
Asserts that the xml reader is at an closing element of given name, and optionally skips past it.
skipWhitespace()
Steps the xml reader over white space.
XMLReader null $xml
Holds the XMLReader used for analyzing an XML dump.
skipPastNodeEnd( $name)
Step the current XML reader to the first element start after the node end of a given name.
assertPageStart( $id, $ns, $name)
Asserts that the xml reader is at the start of a page element and skips over the first tags,...
assertDumpEnd( $name="mediawiki")
Asserts that the xml reader is at the final closing tag of an xml file and closes the reader.
assertText( $id, $text_id, $text_bytes, $text)
assertRevision( $id, $summary, $text_id, $text_bytes, $text_sha1, $text=false, $parentid=false, $model=CONTENT_MODEL_WIKITEXT, $format=CONTENT_FORMAT_WIKITEXT)
Asserts that the xml reader is at a revision and checks its representation before skipping over it.
assertDumpStart( $fname, $skip_siteinfo=true)
Opens an XML file to analyze and optionally skips past siteinfo.
skipToNodeEnd( $name)
Step the current XML reader until node end of given name is found.
string $schemaVersion
XML dump schema version.
assertPageEnd()
Asserts that the xml reader is at the page's closing element and skips to the next element.
assertNodeStart( $name, $skip=true)
Asserts that the xml reader is at an element of given name, and optionally skips past it.
__construct( $schemaVersion)
DumpAsserts constructor.
assertTextNode( $name, $text, $skip_ws=true)
Asserts that the xml reader is at an element of given tag that contains a given text,...
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:244
const CONTENT_FORMAT_WIKITEXT
Definition Defines.php:259