MediaWiki  1.33.0
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();
34  $this->assertTrue( $title->exists() );
35 
36  $this->assertEquals( WikiPage::factory( $title )->getContent()->getText(), $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>
68 EOF
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>
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  // phpcs:enable
161  }
162 
169  public function testSiteInfoContainsNamespaces( $xml, $namespaces ) {
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>
206 EOF
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 
231  public function testUnknownUserHandling( $assign, $create ) {
232  $hookId = -99;
233  $this->setMwGlobals( 'wgHooks', [
234  'ImportHandleUnknownUser' => [ function ( $name ) use ( $assign, $create, &$hookId ) {
235  if ( !$assign ) {
236  $this->fail( 'ImportHandleUnknownUser was called unexpectedly' );
237  }
238 
239  $this->assertEquals( 'UserDoesNotExist', $name );
240  if ( $create ) {
242  $this->assertNotNull( $user );
243  $hookId = $user->getId();
244  return false;
245  }
246  return true;
247  } ]
248  ] );
249 
250  $user = $this->getTestUser()->getUser();
251 
252  $n = ( $assign ? 1 : 0 ) + ( $create ? 2 : 0 );
253 
254  // phpcs:disable Generic.Files.LineLength
255  $source = $this->getDataSource( <<<EOF
256 <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">
257  <page>
258  <title>TestImportPage</title>
259  <ns>0</ns>
260  <id>14</id>
261  <revision>
262  <id>15</id>
263  <timestamp>2016-01-01T0$n:00:00Z</timestamp>
264  <contributor>
265  <username>UserDoesNotExist</username>
266  <id>1</id>
267  </contributor>
268  <model>wikitext</model>
269  <format>text/x-wiki</format>
270  <text xml:space="preserve" bytes="3">foo</text>
271  <sha1>1e6gpc3ehk0mu2jqu8cg42g009s796b</sha1>
272  </revision>
273  <revision>
274  <id>16</id>
275  <timestamp>2016-01-01T0$n:00:01Z</timestamp>
276  <contributor>
277  <username>{$user->getName()}</username>
278  <id>{$user->getId()}</id>
279  </contributor>
280  <model>wikitext</model>
281  <format>text/x-wiki</format>
282  <text xml:space="preserve" bytes="3">bar</text>
283  <sha1>bjhlo6dxh5wivnszm93u4b78fheiy4t</sha1>
284  </revision>
285  </page>
286 </mediawiki>
287 EOF
288  );
289  // phpcs:enable
290 
291  $importer = new WikiImporter( $source, MediaWikiServices::getInstance()->getMainConfig() );
292  $importer->setUsernamePrefix( 'Xxx', $assign );
293  $importer->doImport();
294 
295  $db = wfGetDB( DB_MASTER );
297 
298  $row = $db->selectRow(
299  $revQuery['tables'],
300  $revQuery['fields'],
301  [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0000" ) ],
302  __METHOD__,
303  [],
304  $revQuery['joins']
305  );
306  $this->assertSame(
307  $assign && $create ? 'UserDoesNotExist' : 'Xxx>UserDoesNotExist',
308  $row->rev_user_text
309  );
310  $this->assertSame( $assign && $create ? $hookId : 0, (int)$row->rev_user );
311 
312  $row = $db->selectRow(
313  $revQuery['tables'],
314  $revQuery['fields'],
315  [ 'rev_timestamp' => $db->timestamp( "201601010{$n}0001" ) ],
316  __METHOD__,
317  [],
318  $revQuery['joins']
319  );
320  $this->assertSame( ( $assign ? '' : 'Xxx>' ) . $user->getName(), $row->rev_user_text );
321  $this->assertSame( $assign ? $user->getId() : 0, (int)$row->rev_user );
322  }
323 
324  public static function provideUnknownUserHandling() {
325  return [
326  'no assign' => [ false, false ],
327  'assign, no create' => [ true, false ],
328  'assign, create' => [ true, true ],
329  ];
330  }
331 
332 }
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
ImportTest\testUnknownUserHandling
testUnknownUserHandling( $assign, $create)
provideUnknownUserHandling WikiImporter::setUsernamePrefix ExternalUserNames::addPrefix ExternalUserN...
Definition: ImportTest.php:231
WikiImporter
XML file reader for the page data importer.
Definition: WikiImporter.php:35
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWikiTestCase\getTestUser
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
Definition: MediaWikiTestCase.php:180
version
Prior to version
Definition: maintenance.txt:1
wiki
Prior to maintenance scripts were a hodgepodge of code that had no cohesion or formal method of action Beginning maintenance scripts have been cleaned up to use a unified class Directory structure How to run a script How to write your own DIRECTORY STRUCTURE The maintenance directory of a MediaWiki installation contains several all of which have unique purposes HOW TO RUN A SCRIPT Ridiculously just call php someScript php that s in the top level maintenance directory if not default wiki
Definition: maintenance.txt:1
$namespaces
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:925
ImportTest\getDataSource
getDataSource( $xml)
Definition: ImportTest.php:13
ImportTest\getUnknownTagsXML
getUnknownTagsXML()
Definition: ImportTest.php:39
page
target page
Definition: All_system_messages.txt:1267
$revQuery
$revQuery
Definition: testCompression.php:51
ImportTest\testSiteInfoContainsNamespaces
testSiteInfoContainsNamespaces( $xml, $namespaces)
WikiImporter::handleSiteInfo getSiteInfoXML.
Definition: ImportTest.php:169
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
User\createNew
static createNew( $name, $params=[])
Add a user to the database, return the user object.
Definition: User.php:4303
ImportStringSource
Used for importing XML dumps where the content of the dump is in a string.
Definition: ImportStringSource.php:34
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2670
Revision\getQueryInfo
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition: Revision.php:511
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:138
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
key
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition: hooks.txt:2154
ImportTest\testHandlePageContainsRedirect
testHandlePageContainsRedirect( $xml, $redirectTitle)
WikiImporter::handlePage getRedirectXML.
Definition: ImportTest.php:83
ImportTest\getRedirectXML
getRedirectXML()
Definition: ImportTest.php:104
title
title
Definition: parserTests.txt:245
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:8
ImportTest\getSiteInfoXML
getSiteInfoXML()
Definition: ImportTest.php:187
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1644
Special
wiki Special
Definition: All_system_messages.txt:2667
text
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 as well as the first line of the second redirect text
Definition: All_system_messages.txt:1267
Title
Represents a title within MediaWiki.
Definition: Title.php:40
Test
$source
$source
Definition: mwdoc-filter.php:46
revision
In both all secondary updates will be triggered handle like object that caches derived data representing a revision
Definition: pageupdater.txt:78
true
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:1985
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
ImportTest\testUnknownXMLTags
testUnknownXMLTags( $xml, $text, $title)
WikiImporter getUnknownTagsXML.
Definition: ImportTest.php:24
ImportTest
Test class for Import methods.
Definition: ImportTest.php:11
ForeignTitle
A simple, immutable structure to hold the title of a page on a foreign MediaWiki installation.
Definition: ForeignTitle.php:28
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
redirect
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
Definition: All_system_messages.txt:1267
ImportTest\provideUnknownUserHandling
static provideUnknownUserHandling()
Definition: ImportTest.php:324
MediaWikiTestCase\$db
Database $db
Primary database.
Definition: MediaWikiTestCase.php:61