Allow to define exceptions for Special:UncategorizedCategories on-wiki
authorFlorian <florian.schmidt.stargatewissen@gmail.com>
Wed, 30 Mar 2016 20:05:25 +0000 (22:05 +0200)
committerBrian Wolff <bawolff+wn@gmail.com>
Wed, 11 Jan 2017 17:51:19 +0000 (17:51 +0000)
The new system message uncategorized-categories-exceptionlist can be changed
on-wiki to hold a list of categories, that shouldn't be visible on the special
page Special:UncategorizedCategories (with a trailing "* "), even if
these categories aren't in any category.

Bug: T126117
Change-Id: I65989e40f3caa2fad7b8b35109c0466e01084f72

includes/specials/SpecialUncategorizedcategories.php
languages/i18n/en.json
languages/i18n/qqq.json
tests/phpunit/includes/specials/SpecialUncategorizedcategoriesTest.php [new file with mode: 0644]

index 86d8f89..7c3265d 100644 (file)
  * @ingroup SpecialPage
  */
 class UncategorizedCategoriesPage extends UncategorizedPagesPage {
+       /**
+        * Holds a list of categories, which shouldn't be listed on this special page,
+        * even if it is uncategorized.
+        * @var array
+        */
+       private $exceptionList = null;
+
        function __construct( $name = 'Uncategorizedcategories' ) {
                parent::__construct( $name );
                $this->requestedNamespace = NS_CATEGORY;
        }
 
+       /**
+        * Returns an array of categorie titles (usually without the namespace), which
+        * shouldn't be listed on this page, even if they're uncategorized.
+        *
+        * @return array
+        */
+       private function getExceptionList() {
+               if ( $this->exceptionList === null ) {
+                       $exList = $this->msg( 'uncategorized-categories-exceptionlist' )
+                               ->inContentLanguage()->plain();
+                       $proposedTitles = explode( "\n", $exList );
+                       foreach ( $proposedTitles as $count => $title ) {
+                               if ( strpos( $title, '*' ) !== 0 ) {
+                                       continue;
+                               }
+                               $title = preg_replace( "/^\\*\\s*/", '', $title );
+                               $title = Title::newFromText( $title, NS_CATEGORY );
+                               if ( $title ) {
+                                       $this->exceptionList[] = $title->getDBKey();
+                               }
+                       }
+               }
+               return $this->exceptionList;
+       }
+
+       public function getQueryInfo() {
+               $dbr = wfGetDB( DB_SLAVE );
+               $query = parent::getQueryInfo();
+               $exceptionList = $this->getExceptionList();
+               if ( $exceptionList ) {
+                       $query['conds'][] = 'page_title not in ( ' . $dbr->makeList( $exceptionList ) . ' )';
+               }
+
+               return $query;
+       }
+
        /**
         * Formats the result
         * @param Skin $skin The current skin
index a621f1c..a53b2cc 100644 (file)
        "uncategorizedimages-summary": "",
        "uncategorizedtemplates": "Uncategorized templates",
        "uncategorizedtemplates-summary": "",
+       "uncategorized-categories-exceptionlist": " # Contains a list of catgeories, which shouldn't be mentioned on Special:UncategorizedCategories. One per line, starting with \"*\". Lines starting with another character (including whitespaces) are ignored. Use \"#\" for comments.",
        "unusedcategories": "Unused categories",
        "unusedcategories-summary": "",
        "unusedimages": "Unused files",
index da43aef..eee9b4e 100644 (file)
        "uncategorizedimages-summary": "{{notranslate}}\nused in [[Special:Uncategorizedimages]]. [[mw:Manual:Interface/Special pages summary|mw manual]].",
        "uncategorizedtemplates": "{{doc-special|UncategorizedTemplates}}",
        "uncategorizedtemplates-summary": "{{doc-specialpagesummary|uncategorizedtemplates}}",
+       "uncategorized-categories-exceptionlist": "System message used as a list of exceptions for Special:UncategorizedCategories. {{notranslate}}",
        "unusedcategories": "{{doc-special|UnusedCategories}}",
        "unusedcategories-summary": "{{doc-specialpagesummary|unusedcategories}}",
        "unusedimages": "{{doc-special|UnusedImages}}",
diff --git a/tests/phpunit/includes/specials/SpecialUncategorizedcategoriesTest.php b/tests/phpunit/includes/specials/SpecialUncategorizedcategoriesTest.php
new file mode 100644 (file)
index 0000000..64e78f2
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Tests for Special:Uncategorizedcategories
+ */
+class UncategorizedCategoriesPageTest extends MediaWikiTestCase {
+       /**
+        * @dataProvider provideTestGetQueryInfoData
+        */
+       public function testGetQueryInfo( $msgContent, $expected ) {
+               $msg = new RawMessage( $msgContent );
+               $mockContext = $this->getMockBuilder( 'RequestContext' )->getMock();
+               $mockContext->method( 'msg' )->willReturn( $msg );
+               $special = new UncategorizedCategoriesPage();
+               $special->setContext( $mockContext );
+               $this->assertEquals( [
+                       'tables' => [
+                               0 => 'page',
+                               1 => 'categorylinks',
+                       ],
+                       'fields' => [
+                               'namespace' => 'page_namespace',
+                               'title' => 'page_title',
+                               'value' => 'page_title',
+                       ],
+                       'conds' => [
+                               0 => 'cl_from IS NULL',
+                               'page_namespace' => 14,
+                               'page_is_redirect' => 0,
+                       ] + $expected,
+                       'join_conds' => [
+                               'categorylinks' => [
+                                       0 => 'LEFT JOIN',
+                                       1 => 'cl_from = page_id',
+                               ],
+                       ],
+               ], $special->getQueryInfo() );
+       }
+
+       public function provideTestGetQueryInfoData() {
+               return [
+                       [
+                               "* Stubs\n* Test\n* *\n* * test123",
+                               [ 1 => "page_title not in ( 'Stubs','Test','*','*_test123' )" ]
+                       ],
+                       [
+                               "Stubs\n* Test\n* *\n* * test123",
+                               [ 1 => "page_title not in ( 'Test','*','*_test123' )" ]
+                       ],
+                       [
+                               "* StubsTest\n* *\n* * test123",
+                               [ 1 => "page_title not in ( 'StubsTest','*','*_test123' )" ]
+                       ],
+                       [ "", [] ],
+                       [ "\n\n\n", [] ],
+                       [ "\n", [] ],
+                       [ "Test\n*Test2", [ 1 => "page_title not in ( 'Test2' )" ] ],
+                       [ "Test", [] ],
+                       [ "*Test\nTest2", [ 1 => "page_title not in ( 'Test' )" ] ],
+                       [ "Test\nTest2", [] ],
+               ];
+       }
+}