Merge "Handle missing width nicely in thumb.php"
[lhc/web/wiklou.git] / includes / api / ApiQueryTags.php
index d1e6e28..0e3307b 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,102 @@ 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'] );
+               $fld_source = isset( $prop['source'] );
+               $fld_active = isset( $prop['active'] );
+
+               $limit = $params['limit'];
+               $result = $this->getResult();
+
+               $extensionDefinedTags = array_fill_keys( ChangeTags::listExtensionDefinedTags(), 0 );
+               $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 );
+               $extensionActivatedTags = array_fill_keys( ChangeTags::listExtensionActivatedTags(), 0 );
+
+               $definedTags = array_merge( $extensionDefinedTags, $explicitlyDefinedTags );
+
+               # 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 );
+                       if ( $fld_displayname ) {
+                               $tag['displayname'] = ChangeTags::tagDescription( $tagName );
+                       }
 
-                       return false;
-               }
+                       if ( $fld_description ) {
+                               $msg = $this->msg( "tag-$tagName-description" );
+                               $tag['description'] = $msg->exists() ? $msg->text() : '';
+                       }
 
-               $tag = array();
-               $tag['name'] = $tagName;
+                       if ( $fld_hitcount ) {
+                               $tag['hitcount'] = $hitcount;
+                       }
 
-               if ( $this->fld_displayname ) {
-                       $tag['displayname'] = ChangeTags::tagDescription( $tagName );
-               }
+                       $isExtension = isset( $extensionDefinedTags[$tagName] );
+                       $isExplicit = isset( $explicitlyDefinedTags[$tagName] );
 
-               if ( $this->fld_description ) {
-                       $msg = wfMessage( "tag-$tagName-description" );
-                       $tag['description'] = $msg->exists() ? $msg->text() : '';
-               }
-
-               if ( $this->fld_hitcount ) {
-                       $tag['hitcount'] = $hitcount;
-               }
+                       if ( $fld_defined && ( $isExtension || $isExplicit ) ) {
+                               $tag['defined'] = '';
+                       }
 
-               $doneTags[] = $tagName;
+                       if ( $fld_source ) {
+                               $tag['source'] = array();
+                               if ( $isExtension ) {
+                                       $tag['source'][] = 'extension';
+                               }
+                               if ( $isExplicit ) {
+                                       $tag['source'][] = 'manual';
+                               }
+                       }
 
-               $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
-               if ( !$fit ) {
-                       $this->setContinueEnumParameter( 'continue', $tagName );
+                       if ( $fld_active &&
+                               ( $isExplicit || isset( $extensionActivatedTags[$tagName] ) )
+                       ) {
+                               $tag['active'] = '';
+                       }
 
-                       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,16 +160,19 @@ class ApiQueryTags extends ApiQueryBase {
                                        'name',
                                        'displayname',
                                        'description',
-                                       'hitcount'
+                                       'hitcount',
+                                       'defined',
+                                       'source',
+                                       'active',
                                ),
                                ApiBase::PARAM_ISMULTI => true
                        )
                );
        }
 
-       public function getExamplesMessages() {
+       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',
                );
        }