| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Config\SiteConfiguration; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
use MediaWiki\Tests\Site\TestSites; |
| 6 |
use MediaWiki\WikiMap\WikiMap; |
| 7 |
use MediaWiki\WikiMap\WikiReference; |
| 8 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class WikiMapTest extends MediaWikiLangTestCase { |
| 16 |
|
| 17 |
protected function setUp(): void { |
| 18 |
parent::setUp(); |
| 19 |
|
| 20 |
$conf = new SiteConfiguration(); |
| 21 |
$conf->settings = [ |
| 22 |
'wgServer' => [ |
| 23 |
'enwiki' => 'http://en.example.org', |
| 24 |
'ruwiki' => '//ru.example.org', |
| 25 |
'nopathwiki' => '//nopath.example.org', |
| 26 |
'thiswiki' => '//this.wiki.org' |
| 27 |
], |
| 28 |
'wgArticlePath' => [ |
| 29 |
'enwiki' => '/w/$1', |
| 30 |
'ruwiki' => '/wiki/$1', |
| 31 |
], |
| 32 |
]; |
| 33 |
$conf->suffixes = [ 'wiki' ]; |
| 34 |
$this->setMwGlobals( 'wgConf', $conf ); |
| 35 |
$this->overrideConfigValues( [ |
| 36 |
MainConfigNames::LocalDatabases => [ 'enwiki', 'ruwiki', 'nopathwiki' ], |
| 37 |
MainConfigNames::CanonicalServer => '//this.wiki.org', |
| 38 |
MainConfigNames::DBname => 'thiswiki', |
| 39 |
MainConfigNames::DBprefix => '' |
| 40 |
] ); |
| 41 |
|
| 42 |
TestSites::insertIntoDb(); |
| 43 |
} |
| 44 |
|
| 45 |
public static function provideGetWiki() { |
| 46 |
|
| 47 |
$enwiki = new WikiReference( 'http://en.example.org', '/w/$1' ); |
| 48 |
$ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' ); |
| 49 |
|
| 50 |
|
| 51 |
$nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' ); |
| 52 |
|
| 53 |
$enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' ); |
| 54 |
|
| 55 |
return [ |
| 56 |
'unknown' => [ null, 'xyzzy' ], |
| 57 |
'enwiki (wgConf)' => [ $enwiki, 'enwiki' ], |
| 58 |
'ruwiki (wgConf)' => [ $ruwiki, 'ruwiki' ], |
| 59 |
'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ], |
| 60 |
'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ], |
| 61 |
'non MediaWiki site' => [ null, 'spam', false ], |
| 62 |
'boguswiki' => [ null, 'boguswiki' ], |
| 63 |
'nopathwiki' => [ null, 'nopathwiki' ], |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function testGetWiki( $expected, $wikiId, $useWgConf = true ) { |
| 71 |
if ( !$useWgConf ) { |
| 72 |
$this->setMwGlobals( [ |
| 73 |
'wgConf' => new SiteConfiguration(), |
| 74 |
] ); |
| 75 |
} |
| 76 |
|
| 77 |
$this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) ); |
| 78 |
} |
| 79 |
|
| 80 |
public static function provideGetWikiName() { |
| 81 |
return [ |
| 82 |
'unknown' => [ 'xyzzy', 'xyzzy' ], |
| 83 |
'enwiki' => [ 'en.example.org', 'enwiki' ], |
| 84 |
'ruwiki' => [ 'ru.example.org', 'ruwiki' ], |
| 85 |
'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ], |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function testGetWikiName( $expected, $wikiId ) { |
| 93 |
$this->assertEquals( $expected, WikiMap::getWikiName( $wikiId ) ); |
| 94 |
} |
| 95 |
|
| 96 |
public static function provideMakeForeignLink() { |
| 97 |
return [ |
| 98 |
'unknown' => [ false, 'xyzzy', 'Foo' ], |
| 99 |
'enwiki' => [ |
| 100 |
'<a class="external" rel="nofollow" ' . |
| 101 |
'href="http://en.example.org/w/Foo">Foo</a>', |
| 102 |
'enwiki', |
| 103 |
'Foo' |
| 104 |
], |
| 105 |
'ruwiki' => [ |
| 106 |
'<a class="external" rel="nofollow" ' . |
| 107 |
'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>', |
| 108 |
'ruwiki', |
| 109 |
'Фу', |
| 110 |
'вар' |
| 111 |
], |
| 112 |
'enwiktionary (sites)' => [ |
| 113 |
'<a class="external" rel="nofollow" ' . |
| 114 |
'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>', |
| 115 |
'enwiktionary', |
| 116 |
'Kitten', |
| 117 |
'Kittens!' |
| 118 |
], |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) { |
| 126 |
$this->assertEquals( |
| 127 |
$expected, |
| 128 |
WikiMap::makeForeignLink( $wikiId, $page, $text ) |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
public static function provideForeignUserLink() { |
| 133 |
return [ |
| 134 |
'unknown' => [ false, 'xyzzy', 'Foo' ], |
| 135 |
'enwiki' => [ |
| 136 |
'<a class="external" rel="nofollow" ' . |
| 137 |
'href="http://en.example.org/w/User:Foo">User:Foo</a>', |
| 138 |
'enwiki', |
| 139 |
'Foo' |
| 140 |
], |
| 141 |
'ruwiki' => [ |
| 142 |
'<a class="external" rel="nofollow" ' . |
| 143 |
'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>', |
| 144 |
'ruwiki', |
| 145 |
'Фу', |
| 146 |
'вар' |
| 147 |
], |
| 148 |
'enwiktionary (sites)' => [ |
| 149 |
'<a class="external" rel="nofollow" ' . |
| 150 |
'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>', |
| 151 |
'enwiktionary', |
| 152 |
'Dummy', |
| 153 |
'Whatever' |
| 154 |
], |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) { |
| 162 |
$this->assertEquals( $expected, WikiMap::foreignUserLink( $wikiId, $user, $text ) ); |
| 163 |
} |
| 164 |
|
| 165 |
public static function provideGetForeignURL() { |
| 166 |
return [ |
| 167 |
'unknown' => [ false, 'xyzzy', 'Foo' ], |
| 168 |
'enwiki' => [ 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ], |
| 169 |
'enwiktionary (sites)' => [ |
| 170 |
'https://en.wiktionary.org/wiki/Testme', |
| 171 |
'enwiktionary', |
| 172 |
'Testme' |
| 173 |
], |
| 174 |
'ruwiki with fragment' => [ |
| 175 |
'//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80', |
| 176 |
'ruwiki', |
| 177 |
'Фу', |
| 178 |
'вар' |
| 179 |
], |
| 180 |
]; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) { |
| 187 |
$this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function testGetCanonicalServerInfoForAllWikis() { |
| 194 |
$expected = [ |
| 195 |
'thiswiki' => [ |
| 196 |
'url' => '//this.wiki.org', |
| 197 |
'parts' => [ 'scheme' => '', 'host' => 'this.wiki.org', 'delimiter' => '//' ] |
| 198 |
], |
| 199 |
'enwiki' => [ |
| 200 |
'url' => 'http://en.example.org', |
| 201 |
'parts' => [ |
| 202 |
'scheme' => 'http', 'host' => 'en.example.org', 'delimiter' => '://' ] |
| 203 |
], |
| 204 |
'ruwiki' => [ |
| 205 |
'url' => '//ru.example.org', |
| 206 |
'parts' => [ 'scheme' => '', 'host' => 'ru.example.org', 'delimiter' => '//' ] |
| 207 |
] |
| 208 |
]; |
| 209 |
|
| 210 |
$this->assertArrayEquals( |
| 211 |
$expected, |
| 212 |
WikiMap::getCanonicalServerInfoForAllWikis(), |
| 213 |
true, |
| 214 |
true |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
public static function provideGetWikiFromUrl() { |
| 219 |
return [ |
| 220 |
[ 'http://this.wiki.org', 'thiswiki' ], |
| 221 |
[ 'https://this.wiki.org', 'thiswiki' ], |
| 222 |
[ 'http://this.wiki.org/$1', 'thiswiki' ], |
| 223 |
[ 'https://this.wiki.org/$2', 'thiswiki' ], |
| 224 |
[ 'http://en.example.org', 'enwiki' ], |
| 225 |
[ 'https://en.example.org', 'enwiki' ], |
| 226 |
[ 'http://en.example.org/$1', 'enwiki' ], |
| 227 |
[ 'https://en.example.org/$2', 'enwiki' ], |
| 228 |
[ 'http://ru.example.org', 'ruwiki' ], |
| 229 |
[ 'https://ru.example.org', 'ruwiki' ], |
| 230 |
[ 'http://ru.example.org/$1', 'ruwiki' ], |
| 231 |
[ 'https://ru.example.org/$2', 'ruwiki' ], |
| 232 |
[ 'http://not.defined.org', false ] |
| 233 |
]; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function testGetWikiFromUrl( $url, $wiki ) { |
| 241 |
$this->assertEquals( $wiki, WikiMap::getWikiFromUrl( $url ) ); |
| 242 |
} |
| 243 |
|
| 244 |
public static function provideGetWikiIdFromDbDomain() { |
| 245 |
return [ |
| 246 |
[ 'db-prefix_', 'db-prefix_' ], |
| 247 |
[ WikiMap::getCurrentWikiId(), WikiMap::getCurrentWikiId() ], |
| 248 |
[ new DatabaseDomain( 'db-dash', null, 'prefix_' ), 'db-dash-prefix_' ], |
| 249 |
[ WikiMap::getCurrentWikiId(), WikiMap::getCurrentWikiId() ], |
| 250 |
[ new DatabaseDomain( 'db-dash', null, 'prefix_' ), 'db-dash-prefix_' ], |
| 251 |
[ new DatabaseDomain( 'db', 'mediawiki', 'prefix_' ), 'db-prefix_' ], |
| 252 |
[ new DatabaseDomain( 'db', 'custom', 'prefix_' ), 'db-custom-prefix_' ], |
| 253 |
]; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function testGetWikiIdFromDbDomain( $domain, $wikiId ) { |
| 261 |
$this->assertEquals( $wikiId, WikiMap::getWikiIdFromDbDomain( $domain ) ); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function testIsCurrentWikiDomain() { |
| 269 |
$this->overrideConfigValue( MainConfigNames::DBmwschema, 'mediawiki' ); |
| 270 |
|
| 271 |
$localDomain = WikiMap::getCurrentWikiDbDomain()->getId(); |
| 272 |
$this->assertTrue( WikiMap::isCurrentWikiDbDomain( $localDomain ) ); |
| 273 |
|
| 274 |
$localDomain = DatabaseDomain::newFromId( $localDomain ); |
| 275 |
$domain1 = new DatabaseDomain( |
| 276 |
$localDomain->getDatabase(), 'someschema', $localDomain->getTablePrefix() ); |
| 277 |
$domain2 = new DatabaseDomain( |
| 278 |
$localDomain->getDatabase(), null, $localDomain->getTablePrefix() ); |
| 279 |
|
| 280 |
$this->assertFalse( WikiMap::isCurrentWikiDbDomain( $domain1 ), 'Schema not ignored' ); |
| 281 |
$this->assertFalse( WikiMap::isCurrentWikiDbDomain( $domain2 ), 'Null schema not ignored' ); |
| 282 |
|
| 283 |
$this->assertTrue( WikiMap::isCurrentWikiDbDomain( WikiMap::getCurrentWikiDbDomain() ) ); |
| 284 |
} |
| 285 |
|
| 286 |
public static function provideIsCurrentWikiId() { |
| 287 |
return [ |
| 288 |
[ 'db', 'db', null, '' ], |
| 289 |
[ 'db-schema-', 'db', 'schema', '' ], |
| 290 |
[ 'db', 'db', 'mediawiki', '' ], |
| 291 |
[ 'db-prefix_', 'db', null, 'prefix_' ], |
| 292 |
[ 'db-schema-prefix_', 'db', 'schema', 'prefix_' ], |
| 293 |
[ 'db-prefix_', 'db', 'mediawiki', 'prefix_' ], |
| 294 |
|
| 295 |
[ 'db-stuff', 'db-stuff', null, '' ], |
| 296 |
[ 'db-stuff-prefix_', 'db-stuff', null, 'prefix_' ], |
| 297 |
[ 'db-stuff-schema-', 'db-stuff', 'schema', '' ], |
| 298 |
[ 'db-stuff-schema-prefix_', 'db-stuff', 'schema', 'prefix_' ], |
| 299 |
[ 'db-stuff-prefix_', 'db-stuff', 'mediawiki', 'prefix_' ] |
| 300 |
]; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public function testIsCurrentWikiId( $wikiId, $db, $schema, $prefix ) { |
| 310 |
$this->overrideConfigValues( [ |
| 311 |
MainConfigNames::DBname => $db, |
| 312 |
MainConfigNames::DBmwschema => $schema, |
| 313 |
MainConfigNames::DBprefix => $prefix |
| 314 |
] ); |
| 315 |
|
| 316 |
$this->assertTrue( WikiMap::isCurrentWikiId( $wikiId ), "ID matches" ); |
| 317 |
$this->assertNotTrue( WikiMap::isCurrentWikiId( $wikiId . '-more' ), "Bogus ID" ); |
| 318 |
} |
| 319 |
} |