3 use MediaWiki\MediaWikiServices
;
10 * @covers ApiQuerySiteinfo
12 class ApiQuerySiteinfoTest
extends ApiTestCase
{
13 // We don't try to test every single thing for every category, just a sample
15 protected function doQuery( $siprop = null, $extraParams = [] ) {
16 $params = [ 'action' => 'query', 'meta' => 'siteinfo' ];
17 if ( $siprop !== null ) {
18 $params['siprop'] = $siprop;
20 $params = array_merge( $params, $extraParams );
22 $res = $this->doApiRequest( $params );
24 $this->assertArrayNotHasKey( 'warnings', $res[0] );
25 $this->assertCount( 1, $res[0]['query'] );
27 return $res[0]['query'][$siprop === null ?
'general' : $siprop];
30 public function testGeneral() {
31 $this->setMwGlobals( [
32 'wgAllowExternalImagesFrom' => '//localhost/',
35 $data = $this->doQuery();
37 $this->assertSame( Title
::newMainPage()->getPrefixedText(), $data['mainpage'] );
38 $this->assertSame( PHP_VERSION
, $data['phpversion'] );
39 $this->assertSame( [ '//localhost/' ], $data['externalimages'] );
42 public function testLinkPrefixCharset() {
43 $contLang = Language
::factory( 'ar' );
44 $this->setContentLang( $contLang );
45 $this->assertTrue( $contLang->linkPrefixExtension(), 'Sanity check' );
47 $data = $this->doQuery();
49 $this->assertSame( $contLang->linkPrefixCharset(), $data['linkprefixcharset'] );
52 public function testVariants() {
53 $contLang = Language
::factory( 'zh' );
54 $this->setContentLang( $contLang );
55 $this->assertTrue( $contLang->hasVariants(), 'Sanity check' );
57 $data = $this->doQuery();
59 $expected = array_map(
60 function ( $code ) use ( $contLang ) {
61 return [ 'code' => $code, 'name' => $contLang->getVariantname( $code ) ];
63 $contLang->getVariants()
66 $this->assertSame( $expected, $data['variants'] );
69 public function testReadOnly() {
70 $svc = MediaWikiServices
::getInstance()->getReadOnlyMode();
71 $svc->setReason( 'Need more donations' );
73 $data = $this->doQuery();
75 $svc->setReason( false );
78 $this->assertTrue( $data['readonly'] );
79 $this->assertSame( 'Need more donations', $data['readonlyreason'] );
82 public function testNamespaces() {
83 $this->setMwGlobals( 'wgExtraNamespaces', [ '138' => 'Testing' ] );
86 array_keys( MediaWikiServices
::getInstance()->getContentLanguage()->getFormattedNamespaces() ),
87 array_keys( $this->doQuery( 'namespaces' ) )
91 public function testNamespaceAliases() {
92 global $wgNamespaceAliases;
94 $expected = array_merge(
96 MediaWikiServices
::getInstance()->getContentLanguage()->getNamespaceAliases()
98 $expected = array_map(
99 function ( $key, $val ) {
100 return [ 'id' => $val, 'alias' => strtr( $key, '_', ' ' ) ];
102 array_keys( $expected ),
106 // Test that we don't list duplicates
107 $this->mergeMwGlobalArrayValue( 'wgNamespaceAliases', [ 'Talk' => NS_TALK
] );
109 $this->assertSame( $expected, $this->doQuery( 'namespacealiases' ) );
112 public function testSpecialPageAliases() {
114 count( MediaWikiServices
::getInstance()->getSpecialPageFactory()->getNames() ),
115 $this->doQuery( 'specialpagealiases' )
119 public function testMagicWords() {
121 count( MediaWikiServices
::getInstance()->getContentLanguage()->getMagicWords() ),
122 $this->doQuery( 'magicwords' )
127 * @dataProvider interwikiMapProvider
129 public function testInterwikiMap( $filter ) {
130 global $wgServer, $wgScriptPath;
132 $dbw = wfGetDB( DB_MASTER
);
137 'iw_prefix' => 'self',
138 'iw_url' => "$wgServer$wgScriptPath/index.php?title=$1",
139 'iw_api' => "$wgServer$wgScriptPath/api.php",
140 'iw_wikiid' => 'somedbname',
145 'iw_prefix' => 'foreign',
146 'iw_url' => '//foreign.example/wiki/$1',
156 $this->tablesUsed
[] = 'interwiki';
158 $this->setMwGlobals( [
159 'wgLocalInterwikis' => [ 'self' ],
160 'wgExtraInterlanguageLinkPrefixes' => [ 'self' ],
161 'wgExtraLanguageNames' => [ 'self' => 'Recursion' ],
164 MessageCache
::singleton()->enable();
166 $this->editPage( 'MediaWiki:Interlanguage-link-self', 'Self!' );
167 $this->editPage( 'MediaWiki:Interlanguage-link-sitename-self', 'Circular logic' );
171 if ( $filter === null ||
$filter === '!local' ) {
173 'prefix' => 'foreign',
174 'url' => wfExpandUrl( '//foreign.example/wiki/$1', PROTO_CURRENT
),
178 if ( $filter === null ||
$filter === 'local' ) {
183 'language' => 'Recursion',
184 'localinterwiki' => true,
185 'extralanglink' => true,
186 'linktext' => 'Self!',
187 'sitename' => 'Circular logic',
188 'url' => "$wgServer$wgScriptPath/index.php?title=$1",
190 'wikiid' => 'somedbname',
191 'api' => "$wgServer$wgScriptPath/api.php",
195 $data = $this->doQuery( 'interwikimap',
196 $filter === null ?
[] : [ 'sifilteriw' => $filter ] );
198 $this->assertSame( $expected, $data );
201 public function interwikiMapProvider() {
202 return [ [ 'local' ], [ '!local' ], [ null ] ];
206 * @dataProvider dbReplLagProvider
208 public function testDbReplLagInfo( $showHostnames, $includeAll ) {
209 if ( !$showHostnames && $includeAll ) {
210 $this->setExpectedApiException( 'apierror-siteinfo-includealldenied' );
213 $mockLB = $this->getMockBuilder( LoadBalancer
::class )
214 ->disableOriginalConstructor()
215 ->setMethods( [ 'getMaxLag', 'getLagTimes', 'getServerName', '__destruct' ] )
217 $mockLB->method( 'getMaxLag' )->willReturn( [ null, 7, 1 ] );
218 $mockLB->method( 'getLagTimes' )->willReturn( [ 5, 7 ] );
219 $mockLB->method( 'getServerName' )->will( $this->returnValueMap( [
220 [ 0, 'apple' ], [ 1, 'carrot' ]
222 $this->setService( 'DBLoadBalancer', $mockLB );
224 $this->setMwGlobals( 'wgShowHostnames', $showHostnames );
228 $expected[] = [ 'host' => $showHostnames ?
'apple' : '', 'lag' => 5 ];
230 $expected[] = [ 'host' => $showHostnames ?
'carrot' : '', 'lag' => 7 ];
232 $data = $this->doQuery( 'dbrepllag', $includeAll ?
[ 'sishowalldb' => '' ] : [] );
234 $this->assertSame( $expected, $data );
237 public function dbReplLagProvider() {
239 'no hostnames, no showalldb' => [ false, false ],
240 'no hostnames, showalldb' => [ false, true ],
241 'hostnames, no showalldb' => [ true, false ],
242 'hostnames, showalldb' => [ true, true ]
246 public function testStatistics() {
247 $this->setTemporaryHook( 'APIQuerySiteInfoStatisticsInfo',
248 function ( &$data ) {
249 $data['addedstats'] = 42;
254 'pages' => intval( SiteStats
::pages() ),
255 'articles' => intval( SiteStats
::articles() ),
256 'edits' => intval( SiteStats
::edits() ),
257 'images' => intval( SiteStats
::images() ),
258 'users' => intval( SiteStats
::users() ),
259 'activeusers' => intval( SiteStats
::activeUsers() ),
260 'admins' => intval( SiteStats
::numberingroup( 'sysop' ) ),
261 'jobs' => intval( SiteStats
::jobs() ),
265 $this->assertSame( $expected, $this->doQuery( 'statistics' ) );
269 * @dataProvider groupsProvider
271 public function testUserGroups( $numInGroup ) {
272 global $wgGroupPermissions, $wgAutopromote;
274 $this->setGroupPermissions( 'viscount', 'perambulate', 'yes' );
275 $this->setGroupPermissions( 'viscount', 'legislate', '0' );
276 $this->setMwGlobals( [
277 'wgAddGroups' => [ 'viscount' => true, 'bot' => [] ],
278 'wgRemoveGroups' => [ 'viscount' => [ 'sysop' ], 'bot' => [ '*', 'earl' ] ],
279 'wgGroupsAddToSelf' => [ 'bot' => [ 'bureaucrat', 'sysop' ] ],
280 'wgGroupsRemoveFromSelf' => [ 'bot' => [ 'bot' ] ],
283 $data = $this->doQuery( 'usergroups', $numInGroup ?
[ 'sinumberingroup' => '' ] : [] );
292 $this->assertSame( array_keys( $wgGroupPermissions ), $names );
294 foreach ( $data as $val ) {
295 if ( !$numInGroup ) {
296 $expectedSize = null;
297 } elseif ( $val['name'] === 'user' ) {
298 $expectedSize = SiteStats
::users();
299 } elseif ( $val['name'] === '*' ||
isset( $wgAutopromote[$val['name']] ) ) {
300 $expectedSize = null;
302 $expectedSize = SiteStats
::numberingroup( $val['name'] );
305 if ( $expectedSize === null ) {
306 $this->assertArrayNotHasKey( 'number', $val );
308 $this->assertSame( $expectedSize, $val['number'] );
311 if ( $val['name'] === 'viscount' ) {
312 $viscountFound = true;
313 $this->assertSame( [ 'perambulate' ], $val['rights'] );
314 $this->assertSame( User
::getAllGroups(), $val['add'] );
315 } elseif ( $val['name'] === 'bot' ) {
316 $this->assertArrayNotHasKey( 'add', $val );
317 $this->assertArrayNotHasKey( 'remove', $val );
318 $this->assertSame( [ 'bureaucrat', 'sysop' ], $val['add-self'] );
319 $this->assertSame( [ 'bot' ], $val['remove-self'] );
324 public function testFileExtensions() {
325 global $wgFileExtensions;
328 $this->setMwGlobals( 'wgFileExtensions', array_merge( $wgFileExtensions, [ 'png' ] ) );
330 $expected = array_map(
332 return [ 'ext' => $val ];
334 array_unique( $wgFileExtensions )
337 $this->assertSame( $expected, $this->doQuery( 'fileextensions' ) );
340 public function groupsProvider() {
342 'numingroup' => [ true ],
343 'nonumingroup' => [ false ],
347 public function testInstalledLibraries() {
348 // @todo Test no installed.json? Moving installed.json to a different name temporarily
349 // seems a bit scary, but I don't see any other way to do it.
351 // @todo Install extensions/skins somehow so that we can test they're filtered out
354 $path = "$IP/vendor/composer/installed.json";
355 if ( !file_exists( $path ) ) {
356 $this->markTestSkipped( 'No installed libraries' );
359 $expected = ( new ComposerInstalled( $path ) )->getInstalledDependencies();
361 $expected = array_filter( $expected,
363 return strpos( $info['type'], 'mediawiki-' ) !== 0;
367 $expected = array_map(
368 function ( $name, $info ) {
369 return [ 'name' => $name, 'version' => $info['version'] ];
371 array_keys( $expected ),
372 array_values( $expected )
375 $this->assertSame( $expected, $this->doQuery( 'libraries' ) );
378 public function testExtensions() {
379 $tmpdir = $this->getNewTempDirectory();
380 touch( "$tmpdir/ErsatzExtension.php" );
381 touch( "$tmpdir/LICENSE" );
382 touch( "$tmpdir/AUTHORS.txt" );
385 'path' => "$tmpdir/ErsatzExtension.php",
386 'name' => 'Ersatz Extension',
387 'namemsg' => 'ersatz-extension-name',
388 'author' => 'John Smith',
389 'version' => '0.0.2',
390 'url' => 'https://www.example.com/software/ersatz-extension',
391 'description' => 'An extension that is not what it seems.',
392 'descriptionmsg' => 'ersatz-extension-desc',
393 'license-name' => 'PD',
396 $this->setMwGlobals( 'wgExtensionCredits', [ 'api' => [
399 'author' => [ 'John Smith', 'John Smith Jr.', '...' ],
400 'descriptionmsg' => [ 'another-extension-desc', 'param' ] ],
403 $data = $this->doQuery( 'extensions' );
405 $this->assertCount( 2, $data );
407 $this->assertSame( 'api', $data[0]['type'] );
409 $sharedKeys = [ 'name', 'namemsg', 'description', 'descriptionmsg', 'author', 'url',
410 'version', 'license-name' ];
411 foreach ( $sharedKeys as $key ) {
412 $this->assertSame( $val[$key], $data[0][$key] );
415 // @todo Test git info
418 Title
::newFromText( 'Special:Version/License/Ersatz Extension' )->getLinkURL(),
423 Title
::newFromText( 'Special:Version/Credits/Ersatz Extension' )->getLinkURL(),
427 $this->assertSame( 'another-extension-desc', $data[1]['descriptionmsg'] );
428 $this->assertSame( [ 'param' ], $data[1]['descriptionmsgparams'] );
429 $this->assertSame( 'John Smith, John Smith Jr., ...', $data[1]['author'] );
433 * @dataProvider rightsInfoProvider
435 public function testRightsInfo( $page, $url, $text, $expectedUrl, $expectedText ) {
436 $this->setMwGlobals( [
437 'wgRightsPage' => $page,
438 'wgRightsUrl' => $url,
439 'wgRightsText' => $text,
443 [ 'url' => $expectedUrl, 'text' => $expectedText ],
444 $this->doQuery( 'rightsinfo' )
448 public function rightsInfoProvider() {
449 $textUrl = wfExpandUrl( Title
::newFromText( 'License' ), PROTO_CURRENT
);
450 $url = 'http://license.example/';
453 'No rights info' => [ null, null, null, '', '' ],
454 'Only page' => [ 'License', null, null, $textUrl, 'License' ],
455 'Only URL' => [ null, $url, null, $url, '' ],
456 'Only text' => [ null, null, '!!!', '', '!!!' ],
457 // URL is ignored if page is specified
458 'Page and URL' => [ 'License', $url, null, $textUrl, 'License' ],
459 'URL and text' => [ null, $url, '!!!', $url, '!!!' ],
460 'Page and text' => [ 'License', null, '!!!', $textUrl, '!!!' ],
461 'Page and URL and text' => [ 'License', $url, '!!!', $textUrl, '!!!' ],
462 'Pagename "0"' => [ '0', null, null,
463 wfExpandUrl( Title
::newFromText( '0' ), PROTO_CURRENT
), '0' ],
464 'URL "0"' => [ null, '0', null, '0', '' ],
465 'Text "0"' => [ null, null, '0', '', '0' ],
469 public function testRestrictions() {
470 global $wgRestrictionTypes, $wgRestrictionLevels, $wgCascadingRestrictionLevels,
471 $wgSemiprotectedRestrictionLevels;
474 'types' => $wgRestrictionTypes,
475 'levels' => $wgRestrictionLevels,
476 'cascadinglevels' => $wgCascadingRestrictionLevels,
477 'semiprotectedlevels' => $wgSemiprotectedRestrictionLevels,
478 ], $this->doQuery( 'restrictions' ) );
482 * @dataProvider languagesProvider
484 public function testLanguages( $langCode ) {
485 $expected = Language
::fetchLanguageNames( (string)$langCode );
487 $expected = array_map(
488 function ( $code, $name ) {
491 'bcp47' => LanguageCode
::bcp47( $code ),
495 array_keys( $expected ),
496 array_values( $expected )
499 $data = $this->doQuery( 'languages',
500 $langCode !== null ?
[ 'siinlanguagecode' => $langCode ] : [] );
502 $this->assertSame( $expected, $data );
505 public function languagesProvider() {
506 return [ [ null ], [ 'fr' ] ];
509 public function testLanguageVariants() {
510 $expectedKeys = array_filter( LanguageConverter
::$languagesWithVariants,
511 function ( $langCode ) {
512 return !Language
::factory( $langCode )->getConverter() instanceof FakeConverter
;
515 sort( $expectedKeys );
517 $this->assertSame( $expectedKeys, array_keys( $this->doQuery( 'languagevariants' ) ) );
520 public function testLanguageVariantsDisabled() {
521 $this->setMwGlobals( 'wgDisableLangConversion', true );
523 $this->assertSame( [], $this->doQuery( 'languagevariants' ) );
527 * @todo Test a skin with a description that's known to be different in a different language.
528 * Vector will do, but it's not installed by default.
530 * @todo Test that an invalid language code doesn't actually try reading any messages
532 * @dataProvider skinsProvider
534 public function testSkins( $code ) {
535 $data = $this->doQuery( 'skins', $code !== null ?
[ 'siinlanguagecode' => $code ] : [] );
537 $expectedAllowed = Skin
::getAllowedSkins();
538 $expectedDefault = Skin
::normalizeKey( 'default' );
541 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
542 $this->assertSame( $name, $data[$i]['code'] );
544 $msg = wfMessage( "skinname-$name" );
545 if ( $code && Language
::isValidCode( $code ) ) {
546 $msg->inLanguage( $code );
548 $msg->inContentLanguage();
550 if ( $msg->exists() ) {
551 $displayName = $msg->text();
553 $this->assertSame( $displayName, $data[$i]['name'] );
555 if ( !isset( $expectedAllowed[$name] ) ) {
556 $this->assertTrue( $data[$i]['unusable'], "$name must be unusable" );
558 if ( $name === $expectedDefault ) {
559 $this->assertTrue( $data[$i]['default'], "$expectedDefault must be default" );
565 public function skinsProvider() {
567 'No language specified' => [ null ],
569 'Invalid language' => [ '/invalid/' ],
573 public function testExtensionTags() {
574 $expected = array_map(
578 MediaWikiServices
::getInstance()->getParser()->getTags()
581 $this->assertSame( $expected, $this->doQuery( 'extensiontags' ) );
584 public function testFunctionHooks() {
585 $this->assertSame( MediaWikiServices
::getInstance()->getParser()->getFunctionHooks(),
586 $this->doQuery( 'functionhooks' ) );
589 public function testVariables() {
591 MediaWikiServices
::getInstance()->getMagicWordFactory()->getVariableIDs(),
592 $this->doQuery( 'variables' )
596 public function testProtocols() {
597 global $wgUrlProtocols;
599 $this->assertSame( $wgUrlProtocols, $this->doQuery( 'protocols' ) );
602 public function testDefaultOptions() {
603 $this->assertSame( User
::getDefaultOptions(), $this->doQuery( 'defaultoptions' ) );
606 public function testUploadDialog() {
607 global $wgUploadDialog;
609 $this->assertSame( $wgUploadDialog, $this->doQuery( 'uploaddialog' ) );
612 public function testGetHooks() {
615 // Make sure there's something to report on
616 $this->setTemporaryHook( 'somehook',
622 $expectedNames = $wgHooks;
623 ksort( $expectedNames );
625 $actualNames = array_map(
629 $this->doQuery( 'showhooks' )
632 $this->assertSame( array_keys( $expectedNames ), $actualNames );
635 public function testContinuation() {
636 // We make lots and lots of URL protocols that are each 100 bytes
637 global $wgAPIMaxResultSize, $wgUrlProtocols;
639 $this->setMwGlobals( 'wgUrlProtocols', [] );
641 // Just under the limit
642 $chunks = $wgAPIMaxResultSize / 100 - 1;
644 for ( $i = 0; $i < $chunks; $i++
) {
645 $wgUrlProtocols[] = substr( str_repeat( "$i ", 50 ), 0, 100 );
648 $res = $this->doApiRequest( [
650 'meta' => 'siteinfo',
651 'siprop' => 'protocols|languages',
655 wfMessage( 'apiwarn-truncatedresult', Message
::numParam( $wgAPIMaxResultSize ) )
657 $res[0]['warnings']['result']['warnings']
660 $this->assertSame( $wgUrlProtocols, $res[0]['query']['protocols'] );
661 $this->assertArrayNotHasKey( 'languages', $res[0] );
662 $this->assertTrue( $res[0]['batchcomplete'], 'batchcomplete should be true' );
663 $this->assertSame( [ 'siprop' => 'languages', 'continue' => '-||' ], $res[0]['continue'] );