MediaWiki REL1_31
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 ) {
57 $t = Title::newFromText( $name );
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
122 public function testUpdate_externallinks() {
124 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
125
126 $po->addExternalLink( "http://testing.com/wiki/Foo" );
127
128 $this->assertLinksUpdate(
129 $t,
130 $po,
131 'externallinks',
132 'el_to, el_index',
133 'el_from = ' . self::$testingPageId,
134 [
135 [ 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ],
136 ]
137 );
138 }
139
143 public function testUpdate_categorylinks() {
145 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
146
147 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
148
149 $po->addCategory( "Foo", "FOO" );
150
151 $this->assertLinksUpdate(
152 $t,
153 $po,
154 'categorylinks',
155 'cl_to, cl_sortkey',
156 'cl_from = ' . self::$testingPageId,
157 [ [ 'Foo', "FOO\nTESTING" ] ]
158 );
159 }
160
162 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
163
164 $title = Title::newFromText( 'Testing' );
165 $wikiPage = new WikiPage( $title );
166 $wikiPage->doEditContent( new WikitextContent( '[[Category:Foo]]' ), 'added category' );
167 $this->runAllRelatedJobs();
168
170 $title,
171 $wikiPage->getParserOutput( ParserOptions::newCanonical() ),
172 Title::newFromText( 'Category:Foo' ),
173 [ [ 'Foo', '[[:Testing]] added to category' ] ]
174 );
175
176 $wikiPage->doEditContent( new WikitextContent( '[[Category:Bar]]' ), 'replaced category' );
177 $this->runAllRelatedJobs();
178
180 $title,
181 $wikiPage->getParserOutput( ParserOptions::newCanonical() ),
182 Title::newFromText( 'Category:Foo' ),
183 [
184 [ 'Foo', '[[:Testing]] added to category' ],
185 [ 'Foo', '[[:Testing]] removed from category' ],
186 ]
187 );
188
190 $title,
191 $wikiPage->getParserOutput( ParserOptions::newCanonical() ),
192 Title::newFromText( 'Category:Bar' ),
193 [
194 [ 'Bar', '[[:Testing]] added to category' ],
195 ]
196 );
197 }
198
200 $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
201
202 $templateTitle = Title::newFromText( 'Template:TestingTemplate' );
203 $templatePage = new WikiPage( $templateTitle );
204
205 $wikiPage = new WikiPage( Title::newFromText( 'Testing' ) );
206 $wikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
207 $this->runAllRelatedJobs();
208
209 $otherWikiPage = new WikiPage( Title::newFromText( 'Some_other_page' ) );
210 $otherWikiPage->doEditContent( new WikitextContent( '{{TestingTemplate}}' ), 'added template' );
211 $this->runAllRelatedJobs();
212
214 $templateTitle,
215 $templatePage->getParserOutput( ParserOptions::newCanonical() ),
216 Title::newFromText( 'Baz' ),
217 []
218 );
219
220 $templatePage->doEditContent( new WikitextContent( '[[Category:Baz]]' ), 'added category' );
221 $this->runAllRelatedJobs();
222
224 $templateTitle,
225 $templatePage->getParserOutput( ParserOptions::newCanonical() ),
226 Title::newFromText( 'Baz' ),
227 [ [
228 'Baz',
229 '[[:Template:TestingTemplate]] added to category, ' .
230 '[[Special:WhatLinksHere/Template:TestingTemplate|this page is included within other pages]]'
231 ] ]
232 );
233 }
234
238 public function testUpdate_iwlinks() {
240 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
241
242 $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
243 $po->addInterwikiLink( $target );
244
245 $this->assertLinksUpdate(
246 $t,
247 $po,
248 'iwlinks',
249 'iwl_prefix, iwl_title',
250 'iwl_from = ' . self::$testingPageId,
251 [ [ 'linksupdatetest', 'Foo' ] ]
252 );
253 }
254
258 public function testUpdate_templatelinks() {
260 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
261
262 $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
263
264 $this->assertLinksUpdate(
265 $t,
266 $po,
267 'templatelinks',
268 'tl_namespace,
269 tl_title',
270 'tl_from = ' . self::$testingPageId,
271 [ [ NS_TEMPLATE, 'Foo' ] ]
272 );
273 }
274
278 public function testUpdate_imagelinks() {
280 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
281
282 $po->addImage( "Foo.png" );
283
284 $this->assertLinksUpdate(
285 $t,
286 $po,
287 'imagelinks',
288 'il_to',
289 'il_from = ' . self::$testingPageId,
290 [ [ 'Foo.png' ] ]
291 );
292 }
293
297 public function testUpdate_langlinks() {
298 $this->setMwGlobals( [
299 'wgCapitalLinks' => true,
300 ] );
301
303 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
304
305 $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
306
307 $this->assertLinksUpdate(
308 $t,
309 $po,
310 'langlinks',
311 'll_lang, ll_title',
312 'll_from = ' . self::$testingPageId,
313 [ [ 'En', 'Foo' ] ]
314 );
315 }
316
320 public function testUpdate_page_props() {
322
324 list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", self::$testingPageId );
325
326 $fields = [ 'pp_propname', 'pp_value' ];
327 $expected = [];
328
329 $po->setProperty( "bool", true );
330 $expected[] = [ "bool", true ];
331
332 $po->setProperty( "float", 4.0 + 1.0 / 4.0 );
333 $expected[] = [ "float", 4.0 + 1.0 / 4.0 ];
334
335 $po->setProperty( "int", -7 );
336 $expected[] = [ "int", -7 ];
337
338 $po->setProperty( "string", "33 bar" );
339 $expected[] = [ "string", "33 bar" ];
340
341 // compute expected sortkey values
343 $fields[] = 'pp_sortkey';
344
345 foreach ( $expected as &$row ) {
346 $value = $row[1];
347
348 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
349 $row[] = floatval( $value );
350 } else {
351 $row[] = null;
352 }
353 }
354 }
355
356 $this->assertLinksUpdate(
357 $t, $po, 'page_props', $fields, 'pp_page = ' . self::$testingPageId, $expected );
358 }
359
361 $this->setMwGlobals( 'wgPagePropsHaveSortkey', false );
362
363 $this->testUpdate_page_props();
364 }
365
366 // @todo test recursive, too!
367
368 protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput,
369 $table, $fields, $condition, array $expectedRows
370 ) {
371 $update = new LinksUpdate( $title, $parserOutput );
372
373 $update->doUpdate();
374
375 $this->assertSelect( $table, $fields, $condition, $expectedRows );
376 return $update;
377 }
378
380 Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows
381 ) {
383
385 $this->assertSelect(
386 'recentchanges',
387 'rc_title, rc_comment',
388 [
389 'rc_type' => RC_CATEGORIZE,
390 'rc_namespace' => NS_CATEGORY,
391 'rc_title' => $categoryTitle->getDBkey()
392 ],
393 $expectedRows
394 );
395 }
397 $this->assertSelect(
398 [ 'recentchanges', 'comment' ],
399 'rc_title, comment_text',
400 [
401 'rc_type' => RC_CATEGORIZE,
402 'rc_namespace' => NS_CATEGORY,
403 'rc_title' => $categoryTitle->getDBkey(),
404 'comment_id = rc_comment_id',
405 ],
406 $expectedRows
407 );
408 }
409 }
410
411 private function runAllRelatedJobs() {
412 $queueGroup = JobQueueGroup::singleton();
413 while ( $job = $queueGroup->pop( 'refreshLinksPrioritized' ) ) {
414 $job->run();
415 $queueGroup->ack( $job );
416 }
417 while ( $job = $queueGroup->pop( 'categoryMembershipChange' ) ) {
418 $job->run();
419 $queueGroup->ack( $job );
420 }
421 }
422}
int $wgCommentTableSchemaMigrationStage
Comment table schema migration stage.
$wgPagePropsHaveSortkey
Whether the page_props table has a pp_sortkey column.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static singleton( $domain=false)
LinksUpdate LinksUpdate Database ^— make sure temporary tables are used.
assertLinksUpdate(Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows)
testOnAddingAndRemovingCategoryToTemplates_embeddingPagesAreIgnored()
testUpdate_langlinks()
ParserOutput::addLanguageLink.
testOnAddingAndRemovingCategory_recentChangesRowIsAdded()
testUpdate_externallinks()
ParserOutput::addExternalLink.
testUpdate_iwlinks()
ParserOutput::addInterwikiLink.
testUpdate_page_props()
ParserOutput::setProperty.
__construct( $name=null, array $data=[], $dataName='')
makeTitleAndParserOutput( $name, $id)
testUpdate_templatelinks()
ParserOutput::addTemplate.
testUpdate_categorylinks()
ParserOutput::addCategory.
testUpdate_imagelinks()
ParserOutput::addImage.
assertRecentChangeByCategorization(Title $pageTitle, ParserOutput $parserOutput, Title $categoryTitle, $expectedRows)
testUpdate_pagelinks()
ParserOutput::addLink.
Class the manages updates of *_link tables as well as similar extension-managed tables.
Base class that store and restore the Language objects.
insertPage( $pageName, $text='Sample page for unit test.', $namespace=null)
Insert a new page.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
assertSelect( $table, $fields, $condition, array $expectedRows, array $options=[], array $join_conds=[])
Asserts that the given database query yields the rows given by $expectedRows.
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Represents a title within MediaWiki.
Definition Title.php:39
getDBkey()
Get the main part with underscores.
Definition Title.php:947
Class representing a MediaWiki article and history.
Definition WikiPage.php:37
Content object for wiki text pages.
$res
Definition database.txt:21
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like please read the documentation for except in special pages derived from QueryPage It s a common pitfall for new developers to submit code containing SQL queries which examine huge numbers of rows Remember that COUNT * is(N), counting rows in atable is like counting beans in a bucket.------------------------------------------------------------------------ Replication------------------------------------------------------------------------The largest installation of MediaWiki, Wikimedia, uses a large set ofslave MySQL servers replicating writes made to a master MySQL server. Itis important to understand the issues associated with this setup if youwant to write code destined for Wikipedia.It 's often the case that the best algorithm to use for a given taskdepends on whether or not replication is in use. Due to our unabashedWikipedia-centrism, we often just use the replication-friendly version, but if you like, you can use wfGetLB() ->getServerCount() > 1 tocheck to see if replication is in use.===Lag===Lag primarily occurs when large write queries are sent to the master.Writes on the master are executed in parallel, but they are executed inserial when they are replicated to the slaves. The master writes thequery to the binlog when the transaction is committed. The slaves pollthe binlog and start executing the query as soon as it appears. They canservice reads while they are performing a write query, but will not readanything more from the binlog and thus will perform no more writes. Thismeans that if the write query runs for a long time, the slaves will lagbehind the master for the time it takes for the write query to complete.Lag can be exacerbated by high read load. MediaWiki 's load balancer willstop sending reads to a slave when it is lagged by more than 30 seconds.If the load ratios are set incorrectly, or if there is too much loadgenerally, this may lead to a slave permanently hovering around 30seconds lag.If all slaves are lagged by more than 30 seconds, MediaWiki will stopwriting to the database. All edits and other write operations will berefused, with an error returned to the user. This gives the slaves achance to catch up. Before we had this mechanism, the slaves wouldregularly lag by several minutes, making review of recent editsdifficult.In addition to this, MediaWiki attempts to ensure that the user seesevents occurring on the wiki in chronological order. A few seconds of lagcan be tolerated, as long as the user sees a consistent picture fromsubsequent requests. This is done by saving the master binlog positionin the session, and then at the start of each request, waiting for theslave to catch up to that position before doing any reads from it. Ifthis wait times out, reads are allowed anyway, but the request isconsidered to be in "lagged slave mode". Lagged slave mode can bechecked by calling wfGetLB() ->getLaggedSlaveMode(). The onlypractical consequence at present is a warning displayed in the pagefooter.===Lag avoidance===To avoid excessive lag, queries which write large numbers of rows shouldbe split up, generally to write one row at a time. Multi-row INSERT ...SELECT queries are the worst offenders should be avoided altogether.Instead do the select first and then the insert.===Working with lag===Despite our best efforts, it 's not practical to guarantee a low-lagenvironment. Lag will usually be less than one second, but mayoccasionally be up to 30 seconds. For scalability, it 's very importantto keep load on the master low, so simply sending all your queries tothe master is not the answer. So when you have a genuine need forup-to-date data, the following approach is advised:1) Do a quick query to the master for a sequence number or timestamp 2) Run the full query on the slave and check if it matches the data you gotfrom the master 3) If it doesn 't, run the full query on the masterTo avoid swamping the master every time the slaves lag, use of thisapproach should be kept to a minimum. In most cases you should just readfrom the slave and let the user deal with the delay.------------------------------------------------------------------------ Lock contention------------------------------------------------------------------------Due to the high write rate on Wikipedia(and some other wikis), MediaWiki developers need to be very careful to structure their writesto avoid long-lasting locks. By default, MediaWiki opens a transactionat the first query, and commits it before the output is sent. Locks willbe held from the time when the query is done until the commit. So youcan reduce lock time by doing as much processing as possible before youdo your write queries.Often this approach is not good enough, and it becomes necessary toenclose small groups of queries in their own transaction. Use thefollowing syntax:$dbw=wfGetDB(DB_MASTER
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
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
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
const NS_MAIN
Definition Defines.php:74
const NS_TEMPLATE
Definition Defines.php:84
const NS_TALK
Definition Defines.php:75
const MIGRATION_WRITE_BOTH
Definition Defines.php:303
const NS_CATEGORY
Definition Defines.php:88
const RC_CATEGORIZE
Definition Defines.php:156
the array() calling protocol came about after MediaWiki 1.4rc1.
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
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
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:37
const DB_MASTER
Definition defines.php:29
if(count( $args)< 1) $job