MediaWiki  master
LinksUpdateTest.php
Go to the documentation of this file.
1 <?php
2 
10  protected static $testingPageId;
11 
12  function __construct( $name = null, array $data = [], $dataName = '' ) {
13  parent::__construct( $name, $data, $dataName );
14 
15  $this->tablesUsed = array_merge( $this->tablesUsed,
16  [
17  'interwiki',
18  'page_props',
19  'pagelinks',
20  'categorylinks',
21  'langlinks',
22  'externallinks',
23  'imagelinks',
24  'templatelinks',
25  'iwlinks',
26  'recentchanges',
27  ]
28  );
29  }
30 
31  protected function setUp() {
32  parent::setUp();
33  $dbw = wfGetDB( DB_MASTER );
34  $dbw->replace(
35  'interwiki',
36  [ 'iw_prefix' ],
37  [
38  'iw_prefix' => 'linksupdatetest',
39  'iw_url' => 'http://testing.com/wiki/$1',
40  'iw_api' => 'http://testing.com/w/api.php',
41  'iw_local' => 0,
42  'iw_trans' => 0,
43  'iw_wikiid' => 'linksupdatetest',
44  ]
45  );
46  $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
47  }
48 
49  public function addDBDataOnce() {
50  $res = $this->insertPage( 'Testing' );
51  self::$testingPageId = $res['id'];
52  $this->insertPage( 'Some_other_page' );
53  $this->insertPage( 'Template:TestingTemplate' );
54  }
55 
56  protected function makeTitleAndParserOutput( $name, $id ) {
58  $t->mArticleID = $id; # XXX: this is fugly
59 
60  $po = new ParserOutput();
61  $po->setTitleText( $t->getPrefixedText() );
62 
63  return [ $t, $po ];
64  }
65 
69  public function testUpdate_pagelinks() {
72  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
73 
74  $po->addLink( Title::newFromText( "Foo" ) );
75  $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
76  $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
77  $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
78 
79  $update = $this->assertLinksUpdate(
80  $t,
81  $po,
82  'pagelinks',
83  'pl_namespace,
84  pl_title',
85  'pl_from = ' . self::$testingPageId,
86  [ [ NS_MAIN, 'Foo' ] ]
87  );
88  $this->assertArrayEquals( [
89  Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
90  ], $update->getAddedLinks() );
91 
92  $po = new ParserOutput();
93  $po->setTitleText( $t->getPrefixedText() );
94 
95  $po->addLink( Title::newFromText( "Bar" ) );
96  $po->addLink( Title::newFromText( "Talk:Bar" ) );
97 
98  $update = $this->assertLinksUpdate(
99  $t,
100  $po,
101  'pagelinks',
102  'pl_namespace,
103  pl_title',
104  'pl_from = ' . self::$testingPageId,
105  [
106  [ NS_MAIN, 'Bar' ],
107  [ NS_TALK, 'Bar' ],
108  ]
109  );
110  $this->assertArrayEquals( [
111  Title::makeTitle( NS_MAIN, 'Bar' ),
112  Title::makeTitle( NS_TALK, 'Bar' ),
113  ], $update->getAddedLinks() );
114  $this->assertArrayEquals( [
115  Title::makeTitle( NS_MAIN, 'Foo' ),
116  ], $update->getRemovedLinks() );
117  }
118 
124  public function testUpdate_externallinks() {
126  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
127 
128  $po->addExternalLink( "http://testing.com/wiki/Foo" );
129 
130  $update = $this->assertLinksUpdate(
131  $t,
132  $po,
133  'externallinks',
134  'el_to, el_index',
135  'el_from = ' . self::$testingPageId,
136  [
137  [ 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ],
138  ]
139  );
140 
141  $this->assertArrayEquals( [
142  "http://testing.com/wiki/Foo"
143  ], $update->getAddedExternalLinks() );
144 
145  $po = new ParserOutput();
146  $po->setTitleText( $t->getPrefixedText() );
147  $po->addExternalLink( 'http://testing.com/wiki/Bar' );
148  $update = $this->assertLinksUpdate(
149  $t,
150  $po,
151  'externallinks',
152  'el_to, el_index',
153  'el_from = ' . self::$testingPageId,
154  [
155  [ 'http://testing.com/wiki/Bar', 'http://com.testing./wiki/Bar' ],
156  ]
157  );
158 
159  $this->assertArrayEquals( [
160  "http://testing.com/wiki/Bar"
161  ], $update->getAddedExternalLinks() );
162  $this->assertArrayEquals( [
163  "http://testing.com/wiki/Foo"
164  ], $update->getRemovedExternalLinks() );
165  }
166 
170  public function testUpdate_categorylinks() {
172  $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
173 
174  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
175 
176  $po->addCategory( "Foo", "FOO" );
177 
178  $this->assertLinksUpdate(
179  $t,
180  $po,
181  'categorylinks',
182  'cl_to, cl_sortkey',
183  'cl_from = ' . self::$testingPageId,
184  [ [ 'Foo', "FOO\nTESTING" ] ]
185  );
186  }
187 
189  $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
190 
191  $title = Title::newFromText( 'Testing' );
192  $wikiPage = new WikiPage( $title );
193  $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
194  $this->runAllRelatedJobs();
195 
197  $title,
198  $wikiPage->getParserOutput( ParserOptions::newCanonical() ),
199  Title::newFromText( 'Category:Foo' ),
200  [ [ 'Foo', '[[:Testing]] added to category' ] ]
201  );
202 
203  $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
204  $this->runAllRelatedJobs();
205 
207  $title,
208  $wikiPage->getParserOutput( ParserOptions::newCanonical() ),
209  Title::newFromText( 'Category:Foo' ),
210  [
211  [ 'Foo', '[[:Testing]] added to category' ],
212  [ 'Foo', '[[:Testing]] removed from category' ],
213  ]
214  );
215 
217  $title,
218  $wikiPage->getParserOutput( ParserOptions::newCanonical() ),
219  Title::newFromText( 'Category:Bar' ),
220  [
221  [ 'Bar', '[[:Testing]] added to category' ],
222  ]
223  );
224  }
225 
227  $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
228 
229  $templateTitle = Title::newFromText( 'Template:TestingTemplate' );
230  $templatePage = new WikiPage( $templateTitle );
231 
232  $wikiPage = new WikiPage( Title::newFromText( 'Testing' ) );
233  $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
234  $this->runAllRelatedJobs();
235 
236  $otherWikiPage = new WikiPage( Title::newFromText( 'Some_other_page' ) );
237  $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
238  $this->runAllRelatedJobs();
239 
241  $templateTitle,
242  $templatePage->getParserOutput( ParserOptions::newCanonical() ),
243  Title::newFromText( 'Baz' ),
244  []
245  );
246 
247  $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
248  $this->runAllRelatedJobs();
249 
251  $templateTitle,
252  $templatePage->getParserOutput( ParserOptions::newCanonical() ),
253  Title::newFromText( 'Baz' ),
254  [ [
255  'Baz',
256  '[[:Template:TestingTemplate]] added to category, ' .
257  '[[Special:WhatLinksHere/Template:TestingTemplate|this page is included within other pages]]'
258  ] ]
259  );
260  }
261 
265  public function testUpdate_iwlinks() {
267  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
268 
269  $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
270  $po->addInterwikiLink( $target );
271 
272  $this->assertLinksUpdate(
273  $t,
274  $po,
275  'iwlinks',
276  'iwl_prefix, iwl_title',
277  'iwl_from = ' . self::$testingPageId,
278  [ [ 'linksupdatetest', 'Foo' ] ]
279  );
280  }
281 
285  public function testUpdate_templatelinks() {
287  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
288 
289  $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
290 
291  $this->assertLinksUpdate(
292  $t,
293  $po,
294  'templatelinks',
295  'tl_namespace,
296  tl_title',
297  'tl_from = ' . self::$testingPageId,
298  [ [ NS_TEMPLATE, 'Foo' ] ]
299  );
300  }
301 
305  public function testUpdate_imagelinks() {
307  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
308 
309  $po->addImage( "Foo.png" );
310 
311  $this->assertLinksUpdate(
312  $t,
313  $po,
314  'imagelinks',
315  'il_to',
316  'il_from = ' . self::$testingPageId,
317  [ [ 'Foo.png' ] ]
318  );
319  }
320 
324  public function testUpdate_langlinks() {
325  $this->setMwGlobals( [
326  'wgCapitalLinks' => true,
327  ] );
328 
330  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
331 
332  $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
333 
334  $this->assertLinksUpdate(
335  $t,
336  $po,
337  'langlinks',
338  'll_lang, ll_title',
339  'll_from = ' . self::$testingPageId,
340  [ [ 'En', 'Foo' ] ]
341  );
342  }
343 
347  public function testUpdate_page_props() {
349 
351  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
352 
353  $fields = [ 'pp_propname', 'pp_value' ];
354  $expected = [];
355 
356  $po->setProperty( "bool", true );
357  $expected[] = [ "bool", true ];
358 
359  $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
360  $expected[] = [ "float", 4.0 + 1.0 / 4.0 ];
361 
362  $po->setProperty( "int", -7 );
363  $expected[] = [ "int", -7 ];
364 
365  $po->setProperty( "string", "33 bar" );
366  $expected[] = [ "string", "33 bar" ];
367 
368  // compute expected sortkey values
369  if ( $wgPagePropsHaveSortkey ) {
370  $fields[] = 'pp_sortkey';
371 
372  foreach ( $expected as &$row ) {
373  $value = $row[1];
374 
375  if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
376  $row[] = floatval( $value );
377  } else {
378  $row[] = null;
379  }
380  }
381  }
382 
383  $this->assertLinksUpdate(
384  $t, $po, 'page_props', $fields, 'pp_page = ' . self::$testingPageId, $expected );
385  }
386 
388  $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
389 
390  $this->testUpdate_page_props();
391  }
392 
393  // @todo test recursive, too!
394 
395  protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
396  $table, $fields, $condition, array $expectedRows
397  ) {
398  $update = new LinksUpdate( $title, $parserOutput );
399 
400  $update->doUpdate();
401 
402  $this->assertSelect( $table, $fields, $condition, $expectedRows );
403  return $update;
404  }
405 
407  Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows
408  ) {
409  $this->assertSelect(
410  [ 'recentchanges', 'comment' ],
411  'rc_title, comment_text',
412  [
413  'rc_type' => RC_CATEGORIZE,
414  'rc_namespace' => NS_CATEGORY,
415  'rc_title' => $categoryTitle->getDBkey(),
416  'comment_id = rc_comment_id',
417  ],
418  $expectedRows
419  );
420  }
421 
422  private function runAllRelatedJobs() {
423  $queueGroup = JobQueueGroup::singleton();
424  while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
425  $job->run();
426  $queueGroup->ack( $job );
427  }
428  while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
429  $job->run();
430  $queueGroup->ack( $job );
431  }
432  }
433 }
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))
testUpdate_page_props_without_sortkey()
const RC_CATEGORIZE
Definition: Defines.php:146
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$wgPagePropsHaveSortkey
Whether the page_props table has a pp_sortkey column.
const NS_MAIN
Definition: Defines.php:64
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
$value
testUpdate_iwlinks()
ParserOutput::addInterwikiLink.
testUpdate_imagelinks()
ParserOutput::addImage.
const NS_TEMPLATE
Definition: Defines.php:74
testUpdate_pagelinks()
ParserOutput::addLink.
testUpdate_page_props()
ParserOutput::setProperty.
const DB_MASTER
Definition: defines.php:26
testUpdate_externallinks()
ParserOutput::addExternalLink LinksUpdate::getAddedExternalLinks LinksUpdate::getRemovedExternalLinks...
This document provides an overview of the usage of PageUpdater and that is
Definition: pageupdater.txt:3
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
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:1982
testUpdate_templatelinks()
ParserOutput::addTemplate.
insertPage( $pageName, $text='Sample page for unit test.', $namespace=null, User $user=null)
Insert a new page.
getDBkey()
Get the main part with underscores.
Definition: Title.php:1002
$res
Definition: database.txt:21
const NS_CATEGORY
Definition: Defines.php:78
LinksUpdate LinksUpdate Database ^— make sure temporary tables are used.
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
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
testUpdate_langlinks()
ParserOutput::addLanguageLink.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored()
assertSelect( $table, $fields, $condition, array $expectedRows, array $options=[], array $join_conds=[])
Asserts that the given database query yields the rows given by $expectedRows.
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:617
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:589
testOnAddingAndRemovingCategory_recentChangesRowIsAdded()
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
makeTitleAndParserOutput( $name, $id)
if(count( $args)< 1) $job
assertRecentChangeByCategorization(Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows)
assertLinksUpdate(Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows)
static singleton( $domain=false)
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
__construct( $name=null, array $data=[], $dataName='')
const NS_TALK
Definition: Defines.php:65
testUpdate_categorylinks()
ParserOutput::addCategory.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319