MediaWiki  master
LinksUpdateTest.php
Go to the documentation of this file.
1 <?php
2 
9  protected static $testingPageId;
10 
11  function __construct( $name = null, array $data = [], $dataName = '' ) {
12  parent::__construct( $name, $data, $dataName );
13 
14  $this->tablesUsed = array_merge( $this->tablesUsed,
15  [
16  'interwiki',
17  'page_props',
18  'pagelinks',
19  'categorylinks',
20  'langlinks',
21  'externallinks',
22  'imagelinks',
23  'templatelinks',
24  'iwlinks',
25  'recentchanges',
26  ]
27  );
28  }
29 
30  protected function setUp() {
31  parent::setUp();
32  $dbw = wfGetDB( DB_MASTER );
33  $dbw->replace(
34  'interwiki',
35  [ 'iw_prefix' ],
36  [
37  'iw_prefix' => 'linksupdatetest',
38  'iw_url' => 'http://testing.com/wiki/$1',
39  'iw_api' => 'http://testing.com/w/api.php',
40  'iw_local' => 0,
41  'iw_trans' => 0,
42  'iw_wikiid' => 'linksupdatetest',
43  ]
44  );
45  $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
46  }
47 
48  public function addDBDataOnce() {
49  $res = $this->insertPage( 'Testing' );
50  self::$testingPageId = $res['id'];
51  $this->insertPage( 'Some_other_page' );
52  $this->insertPage( 'Template:TestingTemplate' );
53  }
54 
55  protected function makeTitleAndParserOutput( $name, $id ) {
57  $t->mArticleID = $id; # XXX: this is fugly
58 
59  $po = new ParserOutput();
60  $po->setTitleText( $t->getPrefixedText() );
61 
62  return [ $t, $po ];
63  }
64 
68  public function testUpdate_pagelinks() {
71  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
72 
73  $po->addLink( Title::newFromText( "Foo" ) );
74  $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
75  $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
76  $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
77 
78  $update = $this->assertLinksUpdate(
79  $t,
80  $po,
81  'pagelinks',
82  'pl_namespace,
83  pl_title',
84  'pl_from = ' . self::$testingPageId,
85  [ [ NS_MAIN, 'Foo' ] ]
86  );
87  $this->assertArrayEquals( [
88  Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
89  ], $update->getAddedLinks() );
90 
91  $po = new ParserOutput();
92  $po->setTitleText( $t->getPrefixedText() );
93 
94  $po->addLink( Title::newFromText( "Bar" ) );
95  $po->addLink( Title::newFromText( "Talk:Bar" ) );
96 
97  $update = $this->assertLinksUpdate(
98  $t,
99  $po,
100  'pagelinks',
101  'pl_namespace,
102  pl_title',
103  'pl_from = ' . self::$testingPageId,
104  [
105  [ NS_MAIN, 'Bar' ],
106  [ NS_TALK, 'Bar' ],
107  ]
108  );
109  $this->assertArrayEquals( [
110  Title::makeTitle( NS_MAIN, 'Bar' ),
111  Title::makeTitle( NS_TALK, 'Bar' ),
112  ], $update->getAddedLinks() );
113  $this->assertArrayEquals( [
114  Title::makeTitle( NS_MAIN, 'Foo' ),
115  ], $update->getRemovedLinks() );
116  }
117 
121  public function testUpdate_externallinks() {
123  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
124 
125  $po->addExternalLink( "http://testing.com/wiki/Foo" );
126 
127  $this->assertLinksUpdate(
128  $t,
129  $po,
130  'externallinks',
131  'el_to, el_index',
132  'el_from = ' . self::$testingPageId,
133  [
134  [ 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ],
135  ]
136  );
137  }
138 
142  public function testUpdate_categorylinks() {
144  $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
145 
146  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
147 
148  $po->addCategory( "Foo", "FOO" );
149 
150  $this->assertLinksUpdate(
151  $t,
152  $po,
153  'categorylinks',
154  'cl_to, cl_sortkey',
155  'cl_from = ' . self::$testingPageId,
156  [ [ 'Foo', "FOO\nTESTING" ] ]
157  );
158  }
159 
161  $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
162 
163  $title = Title::newFromText( 'Testing' );
164  $wikiPage = new WikiPage( $title );
165  $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
166  $this->runAllRelatedJobs();
167 
169  $title,
170  $wikiPage->getParserOutput( new ParserOptions() ),
171  Title::newFromText( 'Category:Foo' ),
172  [ [ 'Foo', '[[:Testing]] added to category' ] ]
173  );
174 
175  $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
176  $this->runAllRelatedJobs();
177 
179  $title,
180  $wikiPage->getParserOutput( new ParserOptions() ),
181  Title::newFromText( 'Category:Foo' ),
182  [
183  [ 'Foo', '[[:Testing]] added to category' ],
184  [ 'Foo', '[[:Testing]] removed from category' ],
185  ]
186  );
187 
189  $title,
190  $wikiPage->getParserOutput( new ParserOptions() ),
191  Title::newFromText( 'Category:Bar' ),
192  [
193  [ 'Bar', '[[:Testing]] added to category' ],
194  ]
195  );
196  }
197 
199  $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
200 
201  $templateTitle = Title::newFromText( 'Template:TestingTemplate' );
202  $templatePage = new WikiPage( $templateTitle );
203 
204  $wikiPage = new WikiPage( Title::newFromText( 'Testing' ) );
205  $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
206  $this->runAllRelatedJobs();
207 
208  $otherWikiPage = new WikiPage( Title::newFromText( 'Some_other_page' ) );
209  $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
210  $this->runAllRelatedJobs();
211 
213  $templateTitle,
214  $templatePage->getParserOutput( new ParserOptions() ),
215  Title::newFromText( 'Baz' ),
216  []
217  );
218 
219  $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
220  $this->runAllRelatedJobs();
221 
223  $templateTitle,
224  $templatePage->getParserOutput( new ParserOptions() ),
225  Title::newFromText( 'Baz' ),
226  [ [
227  'Baz',
228  '[[:Template:TestingTemplate]] added to category, ' .
229  '[[Special:WhatLinksHere/Template:TestingTemplate|this page is included within other pages]]'
230  ] ]
231  );
232  }
233 
237  public function testUpdate_iwlinks() {
239  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
240 
241  $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
242  $po->addInterwikiLink( $target );
243 
244  $this->assertLinksUpdate(
245  $t,
246  $po,
247  'iwlinks',
248  'iwl_prefix, iwl_title',
249  'iwl_from = ' . self::$testingPageId,
250  [ [ 'linksupdatetest', 'Foo' ] ]
251  );
252  }
253 
257  public function testUpdate_templatelinks() {
259  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
260 
261  $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
262 
263  $this->assertLinksUpdate(
264  $t,
265  $po,
266  'templatelinks',
267  'tl_namespace,
268  tl_title',
269  'tl_from = ' . self::$testingPageId,
270  [ [ NS_TEMPLATE, 'Foo' ] ]
271  );
272  }
273 
277  public function testUpdate_imagelinks() {
279  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
280 
281  $po->addImage( "Foo.png" );
282 
283  $this->assertLinksUpdate(
284  $t,
285  $po,
286  'imagelinks',
287  'il_to',
288  'il_from = ' . self::$testingPageId,
289  [ [ 'Foo.png' ] ]
290  );
291  }
292 
296  public function testUpdate_langlinks() {
297  $this->setMwGlobals( [
298  'wgCapitalLinks' => true,
299  ] );
300 
302  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
303 
304  $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
305 
306  $this->assertLinksUpdate(
307  $t,
308  $po,
309  'langlinks',
310  'll_lang, ll_title',
311  'll_from = ' . self::$testingPageId,
312  [ [ 'En', 'Foo' ] ]
313  );
314  }
315 
319  public function testUpdate_page_props() {
321 
323  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
324 
325  $fields = [ 'pp_propname', 'pp_value' ];
326  $expected = [];
327 
328  $po->setProperty( "bool", true );
329  $expected[] = [ "bool", true ];
330 
331  $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
332  $expected[] = [ "float", 4.0 + 1.0 / 4.0 ];
333 
334  $po->setProperty( "int", -7 );
335  $expected[] = [ "int", -7 ];
336 
337  $po->setProperty( "string", "33 bar" );
338  $expected[] = [ "string", "33 bar" ];
339 
340  // compute expected sortkey values
341  if ( $wgPagePropsHaveSortkey ) {
342  $fields[] = 'pp_sortkey';
343 
344  foreach ( $expected as &$row ) {
345  $value = $row[1];
346 
347  if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
348  $row[] = floatval( $value );
349  } else {
350  $row[] = null;
351  }
352  }
353  }
354 
355  $this->assertLinksUpdate(
356  $t, $po, 'page_props', $fields, 'pp_page = ' . self::$testingPageId, $expected );
357  }
358 
360  $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
361 
362  $this->testUpdate_page_props();
363  }
364 
365  // @todo test recursive, too!
366 
368  $table, $fields, $condition, array $expectedRows
369  ) {
370  $update = new LinksUpdate( $title, $parserOutput );
371 
372  // NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
373  $update->beginTransaction();
374  $update->doUpdate();
375  $update->commitTransaction();
376 
377  $this->assertSelect( $table, $fields, $condition, $expectedRows );
378  return $update;
379  }
380 
382  Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows
383  ) {
384  $this->assertSelect(
385  'recentchanges',
386  'rc_title, rc_comment',
387  [
388  'rc_type' => RC_CATEGORIZE,
389  'rc_namespace' => NS_CATEGORY,
390  'rc_title' => $categoryTitle->getDBkey()
391  ],
392  $expectedRows
393  );
394  }
395 
396  private function runAllRelatedJobs() {
397  $queueGroup = JobQueueGroup::singleton();
398  while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
399  $job->run();
400  $queueGroup->ack( $job );
401  }
402  while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
403  $job->run();
404  $queueGroup->ack( $job );
405  }
406  }
407 }
testUpdate_page_props_without_sortkey()
const RC_CATEGORIZE
Definition: Defines.php:173
makeTitleAndParserOutput($name, $id)
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
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
the array() calling protocol came about after MediaWiki 1.4rc1.
$wgPagePropsHaveSortkey
Whether the page_props table has a pp_sortkey column.
const NS_MAIN
Definition: Defines.php:69
Class the manages updates of *_link tables as well as similar extension-managed tables.
Definition: LinksUpdate.php:30
Set options of the Parser.
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
insertPage($pageName, $text= 'Sample page for unit test.')
Insert a new page.
$value
testUpdate_iwlinks()
ParserOutput::addInterwikiLink.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:256
Represents a title within MediaWiki.
Definition: Title.php:36
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
testUpdate_imagelinks()
ParserOutput::addImage.
const NS_TEMPLATE
Definition: Defines.php:79
testUpdate_pagelinks()
ParserOutput::addLink.
testUpdate_page_props()
ParserOutput::setProperty.
testUpdate_externallinks()
ParserOutput::addExternalLink.
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:1816
Some quick notes on the file repository architecture Functionality is
Definition: README:3
testUpdate_templatelinks()
ParserOutput::addTemplate.
getDBkey()
Get the main part with underscores.
Definition: Title.php:890
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context $parserOutput
Definition: hooks.txt:1020
$res
Definition: database.txt:21
assertSelect($table, $fields, $condition, array $expectedRows)
Asserts that the given database query yields the rows given by $expectedRows.
Base class that store and restore the Language objects.
const NS_CATEGORY
Definition: Defines.php:83
LinksUpdate Database ^— make sure temporary tables are used.
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:527
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead.&$feedLinks conditions will AND in the final query as a Content object as a Content object $title
Definition: hooks.txt:312
testUpdate_langlinks()
ParserOutput::addLanguageLink.
Content object for wiki text pages.
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()
static singleton($wiki=false)
Class representing a MediaWiki article and history.
Definition: WikiPage.php:31
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
if(count($args)< 1) $job
assertRecentChangeByCategorization(Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows)
assertLinksUpdate(Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows)
__construct($name=null, array $data=[], $dataName= '')
const DB_MASTER
Definition: Defines.php:47
const NS_TALK
Definition: Defines.php:70
getParserOutput(Title $title, $revId=null, ParserOptions $options=null, $generateHtml=true)
Parse the Content object and generate a ParserOutput from the result.
testUpdate_categorylinks()
ParserOutput::addCategory.
setMwGlobals($pairs, $value=null)
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:503
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310