Merge "Implement OOUI version of tag filter in ChangeTags"
[lhc/web/wiklou.git] / includes / changetags / ChangeTags.php
index a730116..bf8386f 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 );
@@ -609,17 +611,16 @@ class ChangeTags {
         * Build a text box to select a change tag
         *
         * @param string $selected Tag to select by default
-        * @param bool $fullForm
-        *        - if false, then it returns an array of (label, form).
-        *        - if true, it returns an entire form around the selector.
-        * @param Title $title Title object to send the form to.
-        *        Used when, and only when $fullForm is true.
+        * @param bool $fullForm Affects return value, see below
+        * @param Title $title Title object to send the form to. Used only if $fullForm is true.
+        * @param bool $ooui Use an OOUI TextInputWidget as selector instead of a non-OOUI input field
+        *        You need to call OutputPage::enableOOUI() yourself.
         * @return string|array
-        *        - if $fullForm is false: Array with
-        *        - if $fullForm is true: String, html fragment
+        *        - if $fullForm is false: an array of (label, selector).
+        *        - if $fullForm is true: HTML of entire form built around the selector.
         */
        public static function buildTagFilterSelector( $selected = '',
-               $fullForm = false, Title $title = null
+               $fullForm = false, Title $title = null, $ooui = false
        ) {
                global $wgUseTagFilter;
 
@@ -632,14 +633,24 @@ class ChangeTags {
                                'label',
                                array( 'for' => 'tagfilter' ),
                                wfMessage( 'tag-filter' )->parse()
-                       ),
-                       Xml::input(
+                       )
+               );
+
+               if ( $ooui ) {
+                       $data[] = new OOUI\TextInputWidget( array(
+                               'id' => 'tagfilter',
+                               'name' => 'tagfilter',
+                               'value' => $selected,
+                               'classes' => 'mw-tagfilter-input',
+                       ) );
+               } else {
+                       $data[] = Xml::input(
                                'tagfilter',
                                20,
                                $selected,
                                array( 'class' => 'mw-tagfilter-input mw-ui-input mw-ui-input-inline', 'id' => 'tagfilter' )
-                       )
-               );
+                       );
+               }
 
                if ( !$fullForm ) {
                        return $data;
@@ -747,12 +758,6 @@ class ChangeTags {
                        return Status::newFatal( 'tags-manage-no-permission' );
                }
 
-               // non-existing tags cannot be activated
-               $tagUsage = self::tagUsageStatistics();
-               if ( !isset( $tagUsage[$tag] ) ) {
-                       return Status::newFatal( 'tags-activate-not-found', $tag );
-               }
-
                // defined tags cannot be activated (a defined tag is either extension-
                // defined, in which case the extension chooses whether or not to active it;
                // or user-defined, in which case it is considered active)
@@ -761,6 +766,12 @@ class ChangeTags {
                        return Status::newFatal( 'tags-activate-not-allowed', $tag );
                }
 
+               // non-existing tags cannot be activated
+               $tagUsage = self::tagUsageStatistics();
+               if ( !isset( $tagUsage[$tag] ) ) { // we already know the tag is undefined
+                       return Status::newFatal( 'tags-activate-not-found', $tag );
+               }
+
                return Status::newGood();
        }
 
@@ -885,7 +896,7 @@ class ChangeTags {
 
                // does the tag already exist?
                $tagUsage = self::tagUsageStatistics();
-               if ( isset( $tagUsage[$tag] ) ) {
+               if ( isset( $tagUsage[$tag] ) || in_array( $tag, self::listDefinedTags() ) ) {
                        return Status::newFatal( 'tags-create-already-exists', $tag );
                }
 
@@ -995,11 +1006,11 @@ class ChangeTags {
                        return Status::newFatal( 'tags-manage-no-permission' );
                }
 
-               if ( !isset( $tagUsage[$tag] ) ) {
+               if ( !isset( $tagUsage[$tag] ) && !in_array( $tag, self::listDefinedTags() ) ) {
                        return Status::newFatal( 'tags-delete-not-found', $tag );
                }
 
-               if ( $tagUsage[$tag] > self::MAX_DELETE_USES ) {
+               if ( isset( $tagUsage[$tag] ) && $tagUsage[$tag] > self::MAX_DELETE_USES ) {
                        return Status::newFatal( 'tags-delete-too-many-uses', $tag, self::MAX_DELETE_USES );
                }
 
@@ -1044,6 +1055,7 @@ class ChangeTags {
 
                // store the tag usage statistics
                $tagUsage = self::tagUsageStatistics();
+               $hitcount = isset( $tagUsage[$tag] ) ? $tagUsage[$tag] : 0;
 
                // do it!
                $deleteResult = self::deleteTagEverywhere( $tag );
@@ -1052,7 +1064,7 @@ class ChangeTags {
                }
 
                // log it
-               $logId = self::logTagManagementAction( 'delete', $tag, $reason, $user, $tagUsage[$tag] );
+               $logId = self::logTagManagementAction( 'delete', $tag, $reason, $user, $hitcount );
                $deleteResult->value = $logId;
                return $deleteResult;
        }
@@ -1170,6 +1182,7 @@ class ChangeTags {
        /**
         * Returns a map of any tags used on the wiki to number of edits
         * tagged with them, ordered descending by the hitcount.
+        * This does not include tags defined somewhere that have never been applied.
         *
         * Keeps a short-term cache in memory, so calling this multiple times in the
         * same request should be fine.
@@ -1177,9 +1190,15 @@ class ChangeTags {
         * @return array Array of string => int
         */
        public static function tagUsageStatistics() {
-               $fname = __METHOD__;
+               static $cachedStats = null;
 
-               return ObjectCache::getMainWANInstance()->getWithSetCallback(
+               // Process cache to avoid I/O and repeated regens during holdoff
+               if ( $cachedStats !== null ) {
+                       return $cachedStats;
+               }
+
+               $fname = __METHOD__;
+               $cachedStats = ObjectCache::getMainWANInstance()->getWithSetCallback(
                        wfMemcKey( 'change-tag-statistics' ),
                        function() use ( $fname ) {
                                $out = array();
@@ -1197,18 +1216,14 @@ class ChangeTags {
                                        $out[$row->ct_tag] = $row->hitcount;
                                }
 
-                               foreach ( ChangeTags::listDefinedTags() as $tag ) {
-                                       if ( !isset( $out[$tag] ) ) {
-                                               $out[$tag] = 0;
-                                       }
-                               }
-
                                return $out;
                        },
                        300,
                        array( wfMemcKey( 'change-tag-statistics' ) ),
                        array( 'lockTSE' => INF )
                );
+
+               return $cachedStats;
        }
 
        /**