Index: trunk/phase3/tests/phpunit/includes/MWNamespaceTest.php |
— | — | @@ -0,0 +1,441 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * @author Ashar Voultoiz |
| 5 | + * @copyright Copyright © 2011, Ashar Voultoiz |
| 6 | + * @file |
| 7 | + */ |
| 8 | + |
| 9 | +require_once '/var/www/mediawiki-trunk/includes/Namespace.php'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test class for MWNamespace. |
| 13 | + * Generated by PHPUnit on 2011-02-20 at 21:01:55. |
| 14 | + * |
| 15 | + */ |
| 16 | +class MWNamespaceTest extends PHPUnit_Framework_TestCase { |
| 17 | + /** |
| 18 | + * @var MWNamespace |
| 19 | + */ |
| 20 | + protected $object; |
| 21 | + |
| 22 | + /** |
| 23 | + * Sets up the fixture, for example, opens a network connection. |
| 24 | + * This method is called before a test is executed. |
| 25 | + */ |
| 26 | + protected function setUp() { |
| 27 | + $this->object = new MWNamespace; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Tears down the fixture, for example, closes a network connection. |
| 32 | + * This method is called after a test is executed. |
| 33 | + */ |
| 34 | + protected function tearDown() { |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +#### START OF TESTS ######################################################### |
| 39 | + |
| 40 | + /** |
| 41 | + * @todo Write more texts, handle $wgAllowImageMoving setting |
| 42 | + */ |
| 43 | + public function testIsMovable() { |
| 44 | + $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) ); |
| 45 | + # FIXME : write more tests!! |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Please make sure to change testIsTalk() if you change the assertions below |
| 50 | + */ |
| 51 | + public function testIsMain() { |
| 52 | + // Special namespaces |
| 53 | + $this->assertTrue( MWNamespace::isMain( NS_MEDIA ) ); |
| 54 | + $this->assertTrue( MWNamespace::isMain( NS_SPECIAL ) ); |
| 55 | + |
| 56 | + // Subject pages |
| 57 | + $this->assertTrue( MWNamespace::isMain( NS_MAIN ) ); |
| 58 | + $this->assertTrue( MWNamespace::isMain( NS_USER ) ); |
| 59 | + $this->assertTrue( MWNamespace::isMain( 100 ) ); # user defined |
| 60 | + |
| 61 | + // Talk pages |
| 62 | + $this->assertFalse( MWNamespace::isMain( NS_TALK ) ); |
| 63 | + $this->assertFalse( MWNamespace::isMain( NS_USER_TALK ) ); |
| 64 | + $this->assertFalse( MWNamespace::isMain( 101 ) ); # user defined |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Reverse of testIsMain(). |
| 69 | + * Please update testIsMain() if you change assertions below |
| 70 | + */ |
| 71 | + public function testIsTalk() { |
| 72 | + // Special namespaces |
| 73 | + $this->assertFalse( MWNamespace::isTalk( NS_MEDIA ) ); |
| 74 | + $this->assertFalse( MWNamespace::isTalk( NS_SPECIAL ) ); |
| 75 | + |
| 76 | + // Subject pages |
| 77 | + $this->assertFalse( MWNamespace::isTalk( NS_MAIN ) ); |
| 78 | + $this->assertFalse( MWNamespace::isTalk( NS_USER ) ); |
| 79 | + $this->assertFalse( MWNamespace::isTalk( 100 ) ); # user defined |
| 80 | + |
| 81 | + // Talk pages |
| 82 | + $this->assertTrue( MWNamespace::isTalk( NS_TALK ) ); |
| 83 | + $this->assertTrue( MWNamespace::isTalk( NS_USER_TALK ) ); |
| 84 | + $this->assertTrue( MWNamespace::isTalk( 101 ) ); # user defined |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Regular getTalk() calls |
| 89 | + * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in |
| 90 | + * the function testGetTalkExceptions() |
| 91 | + */ |
| 92 | + public function testGetTalk() { |
| 93 | + $this->assertEquals( MWNamespace::getTalk( NS_MAIN), NS_TALK ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Exceptions with getTalk() |
| 98 | + * NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raise an exception for them. |
| 99 | + * @expectedException MWException |
| 100 | + */ |
| 101 | + public function testGetTalkExceptions() { |
| 102 | + $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) ); |
| 103 | + $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Regular getAssociated() calls |
| 108 | + * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in |
| 109 | + * the function testGetAssociatedExceptions() |
| 110 | + */ |
| 111 | + public function testGetAssociated() { |
| 112 | + $this->assertEquals( MWNamespace::getAssociated( NS_MAIN ), NS_TALK ); |
| 113 | + $this->assertEquals( MWNamespace::getAssociated( NS_TALK ), NS_MAIN ); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + ### Exceptions with getAssociated() |
| 118 | + ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises |
| 119 | + ### an exception for them. |
| 120 | + /** |
| 121 | + * @expectedException MWException |
| 122 | + */ |
| 123 | + public function testGetAssociatedExceptionsForNsMedia() { |
| 124 | + $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) ); |
| 125 | + } |
| 126 | + /** |
| 127 | + * @expectedException MWException |
| 128 | + */ |
| 129 | + public function testGetAssociatedExceptionsForNsSpecial() { |
| 130 | + $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) ); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + */ |
| 135 | + public function testGetSubject() { |
| 136 | + // Special namespaces are their own subjects |
| 137 | + $this->assertEquals( MWNamespace::getSubject( NS_MEDIA ), NS_MEDIA ); |
| 138 | + $this->assertEquals( MWNamespace::getSubject( NS_SPECIAL ), NS_SPECIAL ); |
| 139 | + |
| 140 | + $this->assertEquals( MWNamespace::getSubject( NS_TALK ), NS_MAIN ); |
| 141 | + $this->assertEquals( MWNamespace::getSubject( NS_USER_TALK ), NS_USER ); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @todo Implement testExists(). |
| 146 | + */ |
| 147 | + public function testExists() { |
| 148 | + // Remove the following lines when you implement this test. |
| 149 | + $this->markTestIncomplete( |
| 150 | + 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.' |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @todo Implement testGetCanonicalNamespaces(). |
| 156 | + */ |
| 157 | + public function testGetCanonicalNamespaces() { |
| 158 | + // Remove the following lines when you implement this test. |
| 159 | + $this->markTestIncomplete( |
| 160 | + 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.' |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @todo Implement testGetCanonicalName(). |
| 166 | + */ |
| 167 | + public function testGetCanonicalName() { |
| 168 | + // Remove the following lines when you implement this test. |
| 169 | + $this->markTestIncomplete( |
| 170 | + 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.' |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @todo Implement testGetCanonicalIndex(). |
| 176 | + */ |
| 177 | + public function testGetCanonicalIndex() { |
| 178 | + // Remove the following lines when you implement this test. |
| 179 | + $this->markTestIncomplete( |
| 180 | + 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.' |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @todo Implement testGetValidNamespaces(). |
| 186 | + */ |
| 187 | + public function testGetValidNamespaces() { |
| 188 | + // Remove the following lines when you implement this test. |
| 189 | + $this->markTestIncomplete( |
| 190 | + 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.' |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + */ |
| 196 | + public function testCanTalk() { |
| 197 | + $this->assertFalse( MWNamespace::canTalk( NS_MEDIA ) ); |
| 198 | + $this->assertFalse( MWNamespace::canTalk( NS_SPECIAL ) ); |
| 199 | + |
| 200 | + $this->assertTrue( MWNamespace::canTalk( NS_MAIN ) ); |
| 201 | + $this->assertTrue( MWNamespace::canTalk( NS_TALK ) ); |
| 202 | + $this->assertTrue( MWNamespace::canTalk( NS_USER ) ); |
| 203 | + $this->assertTrue( MWNamespace::canTalk( NS_USER_TALK ) ); |
| 204 | + |
| 205 | + // User defined namespaces |
| 206 | + $this->assertTrue( MWNamespace::canTalk( 100 ) ); |
| 207 | + $this->assertTrue( MWNamespace::canTalk( 101 ) ); |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + */ |
| 212 | + public function testIsContent() { |
| 213 | + // NS_MAIN is a content namespace per DefaultSettings.php |
| 214 | + // and per function definition. |
| 215 | + $this->assertTrue( MWNamespace::isContent( NS_MAIN ) ); |
| 216 | + |
| 217 | + // Other namespaces which are not expected to be content |
| 218 | + $this->assertFalse( MWNamespace::isContent( NS_MEDIA ) ); |
| 219 | + $this->assertFalse( MWNamespace::isContent( NS_SPECIAL ) ); |
| 220 | + $this->assertFalse( MWNamespace::isContent( NS_TALK ) ); |
| 221 | + $this->assertFalse( MWNamespace::isContent( NS_USER ) ); |
| 222 | + $this->assertFalse( MWNamespace::isContent( NS_CATEGORY ) ); |
| 223 | + // User defined namespace: |
| 224 | + $this->assertFalse( MWNamespace::isContent( 100 ) ); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Similar to testIsContent() but alters the $wgContentNamespaces |
| 229 | + * global variable. |
| 230 | + */ |
| 231 | + public function testIsContentWithAdditionsInWgContentNamespaces() { |
| 232 | + // NS_MAIN is a content namespace per DefaultSettings.php |
| 233 | + // and per function definition. |
| 234 | + $this->assertTrue( MWNamespace::isContent( NS_MAIN ) ); |
| 235 | + |
| 236 | + // Tests that user defined namespace #252 is not content: |
| 237 | + $this->assertFalse( MWNamespace::isContent( 252 ) ); |
| 238 | + |
| 239 | + # FIXME: is global saving really required for PHPUnit? |
| 240 | + // Bless namespace # 252 as a content namespace |
| 241 | + global $wgContentNamespaces; |
| 242 | + $savedGlobal = $wgContentNamespaces; |
| 243 | + $wgContentNamespaces[] = 252; |
| 244 | + $this->assertTrue( MWNamespace::isContent( 252 ) ); |
| 245 | + |
| 246 | + // Makes sure NS_MAIN was not impacted |
| 247 | + $this->assertTrue( MWNamespace::isContent( NS_MAIN ) ); |
| 248 | + |
| 249 | + // Restore global |
| 250 | + $wgContentNamespaces = $savedGlobal; |
| 251 | + |
| 252 | + // Verify namespaces after global restauration |
| 253 | + $this->assertTrue( MWNamespace::isContent( NS_MAIN ) ); |
| 254 | + $this->assertFalse( MWNamespace::isContent( 252 ) ); |
| 255 | + } |
| 256 | + |
| 257 | + public function testIsWatchable() { |
| 258 | + // Specials namespaces are not watchable |
| 259 | + $this->assertFalse( MWNamespace::isWatchable( NS_MEDIA ) ); |
| 260 | + $this->assertFalse( MWNamespace::isWatchable( NS_SPECIAL ) ); |
| 261 | + |
| 262 | + // Core defined namespaces are watchables |
| 263 | + $this->assertTrue( MWNamespace::isWatchable( NS_MAIN ) ); |
| 264 | + $this->assertTrue( MWNamespace::isWatchable( NS_TALK ) ); |
| 265 | + |
| 266 | + // Additional, user defined namespaces are watchables |
| 267 | + $this->assertTrue( MWNamespace::isWatchable( 100 ) ); |
| 268 | + $this->assertTrue( MWNamespace::isWatchable( 101 ) ); |
| 269 | + } |
| 270 | + |
| 271 | + public function testHasSubpages() { |
| 272 | + // Special namespaces: |
| 273 | + $this->assertFalse( MWNamespace::hasSubpages( NS_MEDIA ) ); |
| 274 | + $this->assertFalse( MWNamespace::hasSubpages( NS_SPECIAL ) ); |
| 275 | + |
| 276 | + // namespaces without subpages |
| 277 | + $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) ); |
| 278 | + |
| 279 | + // Some namespaces with subpages |
| 280 | + $this->assertTrue( MWNamespace::hasSubpages( NS_TALK ) ); |
| 281 | + $this->assertTrue( MWNamespace::hasSubpages( NS_USER ) ); |
| 282 | + $this->assertTrue( MWNamespace::hasSubpages( NS_USER_TALK ) ); |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + */ |
| 287 | + public function testGetContentNamespaces() { |
| 288 | + $this->assertEquals( |
| 289 | + MWNamespace::getcontentNamespaces(), |
| 290 | + array( NS_MAIN), |
| 291 | + '$wgContentNamespaces is an array with only NS_MAIN by default' |
| 292 | + ); |
| 293 | + |
| 294 | + global $wgContentNamespaces; |
| 295 | + |
| 296 | + # test !is_array( $wgcontentNamespaces ) |
| 297 | + $wgContentNamespaces = ''; |
| 298 | + $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN ); |
| 299 | + $wgContentNamespaces = false; |
| 300 | + $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN ); |
| 301 | + $wgContentNamespaces = null; |
| 302 | + $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN ); |
| 303 | + $wgContentNamespaces = 5; |
| 304 | + $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN ); |
| 305 | + |
| 306 | + # test $wgContentNamespaces === array() |
| 307 | + $wgContentNamespaces = array(); |
| 308 | + $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN ); |
| 309 | + |
| 310 | + # test !in_array( NS_MAIN, $wgContentNamespaces ) |
| 311 | + $wgContentNamespaces = array( NS_USER, NS_CATEGORY ); |
| 312 | + $this->assertEquals( |
| 313 | + MWNamespace::getcontentNamespaces(), |
| 314 | + array( NS_MAIN, NS_USER, NS_CATEGORY ), |
| 315 | + 'NS_MAIN is forced in wgContentNamespaces even if unwanted' |
| 316 | + ); |
| 317 | + |
| 318 | + # test other cases, return $wgcontentNamespaces as is |
| 319 | + $wgContentNamespaces = array( NS_MAIN ); |
| 320 | + $this->assertEquals( |
| 321 | + MWNamespace::getcontentNamespaces(), |
| 322 | + array( NS_MAIN ) |
| 323 | + ); |
| 324 | + |
| 325 | + $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY ); |
| 326 | + $this->assertEquals( |
| 327 | + MWNamespace::getcontentNamespaces(), |
| 328 | + array( NS_MAIN, NS_USER, NS_CATEGORY ) |
| 329 | + ); |
| 330 | + |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * Some namespaces are always capitalized per code definition |
| 335 | + * in MWNamespace::$alwaysCapitalizedNamespaces |
| 336 | + */ |
| 337 | + public function testIsCapitalizedHardcodedAssertions() { |
| 338 | + // NS_MEDIA and NS_FILE are treated the same |
| 339 | + $this->assertEquals( |
| 340 | + MWNamespace::isCapitalized( NS_MEDIA ), |
| 341 | + MWNamespace::isCapitalized( NS_FILE ), |
| 342 | + 'NS_MEDIA and NS_FILE have same capitalization rendering' |
| 343 | + ); |
| 344 | + |
| 345 | + // Boths are capitalized by default |
| 346 | + $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIA ) ); |
| 347 | + $this->assertTrue( MWNamespace::isCapitalized( NS_FILE ) ); |
| 348 | + |
| 349 | + // Always capitalized namespaces |
| 350 | + // @see MWNamespace::$alwaysCapitalizedNamespaces |
| 351 | + $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) ); |
| 352 | + $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) ); |
| 353 | + $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) ); |
| 354 | + } |
| 355 | + |
| 356 | + /** |
| 357 | + * Follows up for testIsCapitalizedHardcodedAssertions() but alter the |
| 358 | + * global $wgCapitalLink setting to have extended coverage. |
| 359 | + * |
| 360 | + * MWNamespace::isCapitalized() rely on two global settings: |
| 361 | + * $wgCapitalLinkOverrides = array(); by default |
| 362 | + * $wgCapitalLinks = true; by default |
| 363 | + * This function test $wgCapitalLinks |
| 364 | + * |
| 365 | + * Global setting correctness is tested against the NS_PROJECT and |
| 366 | + * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials |
| 367 | + */ |
| 368 | + public function testIsCapitalizedWithWgCapitalLinks() { |
| 369 | + global $wgCapitalLinks; |
| 370 | + // Save the global to easily reset to MediaWiki default settings |
| 371 | + $savedGlobal = $wgCapitalLinks; |
| 372 | + |
| 373 | + $wgCapitalLinks = true; |
| 374 | + $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) ); |
| 375 | + $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) ); |
| 376 | + |
| 377 | + $wgCapitalLinks = false; |
| 378 | + // hardcoded namespaces (see above function) are still capitalized: |
| 379 | + $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) ); |
| 380 | + $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) ); |
| 381 | + $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) ); |
| 382 | + // setting is correctly applied |
| 383 | + $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) ); |
| 384 | + $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT_TALK ) ); |
| 385 | + |
| 386 | + // reset global state: |
| 387 | + $wgCapitalLinks = $savedGlobal; |
| 388 | + } |
| 389 | + |
| 390 | + /** |
| 391 | + * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now |
| 392 | + * testing the $wgCapitalLinkOverrides global. |
| 393 | + * |
| 394 | + * @todo split groups of assertions in autonomous testing functions |
| 395 | + */ |
| 396 | + public function testIsCapitalizedWithWgCapitalLinkOverrides() { |
| 397 | + global $wgCapitalLinkOverrides; |
| 398 | + // Save the global to easily reset to MediaWiki default settings |
| 399 | + $savedGlobal = $wgCapitalLinkOverrides; |
| 400 | + |
| 401 | + // Test default settings |
| 402 | + $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) ); |
| 403 | + $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) ); |
| 404 | + // hardcoded namespaces (see above function) are capitalized: |
| 405 | + $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) ); |
| 406 | + $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) ); |
| 407 | + $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) ); |
| 408 | + |
| 409 | + // Hardcoded namespaces remains capitalized |
| 410 | + $wgCapitalLinkOverrides[NS_SPECIAL] = false; |
| 411 | + $wgCapitalLinkOverrides[NS_USER] = false; |
| 412 | + $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false; |
| 413 | + $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) ); |
| 414 | + $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) ); |
| 415 | + $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) ); |
| 416 | + |
| 417 | + $wgCapitalLinkOverrides = $savedGlobal; |
| 418 | + $wgCapitalLinkOverrides[NS_PROJECT] = false; |
| 419 | + $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) ); |
| 420 | + $wgCapitalLinkOverrides[NS_PROJECT] = true ; |
| 421 | + $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) ); |
| 422 | + unset( $wgCapitalLinkOverrides[NS_PROJECT] ); |
| 423 | + $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) ); |
| 424 | + |
| 425 | + // reset global state: |
| 426 | + $wgCapitalLinkOverrides = $savedGlobal; |
| 427 | + } |
| 428 | + |
| 429 | + public function testHasGenderDistinction() { |
| 430 | + // Namespaces with gender distinctions |
| 431 | + $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) ); |
| 432 | + $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) ); |
| 433 | + |
| 434 | + // Other ones, "genderless" |
| 435 | + $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) ); |
| 436 | + $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) ); |
| 437 | + $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) ); |
| 438 | + $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) ); |
| 439 | + |
| 440 | + } |
| 441 | +} |
| 442 | +?> |
Property changes on: trunk/phase3/tests/phpunit/includes/MWNamespaceTest.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 443 | + native |
Index: trunk/phase3/includes/Namespace.php |
— | — | @@ -54,6 +54,22 @@ |
55 | 55 | private static $alwaysCapitalizedNamespaces = array( NS_SPECIAL, NS_USER, NS_MEDIAWIKI ); |
56 | 56 | |
57 | 57 | /** |
| 58 | + * Trow an exception when trying to get the subject or talk page |
| 59 | + * for a given namespace where it does not make sens. |
| 60 | + * Special namespaces are defined in includes/define.php and have |
| 61 | + * a value below 0 (ex: NS_SPECIAL = -1 , NS_MEDIA = -2) |
| 62 | + * |
| 63 | + * @param $ns Int: namespace index |
| 64 | + */ |
| 65 | + private static function isMethodValidFor( $index, $method ) { |
| 66 | + if( $index < NS_MAIN ) { |
| 67 | + throw new MWException( "$method does not make any sens for given namespace $index" ); |
| 68 | + return false; |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
58 | 74 | * Can pages in the given namespace be moved? |
59 | 75 | * |
60 | 76 | * @param $index Int: namespace index |
— | — | @@ -92,6 +108,7 @@ |
93 | 109 | * @return int |
94 | 110 | */ |
95 | 111 | public static function getTalk( $index ) { |
| 112 | + self::isMethodValidFor( $index, __METHOD__ ); |
96 | 113 | return self::isTalk( $index ) |
97 | 114 | ? $index |
98 | 115 | : $index + 1; |
— | — | @@ -99,17 +116,43 @@ |
100 | 117 | |
101 | 118 | /** |
102 | 119 | * Get the subject namespace index for a given namespace |
| 120 | + * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject. |
103 | 121 | * |
104 | 122 | * @param $index Int: Namespace index |
105 | 123 | * @return int |
106 | 124 | */ |
107 | 125 | public static function getSubject( $index ) { |
| 126 | + # Handle special namespaces |
| 127 | + if( $index < NS_MAIN ) { |
| 128 | + return $index; |
| 129 | + } |
| 130 | + |
108 | 131 | return self::isTalk( $index ) |
109 | 132 | ? $index - 1 |
110 | 133 | : $index; |
111 | 134 | } |
112 | 135 | |
113 | 136 | /** |
| 137 | + * Get the associated namespace. |
| 138 | + * For talk namespaces, returns the subject (non-talk) namespace |
| 139 | + * For subject (non-talk) namespaces, returns the talk namespace |
| 140 | + * |
| 141 | + * @param $index Int: namespace index |
| 142 | + * @return int or null if no associated namespace could be found |
| 143 | + */ |
| 144 | + public static function getAssociated( $index ) { |
| 145 | + self::isMethodValidFor( $index, __METHOD__ ); |
| 146 | + |
| 147 | + if( self::isMain( $index ) ) { |
| 148 | + return self::getTalk( $index ); |
| 149 | + } elseif( self::isTalk( $index ) ) { |
| 150 | + return self::getSubject( $index ); |
| 151 | + } else { |
| 152 | + return null; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
114 | 157 | * Returns whether the specified namespace exists |
115 | 158 | */ |
116 | 159 | public static function exists( $index ) { |