Add a way to exclude categories from Special:UnusedCategories
authorAgabi10 <gabilondo.ander@gmail.com>
Fri, 7 Sep 2018 21:32:26 +0000 (21:32 +0000)
committerAgabi10 <gabilondo.ander@gmail.com>
Fri, 16 Nov 2018 13:32:27 +0000 (13:32 +0000)
Added __EXPECT_UNUSED_CATEGORY__ as a behavioral switch. Adding
this switch to category pages prevents them from appearing in
Special:UnusedCategories.

Bug: T96041
Change-Id: I055e59f5311347155e0f801dd5ec9a6d4a68c9cc

RELEASE-NOTES-1.33
includes/MagicWordFactory.php
includes/page/WikiCategoryPage.php
includes/specials/SpecialUnusedcategories.php
languages/messages/MessagesEn.php
tests/phpunit/includes/page/WikiCategoryPageTest.php

index c573a59..df6b5f8 100644 (file)
@@ -30,6 +30,8 @@ production.
 
 === New features in 1.33 ===
 * The 'GetPreferences' hook now receives an additional $context parameter.
+* (T96041) __EXPECT_UNUSED_CATEGORY__ on a category page causes the category
+  to be hidden on Special:UnusedCategories.
 * …
 
 === External library changes in 1.33 ===
index e62716d..4e9bfaf 100644 (file)
@@ -173,6 +173,7 @@ class MagicWordFactory {
                'newsectionlink',
                'nonewsectionlink',
                'hiddencat',
+               'expectunusedcategory',
                'index',
                'noindex',
                'staticredirect',
index 6c93202..2837573 100644 (file)
@@ -59,6 +59,20 @@ class WikiCategoryPage extends WikiPage {
                $pageId = $this->getTitle()->getArticleID();
                $pageProps = PageProps::getInstance()->getProperties( $this->getTitle(), 'hiddencat' );
 
-               return isset( $pageProps[$pageId] ) ? true : false;
+               return isset( $pageProps[$pageId] );
+       }
+
+       /**
+        * Checks if a category is expected to be an unused category.
+        *
+        * @since 1.33
+        *
+        * @return bool
+        */
+       public function isExpectedUnusedCategory() {
+               $pageId = $this->getTitle()->getArticleID();
+               $pageProps = PageProps::getInstance()->getProperties( $this->getTitle(), 'expectunusedcategory' );
+
+               return isset( $pageProps[$pageId] );
        }
 }
index 1469742..2577a10 100644 (file)
@@ -39,7 +39,7 @@ class UnusedCategoriesPage extends QueryPage {
 
        public function getQueryInfo() {
                return [
-                       'tables' => [ 'page', 'categorylinks' ],
+                       'tables' => [ 'page', 'categorylinks', 'page_props' ],
                        'fields' => [
                                'namespace' => 'page_namespace',
                                'title' => 'page_title',
@@ -48,9 +48,16 @@ class UnusedCategoriesPage extends QueryPage {
                        'conds' => [
                                'cl_from IS NULL',
                                'page_namespace' => NS_CATEGORY,
-                               'page_is_redirect' => 0
+                               'page_is_redirect' => 0,
+                               'pp_page IS NULL'
                        ],
-                       'join_conds' => [ 'categorylinks' => [ 'LEFT JOIN', 'cl_to = page_title' ] ]
+                       'join_conds' => [
+                               'categorylinks' => [ 'LEFT JOIN', 'cl_to = page_title' ],
+                               'page_props' => [ 'LEFT JOIN', [
+                                       'page_id = pp_page',
+                                       'pp_propname' => 'expectunusedcategory'
+                               ] ]
+                       ]
                ];
        }
 
index 587f6ea..b5e2c5e 100644 (file)
@@ -359,6 +359,7 @@ $magicWords = [
        'filepath'                => [ 0, 'FILEPATH:' ],
        'tag'                     => [ 0, 'tag' ],
        'hiddencat'               => [ 1, '__HIDDENCAT__' ],
+       'expectunusedcategory'    => [ 1, '__EXPECT_UNUSED_CATEGORY__', '__EXPECT_UNUSED_CAT__', '__EXPECTUNUSEDCATEGORY__', '__EXPECTUNUSEDCAT__' ],
        'pagesincategory'         => [ 1, 'PAGESINCATEGORY', 'PAGESINCAT' ],
        'pagesize'                => [ 1, 'PAGESIZE' ],
        'index'                   => [ 1, '__INDEX__' ],
index 5f1bf0c..9f696c0 100644 (file)
@@ -60,4 +60,46 @@ class WikiCategoryPageTest extends MediaWikiLangTestCase {
 
                ScopedCallback::consume( $scopedOverride );
        }
+
+       /**
+        * @covers WikiCategoryPage::isExpectedUnusedCategory
+        */
+       public function testExpectUnusedCategory_PropertyNotSet() {
+               $title = Title::makeTitle( NS_CATEGORY, 'CategoryPage' );
+               $categoryPage = WikiCategoryPage::factory( $title );
+
+               $pageProps = $this->getMockPageProps();
+               $pageProps->expects( $this->once() )
+                       ->method( 'getProperties' )
+                       ->with( $title, 'expectunusedcategory' )
+                       ->will( $this->returnValue( [] ) );
+
+               $scopedOverride = PageProps::overrideInstance( $pageProps );
+
+               $this->assertFalse( $categoryPage->isExpectedUnusedCategory() );
+
+               ScopedCallback::consume( $scopedOverride );
+       }
+
+       /**
+        * @dataProvider provideCategoryContent
+        * @covers WikiCategoryPage::isExpectedUnusedCategory
+        */
+       public function testExpectUnusedCategory_PropertyIsSet( $isExpectedUnusedCategory ) {
+               $categoryTitle = Title::makeTitle( NS_CATEGORY, 'CategoryPage' );
+               $categoryPage = WikiCategoryPage::factory( $categoryTitle );
+               $returnValue = $isExpectedUnusedCategory ? [ $categoryTitle->getArticleID() => '' ] : [];
+
+               $pageProps = $this->getMockPageProps();
+               $pageProps->expects( $this->once() )
+                       ->method( 'getProperties' )
+                       ->with( $categoryTitle, 'expectunusedcategory' )
+                       ->will( $this->returnValue( $returnValue ) );
+
+               $scopedOverride = PageProps::overrideInstance( $pageProps );
+
+               $this->assertEquals( $isExpectedUnusedCategory, $categoryPage->isExpectedUnusedCategory() );
+
+               ScopedCallback::consume( $scopedOverride );
+       }
 }