API: Rewrite ApiQueryTags to fix continuation
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 26 Nov 2014 18:11:38 +0000 (13:11 -0500)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 28 Nov 2014 08:45:33 +0000 (00:45 -0800)
And while we're at it, may as well include an indicator of whether the
tag is defined.

Bug: T76051
Bug: T76052
Change-Id: If2569ba1414b5f38d49c5f3ce9b6ca9c1a58f1c2

RELEASE-NOTES-1.25
includes/api/ApiQueryTags.php
includes/api/i18n/en.json

index 7181e11..caf29f0 100644 (file)
@@ -116,6 +116,8 @@ production.
 * ApiOpenSearch now supports XML output.
 * ApiOpenSearch will now output descriptions and URLs as array indexes 2 and 3
   in JSON format.
+* (bug T76051) list=tags will now continue correctly.
+* (bug T76052) list=tags can now indicate whether a tag is defined.
 
 === Action API internal changes in 1.25 ===
 * ApiHelp has been rewritten to support i18n and paginated HTML output.
index f3b2652..7f2dc85 100644 (file)
  */
 class ApiQueryTags extends ApiQueryBase {
 
-       /**
-        * @var ApiResult
-        */
-       private $result;
-
-       private $limit;
-       private $fld_displayname = false, $fld_description = false,
-               $fld_hitcount = false;
-
        public function __construct( ApiQuery $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'tg' );
        }
@@ -49,84 +40,77 @@ class ApiQueryTags extends ApiQueryBase {
 
                $prop = array_flip( $params['prop'] );
 
-               $this->fld_displayname = isset( $prop['displayname'] );
-               $this->fld_description = isset( $prop['description'] );
-               $this->fld_hitcount = isset( $prop['hitcount'] );
-
-               $this->limit = $params['limit'];
-               $this->result = $this->getResult();
+               $fld_displayname = isset( $prop['displayname'] );
+               $fld_description = isset( $prop['description'] );
+               $fld_hitcount = isset( $prop['hitcount'] );
+               $fld_defined = isset( $prop['defined'] );
+
+               $limit = $params['limit'];
+               $result = $this->getResult();
+
+               $definedTags = array_fill_keys( ChangeTags::listDefinedTags(), 0 );
+
+               # Fetch defined tags that aren't past the continuation
+               if ( $params['continue'] !== null ) {
+                       $cont = $params['continue'];
+                       $tags = array_filter( array_keys( $definedTags ), function ( $v ) use ( $cont ) {
+                               return $v >= $cont;
+                       } );
+                       $tags = array_fill_keys( $tags, 0 );
+               } else {
+                       $tags = $definedTags;
+               }
 
+               # Merge in all used tags
                $this->addTables( 'change_tag' );
                $this->addFields( 'ct_tag' );
-
-               $this->addFieldsIf( array( 'hitcount' => 'COUNT(*)' ), $this->fld_hitcount );
-
-               $this->addOption( 'LIMIT', $this->limit + 1 );
+               $this->addFields( array( 'hitcount' => $fld_hitcount ? 'COUNT(*)' : '0' ) );
+               $this->addOption( 'LIMIT', $limit + 1 );
                $this->addOption( 'GROUP BY', 'ct_tag' );
                $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
-
                $res = $this->select( __METHOD__ );
-
-               $ok = true;
-
                foreach ( $res as $row ) {
-                       if ( !$ok ) {
-                               break;
-                       }
-                       $ok = $this->doTag( $row->ct_tag, $this->fld_hitcount ? $row->hitcount : 0 );
+                       $tags[$row->ct_tag] = (int)$row->hitcount;
                }
 
-               // include tags with no hits yet
-               foreach ( ChangeTags::listDefinedTags() as $tag ) {
-                       if ( !$ok ) {
+               # Now make sure the array is sorted for proper continuation
+               ksort( $tags );
+
+               $count = 0;
+               foreach ( $tags as $tagName => $hitcount ) {
+                       if ( ++$count > $limit ) {
+                               $this->setContinueEnumParameter( 'continue', $tagName );
                                break;
                        }
-                       $ok = $this->doTag( $tag, 0 );
-               }
-
-               $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
-       }
 
-       private function doTag( $tagName, $hitcount ) {
-               static $count = 0;
-               static $doneTags = array();
+                       $tag = array();
+                       $tag['name'] = $tagName;
 
-               if ( in_array( $tagName, $doneTags ) ) {
-                       return true;
-               }
-
-               if ( ++$count > $this->limit ) {
-                       $this->setContinueEnumParameter( 'continue', $tagName );
-
-                       return false;
-               }
-
-               $tag = array();
-               $tag['name'] = $tagName;
-
-               if ( $this->fld_displayname ) {
-                       $tag['displayname'] = ChangeTags::tagDescription( $tagName );
-               }
-
-               if ( $this->fld_description ) {
-                       $msg = wfMessage( "tag-$tagName-description" );
-                       $tag['description'] = $msg->exists() ? $msg->text() : '';
-               }
+                       if ( $fld_displayname ) {
+                               $tag['displayname'] = ChangeTags::tagDescription( $tagName );
+                       }
 
-               if ( $this->fld_hitcount ) {
-                       $tag['hitcount'] = $hitcount;
-               }
+                       if ( $fld_description ) {
+                               $msg = $this->msg( "tag-$tagName-description" );
+                               $tag['description'] = $msg->exists() ? $msg->text() : '';
+                       }
 
-               $doneTags[] = $tagName;
+                       if ( $fld_hitcount ) {
+                               $tag['hitcount'] = $hitcount;
+                       }
 
-               $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
-               if ( !$fit ) {
-                       $this->setContinueEnumParameter( 'continue', $tagName );
+                       if ( $fld_defined && isset( $definedTags[$tagName] ) ) {
+                               $tag['defined'] = '';
+                       }
 
-                       return false;
+                       $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
+                       if ( !$fit ) {
+                               $this->setContinueEnumParameter( 'continue', $tagName );
+                               break;
+                       }
                }
 
-               return true;
+               $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
        }
 
        public function getCacheMode( $params ) {
@@ -151,7 +135,8 @@ class ApiQueryTags extends ApiQueryBase {
                                        'name',
                                        'displayname',
                                        'description',
-                                       'hitcount'
+                                       'hitcount',
+                                       'defined',
                                ),
                                ApiBase::PARAM_ISMULTI => true
                        )
@@ -160,7 +145,7 @@ class ApiQueryTags extends ApiQueryBase {
 
        protected function getExamplesMessages() {
                return array(
-                       'action=query&list=tags&tgprop=displayname|description|hitcount'
+                       'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
                                => 'apihelp-query+tags-example-simple',
                );
        }
index bb5e5db..e941f1a 100644 (file)
 
        "apihelp-query+tags-description": "List change tags.",
        "apihelp-query+tags-param-limit": "The maximum number of tags to list.",
-       "apihelp-query+tags-param-prop": "Which properties to get:\n;name:Adds name of tag.\n;displayname:Adds system message for the tag.\n;description:Adds description of the tag.\n;hitcount:Adds the amount of revisions that have this tag.",
+       "apihelp-query+tags-param-prop": "Which properties to get:\n;name:Adds name of tag.\n;displayname:Adds system message for the tag.\n;description:Adds description of the tag.\n;hitcount:Adds the amount of revisions that have this tag.\n;defined:Indicate whether the tag is defined.",
        "apihelp-query+tags-example-simple": "List available tags",
 
        "apihelp-query+templates-description": "Returns all pages transcluded on the given pages.",