MediaWiki REL1_32
TitleTest.php
Go to the documentation of this file.
1<?php
2
4
10 protected function setUp() {
11 parent::setUp();
12
13 $this->setMwGlobals( [
14 'wgAllowUserJs' => false,
15 'wgDefaultLanguageVariant' => false,
16 'wgMetaNamespace' => 'Project',
17 ] );
18 $this->setUserLang( 'en' );
19 $this->setContentLang( 'en' );
20 }
21
25 public function testLegalChars() {
26 $titlechars = Title::legalChars();
27
28 foreach ( range( 1, 255 ) as $num ) {
29 $chr = chr( $num );
30 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
31 $this->assertFalse(
32 (bool)preg_match( "/[$titlechars]/", $chr ),
33 "chr($num) = $chr is not a valid titlechar"
34 );
35 } else {
36 $this->assertTrue(
37 (bool)preg_match( "/[$titlechars]/", $chr ),
38 "chr($num) = $chr is a valid titlechar"
39 );
40 }
41 }
42 }
43
44 public static function provideValidSecureAndSplit() {
45 return [
46 [ 'Sandbox' ],
47 [ 'A "B"' ],
48 [ 'A \'B\'' ],
49 [ '.com' ],
50 [ '~' ],
51 [ '#' ],
52 [ '"' ],
53 [ '\'' ],
54 [ 'Talk:Sandbox' ],
55 [ 'Talk:Foo:Sandbox' ],
56 [ 'File:Example.svg' ],
57 [ 'File_talk:Example.svg' ],
58 [ 'Foo/.../Sandbox' ],
59 [ 'Sandbox/...' ],
60 [ 'A~~' ],
61 [ ':A' ],
62 // Length is 256 total, but only title part matters
63 [ 'Category:' . str_repeat( 'x', 248 ) ],
64 [ str_repeat( 'x', 252 ) ],
65 // interwiki prefix
66 [ 'localtestiw: #anchor' ],
67 [ 'localtestiw:' ],
68 [ 'localtestiw:foo' ],
69 [ 'localtestiw: foo # anchor' ],
70 [ 'localtestiw: Talk: Sandbox # anchor' ],
71 [ 'remotetestiw:' ],
72 [ 'remotetestiw: Talk: # anchor' ],
73 [ 'remotetestiw: #bar' ],
74 [ 'remotetestiw: Talk:' ],
75 [ 'remotetestiw: Talk: Foo' ],
76 [ 'localtestiw:remotetestiw:' ],
77 [ 'localtestiw:remotetestiw:foo' ]
78 ];
79 }
80
81 public static function provideInvalidSecureAndSplit() {
82 return [
83 [ '', 'title-invalid-empty' ],
84 [ ':', 'title-invalid-empty' ],
85 [ '__ __', 'title-invalid-empty' ],
86 [ ' __ ', 'title-invalid-empty' ],
87 // Bad characters forbidden regardless of wgLegalTitleChars
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 > B', 'title-invalid-characters' ],
94 [ 'A | B', 'title-invalid-characters' ],
95 [ "A \t B", 'title-invalid-characters' ],
96 [ "A \n B", 'title-invalid-characters' ],
97 // URL encoding
98 [ 'A%20B', 'title-invalid-characters' ],
99 [ 'A%23B', 'title-invalid-characters' ],
100 [ 'A%2523B', 'title-invalid-characters' ],
101 // XML/HTML character entity references
102 // Note: Commented out because they are not marked invalid by the PHP test as
103 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
104 // 'A &eacute; B',
105 // 'A &#233; B',
106 // 'A &#x00E9; B',
107 // Subject of NS_TALK does not roundtrip to NS_MAIN
108 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
109 // Directory navigation
110 [ '.', 'title-invalid-relative' ],
111 [ '..', 'title-invalid-relative' ],
112 [ './Sandbox', 'title-invalid-relative' ],
113 [ '../Sandbox', 'title-invalid-relative' ],
114 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
115 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
116 [ 'Sandbox/.', 'title-invalid-relative' ],
117 [ 'Sandbox/..', 'title-invalid-relative' ],
118 // Tilde
119 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
120 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
121 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
122 // Length
123 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
124 // Namespace prefix without actual title
125 [ 'Talk:', 'title-invalid-empty' ],
126 [ 'Talk:#', 'title-invalid-empty' ],
127 [ 'Category: ', 'title-invalid-empty' ],
128 [ 'Category: #bar', 'title-invalid-empty' ],
129 // interwiki prefix
130 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
131 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
132 ];
133 }
134
135 private function secureAndSplitGlobals() {
136 $this->setMwGlobals( [
137 'wgLocalInterwikis' => [ 'localtestiw' ],
138 'wgHooks' => [
139 'InterwikiLoadPrefix' => [
140 function ( $prefix, &$data ) {
141 if ( $prefix === 'localtestiw' ) {
142 $data = [ 'iw_url' => 'localtestiw' ];
143 } elseif ( $prefix === 'remotetestiw' ) {
144 $data = [ 'iw_url' => 'remotetestiw' ];
145 }
146 return false;
147 }
148 ]
149 ]
150 ] );
151
152 // Reset TitleParser since we modified $wgLocalInterwikis
153 $this->setService( 'TitleParser', new MediaWikiTitleCodec(
154 Language::factory( 'en' ),
155 new GenderCache(),
156 [ 'localtestiw' ]
157 ) );
158 }
159
166 public function testSecureAndSplitValid( $text ) {
167 $this->secureAndSplitGlobals();
168 $this->assertInstanceOf( Title::class, Title::newFromText( $text ), "Valid: $text" );
169 }
170
177 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
178 $this->secureAndSplitGlobals();
179 try {
180 Title::newFromTextThrow( $text ); // should throw
181 $this->assertTrue( false, "Invalid: $text" );
182 } catch ( MalformedTitleException $ex ) {
183 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
184 }
185 }
186
187 public static function provideConvertByteClassToUnicodeClass() {
188 return [
189 [
190 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
191 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
192 ],
193 [
194 'QWERTYf-\\xFF+',
195 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
196 ],
197 [
198 'QWERTY\\x66-\\xFD+',
199 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
200 ],
201 [
202 'QWERTYf-y+',
203 'QWERTYf-y+',
204 ],
205 [
206 'QWERTYf-\\x80+',
207 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
208 ],
209 [
210 'QWERTY\\x66-\\x80+\\x23',
211 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
212 ],
213 [
214 'QWERTY\\x66-\\x80+\\xD3',
215 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
216 ],
217 [
218 '\\\\\\x99',
219 '\\\\\\u0080-\\uFFFF',
220 ],
221 [
222 '-\\x99',
223 '\\-\\u0080-\\uFFFF',
224 ],
225 [
226 'QWERTY\\-\\x99',
227 'QWERTY\\-\\u0080-\\uFFFF',
228 ],
229 [
230 '\\\\x99',
231 '\\\\x99',
232 ],
233 [
234 'A-\\x9F',
235 'A-\\x7F\\u0080-\\uFFFF',
236 ],
237 [
238 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
239 'f-wQWERTYFXZ\\u0080-\\uFFFF',
240 ],
241 [
242 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
243 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
244 ],
245 ];
246 }
247
252 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
253 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
254 }
255
260 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
261 $title = Title::newFromText( $text );
262 $fixed = $title->fixSpecialName();
263 $stuff = explode( '/', $fixed->getDBkey(), 2 );
264 if ( count( $stuff ) == 2 ) {
265 $par = $stuff[1];
266 } else {
267 $par = null;
268 }
269 $this->assertEquals(
270 $expectedParam,
271 $par,
272 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
273 );
274 }
275
277 return [
278 [ 'Special:Version', null ],
279 [ 'Special:Version/', '' ],
280 [ 'Special:Version/param', 'param' ],
281 ];
282 }
283
294 public function testIsValidMoveOperation( $source, $target, $expected ) {
295 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
296 $title = Title::newFromText( $source );
297 $nt = Title::newFromText( $target );
298 $errors = $title->isValidMoveOperation( $nt, false );
299 if ( $expected === true ) {
300 $this->assertTrue( $errors );
301 } else {
302 $errors = $this->flattenErrorsArray( $errors );
303 foreach ( (array)$expected as $error ) {
304 $this->assertContains( $error, $errors );
305 }
306 }
307 }
308
309 public static function provideTestIsValidMoveOperation() {
310 return [
311 // for Title::isValidMoveOperation
312 [ 'Some page', '', 'badtitletext' ],
313 [ 'Test', 'Test', 'selfmove' ],
314 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
315 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
316 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
317 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
318 // for Title::validateFileMoveOperation
319 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
320 ];
321 }
322
334 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
335 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
336 // usually have only one regex, it is more concise to write the lonely regex
337 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
338 // type requisite.
339 if ( is_string( $whitelistRegexp ) ) {
340 $whitelistRegexp = [ $whitelistRegexp ];
341 }
342
343 $this->setMwGlobals( [
344 // So User::isEveryoneAllowed( 'read' ) === false
345 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
346 'wgWhitelistRead' => [ 'some random non sense title' ],
347 'wgWhitelistReadRegexp' => $whitelistRegexp,
348 ] );
349
350 $title = Title::newFromDBkey( $source );
351
352 // New anonymous user with no rights
353 $user = new User;
354 $user->mRights = [];
355 $errors = $title->userCan( $action, $user );
356
357 if ( is_bool( $expected ) ) {
358 # Forge the assertion message depending on the assertion expectation
359 $allowableness = $expected
360 ? " should be allowed"
361 : " should NOT be allowed";
362 $this->assertEquals(
363 $expected,
364 $errors,
365 "User action '$action' on [[$source]] $allowableness."
366 );
367 } else {
368 $errors = $this->flattenErrorsArray( $errors );
369 foreach ( (array)$expected as $error ) {
370 $this->assertContains( $error, $errors );
371 }
372 }
373 }
374
378 public function dataWgWhitelistReadRegexp() {
379 $ALLOWED = true;
380 $DISALLOWED = false;
381
382 return [
383 // Everything, if this doesn't work, we're really in trouble
384 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
385 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
386
387 // We validate against the title name, not the db key
388 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
389 // Main page
390 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
391 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
392 // With spaces
393 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
394 // Unicode multibyte
395 // ...without unicode modifier
396 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
397 // ...with unicode modifier
398 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
399 // Case insensitive
400 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
401 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
402
403 // From DefaultSettings.php:
404 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
405 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
406
407 // With namespaces:
408 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
409 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
410
411 ];
412 }
413
414 public function flattenErrorsArray( $errors ) {
415 $result = [];
416 foreach ( $errors as $error ) {
417 $result[] = $error[0];
418 }
419
420 return $result;
421 }
422
427 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
428 $lang, $variant, $msg = ''
429 ) {
430 // Setup environnement for this test
431 $this->setMwGlobals( [
432 'wgDefaultLanguageVariant' => $variant,
433 'wgAllowUserJs' => true,
434 ] );
435 $this->setUserLang( $lang );
436 $this->setContentLang( $contLang );
437
438 $title = Title::newFromText( $titleText );
439 $this->assertInstanceOf( Title::class, $title,
440 "Test must be passed a valid title text, you gave '$titleText'"
441 );
442 $this->assertEquals( $expected,
443 $title->getPageViewLanguage()->getCode(),
444 $msg
445 );
446 }
447
448 public static function provideGetPageViewLanguage() {
449 # Format:
450 # - expected
451 # - Title name
452 # - content language (expected in most cases)
453 # - wgLang (on some specific pages)
454 # - wgDefaultLanguageVariant
455 # - Optional message
456 return [
457 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
458 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
459 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
460
461 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
462 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
463 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
468 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
469
470 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
479 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
480
481 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
482 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
483
484 ];
485 }
486
491 public function testGetBaseText( $title, $expected, $msg = '' ) {
492 $title = Title::newFromText( $title );
493 $this->assertEquals( $expected,
494 $title->getBaseText(),
495 $msg
496 );
497 }
498
499 public static function provideBaseTitleCases() {
500 return [
501 # Title, expected base, optional message
502 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
503 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
504 ];
505 }
506
511 public function testGetRootText( $title, $expected, $msg = '' ) {
512 $title = Title::newFromText( $title );
513 $this->assertEquals( $expected,
514 $title->getRootText(),
515 $msg
516 );
517 }
518
519 public static function provideRootTitleCases() {
520 return [
521 # Title, expected base, optional message
522 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
523 [ 'User:Foo/Bar/Baz', 'Foo' ],
524 ];
525 }
526
532 public function testGetSubpageText( $title, $expected, $msg = '' ) {
533 $title = Title::newFromText( $title );
534 $this->assertEquals( $expected,
535 $title->getSubpageText(),
536 $msg
537 );
538 }
539
540 public static function provideSubpageTitleCases() {
541 return [
542 # Title, expected base, optional message
543 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
544 [ 'User:John_Doe/subOne', 'subOne' ],
545 ];
546 }
547
548 public static function provideNewFromTitleValue() {
549 return [
550 [ new TitleValue( NS_MAIN, 'Foo' ) ],
551 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
552 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
553 ];
554 }
555
561 $title = Title::newFromTitleValue( $value );
562
563 $dbkey = str_replace( ' ', '_', $value->getText() );
564 $this->assertEquals( $dbkey, $title->getDBkey() );
565 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
566 $this->assertEquals( $value->getFragment(), $title->getFragment() );
567 }
568
569 public static function provideGetTitleValue() {
570 return [
571 [ 'Foo' ],
572 [ 'Foo#bar' ],
573 [ 'User:Hansi_Maier' ],
574 ];
575 }
576
581 public function testGetTitleValue( $text ) {
582 $title = Title::newFromText( $text );
583 $value = $title->getTitleValue();
584
585 $dbkey = str_replace( ' ', '_', $value->getText() );
586 $this->assertEquals( $title->getDBkey(), $dbkey );
587 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
588 $this->assertEquals( $title->getFragment(), $value->getFragment() );
589 }
590
591 public static function provideGetFragment() {
592 return [
593 [ 'Foo', '' ],
594 [ 'Foo#bar', 'bar' ],
595 [ 'Foo#bär', 'bär' ],
596
597 // Inner whitespace is normalized
598 [ 'Foo#bar_bar', 'bar bar' ],
599 [ 'Foo#bar bar', 'bar bar' ],
600 [ 'Foo#bar bar', 'bar bar' ],
601
602 // Leading whitespace is kept, trailing whitespace is trimmed.
603 // XXX: Is this really want we want?
604 [ 'Foo#_bar_bar_', ' bar bar' ],
605 [ 'Foo# bar bar ', ' bar bar' ],
606 ];
607 }
608
616 public function testGetFragment( $full, $fragment ) {
617 $title = Title::newFromText( $full );
618 $this->assertEquals( $fragment, $title->getFragment() );
619 }
620
627 public function testIsAlwaysKnown( $page, $isKnown ) {
628 $title = Title::newFromText( $page );
629 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
630 }
631
632 public static function provideIsAlwaysKnown() {
633 return [
634 [ 'Some nonexistent page', false ],
635 [ 'UTPage', false ],
636 [ '#test', true ],
637 [ 'Special:BlankPage', true ],
638 [ 'Special:SomeNonexistentSpecialPage', false ],
639 [ 'MediaWiki:Parentheses', true ],
640 [ 'MediaWiki:Some nonexistent message', false ],
641 ];
642 }
643
650 public function testIsValid( Title $title, $isValid ) {
651 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
652 }
653
654 public static function provideIsValid() {
655 return [
656 [ Title::makeTitle( NS_MAIN, '' ), false ],
657 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
658 [ Title::makeTitle( NS_MAIN, '|' ), false ],
659 [ Title::makeTitle( NS_MAIN, '#' ), false ],
660 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
661 [ Title::makeTitle( -33, 'Test' ), false ],
662 [ Title::makeTitle( 77663399, 'Test' ), false ],
663 ];
664 }
665
670 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
671 $this->assertTrue( $title->isAlwaysKnown() );
672 }
673
677 public function testExists() {
678 $title = Title::makeTitle( NS_PROJECT, 'New page' );
679 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
680
681 $article = new Article( $title );
682 $page = $article->getPage();
683 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
684
685 // Tell Title it doesn't know whether it exists
686 $title->mArticleID = -1;
687
688 // Tell the link cache it doesn't exists when it really does
689 $linkCache->clearLink( $title );
690 $linkCache->addBadLinkObj( $title );
691
692 $this->assertEquals(
693 false,
694 $title->exists(),
695 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
696 );
697 $this->assertEquals(
698 true,
699 $title->exists( Title::GAID_FOR_UPDATE ),
700 'exists() should re-query database when GAID_FOR_UPDATE is used'
701 );
702 }
703
704 public function provideCanHaveTalkPage() {
705 return [
706 'User page has talk page' => [
707 Title::makeTitle( NS_USER, 'Jane' ), true
708 ],
709 'Talke page has talk page' => [
710 Title::makeTitle( NS_TALK, 'Foo' ), true
711 ],
712 'Special page cannot have talk page' => [
713 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
714 ],
715 'Virtual namespace cannot have talk page' => [
716 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
717 ],
718 ];
719 }
720
728 public function testCanHaveTalkPage( Title $title, $expected ) {
729 $actual = $title->canHaveTalkPage();
730 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
731 }
732
740 public function testCanTalk( Title $title, $expected ) {
741 $actual = $title->canTalk();
742 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
743 }
744
745 public static function provideGetTalkPage_good() {
746 return [
747 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
748 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
749 ];
750 }
751
756 public function testGetTalkPage_good( Title $title, Title $expected ) {
757 $talk = $title->getTalkPage();
758 $this->assertSame(
759 $expected->getPrefixedDBKey(),
760 $talk->getPrefixedDBKey(),
761 $title->getPrefixedDBKey()
762 );
763 }
764
769 public function testGetTalkPageIfDefined_good( Title $title ) {
770 $talk = $title->getTalkPageIfDefined();
771 $this->assertInstanceOf(
772 Title::class,
773 $talk,
774 $title->getPrefixedDBKey()
775 );
776 }
777
778 public static function provideGetTalkPage_bad() {
779 return [
780 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
781 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
782 ];
783 }
784
789 public function testGetTalkPageIfDefined_bad( Title $title ) {
790 $talk = $title->getTalkPageIfDefined();
791 $this->assertNull(
792 $talk,
793 $title->getPrefixedDBKey()
794 );
795 }
796
797 public function provideCreateFragmentTitle() {
798 return [
799 [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
800 [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
801 [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
802 [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
803 ];
804 }
805
810 public function testCreateFragmentTitle( Title $title, $fragment ) {
811 $this->mergeMwGlobalArrayValue( 'wgHooks', [
812 'InterwikiLoadPrefix' => [
813 function ( $prefix, &$iwdata ) {
814 if ( $prefix === 'interwiki' ) {
815 $iwdata = [
816 'iw_url' => 'http://example.com/',
817 'iw_local' => 0,
818 'iw_trans' => 0,
819 ];
820 return false;
821 }
822 },
823 ],
824 ] );
825
826 $fragmentTitle = $title->createFragmentTarget( $fragment );
827
828 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
829 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
830 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
831 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
832 }
833
834 public function provideGetPrefixedText() {
835 return [
836 // ns = 0
837 [
838 Title::makeTitle( NS_MAIN, 'Foo bar' ),
839 'Foo bar'
840 ],
841 // ns = 2
842 [
843 Title::makeTitle( NS_USER, 'Foo bar' ),
844 'User:Foo bar'
845 ],
846 // ns = 3
847 [
848 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
849 'User talk:Foo bar'
850 ],
851 // fragment not included
852 [
853 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
854 'Foo bar'
855 ],
856 // ns = -2
857 [
858 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
859 'Media:Foo bar'
860 ],
861 // non-existent namespace
862 [
863 Title::makeTitle( 100777, 'Foo bar' ),
864 'Special:Badtitle/NS100777:Foo bar'
865 ],
866 ];
867 }
868
873 public function testGetPrefixedText( Title $title, $expected ) {
874 $this->assertEquals( $expected, $title->getPrefixedText() );
875 }
876
877 public function provideGetPrefixedDBKey() {
878 return [
879 // ns = 0
880 [
881 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
882 'Foo_bar'
883 ],
884 // ns = 2
885 [
886 Title::makeTitle( NS_USER, 'Foo_bar' ),
887 'User:Foo_bar'
888 ],
889 // ns = 3
890 [
891 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
892 'User_talk:Foo_bar'
893 ],
894 // fragment not included
895 [
896 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
897 'Foo_bar'
898 ],
899 // ns = -2
900 [
901 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
902 'Media:Foo_bar'
903 ],
904 // non-existent namespace
905 [
906 Title::makeTitle( 100777, 'Foo_bar' ),
907 'Special:Badtitle/NS100777:Foo_bar'
908 ],
909 ];
910 }
911
916 public function testGetPrefixedDBKey( Title $title, $expected ) {
917 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
918 }
919
927 public function testGetFragmentForURL( $titleStr, $expected ) {
928 $this->setMwGlobals( [
929 'wgFragmentMode' => [ 'html5' ],
930 'wgExternalInterwikiFragmentMode' => 'legacy',
931 ] );
932 $dbw = wfGetDB( DB_MASTER );
933 $dbw->insert( 'interwiki',
934 [
935 [
936 'iw_prefix' => 'de',
937 'iw_url' => 'http://de.wikipedia.org/wiki/',
938 'iw_api' => 'http://de.wikipedia.org/w/api.php',
939 'iw_wikiid' => 'dewiki',
940 'iw_local' => 1,
941 'iw_trans' => 0,
942 ],
943 [
944 'iw_prefix' => 'zz',
945 'iw_url' => 'http://zzwiki.org/wiki/',
946 'iw_api' => 'http://zzwiki.org/w/api.php',
947 'iw_wikiid' => 'zzwiki',
948 'iw_local' => 0,
949 'iw_trans' => 0,
950 ],
951 ],
952 __METHOD__,
953 [ 'IGNORE' ]
954 );
955
956 $title = Title::newFromText( $titleStr );
957 self::assertEquals( $expected, $title->getFragmentForURL() );
958
959 $dbw->delete( 'interwiki', '*', __METHOD__ );
960 }
961
962 public function provideGetFragmentForURL() {
963 return [
964 [ 'Foo', '' ],
965 [ 'Foo#ümlåût', '#ümlåût' ],
966 [ 'de:Foo#Bå®', '#Bå®' ],
967 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
968 ];
969 }
970
975 public function testIsRawHtmlMessage( $textForm, $expected ) {
976 $this->setMwGlobals( 'wgRawHtmlMessages', [
977 'foobar',
978 'foo_bar',
979 'foo-bar',
980 ] );
981
982 $title = Title::newFromText( $textForm );
983 $this->assertSame( $expected, $title->isRawHtmlMessage() );
984 }
985
986 public function provideIsRawHtmlMessage() {
987 return [
988 [ 'MediaWiki:Foobar', true ],
989 [ 'MediaWiki:Foo bar', true ],
990 [ 'MediaWiki:Foo-bar', true ],
991 [ 'MediaWiki:foo bar', true ],
992 [ 'MediaWiki:foo-bar', true ],
993 [ 'MediaWiki:foobar', true ],
994 [ 'MediaWiki:some-other-message', false ],
995 [ 'Main Page', false ],
996 ];
997 }
998}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Class for viewing MediaWiki article and history.
Definition Article.php:38
Caches user genders when needed to use correct namespace aliases.
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
A codec for MediaWiki page titles.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Database Title.
Definition TitleTest.php:9
testFixSpecialNameRetainsParameter( $text, $expectedParam)
provideSpecialNamesWithAndWithoutParameter Title::fixSpecialName
testIsAlwaysKnown( $page, $isKnown)
Title::isAlwaysKnown provideIsAlwaysKnown.
testGetFragment( $full, $fragment)
Title::getFragment provideGetFragment.
flattenErrorsArray( $errors)
testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg='')
provideGetPageViewLanguage Title::getPageViewLanguage
static provideBaseTitleCases()
static provideConvertByteClassToUnicodeClass()
testGetFragmentForURL( $titleStr, $expected)
Title::getFragmentForURL provideGetFragmentForURL.
testGetTitleValue( $text)
Title::getTitleValue provideGetTitleValue.
static provideIsValid()
testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected)
Auth-less test of Title::userCan.
static provideTestIsValidMoveOperation()
testGetPrefixedDBKey(Title $title, $expected)
Title::getPrefixedDBKey provideGetPrefixedDBKey.
testIsValidMoveOperation( $source, $target, $expected)
Auth-less test of Title::isValidMoveOperation.
provideCreateFragmentTitle()
testGetBaseText( $title, $expected, $msg='')
provideBaseTitleCases Title::getBaseText
testGetTalkPageIfDefined_good(Title $title)
provideGetTalkPage_good Title::getTalkPageIfDefined
static provideRootTitleCases()
static provideGetTalkPage_bad()
provideGetPrefixedText()
testIsRawHtmlMessage( $textForm, $expected)
Title::isRawHtmlMessage provideIsRawHtmlMessage.
testNewFromTitleValue(TitleValue $value)
Title::newFromTitleValue provideNewFromTitleValue.
static provideIsAlwaysKnown()
static provideGetTitleValue()
static provideGetFragment()
testSecureAndSplitInvalid( $text, $expectedErrorMessage)
See also mediawiki.Title.test.js Title::secureAndSplit provideInvalidSecureAndSplit.
testGetRootText( $title, $expected, $msg='')
provideRootTitleCases Title::getRootText
dataWgWhitelistReadRegexp()
Provides test parameter values for testWgWhitelistReadRegexp()
testGetTalkPage_good(Title $title, Title $expected)
provideGetTalkPage_good Title::getTalkPage
provideIsRawHtmlMessage()
testCanTalk(Title $title, $expected)
provideCanHaveTalkPage Title::canTalk
testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass)
provideConvertByteClassToUnicodeClass Title::convertByteClassToUnicodeClass
testGetSubpageText( $title, $expected, $msg='')
provideGetPrefixedDBKey()
static provideSubpageTitleCases()
testCanHaveTalkPage(Title $title, $expected)
provideCanHaveTalkPage Title::canHaveTalkPage
testGetPrefixedText(Title $title, $expected)
Title::getPrefixedText provideGetPrefixedText.
static provideGetPageViewLanguage()
testLegalChars()
Title::legalChars.
Definition TitleTest.php:25
testExists()
Title::exists.
testSecureAndSplitValid( $text)
See also mediawiki.Title.test.js Title::secureAndSplit provideValidSecureAndSplit.
testIsAlwaysKnownOnInterwiki()
Title::isAlwaysKnown.
static provideInvalidSecureAndSplit()
Definition TitleTest.php:81
static provideNewFromTitleValue()
static provideValidSecureAndSplit()
Definition TitleTest.php:44
testGetTalkPageIfDefined_bad(Title $title)
provideGetTalkPage_bad Title::getTalkPageIfDefined
secureAndSplitGlobals()
testCreateFragmentTitle(Title $title, $fragment)
Title::createFragmentTarget provideCreateFragmentTitle.
provideGetFragmentForURL()
static provideSpecialNamesWithAndWithoutParameter()
testIsValid(Title $title, $isValid)
Title::isValid provideIsValid.
static provideGetTalkPage_good()
provideCanHaveTalkPage()
Represents a page (or page fragment) title within MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
Content object for wiki text pages.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const NS_USER
Definition Defines.php:66
const NS_MAIN
Definition Defines.php:64
const NS_SPECIAL
Definition Defines.php:53
const NS_MEDIA
Definition Defines.php:52
const NS_TALK
Definition Defines.php:65
const NS_USER_TALK
Definition Defines.php:67
const NS_PROJECT
Definition Defines.php:68
const NS_CATEGORY
Definition Defines.php:78
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. 'ImgAuthModifyHeaders':Executed just before a file is streamed to a user via img_auth.php, allowing headers to be modified beforehand. $title:LinkTarget object & $headers:HTTP headers(name=> value, names are case insensitive). Two headers get special handling:If-Modified-Since(value must be a valid HTTP date) and Range(must be of the form "bytes=(\d*-\d*)") will be honored when streaming the file. '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 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name '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 since 1.16! 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 since 1.28! 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:2042
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:994
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:2055
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers change it to an object instance and return false override the list derivative used the name of the old file & $article
Definition hooks.txt:1619
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
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 local account $user
Definition hooks.txt:247
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$source
const DB_MASTER
Definition defines.php:26
if(!isset( $args[0])) $lang