Index: trunk/phase3/tests/phpunit/includes/TitleMethodsTest.php |
— | — | @@ -0,0 +1,78 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class TitleMethodsTest extends MediaWikiTestCase { |
| 5 | + |
| 6 | + public function dataEquals() { |
| 7 | + return array( |
| 8 | + array( 'Main Page', 'Main Page', true ), |
| 9 | + array( 'Main Page', 'Not The Main Page', false ), |
| 10 | + array( 'Main Page', 'Project:Main Page', false ), |
| 11 | + array( 'File:Example.png', 'Image:Example.png', true ), |
| 12 | + array( 'Special:Version', 'Special:Version', true ), |
| 13 | + array( 'Special:Version', 'Special:Recentchanges', false ), |
| 14 | + array( 'Special:Version', 'Main Page', false ), |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @dataProvider dataEquals |
| 20 | + */ |
| 21 | + public function testEquals( $titleA, $titleB, $expectedBool ) { |
| 22 | + $titleA = Title::newFromText( $titleA ); |
| 23 | + $titleB = Title::newFromText( $titleB ); |
| 24 | + |
| 25 | + $this->assertEquals( $titleA->equals( $titleB ), $expectedBool ); |
| 26 | + $this->assertEquals( $titleB->equals( $titleA ), $expectedBool ); |
| 27 | + } |
| 28 | + |
| 29 | + public function dataInNamespace() { |
| 30 | + return array( |
| 31 | + array( 'Main Page', NS_MAIN, true ), |
| 32 | + array( 'Main Page', NS_TALK, false ), |
| 33 | + array( 'Main Page', NS_USER, false ), |
| 34 | + array( 'User:Foo', NS_USER, true ), |
| 35 | + array( 'User:Foo', NS_USER_TALK, false ), |
| 36 | + array( 'User:Foo', NS_TEMPLATE, false ), |
| 37 | + array( 'User_talk:Foo', NS_USER_TALK, true ), |
| 38 | + array( 'User_talk:Foo', NS_USER, false ), |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @dataProvider dataInNamespace |
| 44 | + */ |
| 45 | + public function testInNamespace( $title, $ns, $expectedBool ) { |
| 46 | + $title = Title::newFromText( $title ); |
| 47 | + $this->assertEquals( $title->inNamespace( $ns ), $expectedBool ); |
| 48 | + } |
| 49 | + |
| 50 | + public function testInNamespaces() { |
| 51 | + $mainpage = Title::newFromText( 'Main Page' ); |
| 52 | + $this->assertTrue( $mainpage->inNamespaces( NS_MAIN, NS_USER ) ); |
| 53 | + $this->assertTrue( $mainpage->inNamespaces( array( NS_MAIN, NS_USER ) ) ); |
| 54 | + $this->assertTrue( $mainpage->inNamespaces( array( NS_USER, NS_MAIN ) ) ); |
| 55 | + $this->assertFalse( $mainpage->inNamespaces( array( NS_PROJECT, NS_TEMPLATE ) ) ); |
| 56 | + } |
| 57 | + |
| 58 | + public function dataHasSubjectNamespace() { |
| 59 | + return array( |
| 60 | + array( 'Main Page', NS_MAIN, true ), |
| 61 | + array( 'Main Page', NS_TALK, true ), |
| 62 | + array( 'Main Page', NS_USER, false ), |
| 63 | + array( 'User:Foo', NS_USER, true ), |
| 64 | + array( 'User:Foo', NS_USER_TALK, true ), |
| 65 | + array( 'User:Foo', NS_TEMPLATE, false ), |
| 66 | + array( 'User_talk:Foo', NS_USER_TALK, true ), |
| 67 | + array( 'User_talk:Foo', NS_USER, true ), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @dataProvider dataHasSubjectNamespace |
| 73 | + */ |
| 74 | + public function testHasSubjectNamespace( $title, $ns, $expectedBool ) { |
| 75 | + $title = Title::newFromText( $title ); |
| 76 | + $this->assertEquals( $title->hasSubjectNamespace( $ns ), $expectedBool ); |
| 77 | + } |
| 78 | + |
| 79 | +} |
Index: trunk/phase3/tests/phpunit/includes/MWNamespaceTest.php |
— | — | @@ -39,25 +39,29 @@ |
40 | 40 | /** |
41 | 41 | * Please make sure to change testIsTalk() if you change the assertions below |
42 | 42 | */ |
43 | | - public function testIsMain() { |
| 43 | + public function testIsSubject() { |
44 | 44 | // Special namespaces |
45 | | - $this->assertTrue( MWNamespace::isMain( NS_MEDIA ) ); |
46 | | - $this->assertTrue( MWNamespace::isMain( NS_SPECIAL ) ); |
| 45 | + $this->assertTrue( MWNamespace::isSubject( NS_MEDIA ) ); |
| 46 | + $this->assertTrue( MWNamespace::isSubject( NS_SPECIAL ) ); |
47 | 47 | |
48 | 48 | // Subject pages |
49 | | - $this->assertTrue( MWNamespace::isMain( NS_MAIN ) ); |
50 | | - $this->assertTrue( MWNamespace::isMain( NS_USER ) ); |
51 | | - $this->assertTrue( MWNamespace::isMain( 100 ) ); # user defined |
| 49 | + $this->assertTrue( MWNamespace::isSubject( NS_MAIN ) ); |
| 50 | + $this->assertTrue( MWNamespace::isSubject( NS_USER ) ); |
| 51 | + $this->assertTrue( MWNamespace::isSubject( 100 ) ); # user defined |
52 | 52 | |
53 | 53 | // Talk pages |
54 | | - $this->assertFalse( MWNamespace::isMain( NS_TALK ) ); |
55 | | - $this->assertFalse( MWNamespace::isMain( NS_USER_TALK ) ); |
56 | | - $this->assertFalse( MWNamespace::isMain( 101 ) ); # user defined |
| 54 | + $this->assertFalse( MWNamespace::isSubject( NS_TALK ) ); |
| 55 | + $this->assertFalse( MWNamespace::isSubject( NS_USER_TALK ) ); |
| 56 | + $this->assertFalse( MWNamespace::isSubject( 101 ) ); # user defined |
| 57 | + |
| 58 | + // Back compat |
| 59 | + $this->assertTrue( MWNamespace::isMain( NS_MAIN ) == MWNamespace::isSubject( NS_MAIN ) ); |
| 60 | + $this->assertTrue( MWNamespace::isMain( NS_USER_TALK ) == MWNamespace::isSubject( NS_USER_TALK ) ); |
57 | 61 | } |
58 | 62 | |
59 | 63 | /** |
60 | | - * Reverse of testIsMain(). |
61 | | - * Please update testIsMain() if you change assertions below |
| 64 | + * Reverse of testIsSubject(). |
| 65 | + * Please update testIsSubject() if you change assertions below |
62 | 66 | */ |
63 | 67 | public function testIsTalk() { |
64 | 68 | // Special namespaces |
— | — | @@ -76,12 +80,26 @@ |
77 | 81 | } |
78 | 82 | |
79 | 83 | /** |
| 84 | + */ |
| 85 | + public function testGetSubject() { |
| 86 | + // Special namespaces are their own subjects |
| 87 | + $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) ); |
| 88 | + $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) ); |
| 89 | + |
| 90 | + $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) ); |
| 91 | + $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
80 | 95 | * Regular getTalk() calls |
81 | 96 | * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in |
82 | 97 | * the function testGetTalkExceptions() |
83 | 98 | */ |
84 | 99 | public function testGetTalk() { |
85 | 100 | $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) ); |
| 101 | + $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_TALK ) ); |
| 102 | + $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER ) ); |
| 103 | + $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER_TALK ) ); |
86 | 104 | } |
87 | 105 | |
88 | 106 | /** |
— | — | @@ -108,7 +126,7 @@ |
109 | 127 | * the function testGetAssociatedExceptions() |
110 | 128 | */ |
111 | 129 | public function testGetAssociated() { |
112 | | - $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) ); |
| 130 | + $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) ); |
113 | 131 | $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) ); |
114 | 132 | |
115 | 133 | } |
— | — | @@ -131,17 +149,6 @@ |
132 | 150 | } |
133 | 151 | |
134 | 152 | /** |
135 | | - */ |
136 | | - public function testGetSubject() { |
137 | | - // Special namespaces are their own subjects |
138 | | - $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) ); |
139 | | - $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) ); |
140 | | - |
141 | | - $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) ); |
142 | | - $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) ); |
143 | | - } |
144 | | - |
145 | | - /** |
146 | 153 | * @todo Implement testExists(). |
147 | 154 | */ |
148 | 155 | /* |
— | — | @@ -152,7 +159,41 @@ |
153 | 160 | ); |
154 | 161 | } |
155 | 162 | */ |
| 163 | + |
156 | 164 | /** |
| 165 | + * Test MWNamespace::equals |
| 166 | + * Note if we add a namespace registration system with keys like 'MAIN' |
| 167 | + * we should add tests here for equivilance on things like 'MAIN' == 0 |
| 168 | + * and 'MAIN' == NS_MAIN. |
| 169 | + */ |
| 170 | + public function testEquals() { |
| 171 | + $this->assertTrue( MWNamespace::equals( NS_MAIN, NS_MAIN ) ); |
| 172 | + $this->assertTrue( MWNamespace::equals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN' |
| 173 | + $this->assertTrue( MWNamespace::equals( NS_USER, NS_USER ) ); |
| 174 | + $this->assertTrue( MWNamespace::equals( NS_USER, 2 ) ); |
| 175 | + $this->assertTrue( MWNamespace::equals( NS_USER_TALK, NS_USER_TALK ) ); |
| 176 | + $this->assertTrue( MWNamespace::equals( NS_SPECIAL, NS_SPECIAL ) ); |
| 177 | + $this->assertFalse( MWNamespace::equals( NS_MAIN, NS_TALK ) ); |
| 178 | + $this->assertFalse( MWNamespace::equals( NS_USER, NS_USER_TALK ) ); |
| 179 | + $this->assertFalse( MWNamespace::equals( NS_PROJECT, NS_TEMPLATE ) ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test MWNamespace::subjectEquals |
| 184 | + */ |
| 185 | + public function testSubjectEquals() { |
| 186 | + $this->assertTrue( MWNamespace::subjectEquals( NS_MAIN, NS_MAIN ) ); |
| 187 | + $this->assertTrue( MWNamespace::subjectEquals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN' |
| 188 | + $this->assertTrue( MWNamespace::subjectEquals( NS_USER, NS_USER ) ); |
| 189 | + $this->assertTrue( MWNamespace::subjectEquals( NS_USER, 2 ) ); |
| 190 | + $this->assertTrue( MWNamespace::subjectEquals( NS_USER_TALK, NS_USER_TALK ) ); |
| 191 | + $this->assertTrue( MWNamespace::subjectEquals( NS_SPECIAL, NS_SPECIAL ) ); |
| 192 | + $this->assertTrue( MWNamespace::subjectEquals( NS_MAIN, NS_TALK ) ); |
| 193 | + $this->assertTrue( MWNamespace::subjectEquals( NS_USER, NS_USER_TALK ) ); |
| 194 | + $this->assertFalse( MWNamespace::subjectEquals( NS_PROJECT, NS_TEMPLATE ) ); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
157 | 198 | * @todo Implement testGetCanonicalNamespaces(). |
158 | 199 | */ |
159 | 200 | /* |
Index: trunk/phase3/includes/Title.php |
— | — | @@ -1946,6 +1946,57 @@ |
1947 | 1947 | } |
1948 | 1948 | |
1949 | 1949 | /** |
| 1950 | + * Returns true if the title is inside the specified namespace. |
| 1951 | + * |
| 1952 | + * Please make use of this instead of comparing to getNamespace() |
| 1953 | + * This function is much more resistant to changes we may make |
| 1954 | + * to namespaces than code that makes direct comparisons. |
| 1955 | + * @param $ns The namespace |
| 1956 | + * @return bool |
| 1957 | + * @since 1.19 |
| 1958 | + */ |
| 1959 | + public function inNamespace( $ns ) { |
| 1960 | + return MWNamespace::equals( $this->getNamespace(), $ns ); |
| 1961 | + } |
| 1962 | + |
| 1963 | + /** |
| 1964 | + * Returns true if the title is inside one of the specified namespaces. |
| 1965 | + * |
| 1966 | + * @param ...$namespaces The namespaces to check for |
| 1967 | + * @return bool |
| 1968 | + * @since 1.19 |
| 1969 | + */ |
| 1970 | + public function inNamespaces( /* ... */ ) { |
| 1971 | + $namespaces = func_get_args(); |
| 1972 | + if ( count( $namespaces ) > 0 && is_array( $namespaces[0] ) ) { |
| 1973 | + $namespaces = $namespaces[0]; |
| 1974 | + } |
| 1975 | + |
| 1976 | + foreach ( $namespaces as $ns ) { |
| 1977 | + if ( $this->inNamespace( $ns ) ) { |
| 1978 | + return true; |
| 1979 | + } |
| 1980 | + } |
| 1981 | + |
| 1982 | + return false; |
| 1983 | + } |
| 1984 | + |
| 1985 | + /** |
| 1986 | + * Returns true if the title has the same subject namespace as the |
| 1987 | + * namespace specified. |
| 1988 | + * For example this method will take NS_USER and return true if namespace |
| 1989 | + * is either NS_USER or NS_USER_TALK since both of them have NS_USER |
| 1990 | + * as their subject namespace. |
| 1991 | + * |
| 1992 | + * This is MUCH simpler than individually testing for equivilance |
| 1993 | + * against both NS_USER and NS_USER_TALK, and is also forward compatible. |
| 1994 | + * @since 1.19 |
| 1995 | + */ |
| 1996 | + public function hasSubjectNamespace( $ns ) { |
| 1997 | + return MWNamespace::subjectEquals( $this->getNamespace(), $ns ); |
| 1998 | + } |
| 1999 | + |
| 2000 | + /** |
1950 | 2001 | * Does this have subpages? (Warning, usually requires an extra DB query.) |
1951 | 2002 | * |
1952 | 2003 | * @return Bool |
Index: trunk/phase3/includes/Namespace.php |
— | — | @@ -58,12 +58,21 @@ |
59 | 59 | * |
60 | 60 | * @param $index Int: namespace index |
61 | 61 | * @return bool |
| 62 | + * @since 1.19 |
62 | 63 | */ |
63 | | - public static function isMain( $index ) { |
| 64 | + public static function isSubject( $index ) { |
64 | 65 | return !self::isTalk( $index ); |
65 | 66 | } |
66 | 67 | |
67 | 68 | /** |
| 69 | + * @see self::isSubject |
| 70 | + * @deprecated Please use the more consistently named isSubject (since 1.19) |
| 71 | + */ |
| 72 | + public static function isMain( $index ) { |
| 73 | + return self::isSubject( $index ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
68 | 77 | * Is the given namespace a talk namespace? |
69 | 78 | * |
70 | 79 | * @param $index Int: namespace index |
— | — | @@ -131,6 +140,7 @@ |
132 | 141 | * @param $index |
133 | 142 | * |
134 | 143 | * @return bool |
| 144 | + * @since 1.19 |
135 | 145 | */ |
136 | 146 | public static function exists( $index ) { |
137 | 147 | $nslist = self::getCanonicalNamespaces(); |
— | — | @@ -138,6 +148,39 @@ |
139 | 149 | } |
140 | 150 | |
141 | 151 | /** |
| 152 | + * Returns whether the specified namespaces are the same namespace |
| 153 | + * |
| 154 | + * @note It's possible that in the future we may start using something |
| 155 | + * other than just namespace indexes. Under that circumstance making use |
| 156 | + * of this function rather than directly doing comparison will make |
| 157 | + * sure that code will not potentially break. |
| 158 | + * |
| 159 | + * @param $ns1 The first namespace index |
| 160 | + * @param $ns2 The second namespae index |
| 161 | + * |
| 162 | + * @return bool |
| 163 | + * @since 1.19 |
| 164 | + */ |
| 165 | + public static function equals( $ns1, $ns2 ) { |
| 166 | + return $ns1 == $ns2; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns whether the specified namespaces share the same subject. |
| 171 | + * eg: NS_USER and NS_USER wil return true, as well |
| 172 | + * NS_USER and NS_USER_TALK will return true. |
| 173 | + * |
| 174 | + * @param $ns1 The first namespace index |
| 175 | + * @param $ns2 The second namespae index |
| 176 | + * |
| 177 | + * @return bool |
| 178 | + * @since 1.19 |
| 179 | + */ |
| 180 | + public static function subjectEquals( $ns1, $ns2 ) { |
| 181 | + return self::getSubject( $ns1 ) == self::getSubject( $ns2 ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
142 | 185 | * Returns array of all defined namespaces with their canonical |
143 | 186 | * (English) names. |
144 | 187 | * |