Fix spaes from r80362
[lhc/web/wiklou.git] / includes / api / ApiQueryCategoryMembers.php
index d0919d0..70ca5a1 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,41 +55,50 @@ 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 ( is_null( $categoryTitle ) || $categoryTitle->getNamespace() != NS_CATEGORY ) {
-                       $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
+               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 ( !$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'] );
                $fld_ids = isset( $prop['ids'] );
                $fld_title = isset( $prop['title'] );
                $fld_sortkey = isset( $prop['sortkey'] );
+               $fld_sortkeyprefix = isset( $prop['sortkeyprefix'] );
                $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' ) );
+                       $this->addFields( array( 'cl_from', 'page_namespace', 'page_title' ) );
                        $this->addFieldsIf( 'page_id', $fld_ids );
+                       $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
+                       $this->addFieldsIf( 'cl_sortkey', $fld_sortkey );
                } else {
                        $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
                        $this->addFields( array( 'cl_from', 'cl_sortkey' ) );
                }
 
                $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,18 +108,37 @@ 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->addOption( 'ORDER BY', 'cl_type' );
+
+                       $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 );
 
                $count = 0;
-               $lastSortKey = null;
+               $lastFrom = null;
                $res = $this->select( __METHOD__ );
                foreach ( $res as $row ) {
                        if ( ++ $count > $limit ) {
@@ -118,7 +147,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                if ( $params['sort'] == 'timestamp' ) {
                                        $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
                                } else {
-                                       $this->setContinueEnumParameter( 'continue', $this->getContinueStr( $row, $lastSortKey ) );
+                                       $this->setContinueEnumParameter( 'continue', $lastFrom );
                                }
                                break;
                        }
@@ -143,6 +172,12 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                if ( $fld_sortkey ) {
                                        $vals['sortkey'] = $row->cl_sortkey;
                                }
+                               if ( $fld_sortkeyprefix ) {
+                                       $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
+                               }
+                               if ( $fld_type  ) {
+                                       $vals['type'] = $row->cl_type;
+                               }
                                if ( $fld_timestamp ) {
                                        $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
                                }
@@ -152,14 +187,14 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                        if ( $params['sort'] == 'timestamp' ) {
                                                $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
                                        } else {
-                                               $this->setContinueEnumParameter( 'continue', $this->getContinueStr( $row, $lastSortKey ) );
+                                               $this->setContinueEnumParameter( 'continue', $lastFrom );
                                        }
                                        break;
                                }
                        } else {
                                $resultPageSet->processDbRow( $row );
                        }
-                       $lastSortKey = $row->cl_sortkey; // detect duplicate sortkeys
+                       $lastFrom = $row->cl_from; // detect duplicate sortkeys
                }
 
                if ( is_null( $resultPageSet ) ) {
@@ -168,14 +203,6 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                }
        }
 
-       private function getContinueStr( $row, $lastSortKey ) {
-               $ret = $row->cl_sortkey . '|';
-               if ( $row->cl_sortkey == $lastSortKey ) { // duplicate sort key, add cl_from
-                       $ret .= $row->cl_from;
-               }
-               return $ret;
-       }
-
        /**
         * Add DB WHERE clause to continue previous query based on 'continue' parameter
         */
@@ -184,31 +211,21 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                        return; // This is not a continuation request
                }
 
-               $pos = strrpos( $continue, '|' );
-               $sortkey = substr( $continue, 0, $pos );
-               $fromstr = substr( $continue, $pos + 1 );
-               $from = intval( $fromstr );
-
-               if ( $from == 0 && strlen( $fromstr ) > 0 ) {
-                       $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
-               }
-
-               $encSortKey = $this->getDB()->addQuotes( $sortkey );
-               $encFrom = $this->getDB()->addQuotes( $from );
+               $encFrom = $this->getDB()->addQuotes( intval( $continue ) );
 
                $op = ( $dir == 'desc' ? '<' : '>' );
 
-               if ( $from != 0 ) {
-                       // Duplicate sort key continue
-                       $this->addWhere( "cl_sortkey$op$encSortKey OR (cl_sortkey=$encSortKey AND cl_from$op=$encFrom)" );
-               } else {
-                       $this->addWhere( "cl_sortkey$op=$encSortKey" );
-               }
+               $this->addWhere( "cl_from $op $encFrom" );
        }
 
        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 +233,8 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                        'ids',
                                        'title',
                                        'sortkey',
+                                       'sortkeyprefix',
+                                       'type',
                                        'timestamp',
                                )
                        ),
@@ -223,6 +242,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 +288,19 @@ 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',
-                               ' sortkey    - Adds the sortkey used for the category',
-                               ' timestamp  - Adds the timestamp of when the page was included',
+                               ' ids           - Adds the page ID',
+                               ' title         - Adds the title and namespace ID of the page',
+                               ' sortkey       - Adds the sortkey used for the category (note, may be non human readable)',
+                               ' sortkeyprefix - Adds the sortkey prefix 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 +326,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' ),
                ) );
        }