Merge "Use CSS columns instead of tables in Special:SpecialPages (2)"
[lhc/web/wiklou.git] / includes / changetags / ChangeTags.php
index f712b26..12f738f 100644 (file)
@@ -153,8 +153,10 @@ class ChangeTags {
         *
         * @since 1.25
         */
-       public static function updateTags( $tagsToAdd, $tagsToRemove, &$rc_id = null,
-               &$rev_id = null, &$log_id = null, $params = null ) {
+       public static function updateTags(
+               $tagsToAdd, $tagsToRemove,
+               &$rc_id = null, &$rev_id = null, &$log_id = null, $params = null
+       ) {
 
                $tagsToAdd = array_filter( (array)$tagsToAdd ); // Make sure we're submitting all tags...
                $tagsToRemove = array_filter( (array)$tagsToRemove );
@@ -169,18 +171,28 @@ class ChangeTags {
                // Might as well look for rcids and so on.
                if ( !$rc_id ) {
                        // Info might be out of date, somewhat fractionally, on slave.
+                       // LogEntry/LogPage and WikiPage match rev/log/rc timestamps,
+                       // so use that relation to avoid full table scans.
                        if ( $log_id ) {
                                $rc_id = $dbw->selectField(
-                                       'recentchanges',
+                                       array( 'logging', 'recentchanges' ),
                                        'rc_id',
-                                       array( 'rc_logid' => $log_id ),
+                                       array(
+                                               'log_id' => $log_id,
+                                               'rc_timestamp = log_timestamp',
+                                               'rc_logid = log_id'
+                                       ),
                                        __METHOD__
                                );
                        } elseif ( $rev_id ) {
                                $rc_id = $dbw->selectField(
-                                       'recentchanges',
+                                       array( 'revision', 'recentchanges' ),
                                        'rc_id',
-                                       array( 'rc_this_oldid' => $rev_id ),
+                                       array(
+                                               'rev_id' => $rev_id,
+                                               'rc_timestamp = rev_timestamp',
+                                               'rc_this_oldid = rev_id'
+                                       ),
                                        __METHOD__
                                );
                        }
@@ -611,17 +623,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;
 
@@ -634,14 +645,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;
@@ -749,12 +770,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)
@@ -763,6 +778,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();
        }
 
@@ -887,7 +908,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 );
                }
 
@@ -997,11 +1018,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 );
                }
 
@@ -1046,6 +1067,7 @@ class ChangeTags {
 
                // store the tag usage statistics
                $tagUsage = self::tagUsageStatistics();
+               $hitcount = isset( $tagUsage[$tag] ) ? $tagUsage[$tag] : 0;
 
                // do it!
                $deleteResult = self::deleteTagEverywhere( $tag );
@@ -1054,7 +1076,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;
        }
@@ -1172,6 +1194,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.
@@ -1205,12 +1228,6 @@ class ChangeTags {
                                        $out[$row->ct_tag] = $row->hitcount;
                                }
 
-                               foreach ( ChangeTags::listDefinedTags() as $tag ) {
-                                       if ( !isset( $out[$tag] ) ) {
-                                               $out[$tag] = 0;
-                                       }
-                               }
-
                                return $out;
                        },
                        300,