MediaWiki  1.23.8
WikitextContentTest.php
Go to the documentation of this file.
1 <?php
2 
10  static $sections = "Intro
11 
12 == stuff ==
13 hello world
14 
15 == test ==
16 just a test
17 
18 == foo ==
19 more stuff
20 ";
21 
22  public function newContent( $text ) {
23  return new WikitextContent( $text );
24  }
25 
26  public static function dataGetParserOutput() {
27  return array(
28  array(
29  "WikitextContentTest_testGetParserOutput",
31  "hello ''world''\n",
32  "<p>hello <i>world</i>\n</p>"
33  ),
34  // TODO: more...?
35  );
36  }
37 
38  public static function dataGetSecondaryDataUpdates() {
39  return array(
40  array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
41  CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
42  array(
43  'LinksUpdate' => array(
44  'mRecursive' => true,
45  'mLinks' => array()
46  )
47  )
48  ),
49  array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
50  CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
51  array(
52  'LinksUpdate' => array(
53  'mRecursive' => true,
54  'mLinks' => array(
55  array( 'World_test_21344' => 0 )
56  )
57  )
58  )
59  ),
60  // TODO: more...?
61  );
62  }
63 
69  public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
70  $ns = $this->getDefaultWikitextNS();
72 
73  $content = ContentHandler::makeContent( $text, $title, $model );
74 
75  $page = WikiPage::factory( $title );
76  $page->doEditContent( $content, '' );
77 
78  $updates = $content->getSecondaryDataUpdates( $title );
79 
80  // make updates accessible by class name
81  foreach ( $updates as $update ) {
82  $class = get_class( $update );
83  $updates[$class] = $update;
84  }
85 
86  foreach ( $expectedStuff as $class => $fieldValues ) {
87  $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
88 
89  $update = $updates[$class];
90 
91  foreach ( $fieldValues as $field => $value ) {
92  $v = $update->$field; #if the field doesn't exist, just crash and burn
93  $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
94  }
95  }
96 
97  $page->doDeleteArticle( '' );
98  }
99 
100  public static function dataGetSection() {
101  return array(
102  array( WikitextContentTest::$sections,
103  "0",
104  "Intro"
105  ),
106  array( WikitextContentTest::$sections,
107  "2",
108  "== test ==
109 just a test"
110  ),
111  array( WikitextContentTest::$sections,
112  "8",
113  false
114  ),
115  );
116  }
117 
122  public function testGetSection( $text, $sectionId, $expectedText ) {
123  $content = $this->newContent( $text );
124 
125  $sectionContent = $content->getSection( $sectionId );
126  if ( is_object( $sectionContent ) ) {
127  $sectionText = $sectionContent->getNativeData();
128  } else {
129  $sectionText = $sectionContent;
130  }
131 
132  $this->assertEquals( $expectedText, $sectionText );
133  }
134 
135  public static function dataReplaceSection() {
136  return array(
137  array( WikitextContentTest::$sections,
138  "0",
139  "No more",
140  null,
141  trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
142  ),
143  array( WikitextContentTest::$sections,
144  "",
145  "No more",
146  null,
147  "No more"
148  ),
149  array( WikitextContentTest::$sections,
150  "2",
151  "== TEST ==\nmore fun",
152  null,
153  trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) )
154  ),
155  array( WikitextContentTest::$sections,
156  "8",
157  "No more",
158  null,
159  WikitextContentTest::$sections
160  ),
161  array( WikitextContentTest::$sections,
162  "new",
163  "No more",
164  "New",
165  trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
166  ),
167  );
168  }
169 
174  public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
175  $content = $this->newContent( $text );
176  $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
177 
178  $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
179  }
180 
184  public function testAddSectionHeader() {
185  $content = $this->newContent( 'hello world' );
186  $content = $content->addSectionHeader( 'test' );
187 
188  $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
189  }
190 
191  public static function dataPreSaveTransform() {
192  return array(
193  array( 'hello this is ~~~',
194  "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
195  ),
196  array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
197  'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
198  ),
199  array( // rtrim
200  " Foo \n ",
201  " Foo",
202  ),
203  );
204  }
205 
206  public static function dataPreloadTransform() {
207  return array(
208  array( 'hello this is ~~~',
209  "hello this is ~~~",
210  ),
211  array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
212  'hello \'\'this\'\' is bar',
213  ),
214  );
215  }
216 
217  public static function dataGetRedirectTarget() {
218  return array(
219  array( '#REDIRECT [[Test]]',
220  'Test',
221  ),
222  array( '#REDIRECT Test',
223  null,
224  ),
225  array( '* #REDIRECT [[Test]]',
226  null,
227  ),
228  );
229  }
230 
231  public static function dataGetTextForSummary() {
232  return array(
233  array( "hello\nworld.",
234  16,
235  'hello world.',
236  ),
237  array( 'hello world.',
238  8,
239  'hello...',
240  ),
241  array( '[[hello world]].',
242  8,
243  'hel...',
244  ),
245  );
246  }
247 
248  public static function dataIsCountable() {
249  return array(
250  array( '',
251  null,
252  'any',
253  true
254  ),
255  array( 'Foo',
256  null,
257  'any',
258  true
259  ),
260  array( 'Foo',
261  null,
262  'comma',
263  false
264  ),
265  array( 'Foo, bar',
266  null,
267  'comma',
268  true
269  ),
270  array( 'Foo',
271  null,
272  'link',
273  false
274  ),
275  array( 'Foo [[bar]]',
276  null,
277  'link',
278  true
279  ),
280  array( 'Foo',
281  true,
282  'link',
283  true
284  ),
285  array( 'Foo [[bar]]',
286  false,
287  'link',
288  false
289  ),
290  array( '#REDIRECT [[bar]]',
291  true,
292  'any',
293  false
294  ),
295  array( '#REDIRECT [[bar]]',
296  true,
297  'comma',
298  false
299  ),
300  array( '#REDIRECT [[bar]]',
301  true,
302  'link',
303  false
304  ),
305  );
306  }
307 
311  public function testMatchMagicWord() {
312  $mw = MagicWord::get( "staticredirect" );
313 
314  $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
315  $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
316 
317  $content = $this->newContent( "#REDIRECT [[FOO]]" );
318  $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
319  }
320 
324  public function testUpdateRedirect() {
325  $target = Title::newFromText( "testUpdateRedirect_target" );
326 
327  // test with non-redirect page
328  $content = $this->newContent( "hello world." );
329  $newContent = $content->updateRedirect( $target );
330 
331  $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
332 
333  // test with actual redirect
334  $content = $this->newContent( "#REDIRECT [[Someplace]]" );
335  $newContent = $content->updateRedirect( $target );
336 
337  $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
338  $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
339 
340  $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
341  }
342 
346  public function testGetModel() {
347  $content = $this->newContent( "hello world." );
348 
349  $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
350  }
351 
355  public function testGetContentHandler() {
356  $content = $this->newContent( "hello world." );
357 
358  $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
359  }
360 
361  public static function dataEquals() {
362  return array(
363  array( new WikitextContent( "hallo" ), null, false ),
364  array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
365  array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
366  array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
367  array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
368  );
369  }
370 
371  public static function dataGetDeletionUpdates() {
372  return array(
373  array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
374  CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
375  array( 'LinksDeletionUpdate' => array() )
376  ),
377  array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
378  CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
379  array( 'LinksDeletionUpdate' => array() )
380  ),
381  // @todo more...?
382  );
383  }
384 }
WikitextContentTest\testGetSecondaryDataUpdates
testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff)
@dataProvider dataGetSecondaryDataUpdates @group Database @covers WikitextContent::getSecondaryDataUp...
Definition: WikitextContentTest.php:69
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
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
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
WikitextContentTest\dataGetSecondaryDataUpdates
static dataGetSecondaryDataUpdates()
Definition: WikitextContentTest.php:38
TextContentTest
@group ContentHandler @group Database ^— needed, because we do need the database to test link updates
Definition: TextContentTest.php:8
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:283
WikitextContentTest\newContent
newContent( $text)
Definition: WikitextContentTest.php:22
WikitextContentTest\$sections
static $sections
Definition: WikitextContentTest.php:10
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
MediaWikiTestCase\getDefaultWikitextNS
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
Definition: MediaWikiTestCase.php:903
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:33
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:144
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$value
$value
Definition: styleTest.css.php:45
LinksDeletionUpdate
Update object handling the cleanup of links tables after a page was deleted.
Definition: LinksUpdate.php:871
WikitextContentTest\dataGetParserOutput
static dataGetParserOutput()
Definition: WikitextContentTest.php:26
as
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
WikitextContentTest
@group ContentHandler
Definition: WikitextContentTest.php:9