3use Wikimedia\TestingAccessWrapper;
7 protected function setUp() {
11 'wgForceUIMsgAsContentMsg' => [],
23 $this->assertSame( $key, $message->getKey() );
24 $this->assertSame(
$params, $message->getParams() );
25 $this->assertEquals( $expectedLang, $message->getLanguage() );
27 $messageSpecifier = $this->getMockForAbstractClass(
'MessageSpecifier' );
28 $messageSpecifier->expects( $this->any() )
29 ->method(
'getKey' )->will( $this->returnValue( $key ) );
30 $messageSpecifier->expects( $this->any() )
31 ->method(
'getParams' )->will( $this->returnValue(
$params ) );
32 $message =
new Message( $messageSpecifier, [], $language );
34 $this->assertSame( $key, $message->getKey() );
35 $this->assertSame(
$params, $message->getParams() );
36 $this->assertEquals( $expectedLang, $message->getLanguage() );
40 $langDe = Language::factory(
'de' );
41 $langEn = Language::factory(
'en' );
44 [ $langDe,
'foo', [], $langDe ],
45 [ $langDe,
'foo', [
'bar' ], $langDe ],
46 [ $langEn,
'foo', [
'bar' ], null ]
77 [ Message::rawParam(
'baz' ) ],
78 [ Message::rawParam(
'baz' ) ],
81 [ Message::rawParam(
'baz' ),
'foo' ],
82 [ Message::rawParam(
'baz' ),
'foo' ],
85 [ Message::rawParam(
'baz' ) ],
86 [ [ Message::rawParam(
'baz' ) ] ],
89 [ Message::rawParam(
'baz' ),
'foo' ],
90 [ [ Message::rawParam(
'baz' ),
'foo' ] ],
95 [ [
'baz',
'foo' ],
'hhh' ],
96 [ [
'baz',
'foo' ],
'hhh' ],
99 [ [
'baz',
'foo' ],
'hhh', [
'ahahahahha' ] ],
100 [ [
'baz',
'foo' ],
'hhh', [
'ahahahahha' ] ],
103 [ [
'baz',
'foo' ], [
'ahahahahha' ] ],
104 [ [
'baz',
'foo' ], [
'ahahahahha' ] ],
107 [ [
'baz' ], [
'ahahahahha' ] ],
108 [ [
'baz' ], [
'ahahahahha' ] ],
119 $msg =
new Message(
'imasomething' );
121 $returned = call_user_func_array( [ $msg,
'params' ],
$args );
123 $this->assertSame( $msg, $returned );
124 $this->assertSame( $expected, $msg->getParams() );
129 [
'foo', [
'bar' ],
'en' ],
130 [
'foo', [
'bar' ],
'de' ]
140 $language = Language::factory( $languageCode );
143 $this->assertEquals( $language, $message->getLanguage() );
150 'expected' => [
'mainpage' ],
153 'key' => [
'mainpage' ],
154 'expected' => [
'mainpage' ],
157 'key' => [
'mainpage-foo',
'mainpage-bar',
'mainpage' ],
158 'expected' => [
'mainpage-foo',
'mainpage-bar',
'mainpage' ],
163 'exception' =>
'InvalidArgumentException',
168 'exception' =>
'InvalidArgumentException',
173 'exception' =>
'InvalidArgumentException',
185 public function testKeys( $key, $expected, $exception =
null ) {
187 $this->setExpectedException( $exception );
191 $this->assertContains( $msg->getKey(), $expected );
192 $this->assertSame( $expected, $msg->getKeysToTry() );
193 $this->assertSame( count( $expected ) > 1, $msg->isMultiKey() );
200 $this->assertInstanceOf(
'Message',
wfMessage(
'mainpage' ) );
201 $this->assertInstanceOf(
'Message',
wfMessage(
'i-dont-exist-evar' ) );
208 $this->assertInstanceOf(
'Message', Message::newFromKey(
'mainpage' ) );
209 $this->assertInstanceOf(
'Message', Message::newFromKey(
'i-dont-exist-evar' ) );
217 $this->assertSame(
'Return to $1.',
wfMessage(
'returnto' )->
text() );
218 $this->assertSame(
'Return to $1.',
wfMessage(
'returnto', [] )->
text() );
225 wfMessage(
'returnto', [ Message::numParam( 1024 ) ] )->
text()
228 'You have foo (bar).',
232 'You have foo (bar).',
236 'You have 1,024 (bar).',
238 'youhavenewmessages',
239 Message::numParam( 1024 ),
'bar'
243 'You have foo (2,048).',
245 'youhavenewmessages',
246 'foo', Message::numParam( 2048 )
250 'You have 1,024 (2,048).',
252 'youhavenewmessages',
253 [ Message::numParam( 1024 ), Message::numParam( 2048 ) ]
262 $this->assertTrue(
wfMessage(
'mainpage' )->exists() );
263 $this->assertTrue(
wfMessage(
'mainpage' )->params( [] )->exists() );
264 $this->assertTrue(
wfMessage(
'mainpage' )->rawParams(
'foo', 123 )->exists() );
265 $this->assertFalse(
wfMessage(
'i-dont-exist-evar' )->exists() );
266 $this->assertFalse(
wfMessage(
'i-dont-exist-evar' )->params( [] )->exists() );
267 $this->assertFalse(
wfMessage(
'i-dont-exist-evar' )->rawParams(
'foo', 123 )->exists() );
278 $this->assertSame(
'Main Page',
wfMessage(
'mainpage' )->
text() );
279 $this->assertSame(
'⧼i-dont-exist-evar⧽',
wfMessage(
'i-dont-exist-evar' )->
text() );
280 $this->assertSame(
'⧼i<dont>exist-evar⧽',
wfMessage(
'i<dont>exist-evar' )->
text() );
281 $this->assertSame(
'⧼i-dont-exist-evar⧽',
wfMessage(
'i-dont-exist-evar' )->
plain() );
282 $this->assertSame(
'⧼i<dont>exist-evar⧽',
wfMessage(
'i<dont>exist-evar' )->
plain() );
283 $this->assertSame(
'⧼i-dont-exist-evar⧽',
wfMessage(
'i-dont-exist-evar' )->escaped() );
285 '⧼i<dont>exist-evar⧽',
286 wfMessage(
'i<dont>exist-evar' )->escaped()
293 [
'mainpage',
'plain',
'Main Page',
'Main Page' ],
294 [
'i-dont-exist-evar',
'plain',
'⧼i-dont-exist-evar⧽',
'⧼i-dont-exist-evar⧽' ],
295 [
'i-dont-exist-evar',
'escaped',
'⧼i-dont-exist-evar⧽',
'⧼i-dont-exist-evar⧽' ],
296 [
'script>alert(1)</script',
'escaped',
'⧼script>alert(1)</script⧽',
297 '⧼script>alert(1)</script⧽' ],
298 [
'script>alert(1)</script',
'plain',
'⧼script>alert(1)</script⧽',
299 '⧼script>alert(1)</script⧽' ],
308 public function testToString( $key, $format, $expect, $expectImplicit ) {
310 $this->assertSame( $expect, $msg->$format() );
311 $this->assertSame( $expect, $msg->toString(),
'toString is unaffected by previous call' );
312 $this->assertSame( $expectImplicit, $msg->__toString() );
313 $this->assertSame( $expect, $msg->toString(),
'toString is unaffected by __toString' );
318 [
'<span>foo</span>',
'parse',
'<span>foo</span>',
'<span>foo</span>' ],
319 [
'<span>foo</span>',
'escaped',
'<span>foo</span>',
320 '<span>foo</span>' ],
321 [
'<span>foo</span>',
'plain',
'<span>foo</span>',
'<span>foo</span>' ],
322 [
'<script>alert(1)</script>',
'parse',
'<script>alert(1)</script>',
323 '<script>alert(1)</script>' ],
324 [
'<script>alert(1)</script>',
'escaped',
'<script>alert(1)</script>',
325 '<script>alert(1)</script>' ],
326 [
'<script>alert(1)</script>',
'plain',
'<script>alert(1)</script>',
327 '<script>alert(1)</script>' ],
338 $msg = $this->getMockBuilder( Message::class )->setMethods( [
'fetchMessage' ] )
339 ->disableOriginalConstructor()
341 $msg->expects( $this->any() )->method(
'fetchMessage' )->willReturn( $message );
343 $this->assertSame( $expect, $msg->$format() );
344 $this->assertSame( $expect, $msg->toString(),
'toString is unaffected by previous call' );
345 $this->assertSame( $expectImplicit, $msg->__toString() );
346 $this->assertSame( $expect, $msg->toString(),
'toString is unaffected by __toString' );
353 $this->assertSame(
'Main Page',
wfMessage(
'mainpage' )->inLanguage(
'en' )->
text() );
354 $this->assertSame(
'Заглавная страница',
359 $this->assertSame(
'Main Page', $msg->inLanguage( Language::factory(
'en' ) )->text() );
361 'Заглавная страница',
362 $msg->inLanguage( Language::factory(
'ru' ) )->text()
372 '(Заглавная страница)',
376 '(Заглавная страница $1)',
380 '(Заглавная страница)',
381 wfMessage(
'parentheses' )->rawParams(
'Заглавная страница' )->
plain()
384 '(Заглавная страница $1)',
385 wfMessage(
'parentheses' )->rawParams(
'Заглавная страница $1' )->
plain()
395 $this->assertSame(
'example &', $msg->plain() );
396 $this->assertSame(
'example &', $msg->escaped() );
409 $msg =
new RawMessage(
'<html><script>alert("xss")</script></html>' );
410 $txt =
'<span class="error"><html> tags cannot be' .
411 ' used outside of normal pages.</span>';
412 $this->assertSame( $txt, $msg->parse() );
421 $msg =
new RawMessage(
'$1$2$3$4$5$6$7$8$9$10$11$12' );
423 $params = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k' ];
426 $msg->params(
$params )->plain(),
427 'Params > 9 are replaced correctly'
431 $params = [
'ab',
'bc',
'cd' ];
433 'Params: ab, bc, cd',
434 $msg->params(
$params )->text()
443 $lang = Language::factory(
'en' );
447 $lang->formatNum( 123456.789 ),
448 $msg->inLanguage(
$lang )->numParams( 123456.789 )->plain(),
449 'numParams is handled correctly'
458 $lang = Language::factory(
'en' );
462 $lang->formatDuration( 1234 ),
463 $msg->inLanguage(
$lang )->durationParams( 1234 )->plain(),
464 'durationParams is handled correctly'
475 $lang = Language::factory(
'en' );
481 'expiryParams is handled correctly'
490 $lang = Language::factory(
'en' );
494 $lang->formatTimePeriod( 1234 ),
495 $msg->inLanguage(
$lang )->timeperiodParams( 1234 )->plain(),
496 'timeperiodParams is handled correctly'
505 $lang = Language::factory(
'en' );
509 $lang->formatSize( 123456 ),
510 $msg->inLanguage(
$lang )->sizeParams( 123456 )->plain(),
511 'sizeParams is handled correctly'
520 $lang = Language::factory(
'en' );
524 $lang->formatBitrate( 123456 ),
525 $msg->inLanguage(
$lang )->bitrateParams( 123456 )->plain(),
526 'bitrateParams is handled correctly'
533 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
539 'one $2 <div>foo</div> [[Bar]] {{Baz}} <',
544 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
549 'one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;',
554 "<p>one $2 <div>foo</div> [[Bar]] {{Baz}} &lt;\n</p>",
570 $lang = Language::factory(
'en' );
575 '<div>foo</div> [[Bar]] {{Baz}} <',
579 $msg->inLanguage(
$lang )->plaintextParams(
$params )->$format(),
580 "Fail formatting for $format"
585 $lang = Language::factory(
'de' );
590 'Simple comma list' => [
597 'Simple semicolon list' => [
604 'Simple pipe list' => [
611 'Simple text list' => [
625 'List with all "before" params, ->text()' => [
626 [
"''link''", Message::numParam( 12345678 ) ],
629 '\'\
'link\'\'; 12,345,678'
632 'List with all "before" params, ->parse()' => [
633 [
"''link''", Message::numParam( 12345678 ) ],
636 '<i>link</i>; 12,345,678'
639 'List with all "after" params, ->text()' => [
640 [ $msg1, $msg2, Message::rawParam(
'[[foo]]' ) ],
643 'Main Page; \'\'link\'\'; [[foo]]'
646 'List with all "after" params, ->parse()' => [
647 [ $msg1, $msg2, Message::rawParam(
'[[foo]]' ) ],
650 'Main Page; <i>link</i>; [[foo]]'
653 'List with both "before" and "after" params, ->text()' => [
654 [ $msg1, $msg2, Message::rawParam(
'[[foo]]' ),
"''link''", Message::numParam( 12345678 ) ],
657 'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678'
660 'List with both "before" and "after" params, ->parse()' => [
661 [ $msg1, $msg2, Message::rawParam(
'[[foo]]' ),
"''link''", Message::numParam( 12345678 ) ],
664 'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678'
676 $lang = Language::factory(
'en' );
679 $msg->params( [ Message::listParam( $list,
$type ) ] );
682 $msg->inLanguage(
$lang )->$format()
691 'wgScript' =>
'/wiki/index.php',
692 'wgArticlePath' =>
'/wiki/$1',
695 $msg =
new Message(
'returnto', [
697 'foo',
new Message(
'mainpage', [], Language::factory(
'en' ) )
698 ], Language::factory(
'de' ) )
699 ], Language::factory(
'es' ) );
702 'Volver a [[Special:ApiHelp/foo|Página principal]].',
704 'Process with ->text()'
707 '<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página '
708 .
"principal</a>.\n</p>",
709 $msg->parseAsBlock(),
710 'Process with ->parseAsBlock()'
717 "''&'' <x><!-- x -->",
722 "''&'' <x><!-- x -->",
726 '<i>&</i> <x>',
731 "<p><i>&</i> <x>\n</p>",
747 $msg =
new RawMessage(
"''&'' <x><!-- x -->" );
750 $msg->inLanguage(
'en' )->$format()
762 $this->assertSame(
'Hauptseite', $msg->inLanguage(
'de' )->plain(),
"inLanguage( 'de' )" );
763 $this->assertSame(
'Main Page', $msg->inContentLanguage()->plain(),
"inContentLanguage()" );
764 $this->assertSame(
'Accueil', $msg->inLanguage(
'fr' )->plain(),
"inLanguage( 'fr' )" );
772 'wgForceUIMsgAsContentMsg' => [
'mainpage' ],
781 $msg->inContentLanguage()->plain(),
782 'inContentLanguage() with ForceUIMsg override enabled'
784 $this->assertSame(
'Main Page', $msg->inLanguage(
'en' )->plain(),
"inLanguage( 'en' )" );
787 $msg->inContentLanguage()->plain(),
788 'inContentLanguage() with ForceUIMsg override enabled'
790 $this->assertSame(
'Hauptseite', $msg->inLanguage(
'de' )->plain(),
"inLanguage( 'de' )" );
806 $msg =
new Message(
'parentheses' );
807 $msg->rawParams(
'<a>foo</a>' );
808 $msg->title( Title::newFromText(
'Testing' ) );
809 $this->assertSame(
'(<a>foo</a>)', $msg->parse(),
'Sanity check' );
811 $this->assertSame(
'(<a>foo</a>)', $msg->parse() );
812 $title = TestingAccessWrapper::newFromObject( $msg )->title;
813 $this->assertInstanceOf(
'Title',
$title );
814 $this->assertSame(
'Testing',
$title->getFullText() );
816 $msg =
new Message(
'mainpage' );
817 $msg->inLanguage(
'de' );
818 $this->assertSame(
'Hauptseite', $msg->plain(),
'Sanity check' );
820 $this->assertSame(
'Hauptseite', $msg->plain() );
828 $message = Message::newFromSpecifier(
$value );
829 $this->assertInstanceOf( Message::class, $message );
831 $this->assertInstanceOf( get_class(
$value ), $message );
832 $this->assertEquals(
$value, $message );
834 $this->assertSame( $expectedText, $message->text() );
838 $messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class );
839 $messageSpecifier->expects( $this->any() )->method(
'getKey' )->willReturn(
'mainpage' );
840 $messageSpecifier->expects( $this->any() )->method(
'getParams' )->willReturn( [] );
843 'string' => [
'mainpage',
'Main Page' ],
844 'array' => [ [
'youhavenewmessages',
'foo',
'bar' ],
'You have foo (bar).' ],
845 'Message' => [
new Message(
'youhavenewmessages', [
'foo',
'bar' ] ),
'You have foo (bar).' ],
846 'RawMessage' => [
new RawMessage(
'foo ($1)', [
'bar' ] ),
'foo (bar)' ],
847 'ApiMessage' => [
new ApiMessage( [
'mainpage' ],
'code', [
'data' ] ),
'Main Page' ],
848 'MessageSpecifier' => [ $messageSpecifier,
'Main Page' ],
849 'nested RawMessage' => [ [
new RawMessage(
'foo ($1)', [
'bar' ] ) ],
'foo (bar)' ],
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
unserialize( $serialized)
$wgParserConf
Parser configuration.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Extension of Message implementing IApiMessage.
static destroyInstance()
Destroy the singleton instance.
testExpiryParams()
FIXME: This should not need database, but Language::formatExpiry does (T57912) Database Message::expi...
testConstructorParams( $expected, $args)
Message::__construct Message::getParams provideConstructorParams.
static provideToString_raw()
static provideConstructorParams()
testParser( $expect, $format)
Message::text Message::parse Message::parseAsBlock Message::toString Message::transformText Message::...
testToString( $key, $format, $expect, $expectImplicit)
Message::toString Message::__toString provideToString.
testToString_raw( $message, $format, $expect, $expectImplicit)
Message::toString Message::__toString provideToString_raw.
testInLanguageThrows()
MWException Message::inLanguage.
static provideListParam()
testInLanguage()
Message::inLanguage.
testInContentLanguage()
Message::inContentLanguage.
static provideConstructorLanguage()
testSizeParams()
Message::sizeParam Message::sizeParams.
testRawParams()
Message::rawParam Message::rawParams.
testDurationParams()
Message::durationParam Message::durationParams.
testNumParams()
Message::numParam Message::numParams.
testNewFromSpecifier( $value, $expectedText)
Message::newFromSpecifier provideNewFromSpecifier.
testReplaceManyParams()
Message::params Message::toString Message::replaceParameters.
testExists()
Message::exists.
static provideConstructor()
testWfMessageParams()
wfMessage Message::__construct
testListParam( $list, $type, $format, $expect)
Message::listParam Message::extractParam Message::formatListParam provideListParam.
testTimeperiodParams()
Message::timeperiodParam Message::timeperiodParams.
testBitrateParams()
Message::bitrateParam Message::bitrateParams.
testConstructorLanguage( $key, $params, $languageCode)
Message::__construct Message::getLanguage provideConstructorLanguage.
provideNewFromSpecifier()
testKeys( $key, $expected, $exception=null)
Message::__construct Message::getKey Message::isMultiKey Message::getKeysToTry provideKeys.
testConstructor( $expectedLang, $key, $params, $language)
Message::__construct provideConstructor.
testNewFromKey()
Message::newFromKey.
testPlaintextParams( $expect, $format)
Message::plaintextParam Message::plaintextParams Message::formatPlaintext Message::toString Message::...
testInContentLanguageOverride()
Message::inContentLanguage.
static providePlaintextParams()
testToStringKey()
Message::__construct Message::text Message::plain Message::escaped Message::toString.
testSerialization()
Message::serialize Message::unserialize.
testRawMessage()
RawMessage::__construct RawMessage::fetchMessage.
testMessageAsParam()
Message::extractParam.
The Message class provides methods which fulfil two basic services:
Variant of the Message class.
when a variable name is used in a it is silently declared as a new local masking the global
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
namespace and then decline to actually register it file or subcat img or subcat $title
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "<div ...>$1</div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
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
if(!isset( $args[0])) $lang