MediaWiki REL1_31
ImportTest.php
Go to the documentation of this file.
1<?php
3
12
13 private function getDataSource( $xml ) {
14 return new ImportStringSource( $xml );
15 }
16
24 public function testUnknownXMLTags( $xml, $text, $title ) {
25 $source = $this->getDataSource( $xml );
26
27 $importer = new WikiImporter(
28 $source,
29 MediaWikiServices::getInstance()->getMainConfig()
30 );
31
32 $importer->doImport();
33 $title = Title::newFromText( $title );
34 $this->assertTrue( $title->exists() );
35
36 $this->assertEquals( WikiPage::factory( $title )->getContent()->getNativeData(), $text );
37 }
38
39 public function getUnknownTagsXML() {
40 // phpcs:disable Generic.Files.LineLength
41 return [
42 [
43 <<< EOF
44<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">
45 <page unknown="123" dontknow="533">
46 <title>TestImportPage</title>
47 <unknowntag>Should be ignored</unknowntag>
48 <ns>0</ns>
49 <id unknown="123" dontknow="533">14</id>
50 <revision>
51 <id unknown="123" dontknow="533">15</id>
52 <unknowntag>Should be ignored</unknowntag>
53 <timestamp>2016-01-03T11:18:43Z</timestamp>
54 <contributor>
55 <unknowntag>Should be ignored</unknowntag>
56 <username unknown="123" dontknow="533">Admin</username>
57 <id>1</id>
58 </contributor>
59 <model>wikitext</model>
60 <format>text/x-wiki</format>
61 <text xml:space="preserve" bytes="0">noitazinagro tseb eht si ikiWaideM</text>
62 <sha1>phoiac9h4m842xq45sp7s6u21eteeq1</sha1>
63 <unknowntag>Should be ignored</unknowntag>
64 </revision>
65 </page>
66 <unknowntag>Should be ignored</unknowntag>
67</mediawiki>
68EOF
69 ,
70 'noitazinagro tseb eht si ikiWaideM',
71 'TestImportPage'
72 ]
73 ];
74 // phpcs:enable
75 }
76
83 public function testHandlePageContainsRedirect( $xml, $redirectTitle ) {
84 $source = $this->getDataSource( $xml );
85
86 $redirect = null;
87 $callback = function ( Title $title, ForeignTitle $foreignTitle, $revCount,
88 $sRevCount, $pageInfo ) use ( &$redirect ) {
89 if ( array_key_exists( 'redirect', $pageInfo ) ) {
90 $redirect = $pageInfo['redirect'];
91 }
92 };
93
94 $importer = new WikiImporter(
95 $source,
96 MediaWikiServices::getInstance()->getMainConfig()
97 );
98 $importer->setPageOutCallback( $callback );
99 $importer->doImport();
100
101 $this->assertEquals( $redirectTitle, $redirect );
102 }
103
104 public function getRedirectXML() {
105 // phpcs:disable Generic.Files.LineLength
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>
130EOF
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>
155EOF
156 ,
157 null
158 ],
159 ];
160 // phpcs:enable
161 }
162
170 $source = $this->getDataSource( $xml );
171
172 $importNamespaces = null;
173 $callback = function ( array $siteinfo, $innerImporter ) use ( &$importNamespaces ) {
174 $importNamespaces = $siteinfo['_namespaces'];
175 };
176
177 $importer = new WikiImporter(
178 $source,
179 MediaWikiServices::getInstance()->getMainConfig()
180 );
181 $importer->setSiteInfoCallback( $callback );
182 $importer->doImport();
183
184 $this->assertEquals( $importNamespaces, $namespaces );
185 }
186
187 public function getSiteInfoXML() {
188 // phpcs:disable Generic.Files.LineLength
189 return [
190 [
191 <<< EOF
192<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">
193 <siteinfo>
194 <namespaces>
195 <namespace key="-2" case="first-letter">Media</namespace>
196 <namespace key="-1" case="first-letter">Special</namespace>
197 <namespace key="0" case="first-letter" />
198 <namespace key="1" case="first-letter">Talk</namespace>
199 <namespace key="2" case="first-letter">User</namespace>
200 <namespace key="3" case="first-letter">User talk</namespace>
201 <namespace key="100" case="first-letter">Portal</namespace>
202 <namespace key="101" case="first-letter">Portal talk</namespace>
203 </namespaces>
204 </siteinfo>
205</mediawiki>
206EOF
207 ,
208 [
209 '-2' => 'Media',
210 '-1' => 'Special',
211 '0' => '',
212 '1' => 'Talk',
213 '2' => 'User',
214 '3' => 'User talk',
215 '100' => 'Portal',
216 '101' => 'Portal talk',
217 ]
218 ],
219 ];
220 // phpcs:enable
221 }
222
228 public function testUnknownUserHandling( $assign, $create ) {
229 $hookId = -99;
230 $this->setMwGlobals( 'wgHooks', [
231 'ImportHandleUnknownUser' => [ function ( $name ) use ( $assign, $create, &$hookId ) {
232 if ( !$assign ) {
233 $this->fail( 'ImportHandleUnknownUser was called unexpectedly' );
234 }
235
236 $this->assertEquals( 'UserDoesNotExist', $name );
237 if ( $create ) {
238 $user = User::createNew( $name );
239 $this->assertNotNull( $user );
240 $hookId = $user->getId();
241 return false;
242 }
243 return true;
244 } ]
245 ] );
246
247 $user = $this->getTestUser()->getUser();
248
249 $n = ( $assign ? 1 : 0 ) + ( $create ? 2 : 0 );
250
251 // phpcs:disable Generic.Files.LineLength
252 $source = $this->getDataSource( <<<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>
284EOF
285 );
286 // phpcs:enable
287
288 $importer = new WikiImporter( $source, MediaWikiServices::getInstance()->getMainConfig() );
289 $importer->setUsernamePrefix( 'Xxx', $assign );
290 $importer->doImport();
291
292 $db = wfGetDB( DB_MASTER );
293 $revQuery = Revision::getQueryInfo();
294
295 $row = $db->selectRow(
296 $revQuery['tables'],
297 $revQuery['fields'],
298 [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0000" ) ],
299 __METHOD__,
300 [],
301 $revQuery['joins']
302 );
303 $this->assertSame(
304 $assign && $create ? 'UserDoesNotExist' : 'Xxx>UserDoesNotExist',
305 $row->rev_user_text
306 );
307 $this->assertSame( $assign && $create ? $hookId : 0, (int)$row->rev_user );
308
309 $row = $db->selectRow(
310 $revQuery['tables'],
311 $revQuery['fields'],
312 [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0001" ) ],
313 __METHOD__,
314 [],
315 $revQuery['joins']
316 );
317 $this->assertSame( ( $assign ? '' : 'Xxx>' ) . $user->getName(), $row->rev_user_text );
318 $this->assertSame( $assign ? $user->getId() : 0, (int)$row->rev_user );
319 }
320
321 public static function provideUnknownUserHandling() {
322 return [
323 'no assign' => [ false, false ],
324 'assign, no create' => [ true, false ],
325 'assign, create' => [ true, true ],
326 ];
327 }
328
329}
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second redirect
to move a page</td >< td > &*You are moving the page across namespaces
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
A simple, immutable structure to hold the title of a page on a foreign MediaWiki installation.
Used for importing XML dumps where the content of the dump is in a string.
Test class for Import methods.
testSiteInfoContainsNamespaces( $xml, $namespaces)
WikiImporter::handleSiteInfo getSiteInfoXML.
testUnknownXMLTags( $xml, $text, $title)
WikiImporter getUnknownTagsXML.
static provideUnknownUserHandling()
testHandlePageContainsRedirect( $xml, $redirectTitle)
WikiImporter::handlePage getRedirectXML.
testUnknownUserHandling( $assign, $create)
provideUnknownUserHandling
getUnknownTagsXML()
getDataSource( $xml)
Base class that store and restore the Language objects.
Database $db
Primary database.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
static createNew( $name, $params=[])
Add a user to the database, return the user object.
Definition User.php:4297
XML file reader for the page data importer.
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Single row SELECT wrapper.
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by wfTimestamp() to the format used for inserting ...
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition design.txt:26
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:934
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
Prior to version
$source
const DB_MASTER
Definition defines.php:29