Simplify canUpdateTags function in ChangeTags
authorcenarium <cenarium.sysop@gmail.com>
Sat, 13 Jun 2015 16:32:51 +0000 (18:32 +0200)
committercenarium <cenarium.sysop@gmail.com>
Sun, 14 Jun 2015 19:06:20 +0000 (21:06 +0200)
This simplifies the function canUpdateTags by checking that none of the
tags are defined by an extension instead of checking that all of them
are either manually defined or not defined at all.
Doing so avoids a cache call and makes it easy to propose only relevant
tags for addition/removal to users at Special:EditTags.
This also makes the respective checks for adding/removing tags only when
necessary.

Change-Id: Iddee4d4efb109b0fccf1ece400d166147e1700fc

includes/changetags/ChangeTags.php

index 79763bd..f712b26 100644 (file)
@@ -417,21 +417,23 @@ class ChangeTags {
                        return Status::newFatal( 'tags-update-no-permission' );
                }
 
-               // to be added, a tag has to be explicitly defined
-               // @todo Allow extensions to define tags that can be applied by users...
-               $explicitlyDefinedTags = self::listExplicitlyDefinedTags();
-               $diff = array_diff( $tagsToAdd, $explicitlyDefinedTags );
-               if ( $diff ) {
-                       return self::restrictedTagError( 'tags-update-add-not-allowed-one',
-                               'tags-update-add-not-allowed-multi', $diff );
+               if ( $tagsToAdd ) {
+                       // to be added, a tag has to be explicitly defined
+                       // @todo Allow extensions to define tags that can be applied by users...
+                       $explicitlyDefinedTags = self::listExplicitlyDefinedTags();
+                       $diff = array_diff( $tagsToAdd, $explicitlyDefinedTags );
+                       if ( $diff ) {
+                               return self::restrictedTagError( 'tags-update-add-not-allowed-one',
+                                       'tags-update-add-not-allowed-multi', $diff );
+                       }
                }
 
-               // to be removed, a tag has to be either explicitly defined or not defined
-               // at all
-               $definedTags = self::listDefinedTags();
-               $diff = array_diff( $tagsToRemove, $explicitlyDefinedTags );
-               if ( $diff ) {
-                       $intersect = array_intersect( $diff, $definedTags );
+               if ( $tagsToRemove ) {
+                       // to be removed, a tag must not be defined by an extension, or equivalently it
+                       // has to be either explicitly defined or not defined at all
+                       // (assuming no edge case of a tag both explicitly-defined and extension-defined)
+                       $extensionDefinedTags = self::listExtensionDefinedTags();
+                       $intersect = array_intersect( $tagsToRemove, $extensionDefinedTags );
                        if ( $intersect ) {
                                return self::restrictedTagError( 'tags-update-remove-not-allowed-one',
                                        'tags-update-remove-not-allowed-multi', $intersect );