Bleh, didn't press save again (r80350)
[lhc/web/wiklou.git] / includes / api / ApiQueryCategoryMembers.php
index d0919d0..9310011 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-
 /**
- * Created on June 14, 2007
  *
- * API for MediaWiki 1.8+
+ *
+ * Created on June 14, 2007
  *
  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
@@ -21,6 +20,8 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
 if ( !defined( 'MEDIAWIKI' ) ) {
@@ -54,13 +55,22 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
        private function run( $resultPageSet = null ) {
                $params = $this->extractRequestParams();
 
-               if ( !isset( $params['title'] ) || is_null( $params['title'] ) ) {
-                       $this->dieUsage( 'The cmtitle parameter is required', 'notitle' );
-               }
-               $categoryTitle = Title::newFromText( $params['title'] );
+               $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
+
+               if ( isset( $params['title'] ) ) {
+                       $categoryTitle = Title::newFromText( $params['title'] );
+
+                       if ( is_null( $categoryTitle ) || $categoryTitle->getNamespace() != NS_CATEGORY ) {
+                               $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
+                       }
+               } elseif( isset( $params['pageid'] ) ) {
+                       $categoryTitle = Title::newFromID( $params['pageid'] );
 
-               if ( is_null( $categoryTitle ) || $categoryTitle->getNamespace() != NS_CATEGORY ) {
-                       $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
+                       if ( !$categoryTitle ) {
+                               $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
+                       } elseif ( $categoryTitle->getNamespace() != NS_CATEGORY ) {
+                               $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
+                       }
                }
 
                $prop = array_flip( $params['prop'] );
@@ -68,6 +78,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                $fld_title = isset( $prop['title'] );
                $fld_sortkey = isset( $prop['sortkey'] );
                $fld_timestamp = isset( $prop['timestamp'] );
+               $fld_type = isset( $prop['type'] );
 
                if ( is_null( $resultPageSet ) ) {
                        $this->addFields( array( 'cl_from', 'cl_sortkey', 'page_namespace', 'page_title' ) );
@@ -78,17 +89,13 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                }
 
                $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
+               $this->addFieldsIf( 'cl_type', $fld_type );
+
                $this->addTables( array( 'page', 'categorylinks' ) );   // must be in this order for 'USE INDEX'
-                                                                       // Not needed after bug 10280 is applied to servers
-               if ( $params['sort'] == 'timestamp' ) {
-                       $this->addOption( 'USE INDEX', 'cl_timestamp' );
-               } else {
-                       $this->addOption( 'USE INDEX', 'cl_sortkey' );
-               }
 
-               $this->addWhere( 'cl_from=page_id' );
-               $this->setContinuation( $params['continue'], $params['dir'] );
                $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
+               $this->addWhereFld( 'cl_type', $params['type'] );
+
                // Scanning large datasets for rare categories sucks, and I already told
                // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
                global $wgMiserMode;
@@ -98,13 +105,30 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                } else {
                        $this->addWhereFld( 'page_namespace', $params['namespace'] );
                }
+
+               $dir = $params['dir'] == 'asc' ? 'newer' : 'older';
+
                if ( $params['sort'] == 'timestamp' ) {
-                       $this->addWhereRange( 'cl_timestamp', ( $params['dir'] == 'asc' ? 'newer' : 'older' ), $params['start'], $params['end'] );
+                       $this->addWhereRange( 'cl_timestamp',
+                               $dir,
+                               $params['start'],
+                               $params['end'] );
+
+                       $this->addOption( 'USE INDEX', 'cl_timestamp' );
                } else {
-                       $this->addWhereRange( 'cl_sortkey', ( $params['dir'] == 'asc' ? 'newer' : 'older' ), $params['startsortkey'], $params['endsortkey'] );
-                       $this->addWhereRange( 'cl_from', ( $params['dir'] == 'asc' ? 'newer' : 'older' ), null, null );
+                       $this->addWhereRange( 'cl_sortkey',
+                               $dir,
+                               $params['startsortkey'],
+                               $params['endsortkey'] );
+
+                       $this->addWhereRange( 'cl_from', $dir, null, null );
+                       $this->addOption( 'USE INDEX', 'cl_sortkey' );
                }
 
+               $this->setContinuation( $params['continue'], $params['dir'] );
+
+               $this->addWhere( 'cl_from=page_id' );
+
                $limit = $params['limit'];
                $this->addOption( 'LIMIT', $limit + 1 );
 
@@ -143,6 +167,9 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                if ( $fld_sortkey ) {
                                        $vals['sortkey'] = $row->cl_sortkey;
                                }
+                               if ( $fld_type  ) {
+                                       $vals['type'] = $row->cl_type;
+                               }
                                if ( $fld_timestamp ) {
                                        $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
                                }
@@ -208,7 +235,12 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
 
        public function getAllowedParams() {
                return array(
-                       'title' => null,
+                       'title' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                       ),
+                       'pageid' => array(
+                               ApiBase::PARAM_TYPE => 'integer'
+                       ),
                        'prop' => array(
                                ApiBase::PARAM_DFLT => 'ids|title',
                                ApiBase::PARAM_ISMULTI => true,
@@ -216,6 +248,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                        'ids',
                                        'title',
                                        'sortkey',
+                                       'type',
                                        'timestamp',
                                )
                        ),
@@ -223,6 +256,15 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                ApiBase::PARAM_ISMULTI => true,
                                ApiBase::PARAM_TYPE => 'namespace',
                        ),
+                       'type' => array(
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_DFLT => 'page|subcat|file',
+                               ApiBase::PARAM_TYPE => array(
+                                       'page',
+                                       'subcat',
+                                       'file'
+                               )
+                       ),
                        'continue' => null,
                        'limit' => array(
                                ApiBase::PARAM_TYPE => 'limit',
@@ -260,15 +302,18 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                global $wgMiserMode;
                $p = $this->getModulePrefix();
                $desc = array(
-                       'title' => 'Which category to enumerate (required). Must include Category: prefix',
+                       'title' => 'Which category to enumerate (required). Must include Category: prefix. Cannot be used together with cmpageid',
+                       'pageid' => 'Page ID of the category to enumerate. Cannot be used together with cmtitle',
                        'prop' => array(
                                'What pieces of information to include',
-                               ' ids        - Adds the page id',
-                               ' title      - Adds the title and namespace id of the page',
+                               ' ids        - Adds the page ID',
+                               ' title      - Adds the title and namespace ID of the page',
                                ' sortkey    - Adds the sortkey used for the category',
+                               ' type       - Adds the type that the page has been categorised as',
                                ' timestamp  - Adds the timestamp of when the page was included',
                        ),
                        'namespace' => 'Only include pages in these namespaces',
+                       'type' => 'What type of category members to include',
                        'sort' => 'Property to sort by',
                        'dir' => 'In which direction to sort',
                        'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
@@ -294,9 +339,11 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
 
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
-                       array( 'code' => 'notitle', 'info' => 'The cmtitle parameter is required' ),
+                       array( 'code' => 'cmmissingparam', 'info' => 'One of the parameters title, pageid is required' ),
+                       array( 'code' => 'cminvalidparammix', 'info' => 'The parameters title, pageid can not be used together' ),
                        array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
                        array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
+                       array( 'nosuchpageid', 'pageid' ),
                ) );
        }