MediaWiki  1.23.13
LinksUpdateTest.php
Go to the documentation of this file.
1 <?php
2 
8 
9  function __construct( $name = null, array $data = array(), $dataName = '' ) {
10  parent::__construct( $name, $data, $dataName );
11 
12  $this->tablesUsed = array_merge( $this->tablesUsed,
13  array(
14  'interwiki',
15  'page_props',
16  'pagelinks',
17  'categorylinks',
18  'langlinks',
19  'externallinks',
20  'imagelinks',
21  'templatelinks',
22  'iwlinks'
23  )
24  );
25  }
26 
27  protected function setUp() {
28  parent::setUp();
29  $dbw = wfGetDB( DB_MASTER );
30  $dbw->replace(
31  'interwiki',
32  array( 'iw_prefix' ),
33  array(
34  'iw_prefix' => 'linksupdatetest',
35  'iw_url' => 'http://testing.com/wiki/$1',
36  'iw_api' => 'http://testing.com/w/api.php',
37  'iw_local' => 0,
38  'iw_trans' => 0,
39  'iw_wikiid' => 'linksupdatetest',
40  )
41  );
42  }
43 
44  protected function makeTitleAndParserOutput( $name, $id ) {
46  $t->mArticleID = $id; # XXX: this is fugly
47 
48  $po = new ParserOutput();
49  $po->setTitleText( $t->getPrefixedText() );
50 
51  return array( $t, $po );
52  }
53 
57  public function testUpdate_pagelinks() {
59  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
60 
61  $po->addLink( Title::newFromText( "Foo" ) );
62  $po->addLink( Title::newFromText( "Special:Foo" ) ); // special namespace should be ignored
63  $po->addLink( Title::newFromText( "linksupdatetest:Foo" ) ); // interwiki link should be ignored
64  $po->addLink( Title::newFromText( "#Foo" ) ); // hash link should be ignored
65 
66  $update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
67  array( NS_MAIN, 'Foo' ),
68  ) );
69  $this->assertArrayEquals( array(
70  Title::makeTitle( NS_MAIN, 'Foo' ), // newFromText doesn't yield the same internal state....
71  ), $update->getAddedLinks() );
72 
73  $po = new ParserOutput();
74  $po->setTitleText( $t->getPrefixedText() );
75 
76  $po->addLink( Title::newFromText( "Bar" ) );
77  $po->addLink( Title::newFromText( "Talk:Bar" ) );
78 
79  $update = $this->assertLinksUpdate( $t, $po, 'pagelinks', 'pl_namespace, pl_title', 'pl_from = 111', array(
80  array( NS_MAIN, 'Bar' ),
81  array( NS_TALK, 'Bar' ),
82  ) );
83  $this->assertArrayEquals( array(
84  Title::makeTitle( NS_MAIN, 'Bar' ),
85  Title::makeTitle( NS_TALK, 'Bar' ),
86  ), $update->getAddedLinks() );
87  $this->assertArrayEquals( array(
88  Title::makeTitle( NS_MAIN, 'Foo' ),
89  ), $update->getRemovedLinks() );
90  }
91 
95  public function testUpdate_externallinks() {
97  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
98 
99  $po->addExternalLink( "http://testing.com/wiki/Foo" );
100 
101  $this->assertLinksUpdate( $t, $po, 'externallinks', 'el_to, el_index', 'el_from = 111', array(
102  array( 'http://testing.com/wiki/Foo', 'http://com.testing./wiki/Foo' ),
103  ) );
104  }
105 
109  public function testUpdate_categorylinks() {
111  $this->setMwGlobals( 'wgCategoryCollation', 'uppercase' );
112 
113  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
114 
115  $po->addCategory( "Foo", "FOO" );
116 
117  $this->assertLinksUpdate( $t, $po, 'categorylinks', 'cl_to, cl_sortkey', 'cl_from = 111', array(
118  array( 'Foo', "FOO\nTESTING" ),
119  ) );
120  }
121 
125  public function testUpdate_iwlinks() {
127  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
128 
129  $target = Title::makeTitleSafe( NS_MAIN, "Foo", '', 'linksupdatetest' );
130  $po->addInterwikiLink( $target );
131 
132  $this->assertLinksUpdate( $t, $po, 'iwlinks', 'iwl_prefix, iwl_title', 'iwl_from = 111', array(
133  array( 'linksupdatetest', 'Foo' ),
134  ) );
135  }
136 
140  public function testUpdate_templatelinks() {
142  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
143 
144  $po->addTemplate( Title::newFromText( "Template:Foo" ), 23, 42 );
145 
146  $this->assertLinksUpdate( $t, $po, 'templatelinks', 'tl_namespace, tl_title', 'tl_from = 111', array(
147  array( NS_TEMPLATE, 'Foo' ),
148  ) );
149  }
150 
154  public function testUpdate_imagelinks() {
156  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
157 
158  $po->addImage( "Foo.png" );
159 
160  $this->assertLinksUpdate( $t, $po, 'imagelinks', 'il_to', 'il_from = 111', array(
161  array( 'Foo.png' ),
162  ) );
163  }
164 
168  public function testUpdate_langlinks() {
170  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
171 
172  $po->addLanguageLink( Title::newFromText( "en:Foo" )->getFullText() );
173 
174  $this->assertLinksUpdate( $t, $po, 'langlinks', 'll_lang, ll_title', 'll_from = 111', array(
175  array( 'En', 'Foo' ),
176  ) );
177  }
178 
182  public function testUpdate_page_props() {
184  list( $t, $po ) = $this->makeTitleAndParserOutput( "Testing", 111 );
185 
186  $po->setProperty( "foo", "bar" );
187 
188  $this->assertLinksUpdate( $t, $po, 'page_props', 'pp_propname, pp_value', 'pp_page = 111', array(
189  array( 'foo', 'bar' ),
190  ) );
191  }
192 
193  // @todo test recursive, too!
194 
195  protected function assertLinksUpdate( Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows ) {
196  $update = new LinksUpdate( $title, $parserOutput );
197 
198  //NOTE: make sure LinksUpdate does not generate warnings when called inside a transaction.
199  $update->beginTransaction();
200  $update->doUpdate();
201  $update->commitTransaction();
202 
203  $this->assertSelect( $table, $fields, $condition, $expectedRows );
204  return $update;
205  }
206 }
LinksUpdateTest\setUp
setUp()
Definition: LinksUpdateTest.php:27
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
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:189
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:764
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
ParserOutput
Definition: ParserOutput.php:24
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LinksUpdateTest\testUpdate_templatelinks
testUpdate_templatelinks()
@covers ParserOutput::addTemplate
Definition: LinksUpdateTest.php:140
is
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
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3706
LinksUpdateTest\testUpdate_iwlinks
testUpdate_iwlinks()
@covers ParserOutput::addInterwikiLink
Definition: LinksUpdateTest.php:125
LinksUpdateTest\testUpdate_pagelinks
testUpdate_pagelinks()
@covers ParserOutput::addLink
Definition: LinksUpdateTest.php:57
LinksUpdate
See docs/deferred.txt.
Definition: LinksUpdate.php:28
NS_TEMPLATE
const NS_TEMPLATE
Definition: Defines.php:89
LinksUpdateTest\testUpdate_externallinks
testUpdate_externallinks()
@covers ParserOutput::addExternalLink
Definition: LinksUpdateTest.php:95
LinksUpdateTest\__construct
__construct( $name=null, array $data=array(), $dataName='')
Definition: LinksUpdateTest.php:9
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
LinksUpdateTest\testUpdate_page_props
testUpdate_page_props()
@covers ParserOutput::setProperty
Definition: LinksUpdateTest.php:182
MediaWikiTestCase\assertSelect
assertSelect( $table, $fields, $condition, array $expectedRows)
Asserts that the given database query yields the rows given by $expectedRows.
Definition: MediaWikiTestCase.php:703
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
LinksUpdateTest\testUpdate_langlinks
testUpdate_langlinks()
@covers ParserOutput::addLanguageLink
Definition: LinksUpdateTest.php:168
list
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
LinksUpdateTest\makeTitleAndParserOutput
makeTitleAndParserOutput( $name, $id)
Definition: LinksUpdateTest.php:44
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
LinksUpdateTest\assertLinksUpdate
assertLinksUpdate(Title $title, ParserOutput $parserOutput, $table, $fields, $condition, array $expectedRows)
Definition: LinksUpdateTest.php:195
LinksUpdateTest\testUpdate_categorylinks
testUpdate_categorylinks()
@covers ParserOutput::addCategory
Definition: LinksUpdateTest.php:109
Title
Represents a title within MediaWiki.
Definition: Title.php:35
LinksUpdateTest
@group Database ^— make sure temporary tables are used.
Definition: LinksUpdateTest.php:7
NS_TALK
const NS_TALK
Definition: Defines.php:80
$t
$t
Definition: testCompression.php:65
LinksUpdateTest\testUpdate_imagelinks
testUpdate_imagelinks()
@covers ParserOutput::addImage
Definition: LinksUpdateTest.php:154