MediaWiki  1.30.0
TitleTest.php
Go to the documentation of this file.
1 <?php
2 
7 class TitleTest extends MediaWikiTestCase {
8  protected function setUp() {
9  parent::setUp();
10 
11  $this->setMwGlobals( [
12  'wgAllowUserJs' => false,
13  'wgDefaultLanguageVariant' => false,
14  'wgMetaNamespace' => 'Project',
15  ] );
16  $this->setUserLang( 'en' );
17  $this->setContentLang( 'en' );
18  }
19 
23  public function testLegalChars() {
24  $titlechars = Title::legalChars();
25 
26  foreach ( range( 1, 255 ) as $num ) {
27  $chr = chr( $num );
28  if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
29  $this->assertFalse(
30  (bool)preg_match( "/[$titlechars]/", $chr ),
31  "chr($num) = $chr is not a valid titlechar"
32  );
33  } else {
34  $this->assertTrue(
35  (bool)preg_match( "/[$titlechars]/", $chr ),
36  "chr($num) = $chr is a valid titlechar"
37  );
38  }
39  }
40  }
41 
42  public static function provideValidSecureAndSplit() {
43  return [
44  [ 'Sandbox' ],
45  [ 'A "B"' ],
46  [ 'A \'B\'' ],
47  [ '.com' ],
48  [ '~' ],
49  [ '#' ],
50  [ '"' ],
51  [ '\'' ],
52  [ 'Talk:Sandbox' ],
53  [ 'Talk:Foo:Sandbox' ],
54  [ 'File:Example.svg' ],
55  [ 'File_talk:Example.svg' ],
56  [ 'Foo/.../Sandbox' ],
57  [ 'Sandbox/...' ],
58  [ 'A~~' ],
59  [ ':A' ],
60  // Length is 256 total, but only title part matters
61  [ 'Category:' . str_repeat( 'x', 248 ) ],
62  [ str_repeat( 'x', 252 ) ],
63  // interwiki prefix
64  [ 'localtestiw: #anchor' ],
65  [ 'localtestiw:' ],
66  [ 'localtestiw:foo' ],
67  [ 'localtestiw: foo # anchor' ],
68  [ 'localtestiw: Talk: Sandbox # anchor' ],
69  [ 'remotetestiw:' ],
70  [ 'remotetestiw: Talk: # anchor' ],
71  [ 'remotetestiw: #bar' ],
72  [ 'remotetestiw: Talk:' ],
73  [ 'remotetestiw: Talk: Foo' ],
74  [ 'localtestiw:remotetestiw:' ],
75  [ 'localtestiw:remotetestiw:foo' ]
76  ];
77  }
78 
79  public static function provideInvalidSecureAndSplit() {
80  return [
81  [ '', 'title-invalid-empty' ],
82  [ ':', 'title-invalid-empty' ],
83  [ '__ __', 'title-invalid-empty' ],
84  [ ' __ ', 'title-invalid-empty' ],
85  // Bad characters forbidden regardless of wgLegalTitleChars
86  [ 'A [ B', 'title-invalid-characters' ],
87  [ 'A ] B', 'title-invalid-characters' ],
88  [ 'A { B', 'title-invalid-characters' ],
89  [ 'A } B', 'title-invalid-characters' ],
90  [ 'A < B', 'title-invalid-characters' ],
91  [ 'A > B', 'title-invalid-characters' ],
92  [ 'A | B', 'title-invalid-characters' ],
93  [ "A \t B", 'title-invalid-characters' ],
94  [ "A \n B", 'title-invalid-characters' ],
95  // URL encoding
96  [ 'A%20B', 'title-invalid-characters' ],
97  [ 'A%23B', 'title-invalid-characters' ],
98  [ 'A%2523B', 'title-invalid-characters' ],
99  // XML/HTML character entity references
100  // Note: Commented out because they are not marked invalid by the PHP test as
101  // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
102  // 'A &eacute; B',
103  // 'A &#233; B',
104  // 'A &#x00E9; B',
105  // Subject of NS_TALK does not roundtrip to NS_MAIN
106  [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
107  // Directory navigation
108  [ '.', 'title-invalid-relative' ],
109  [ '..', 'title-invalid-relative' ],
110  [ './Sandbox', 'title-invalid-relative' ],
111  [ '../Sandbox', 'title-invalid-relative' ],
112  [ 'Foo/./Sandbox', 'title-invalid-relative' ],
113  [ 'Foo/../Sandbox', 'title-invalid-relative' ],
114  [ 'Sandbox/.', 'title-invalid-relative' ],
115  [ 'Sandbox/..', 'title-invalid-relative' ],
116  // Tilde
117  [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
118  [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
119  [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
120  // Length
121  [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
122  // Namespace prefix without actual title
123  [ 'Talk:', 'title-invalid-empty' ],
124  [ 'Talk:#', 'title-invalid-empty' ],
125  [ 'Category: ', 'title-invalid-empty' ],
126  [ 'Category: #bar', 'title-invalid-empty' ],
127  // interwiki prefix
128  [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
129  [ 'localtestiw: Talk:', 'title-invalid-empty' ]
130  ];
131  }
132 
133  private function secureAndSplitGlobals() {
134  $this->setMwGlobals( [
135  'wgLocalInterwikis' => [ 'localtestiw' ],
136  'wgHooks' => [
137  'InterwikiLoadPrefix' => [
138  function ( $prefix, &$data ) {
139  if ( $prefix === 'localtestiw' ) {
140  $data = [ 'iw_url' => 'localtestiw' ];
141  } elseif ( $prefix === 'remotetestiw' ) {
142  $data = [ 'iw_url' => 'remotetestiw' ];
143  }
144  return false;
145  }
146  ]
147  ]
148  ] );
149 
150  // Reset TitleParser since we modified $wgLocalInterwikis
151  $this->setService( 'TitleParser', new MediaWikiTitleCodec(
152  Language::factory( 'en' ),
153  new GenderCache(),
154  [ 'localtestiw' ]
155  ) );
156  }
157 
164  public function testSecureAndSplitValid( $text ) {
165  $this->secureAndSplitGlobals();
166  $this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" );
167  }
168 
175  public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
176  $this->secureAndSplitGlobals();
177  try {
178  Title::newFromTextThrow( $text ); // should throw
179  $this->assertTrue( false, "Invalid: $text" );
180  } catch ( MalformedTitleException $ex ) {
181  $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
182  }
183  }
184 
185  public static function provideConvertByteClassToUnicodeClass() {
186  return [
187  [
188  ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
189  ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
190  ],
191  [
192  'QWERTYf-\\xFF+',
193  'QWERTYf-\\x7F+\\u0080-\\uFFFF',
194  ],
195  [
196  'QWERTY\\x66-\\xFD+',
197  'QWERTYf-\\x7F+\\u0080-\\uFFFF',
198  ],
199  [
200  'QWERTYf-y+',
201  'QWERTYf-y+',
202  ],
203  [
204  'QWERTYf-\\x80+',
205  'QWERTYf-\\x7F+\\u0080-\\uFFFF',
206  ],
207  [
208  'QWERTY\\x66-\\x80+\\x23',
209  'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
210  ],
211  [
212  'QWERTY\\x66-\\x80+\\xD3',
213  'QWERTYf-\\x7F+\\u0080-\\uFFFF',
214  ],
215  [
216  '\\\\\\x99',
217  '\\\\\\u0080-\\uFFFF',
218  ],
219  [
220  '-\\x99',
221  '\\-\\u0080-\\uFFFF',
222  ],
223  [
224  'QWERTY\\-\\x99',
225  'QWERTY\\-\\u0080-\\uFFFF',
226  ],
227  [
228  '\\\\x99',
229  '\\\\x99',
230  ],
231  [
232  'A-\\x9F',
233  'A-\\x7F\\u0080-\\uFFFF',
234  ],
235  [
236  '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
237  'f-wQWERTYFXZ\\u0080-\\uFFFF',
238  ],
239  [
240  '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
241  'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
242  ],
243  ];
244  }
245 
250  public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
251  $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
252  }
253 
258  public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
259  $title = Title::newFromText( $text );
260  $fixed = $title->fixSpecialName();
261  $stuff = explode( '/', $fixed->getDBkey(), 2 );
262  if ( count( $stuff ) == 2 ) {
263  $par = $stuff[1];
264  } else {
265  $par = null;
266  }
267  $this->assertEquals(
268  $expectedParam,
269  $par,
270  "T33100 regression check: Title->fixSpecialName() should preserve parameter"
271  );
272  }
273 
275  return [
276  [ 'Special:Version', null ],
277  [ 'Special:Version/', '' ],
278  [ 'Special:Version/param', 'param' ],
279  ];
280  }
281 
293  public function testIsValidMoveOperation( $source, $target, $expected ) {
294  $this->setMwGlobals( 'wgContentHandlerUseDB', false );
296  $nt = Title::newFromText( $target );
297  $errors = $title->isValidMoveOperation( $nt, false );
298  if ( $expected === true ) {
299  $this->assertTrue( $errors );
300  } else {
301  $errors = $this->flattenErrorsArray( $errors );
302  foreach ( (array)$expected as $error ) {
303  $this->assertContains( $error, $errors );
304  }
305  }
306  }
307 
308  public static function provideTestIsValidMoveOperation() {
309  return [
310  // for Title::isValidMoveOperation
311  [ 'Some page', '', 'badtitletext' ],
312  [ 'Test', 'Test', 'selfmove' ],
313  [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
314  [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
315  [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
316  [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
317  // for Title::validateFileMoveOperation
318  [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
319  ];
320  }
321 
333  public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
334  // $wgWhitelistReadRegexp must be an array. Since the provided test cases
335  // usually have only one regex, it is more concise to write the lonely regex
336  // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
337  // type requisite.
338  if ( is_string( $whitelistRegexp ) ) {
339  $whitelistRegexp = [ $whitelistRegexp ];
340  }
341 
342  $this->setMwGlobals( [
343  // So User::isEveryoneAllowed( 'read' ) === false
344  'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
345  'wgWhitelistRead' => [ 'some random non sense title' ],
346  'wgWhitelistReadRegexp' => $whitelistRegexp,
347  ] );
348 
350 
351  // New anonymous user with no rights
352  $user = new User;
353  $user->mRights = [];
354  $errors = $title->userCan( $action, $user );
355 
356  if ( is_bool( $expected ) ) {
357  # Forge the assertion message depending on the assertion expectation
358  $allowableness = $expected
359  ? " should be allowed"
360  : " should NOT be allowed";
361  $this->assertEquals(
362  $expected,
363  $errors,
364  "User action '$action' on [[$source]] $allowableness."
365  );
366  } else {
367  $errors = $this->flattenErrorsArray( $errors );
368  foreach ( (array)$expected as $error ) {
369  $this->assertContains( $error, $errors );
370  }
371  }
372  }
373 
377  public function dataWgWhitelistReadRegexp() {
378  $ALLOWED = true;
379  $DISALLOWED = false;
380 
381  return [
382  // Everything, if this doesn't work, we're really in trouble
383  [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
384  [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
385 
386  // We validate against the title name, not the db key
387  [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
388  // Main page
389  [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
390  [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
391  // With spaces
392  [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
393  // Unicode multibyte
394  // ...without unicode modifier
395  [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
396  // ...with unicode modifier
397  [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
398  // Case insensitive
399  [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
400  [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
401 
402  // From DefaultSettings.php:
403  [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
404  [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
405 
406  // With namespaces:
407  [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
408  [ null, 'Special:Newpages', 'read', $DISALLOWED ],
409 
410  ];
411  }
412 
413  public function flattenErrorsArray( $errors ) {
414  $result = [];
415  foreach ( $errors as $error ) {
416  $result[] = $error[0];
417  }
418 
419  return $result;
420  }
421 
426  public function testGetPageViewLanguage( $expected, $titleText, $contLang,
427  $lang, $variant, $msg = ''
428  ) {
429  // Setup environnement for this test
430  $this->setMwGlobals( [
431  'wgDefaultLanguageVariant' => $variant,
432  'wgAllowUserJs' => true,
433  ] );
434  $this->setUserLang( $lang );
435  $this->setContentLang( $contLang );
436 
437  $title = Title::newFromText( $titleText );
438  $this->assertInstanceOf( 'Title', $title,
439  "Test must be passed a valid title text, you gave '$titleText'"
440  );
441  $this->assertEquals( $expected,
442  $title->getPageViewLanguage()->getCode(),
443  $msg
444  );
445  }
446 
447  public static function provideGetPageViewLanguage() {
448  # Format:
449  # - expected
450  # - Title name
451  # - wgContLang (expected in most case)
452  # - wgLang (on some specific pages)
453  # - wgDefaultLanguageVariant
454  # - Optional message
455  return [
456  [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
457  [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
458  [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
459 
460  [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
461  [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
462  [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
463  [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
464  [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
465  [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
466  [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
467  [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
468 
469  [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
470  [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
471  [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
472  [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
473  [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
474  [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
475  [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
476  [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
477  [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
478  [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
479 
480  [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
481  [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
482 
483  ];
484  }
485 
490  public function testGetBaseText( $title, $expected, $msg = '' ) {
492  $this->assertEquals( $expected,
493  $title->getBaseText(),
494  $msg
495  );
496  }
497 
498  public static function provideBaseTitleCases() {
499  return [
500  # Title, expected base, optional message
501  [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
502  [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
503  ];
504  }
505 
510  public function testGetRootText( $title, $expected, $msg = '' ) {
512  $this->assertEquals( $expected,
513  $title->getRootText(),
514  $msg
515  );
516  }
517 
518  public static function provideRootTitleCases() {
519  return [
520  # Title, expected base, optional message
521  [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
522  [ 'User:Foo/Bar/Baz', 'Foo' ],
523  ];
524  }
525 
531  public function testGetSubpageText( $title, $expected, $msg = '' ) {
533  $this->assertEquals( $expected,
534  $title->getSubpageText(),
535  $msg
536  );
537  }
538 
539  public static function provideSubpageTitleCases() {
540  return [
541  # Title, expected base, optional message
542  [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
543  [ 'User:John_Doe/subOne', 'subOne' ],
544  ];
545  }
546 
547  public static function provideNewFromTitleValue() {
548  return [
549  [ new TitleValue( NS_MAIN, 'Foo' ) ],
550  [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
551  [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
552  ];
553  }
554 
560 
561  $dbkey = str_replace( ' ', '_', $value->getText() );
562  $this->assertEquals( $dbkey, $title->getDBkey() );
563  $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
564  $this->assertEquals( $value->getFragment(), $title->getFragment() );
565  }
566 
567  public static function provideGetTitleValue() {
568  return [
569  [ 'Foo' ],
570  [ 'Foo#bar' ],
571  [ 'User:Hansi_Maier' ],
572  ];
573  }
574 
578  public function testGetTitleValue( $text ) {
579  $title = Title::newFromText( $text );
580  $value = $title->getTitleValue();
581 
582  $dbkey = str_replace( ' ', '_', $value->getText() );
583  $this->assertEquals( $title->getDBkey(), $dbkey );
584  $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
585  $this->assertEquals( $title->getFragment(), $value->getFragment() );
586  }
587 
588  public static function provideGetFragment() {
589  return [
590  [ 'Foo', '' ],
591  [ 'Foo#bar', 'bar' ],
592  [ 'Foo#bär', 'bär' ],
593 
594  // Inner whitespace is normalized
595  [ 'Foo#bar_bar', 'bar bar' ],
596  [ 'Foo#bar bar', 'bar bar' ],
597  [ 'Foo#bar bar', 'bar bar' ],
598 
599  // Leading whitespace is kept, trailing whitespace is trimmed.
600  // XXX: Is this really want we want?
601  [ 'Foo#_bar_bar_', ' bar bar' ],
602  [ 'Foo# bar bar ', ' bar bar' ],
603  ];
604  }
605 
612  public function testGetFragment( $full, $fragment ) {
613  $title = Title::newFromText( $full );
614  $this->assertEquals( $fragment, $title->getFragment() );
615  }
616 
623  public function testIsAlwaysKnown( $page, $isKnown ) {
624  $title = Title::newFromText( $page );
625  $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
626  }
627 
628  public static function provideIsAlwaysKnown() {
629  return [
630  [ 'Some nonexistent page', false ],
631  [ 'UTPage', false ],
632  [ '#test', true ],
633  [ 'Special:BlankPage', true ],
634  [ 'Special:SomeNonexistentSpecialPage', false ],
635  [ 'MediaWiki:Parentheses', true ],
636  [ 'MediaWiki:Some nonexistent message', false ],
637  ];
638  }
639 
646  public function testIsValid( Title $title, $isValid ) {
647  $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
648  }
649 
650  public static function provideIsValid() {
651  return [
652  [ Title::makeTitle( NS_MAIN, '' ), false ],
653  [ Title::makeTitle( NS_MAIN, '<>' ), false ],
654  [ Title::makeTitle( NS_MAIN, '|' ), false ],
655  [ Title::makeTitle( NS_MAIN, '#' ), false ],
656  [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
657  [ Title::makeTitle( -33, 'Test' ), false ],
658  [ Title::makeTitle( 77663399, 'Test' ), false ],
659  ];
660  }
661 
665  public function testIsAlwaysKnownOnInterwiki() {
666  $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
667  $this->assertTrue( $title->isAlwaysKnown() );
668  }
669 
673  public function testExists() {
674  $title = Title::makeTitle( NS_PROJECT, 'New page' );
675  $linkCache = LinkCache::singleton();
676 
677  $article = new Article( $title );
678  $page = $article->getPage();
679  $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
680 
681  // Tell Title it doesn't know whether it exists
682  $title->mArticleID = -1;
683 
684  // Tell the link cache it doesn't exists when it really does
685  $linkCache->clearLink( $title );
686  $linkCache->addBadLinkObj( $title );
687 
688  $this->assertEquals(
689  false,
690  $title->exists(),
691  'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
692  );
693  $this->assertEquals(
694  true,
695  $title->exists( Title::GAID_FOR_UPDATE ),
696  'exists() should re-query database when GAID_FOR_UPDATE is used'
697  );
698  }
699 
700  public function provideCanHaveTalkPage() {
701  return [
702  'User page has talk page' => [
703  Title::makeTitle( NS_USER, 'Jane' ), true
704  ],
705  'Talke page has talk page' => [
706  Title::makeTitle( NS_TALK, 'Foo' ), true
707  ],
708  'Special page cannot have talk page' => [
709  Title::makeTitle( NS_SPECIAL, 'Thing' ), false
710  ],
711  'Virtual namespace cannot have talk page' => [
712  Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
713  ],
714  ];
715  }
716 
724  public function testCanHaveTalkPage( Title $title, $expected ) {
725  $actual = $title->canHaveTalkPage();
726  $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
727  }
728 
736  public function testCanTalk( Title $title, $expected ) {
737  $actual = $title->canTalk();
738  $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
739  }
740 
741  public static function provideGetTalkPage_good() {
742  return [
743  [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
744  [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
745  ];
746  }
747 
752  public function testGetTalkPage_good( Title $title, Title $expected ) {
753  $talk = $title->getTalkPage();
754  $this->assertSame(
755  $expected->getPrefixedDBKey(),
756  $talk->getPrefixedDBKey(),
757  $title->getPrefixedDBKey()
758  );
759  }
760 
766  $talk = $title->getTalkPageIfDefined();
767  $this->assertInstanceOf(
768  Title::class,
769  $talk,
770  $title->getPrefixedDBKey()
771  );
772  }
773 
774  public static function provideGetTalkPage_bad() {
775  return [
776  [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
777  [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
778  ];
779  }
780 
786  $talk = $title->getTalkPageIfDefined();
787  $this->assertNull(
788  $talk,
789  $title->getPrefixedDBKey()
790  );
791  }
792 
793  public function provideCreateFragmentTitle() {
794  return [
795  [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
796  [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
797  [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
798  [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
799  ];
800  }
801 
806  public function testCreateFragmentTitle( Title $title, $fragment ) {
807  $this->mergeMwGlobalArrayValue( 'wgHooks', [
808  'InterwikiLoadPrefix' => [
809  function ( $prefix, &$iwdata ) {
810  if ( $prefix === 'interwiki' ) {
811  $iwdata = [
812  'iw_url' => 'http://example.com/',
813  'iw_local' => 0,
814  'iw_trans' => 0,
815  ];
816  return false;
817  }
818  },
819  ],
820  ] );
821 
822  $fragmentTitle = $title->createFragmentTarget( $fragment );
823 
824  $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
825  $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
826  $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
827  $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
828  }
829 
830  public function provideGetPrefixedText() {
831  return [
832  // ns = 0
833  [
834  Title::makeTitle( NS_MAIN, 'Foo bar' ),
835  'Foo bar'
836  ],
837  // ns = 2
838  [
839  Title::makeTitle( NS_USER, 'Foo bar' ),
840  'User:Foo bar'
841  ],
842  // ns = 3
843  [
844  Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
845  'User talk:Foo bar'
846  ],
847  // fragment not included
848  [
849  Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
850  'Foo bar'
851  ],
852  // ns = -2
853  [
854  Title::makeTitle( NS_MEDIA, 'Foo bar' ),
855  'Media:Foo bar'
856  ],
857  // non-existent namespace
858  [
859  Title::makeTitle( 100777, 'Foo bar' ),
860  'Special:Badtitle/NS100777:Foo bar'
861  ],
862  ];
863  }
864 
869  public function testGetPrefixedText( Title $title, $expected ) {
870  $this->assertEquals( $expected, $title->getPrefixedText() );
871  }
872 
873  public function provideGetPrefixedDBKey() {
874  return [
875  // ns = 0
876  [
877  Title::makeTitle( NS_MAIN, 'Foo_bar' ),
878  'Foo_bar'
879  ],
880  // ns = 2
881  [
882  Title::makeTitle( NS_USER, 'Foo_bar' ),
883  'User:Foo_bar'
884  ],
885  // ns = 3
886  [
887  Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
888  'User_talk:Foo_bar'
889  ],
890  // fragment not included
891  [
892  Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
893  'Foo_bar'
894  ],
895  // ns = -2
896  [
897  Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
898  'Media:Foo_bar'
899  ],
900  // non-existent namespace
901  [
902  Title::makeTitle( 100777, 'Foo_bar' ),
903  'Special:Badtitle/NS100777:Foo_bar'
904  ],
905  ];
906  }
907 
912  public function testGetPrefixedDBKey( Title $title, $expected ) {
913  $this->assertEquals( $expected, $title->getPrefixedDBkey() );
914  }
915 }
TitleTest\provideGetPrefixedText
provideGetPrefixedText()
Definition: TitleTest.php:830
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
TitleTest\provideSpecialNamesWithAndWithoutParameter
static provideSpecialNamesWithAndWithoutParameter()
Definition: TitleTest.php:274
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:268
MediaWikiTitleCodec
A codec for MediaWiki page titles.
Definition: MediaWikiTitleCodec.php:39
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
TitleTest\testLegalChars
testLegalChars()
Title::legalChars.
Definition: TitleTest.php:23
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:807
TitleTest\provideCanHaveTalkPage
provideCanHaveTalkPage()
Definition: TitleTest.php:700
TitleTest\provideConvertByteClassToUnicodeClass
static provideConvertByteClassToUnicodeClass()
Definition: TitleTest.php:185
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
MalformedTitleException\getErrorMessage
getErrorMessage()
Definition: MalformedTitleException.php:64
captcha-old.count
count
Definition: captcha-old.py:249
TitleTest\provideIsAlwaysKnown
static provideIsAlwaysKnown()
Definition: TitleTest.php:628
TitleTest\testCreateFragmentTitle
testCreateFragmentTitle(Title $title, $fragment)
Title::createFragmentTarget provideCreateFragmentTitle.
Definition: TitleTest.php:806
TitleTest\testConvertByteClassToUnicodeClass
testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass)
provideConvertByteClassToUnicodeClass Title::convertByteClassToUnicodeClass
Definition: TitleTest.php:250
TitleTest\testGetPageViewLanguage
testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg='')
provideGetPageViewLanguage Title::getPageViewLanguage
Definition: TitleTest.php:426
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED! Use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language & $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED! Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language & $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1963
TitleTest\testGetTalkPage_good
testGetTalkPage_good(Title $title, Title $expected)
provideGetTalkPage_good Title::getTalkPage
Definition: TitleTest.php:752
TitleTest\provideBaseTitleCases
static provideBaseTitleCases()
Definition: TitleTest.php:498
GenderCache
Caches user genders when needed to use correct namespace aliases.
Definition: GenderCache.php:31
TitleTest\provideSubpageTitleCases
static provideSubpageTitleCases()
Definition: TitleTest.php:539
TitleTest\provideTestIsValidMoveOperation
static provideTestIsValidMoveOperation()
Definition: TitleTest.php:308
User
User
Definition: All_system_messages.txt:425
Title\convertByteClassToUnicodeClass
static convertByteClassToUnicodeClass( $byteClass)
Utility method for converting a character sequence from bytes to Unicode.
Definition: Title.php:646
TitleTest\testGetSubpageText
testGetSubpageText( $title, $expected, $msg='')
Definition: TitleTest.php:531
TitleTest\testSecureAndSplitInvalid
testSecureAndSplitInvalid( $text, $expectedErrorMessage)
See also mediawiki.Title.test.js Title::secureAndSplit provideInvalidSecureAndSplit.
Definition: TitleTest.php:175
php
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
TitleTest\testGetFragment
testGetFragment( $full, $fragment)
provideGetFragment
Definition: TitleTest.php:612
NS_MAIN
const NS_MAIN
Definition: Defines.php:65
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:54
TitleTest\testIsAlwaysKnownOnInterwiki
testIsAlwaysKnownOnInterwiki()
Title::isAlwaysKnown.
Definition: TitleTest.php:665
TitleTest\provideGetTalkPage_bad
static provideGetTalkPage_bad()
Definition: TitleTest.php:774
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:69
TitleTest\testCanTalk
testCanTalk(Title $title, $expected)
provideCanHaveTalkPage Title::canTalk
Definition: TitleTest.php:736
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:672
$article
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function array $article
Definition: hooks.txt:77
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
TitleTest\dataWgWhitelistReadRegexp
dataWgWhitelistReadRegexp()
Provides test parameter values for testWgWhitelistReadRegexp()
Definition: TitleTest.php:377
TitleTest\testIsValidMoveOperation
testIsValidMoveOperation( $source, $target, $expected)
Auth-less test of Title::isValidMoveOperation.
Definition: TitleTest.php:293
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:529
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:79
MediaWikiTestCase\setUserLang
setUserLang( $lang)
Definition: MediaWikiTestCase.php:877
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:33
TitleTest\testSecureAndSplitValid
testSecureAndSplitValid( $text)
See also mediawiki.Title.test.js Title::secureAndSplit provideValidSecureAndSplit.
Definition: TitleTest.php:164
TitleTest
Database Title.
Definition: TitleTest.php:7
TitleTest\testFixSpecialNameRetainsParameter
testFixSpecialNameRetainsParameter( $text, $expectedParam)
provideSpecialNamesWithAndWithoutParameter Title::fixSpecialName
Definition: TitleTest.php:258
TitleTest\provideGetPageViewLanguage
static provideGetPageViewLanguage()
Definition: TitleTest.php:447
Title\newFromTextThrow
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition: Title.php:301
MediaWikiTestCase\setContentLang
setContentLang( $lang)
Definition: MediaWikiTestCase.php:886
Title\newFromTitleValue
static newFromTitleValue(TitleValue $titleValue)
Create a new Title from a TitleValue.
Definition: Title.php:228
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:68
TitleTest\provideGetPrefixedDBKey
provideGetPrefixedDBKey()
Definition: TitleTest.php:873
TitleTest\provideNewFromTitleValue
static provideNewFromTitleValue()
Definition: TitleTest.php:547
$value
$value
Definition: styleTest.css.php:45
TitleTest\testIsAlwaysKnown
testIsAlwaysKnown( $page, $isKnown)
Title::isAlwaysKnown provideIsAlwaysKnown.
Definition: TitleTest.php:623
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:53
TitleTest\provideGetFragment
static provideGetFragment()
Definition: TitleTest.php:588
TitleTest\flattenErrorsArray
flattenErrorsArray( $errors)
Definition: TitleTest.php:413
Title\GAID_FOR_UPDATE
const GAID_FOR_UPDATE
Used to be GAID_FOR_UPDATE define.
Definition: Title.php:54
Title\newFromDBkey
static newFromDBkey( $key)
Create a new Title from a prefixed DB key.
Definition: Title.php:209
TitleTest\testGetBaseText
testGetBaseText( $title, $expected, $msg='')
provideBaseTitleCases Title::getBaseText
Definition: TitleTest.php:490
TitleTest\testNewFromTitleValue
testNewFromTitleValue(TitleValue $value)
provideNewFromTitleValue
Definition: TitleTest.php:558
TitleTest\testGetTitleValue
testGetTitleValue( $text)
provideGetTitleValue
Definition: TitleTest.php:578
TitleTest\provideCreateFragmentTitle
provideCreateFragmentTitle()
Definition: TitleTest.php:793
TitleTest\provideIsValid
static provideIsValid()
Definition: TitleTest.php:650
TitleTest\provideInvalidSecureAndSplit
static provideInvalidSecureAndSplit()
Definition: TitleTest.php:79
Title
Represents a title within MediaWiki.
Definition: Title.php:39
TitleTest\provideValidSecureAndSplit
static provideValidSecureAndSplit()
Definition: TitleTest.php:42
TitleTest\testGetPrefixedText
testGetPrefixedText(Title $title, $expected)
Title::getPrefixedText provideGetPrefixedText.
Definition: TitleTest.php:869
TitleTest\secureAndSplitGlobals
secureAndSplitGlobals()
Definition: TitleTest.php:133
TitleTest\testGetTalkPageIfDefined_bad
testGetTalkPageIfDefined_bad(Title $title)
provideGetTalkPage_bad Title::getTalkPageIfDefined
Definition: TitleTest.php:785
MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition: MalformedTitleException.php:25
TitleTest\provideRootTitleCases
static provideRootTitleCases()
Definition: TitleTest.php:518
TitleTest\testIsValid
testIsValid(Title $title, $isValid)
Title::isValid provideIsValid.
Definition: TitleTest.php:646
TitleTest\testCanHaveTalkPage
testCanHaveTalkPage(Title $title, $expected)
provideCanHaveTalkPage Title::canHaveTalkPage
Definition: TitleTest.php:724
TitleTest\testGetRootText
testGetRootText( $title, $expected, $msg='')
provideRootTitleCases Title::getRootText
Definition: TitleTest.php:510
LinkCache\singleton
static singleton()
Get an instance of this class.
Definition: LinkCache.php:67
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
TitleTest\testGetTalkPageIfDefined_good
testGetTalkPageIfDefined_good(Title $title)
provideGetTalkPage_good Title::getTalkPageIfDefined
Definition: TitleTest.php:765
TitleTest\testGetPrefixedDBKey
testGetPrefixedDBKey(Title $title, $expected)
Title::getPrefixedDBKey provideGetPrefixedDBKey.
Definition: TitleTest.php:912
NS_USER
const NS_USER
Definition: Defines.php:67
$source
$source
Definition: mwdoc-filter.php:46
true
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:1965
NS_TALK
const NS_TALK
Definition: Defines.php:66
TitleTest\testExists
testExists()
Title::exists.
Definition: TitleTest.php:673
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
TitleTest\provideGetTitleValue
static provideGetTitleValue()
Definition: TitleTest.php:567
TitleTest\testWgWhitelistReadRegexp
testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected)
Auth-less test of Title::userCan.
Definition: TitleTest.php:333
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Title\legalChars
static legalChars()
Get a regex character class describing the legal characters in a link.
Definition: Title.php:618
Article
Class for viewing MediaWiki article and history.
Definition: Article.php:35
TitleTest\provideGetTalkPage_good
static provideGetTalkPage_good()
Definition: TitleTest.php:741
MediaWikiTestCase\setService
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
Definition: MediaWikiTestCase.php:622
TitleTest\setUp
setUp()
Definition: TitleTest.php:8
array
the array() calling protocol came about after MediaWiki 1.4rc1.
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36