Mwjames has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/87642


Change subject: Add an option to omit annotation of hidden categories
......................................................................

Add an option to omit annotation of hidden categories

In case a category is marked as "hidden", option [0] can be used to omit
its annotation.

Pages that involve a hidden category need to be refreshed to ensure that
the StoreUpdater can make use of this information.

[0] $smwgShowHiddenCategories (default = true, legacy behaviour)

[1] https://www.mediawiki.org/wiki/Help:Categories#Hidden_categories

Bug: 54867
Change-Id: Ie3840872beee10cf96b0a311ba2dbd960bc7fb93
---
M SemanticMediaWiki.settings.php
M includes/BasePropertyAnnotator.php
M includes/Settings.php
M tests/phpunit/includes/BasePropertyAnnotatorTest.php
M tests/phpunit/includes/hooks/ParserAfterTidyTest.php
5 files changed, 180 insertions(+), 15 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/42/87642/1

diff --git a/SemanticMediaWiki.settings.php b/SemanticMediaWiki.settings.php
index 9b01058..1d53953 100644
--- a/SemanticMediaWiki.settings.php
+++ b/SemanticMediaWiki.settings.php
@@ -669,13 +669,15 @@
 ##
 
 ###
-# Support of deferred update of properties using the JobQueue/CacheStore
-# to lighten performance degradation when changing properties
+# This option enables to omit categories (marked with __HIDDENCAT__) from
+# the annotation process.
 #
-# In order to enable deferred updates, it requires the CacheStore
-# (e.g smwgCacheType) to be enabled and PropertyPageIdMapper to be present.
+# If a category is updated of either being hidden or visible, pages need to
+# be refreshed to ensure that the StoreUpdater can make use of the changed
+# environment.
 #
 # @since 1.9
+# @default true (true = legacy behaviour, false = not to show hidden 
categories)
 ##
-$smwgDeferredPropertyUpdate = false;
+$smwgShowHiddenCategories = true;
 ##
diff --git a/includes/BasePropertyAnnotator.php 
b/includes/BasePropertyAnnotator.php
index 4734ca9..e1a539e 100644
--- a/includes/BasePropertyAnnotator.php
+++ b/includes/BasePropertyAnnotator.php
@@ -39,6 +39,9 @@
        /** @var Job */
        protected $dispatcherJob = null;
 
+       /** @var array|null */
+       protected $hiddenCategories = null;
+
        /**
         * @since 1.9
         *
@@ -73,6 +76,11 @@
 
                // Iterate over available categories
                foreach ( $categoryLinks as $catname ) {
+
+                       if ( !$this->settings->get( 'smwgShowHiddenCategories' 
) && $this->isHiddenCategory( $catname ) ) {
+                               continue;
+                       }
+
                        if ( $this->settings->get( 'smwgCategoriesAsInstances' 
) && ( $this->semanticData->getSubject()->getNamespace() !== NS_CATEGORY ) ) {
                                $this->semanticData->addPropertyObjectValue(
                                        new DIProperty( 
DIProperty::TYPE_CATEGORY ),
@@ -182,4 +190,31 @@
                return $this;
        }
 
+       /**
+        * Whether a category is specified as hidden
+        *
+        * @since 1.9
+        *
+        * @param  $catName
+        *
+        * @return boolean
+        */
+       protected function isHiddenCategory( $catName ) {
+
+               if ( $this->hiddenCategories === null ) {
+                       $wikipage = Wikipage::factory( 
$this->semanticData->getSubject()->getTitle() );
+                       $this->hiddenCategories = 
$wikipage->getHiddenCategories();
+               }
+
+               foreach ( $this->hiddenCategories as $hiddenCategory ) {
+
+                       if ( $hiddenCategory->getText() === $catName ) {
+                               return true;
+                       };
+
+               }
+
+               return false;
+       }
+
 }
diff --git a/includes/Settings.php b/includes/Settings.php
index 372c0c9..f066222 100644
--- a/includes/Settings.php
+++ b/includes/Settings.php
@@ -120,7 +120,7 @@
                        'smwgFixedProperties' => 
$GLOBALS['smwgFixedProperties'],
                        'smwgPropertyLowUsageThreshold' => 
$GLOBALS['smwgPropertyLowUsageThreshold'],
                        'smwgPropertyZeroCountDisplay' => 
$GLOBALS['smwgPropertyZeroCountDisplay'],
-                       'smwgDeferredPropertyUpdate' => 
$GLOBALS['smwgDeferredPropertyUpdate'],
+                       'smwgShowHiddenCategories' => 
$GLOBALS['smwgShowHiddenCategories'],
                        'smwgFactboxUseCache' => 
$GLOBALS['smwgFactboxUseCache'],
                        'smwgFactboxCacheRefreshOnPurge' => 
$GLOBALS['smwgFactboxCacheRefreshOnPurge']
                );
diff --git a/tests/phpunit/includes/BasePropertyAnnotatorTest.php 
b/tests/phpunit/includes/BasePropertyAnnotatorTest.php
index 718741c..8c4ec65 100644
--- a/tests/phpunit/includes/BasePropertyAnnotatorTest.php
+++ b/tests/phpunit/includes/BasePropertyAnnotatorTest.php
@@ -99,6 +99,38 @@
 
        /**
         * @test BasePropertyAnnotator::addCategories
+        * @dataProvider hiddenCategoriesDataProvider
+        *
+        * @since 1.9
+        *
+        * @param array $setup
+        * @param array $expected
+        */
+       public function testAddCategoriesWithHiddenCategories( array $setup, 
array $expected ) {
+
+               $semanticData = new SemanticData(
+                       $this->newSubject( $this->newTitle( $setup['namespace'] 
) )
+               );
+
+               $instance  = $this->newInstance( $semanticData, 
$setup['settings'] );
+               $reflector = $this->newReflector();
+
+               $hiddenCategories = $reflector->getProperty( 'hiddenCategories' 
);
+               $hiddenCategories->setAccessible( true );
+               $hiddenCategories->setValue( $instance, $setup['hidCategories'] 
);
+
+               $instance->addCategories( $setup['categories'] );
+
+               $this->assertSemanticData(
+                       $semanticData,
+                       $expected,
+                       'Asserts that addCategories() adds expected triples'
+               );
+
+       }
+
+       /**
+        * @test BasePropertyAnnotator::addCategories
         * @dataProvider categoriesDataProvider
         *
         * @since 1.9
@@ -454,14 +486,15 @@
 
                $provider = array();
 
-               // Standard category
+               // #0 Standard category
                $provider[] = array(
                        array(
-                               'namespace'  => NS_MAIN,
-                               'categories' => array( 'Foo', 'Bar' ),
+                               'namespace'     => NS_MAIN,
+                               'categories'    => array( 'Foo', 'Bar' ),
                                'settings'   => array(
                                        'smwgUseCategoryHierarchy'  => false,
-                                       'smwgCategoriesAsInstances' => true
+                                       'smwgCategoriesAsInstances' => true,
+                                       'smwgShowHiddenCategories'  => true
                                )
                        ),
                        array(
@@ -471,14 +504,15 @@
                        )
                );
 
-               // Category hierarchy or Sub-category
+               // #1 Category hierarchy or Sub-category
                $provider[] = array(
                        array(
-                               'namespace'  => NS_CATEGORY,
-                               'categories' => array( 'Foo', 'Bar' ),
+                               'namespace'     => NS_CATEGORY,
+                               'categories'    => array( 'Foo', 'Bar' ),
                                'settings'   => array(
                                        'smwgUseCategoryHierarchy'  => true,
-                                       'smwgCategoriesAsInstances' => false
+                                       'smwgCategoriesAsInstances' => false,
+                                       'smwgShowHiddenCategories'  => true
                                )
                        ),
                        array(
@@ -490,4 +524,97 @@
 
                return $provider;
        }
+
+       /**
+        * Provides array of category names and hidden categories
+        * @return array
+        */
+       public function hiddenCategoriesDataProvider() {
+
+               $provider = array();
+
+               $hidCategory = $this->newMockBuilder()->newObject( 'Title', 
array(
+                       'getNamespace' => NS_CATEGORY,
+                       'getText'      => 'Bar'
+               ) );
+
+               // #0 Standard category, show hidden category
+               $provider[] = array(
+                       array(
+                               'namespace'     => NS_MAIN,
+                               'categories'    => array( 'Foo', 'Bar' ),
+                               'hidCategories' => array( $hidCategory ),
+                               'settings'   => array(
+                                       'smwgUseCategoryHierarchy'  => false,
+                                       'smwgCategoriesAsInstances' => true,
+                                       'smwgShowHiddenCategories'  => true
+                               )
+                       ),
+                       array(
+                               'propertyCount' => 1,
+                               'propertyKey'   => '_INST',
+                               'propertyValue' => array( 'Foo', 'Bar' ),
+                       )
+               );
+
+               // #1 Standard category, omit hidden category
+               $provider[] = array(
+                       array(
+                               'namespace'     => NS_MAIN,
+                               'categories'    => array( 'Foo', 'Bar' ),
+                               'hidCategories' => array( $hidCategory ),
+                               'settings'   => array(
+                                       'smwgUseCategoryHierarchy'  => false,
+                                       'smwgCategoriesAsInstances' => true,
+                                       'smwgShowHiddenCategories'  => false
+                               )
+                       ),
+                       array(
+                               'propertyCount' => 1,
+                               'propertyKey'   => '_INST',
+                               'propertyValue' => array( 'Foo' ),
+                       )
+               );
+
+               // #2 Category hierarchy or Sub-category, show hidden category
+               $provider[] = array(
+                       array(
+                               'namespace'     => NS_CATEGORY,
+                               'categories'    => array( 'Foo', 'Bar' ),
+                               'hidCategories' => array( $hidCategory ),
+                               'settings'   => array(
+                                       'smwgUseCategoryHierarchy'  => true,
+                                       'smwgCategoriesAsInstances' => false,
+                                       'smwgShowHiddenCategories'  => true
+                               )
+                       ),
+                       array(
+                               'propertyCount' => 1,
+                               'propertyKey'   => '_SUBC',
+                               'propertyValue' => array( 'Foo', 'Bar' ),
+                       )
+               );
+
+               // #3 Category hierarchy or Sub-category, omit hidden category
+               $provider[] = array(
+                       array(
+                               'namespace'     => NS_CATEGORY,
+                               'categories'    => array( 'Foo', 'Bar' ),
+                               'hidCategories' => array( $hidCategory ),
+                               'settings'   => array(
+                                       'smwgUseCategoryHierarchy'  => true,
+                                       'smwgCategoriesAsInstances' => false,
+                                       'smwgShowHiddenCategories'  => false
+                               )
+                       ),
+                       array(
+                               'propertyCount' => 1,
+                               'propertyKey'   => '_SUBC',
+                               'propertyValue' => array( 'Foo' ),
+                       )
+               );
+
+               return $provider;
+       }
+
 }
diff --git a/tests/phpunit/includes/hooks/ParserAfterTidyTest.php 
b/tests/phpunit/includes/hooks/ParserAfterTidyTest.php
index d3deac9..4b0d9e0 100644
--- a/tests/phpunit/includes/hooks/ParserAfterTidyTest.php
+++ b/tests/phpunit/includes/hooks/ParserAfterTidyTest.php
@@ -160,7 +160,8 @@
                        'smwgCacheType'             => 'hash',
                        'smwgEnableUpdateJobs'      => false,
                        'smwgUseCategoryHierarchy'  => false,
-                       'smwgCategoriesAsInstances' => true
+                       'smwgCategoriesAsInstances' => true,
+                       'smwgShowHiddenCategories'  => true
                ) );
 
                $container = $instance->getDependencyBuilder()->getContainer();

-- 
To view, visit https://gerrit.wikimedia.org/r/87642
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie3840872beee10cf96b0a311ba2dbd960bc7fb93
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to