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 testNamespacesBasic() {
84 array_keys( MediaWikiServices
::getInstance()->getContentLanguage()->getFormattedNamespaces() ),
85 array_keys( $this->doQuery( 'namespaces' ) )
89 public function testNamespacesExtraNS() {
90 $this->setMwGlobals( 'wgExtraNamespaces', [ '138' => 'Testing' ] );
92 array_keys( MediaWikiServices
::getInstance()->getContentLanguage()->getFormattedNamespaces() ),
93 array_keys( $this->doQuery( 'namespaces' ) )
97 public function testNamespacesProtection() {
99 'wgNamespaceProtection',
103 '4' => 'editsemiprotected',
109 'move-categorypages',
114 $data = $this->doQuery( 'namespaces' );
115 $this->assertArrayNotHasKey( 'namespaceprotection', $data['0'] );
116 $this->assertArrayNotHasKey( 'namespaceprotection', $data['2'] );
117 $this->assertSame( 'editsemiprotected', $data['4']['namespaceprotection'] );
118 $this->assertSame( 'editinterface|noratelimit', $data['8']['namespaceprotection'] );
119 $this->assertSame( 'move-categorypages', $data['14']['namespaceprotection'] );
122 public function testNamespaceAliases() {
123 global $wgNamespaceAliases;
125 $expected = array_merge(
127 MediaWikiServices
::getInstance()->getContentLanguage()->getNamespaceAliases()
129 $expected = array_map(
130 function ( $key, $val ) {
131 return [ 'id' => $val, 'alias' => strtr( $key, '_', ' ' ) ];
133 array_keys( $expected ),
137 // Test that we don't list duplicates
138 $this->mergeMwGlobalArrayValue( 'wgNamespaceAliases', [ 'Talk' => NS_TALK
] );
140 $this->assertSame( $expected, $this->doQuery( 'namespacealiases' ) );
143 public function testSpecialPageAliases() {
145 count( MediaWikiServices
::getInstance()->getSpecialPageFactory()->getNames() ),
146 $this->doQuery( 'specialpagealiases' )
150 public function testMagicWords() {
152 count( MediaWikiServices
::getInstance()->getContentLanguage()->getMagicWords() ),
153 $this->doQuery( 'magicwords' )
158 * @dataProvider interwikiMapProvider
160 public function testInterwikiMap( $filter ) {
161 global $wgServer, $wgScriptPath;
163 $dbw = wfGetDB( DB_MASTER
);
168 'iw_prefix' => 'self',
169 'iw_url' => "$wgServer$wgScriptPath/index.php?title=$1",
170 'iw_api' => "$wgServer$wgScriptPath/api.php",
171 'iw_wikiid' => 'somedbname',
176 'iw_prefix' => 'foreign',
177 'iw_url' => '//foreign.example/wiki/$1',
187 $this->tablesUsed
[] = 'interwiki';
189 $this->setMwGlobals( [
190 'wgLocalInterwikis' => [ 'self' ],
191 'wgExtraInterlanguageLinkPrefixes' => [ 'self' ],
192 'wgExtraLanguageNames' => [ 'self' => 'Recursion' ],
195 MessageCache
::singleton()->enable();
197 $this->editPage( 'MediaWiki:Interlanguage-link-self', 'Self!' );
198 $this->editPage( 'MediaWiki:Interlanguage-link-sitename-self', 'Circular logic' );
202 if ( $filter === null ||
$filter === '!local' ) {
204 'prefix' => 'foreign',
205 'url' => wfExpandUrl( '//foreign.example/wiki/$1', PROTO_CURRENT
),
209 if ( $filter === null ||
$filter === 'local' ) {
214 'language' => 'Recursion',
215 'localinterwiki' => true,
216 'extralanglink' => true,
217 'linktext' => 'Self!',
218 'sitename' => 'Circular logic',
219 'url' => "$wgServer$wgScriptPath/index.php?title=$1",
221 'wikiid' => 'somedbname',
222 'api' => "$wgServer$wgScriptPath/api.php",
226 $data = $this->doQuery( 'interwikimap',
227 $filter === null ?
[] : [ 'sifilteriw' => $filter ] );
229 $this->assertSame( $expected, $data );
232 public function interwikiMapProvider() {
233 return [ [ 'local' ], [ '!local' ], [ null ] ];
237 * @dataProvider dbReplLagProvider
239 public function testDbReplLagInfo( $showHostnames, $includeAll ) {
240 if ( !$showHostnames && $includeAll ) {
241 $this->setExpectedApiException( 'apierror-siteinfo-includealldenied' );
244 $mockLB = $this->createMock( LoadBalancer
::class );
245 $mockLB->method( 'getMaxLag' )->willReturn( [ null, 7, 1 ] );
246 $mockLB->method( 'getLagTimes' )->willReturn( [ 5, 7 ] );
247 $mockLB->method( 'getServerName' )->will( $this->returnValueMap( [
248 [ 0, 'apple' ], [ 1, 'carrot' ]
250 $mockLB->method( 'getLocalDomainID' )->willReturn( 'testdomain' );
251 $mockLB->expects( $this->never() )->method( $this->anythingBut(
252 'getMaxLag', 'getLagTimes', 'getServerName', 'getLocalDomainID', '__destruct'
254 $this->setService( 'DBLoadBalancer', $mockLB );
256 $this->setMwGlobals( 'wgShowHostnames', $showHostnames );
260 $expected[] = [ 'host' => $showHostnames ?
'apple' : '', 'lag' => 5 ];
262 $expected[] = [ 'host' => $showHostnames ?
'carrot' : '', 'lag' => 7 ];
264 $data = $this->doQuery( 'dbrepllag', $includeAll ?
[ 'sishowalldb' => '' ] : [] );
266 $this->assertSame( $expected, $data );
269 public function dbReplLagProvider() {
271 'no hostnames, no showalldb' => [ false, false ],
272 'no hostnames, showalldb' => [ false, true ],
273 'hostnames, no showalldb' => [ true, false ],
274 'hostnames, showalldb' => [ true, true ]
278 public function testStatistics() {
279 $this->setTemporaryHook( 'APIQuerySiteInfoStatisticsInfo',
280 function ( &$data ) {
281 $data['addedstats'] = 42;
286 'pages' => intval( SiteStats
::pages() ),
287 'articles' => intval( SiteStats
::articles() ),
288 'edits' => intval( SiteStats
::edits() ),
289 'images' => intval( SiteStats
::images() ),
290 'users' => intval( SiteStats
::users() ),
291 'activeusers' => intval( SiteStats
::activeUsers() ),
292 'admins' => intval( SiteStats
::numberingroup( 'sysop' ) ),
293 'jobs' => intval( SiteStats
::jobs() ),
297 $this->assertSame( $expected, $this->doQuery( 'statistics' ) );
301 * @dataProvider groupsProvider
303 public function testUserGroups( $numInGroup ) {
304 global $wgGroupPermissions, $wgAutopromote;
306 $this->setGroupPermissions( 'viscount', 'perambulate', 'yes' );
307 $this->setGroupPermissions( 'viscount', 'legislate', '0' );
308 $this->setMwGlobals( [
309 'wgAddGroups' => [ 'viscount' => true, 'bot' => [] ],
310 'wgRemoveGroups' => [ 'viscount' => [ 'sysop' ], 'bot' => [ '*', 'earl' ] ],
311 'wgGroupsAddToSelf' => [ 'bot' => [ 'bureaucrat', 'sysop' ] ],
312 'wgGroupsRemoveFromSelf' => [ 'bot' => [ 'bot' ] ],
315 $data = $this->doQuery( 'usergroups', $numInGroup ?
[ 'sinumberingroup' => '' ] : [] );
324 $this->assertSame( array_keys( $wgGroupPermissions ), $names );
326 foreach ( $data as $val ) {
327 if ( !$numInGroup ) {
328 $expectedSize = null;
329 } elseif ( $val['name'] === 'user' ) {
330 $expectedSize = SiteStats
::users();
331 } elseif ( $val['name'] === '*' ||
isset( $wgAutopromote[$val['name']] ) ) {
332 $expectedSize = null;
334 $expectedSize = SiteStats
::numberingroup( $val['name'] );
337 if ( $expectedSize === null ) {
338 $this->assertArrayNotHasKey( 'number', $val );
340 $this->assertSame( $expectedSize, $val['number'] );
343 if ( $val['name'] === 'viscount' ) {
344 $viscountFound = true;
345 $this->assertSame( [ 'perambulate' ], $val['rights'] );
346 $this->assertSame( User
::getAllGroups(), $val['add'] );
347 } elseif ( $val['name'] === 'bot' ) {
348 $this->assertArrayNotHasKey( 'add', $val );
349 $this->assertArrayNotHasKey( 'remove', $val );
350 $this->assertSame( [ 'bureaucrat', 'sysop' ], $val['add-self'] );
351 $this->assertSame( [ 'bot' ], $val['remove-self'] );
356 public function testFileExtensions() {
357 global $wgFileExtensions;
360 $this->setMwGlobals( 'wgFileExtensions', array_merge( $wgFileExtensions, [ 'png' ] ) );
362 $expected = array_map(
364 return [ 'ext' => $val ];
366 array_unique( $wgFileExtensions )
369 $this->assertSame( $expected, $this->doQuery( 'fileextensions' ) );
372 public function groupsProvider() {
374 'numingroup' => [ true ],
375 'nonumingroup' => [ false ],
379 public function testInstalledLibraries() {
380 // @todo Test no installed.json? Moving installed.json to a different name temporarily
381 // seems a bit scary, but I don't see any other way to do it.
383 // @todo Install extensions/skins somehow so that we can test they're filtered out
386 $path = "$IP/vendor/composer/installed.json";
387 if ( !file_exists( $path ) ) {
388 $this->markTestSkipped( 'No installed libraries' );
391 $expected = ( new ComposerInstalled( $path ) )->getInstalledDependencies();
393 $expected = array_filter( $expected,
395 return strpos( $info['type'], 'mediawiki-' ) !== 0;
399 $expected = array_map(
400 function ( $name, $info ) {
401 return [ 'name' => $name, 'version' => $info['version'] ];
403 array_keys( $expected ),
404 array_values( $expected )
407 $this->assertSame( $expected, $this->doQuery( 'libraries' ) );
410 public function testExtensions() {
411 $tmpdir = $this->getNewTempDirectory();
412 touch( "$tmpdir/ErsatzExtension.php" );
413 touch( "$tmpdir/LICENSE" );
414 touch( "$tmpdir/AUTHORS.txt" );
417 'path' => "$tmpdir/ErsatzExtension.php",
418 'name' => 'Ersatz Extension',
419 'namemsg' => 'ersatz-extension-name',
420 'author' => 'John Smith',
421 'version' => '0.0.2',
422 'url' => 'https://www.example.com/software/ersatz-extension',
423 'description' => 'An extension that is not what it seems.',
424 'descriptionmsg' => 'ersatz-extension-desc',
425 'license-name' => 'PD',
428 $this->setMwGlobals( 'wgExtensionCredits', [ 'api' => [
431 'author' => [ 'John Smith', 'John Smith Jr.', '...' ],
432 'descriptionmsg' => [ 'another-extension-desc', 'param' ] ],
435 $data = $this->doQuery( 'extensions' );
437 $this->assertCount( 2, $data );
439 $this->assertSame( 'api', $data[0]['type'] );
441 $sharedKeys = [ 'name', 'namemsg', 'description', 'descriptionmsg', 'author', 'url',
442 'version', 'license-name' ];
443 foreach ( $sharedKeys as $key ) {
444 $this->assertSame( $val[$key], $data[0][$key] );
447 // @todo Test git info
450 Title
::newFromText( 'Special:Version/License/Ersatz Extension' )->getLinkURL(),
455 Title
::newFromText( 'Special:Version/Credits/Ersatz Extension' )->getLinkURL(),
459 $this->assertSame( 'another-extension-desc', $data[1]['descriptionmsg'] );
460 $this->assertSame( [ 'param' ], $data[1]['descriptionmsgparams'] );
461 $this->assertSame( 'John Smith, John Smith Jr., ...', $data[1]['author'] );
465 * @dataProvider rightsInfoProvider
467 public function testRightsInfo( $page, $url, $text, $expectedUrl, $expectedText ) {
468 $this->setMwGlobals( [
469 'wgRightsPage' => $page,
470 'wgRightsUrl' => $url,
471 'wgRightsText' => $text,
475 [ 'url' => $expectedUrl, 'text' => $expectedText ],
476 $this->doQuery( 'rightsinfo' )
480 public function rightsInfoProvider() {
481 $textUrl = wfExpandUrl( Title
::newFromText( 'License' ), PROTO_CURRENT
);
482 $url = 'http://license.example/';
485 'No rights info' => [ null, null, null, '', '' ],
486 'Only page' => [ 'License', null, null, $textUrl, 'License' ],
487 'Only URL' => [ null, $url, null, $url, '' ],
488 'Only text' => [ null, null, '!!!', '', '!!!' ],
489 // URL is ignored if page is specified
490 'Page and URL' => [ 'License', $url, null, $textUrl, 'License' ],
491 'URL and text' => [ null, $url, '!!!', $url, '!!!' ],
492 'Page and text' => [ 'License', null, '!!!', $textUrl, '!!!' ],
493 'Page and URL and text' => [ 'License', $url, '!!!', $textUrl, '!!!' ],
494 'Pagename "0"' => [ '0', null, null,
495 wfExpandUrl( Title
::newFromText( '0' ), PROTO_CURRENT
), '0' ],
496 'URL "0"' => [ null, '0', null, '0', '' ],
497 'Text "0"' => [ null, null, '0', '', '0' ],
501 public function testRestrictions() {
502 global $wgRestrictionTypes, $wgRestrictionLevels, $wgCascadingRestrictionLevels,
503 $wgSemiprotectedRestrictionLevels;
506 'types' => $wgRestrictionTypes,
507 'levels' => $wgRestrictionLevels,
508 'cascadinglevels' => $wgCascadingRestrictionLevels,
509 'semiprotectedlevels' => $wgSemiprotectedRestrictionLevels,
510 ], $this->doQuery( 'restrictions' ) );
514 * @dataProvider languagesProvider
516 public function testLanguages( $langCode ) {
517 $expected = Language
::fetchLanguageNames( (string)$langCode );
519 $expected = array_map(
520 function ( $code, $name ) {
523 'bcp47' => LanguageCode
::bcp47( $code ),
527 array_keys( $expected ),
528 array_values( $expected )
531 $data = $this->doQuery( 'languages',
532 $langCode !== null ?
[ 'siinlanguagecode' => $langCode ] : [] );
534 $this->assertSame( $expected, $data );
537 public function languagesProvider() {
538 return [ [ null ], [ 'fr' ] ];
541 public function testLanguageVariants() {
542 $expectedKeys = array_filter( LanguageConverter
::$languagesWithVariants,
543 function ( $langCode ) {
544 return !Language
::factory( $langCode )->getConverter() instanceof FakeConverter
;
547 sort( $expectedKeys );
549 $this->assertSame( $expectedKeys, array_keys( $this->doQuery( 'languagevariants' ) ) );
552 public function testLanguageVariantsDisabled() {
553 $this->setMwGlobals( 'wgDisableLangConversion', true );
555 $this->assertSame( [], $this->doQuery( 'languagevariants' ) );
559 * @todo Test a skin with a description that's known to be different in a different language.
560 * Vector will do, but it's not installed by default.
562 * @todo Test that an invalid language code doesn't actually try reading any messages
564 * @dataProvider skinsProvider
566 public function testSkins( $code ) {
567 $data = $this->doQuery( 'skins', $code !== null ?
[ 'siinlanguagecode' => $code ] : [] );
569 $expectedAllowed = Skin
::getAllowedSkins();
570 $expectedDefault = Skin
::normalizeKey( 'default' );
573 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
574 $this->assertSame( $name, $data[$i]['code'] );
576 $msg = wfMessage( "skinname-$name" );
577 if ( $code && Language
::isValidCode( $code ) ) {
578 $msg->inLanguage( $code );
580 $msg->inContentLanguage();
582 if ( $msg->exists() ) {
583 $displayName = $msg->text();
585 $this->assertSame( $displayName, $data[$i]['name'] );
587 if ( !isset( $expectedAllowed[$name] ) ) {
588 $this->assertTrue( $data[$i]['unusable'], "$name must be unusable" );
590 if ( $name === $expectedDefault ) {
591 $this->assertTrue( $data[$i]['default'], "$expectedDefault must be default" );
597 public function skinsProvider() {
599 'No language specified' => [ null ],
601 'Invalid language' => [ '/invalid/' ],
605 public function testExtensionTags() {
606 $expected = array_map(
610 MediaWikiServices
::getInstance()->getParser()->getTags()
613 $this->assertSame( $expected, $this->doQuery( 'extensiontags' ) );
616 public function testFunctionHooks() {
617 $this->assertSame( MediaWikiServices
::getInstance()->getParser()->getFunctionHooks(),
618 $this->doQuery( 'functionhooks' ) );
621 public function testVariables() {
623 MediaWikiServices
::getInstance()->getMagicWordFactory()->getVariableIDs(),
624 $this->doQuery( 'variables' )
628 public function testProtocols() {
629 global $wgUrlProtocols;
631 $this->assertSame( $wgUrlProtocols, $this->doQuery( 'protocols' ) );
634 public function testDefaultOptions() {
635 $this->assertSame( User
::getDefaultOptions(), $this->doQuery( 'defaultoptions' ) );
638 public function testUploadDialog() {
639 global $wgUploadDialog;
641 $this->assertSame( $wgUploadDialog, $this->doQuery( 'uploaddialog' ) );
644 public function testGetHooks() {
647 // Make sure there's something to report on
648 $this->setTemporaryHook( 'somehook',
654 $expectedNames = $wgHooks;
655 ksort( $expectedNames );
657 $actualNames = array_map(
661 $this->doQuery( 'showhooks' )
664 $this->assertSame( array_keys( $expectedNames ), $actualNames );
667 public function testContinuation() {
668 // Use $wgUrlProtocols to forge the size of the API query
669 global $wgAPIMaxResultSize, $wgUrlProtocols;
671 $protocol = 'foo://';
673 $this->setMwGlobals( 'wgUrlProtocols', [ $protocol ] );
674 $this->setMwGlobals( 'wgAPIMaxResultSize', strlen( $protocol ) );
676 $res = $this->doApiRequest( [
678 'meta' => 'siteinfo',
679 'siprop' => 'protocols|languages',
683 wfMessage( 'apiwarn-truncatedresult', Message
::numParam( $wgAPIMaxResultSize ) )
685 $res[0]['warnings']['result']['warnings']
688 $this->assertSame( $wgUrlProtocols, $res[0]['query']['protocols'] );
689 $this->assertArrayNotHasKey( 'languages', $res[0] );
690 $this->assertTrue( $res[0]['batchcomplete'], 'batchcomplete should be true' );
691 $this->assertSame( [ 'siprop' => 'languages', 'continue' => '-||' ], $res[0]['continue'] );