* (bug 27479) API error when using both prop=pageprops and prop=info&inprop=displaytitle
[lhc/web/wiklou.git] / includes / api / ApiQueryCategoryMembers.php
index 0778f38..0f469dd 100644 (file)
@@ -77,12 +77,15 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                $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' ) );
@@ -90,19 +93,10 @@ 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->addTables( array( 'page', 'categorylinks' ) );   // must be in this order for 'USE INDEX'
 
-               $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
@@ -119,23 +113,30 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
 
                if ( $params['sort'] == 'timestamp' ) {
                        $this->addWhereRange( 'cl_timestamp',
-                                                                       $dir,
-                                                                       $params['start'],
-                                                                       $params['end'] );
+                               $dir,
+                               $params['start'],
+                               $params['end'] );
+
+                       $this->addOption( 'USE INDEX', 'cl_timestamp' );
                } else {
+                       // The below produces ORDER BY cl_type, cl_sortkey, cl_from, possibly with DESC added to each of them
+                       $this->addWhereRange( 'cl_type', $dir, null, null );
                        $this->addWhereRange( 'cl_sortkey',
-                                                                       $dir,
-                                                                       $params['startsortkey'],
-                                                                       $params['endsortkey'] );
-
+                               $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;
                $res = $this->select( __METHOD__ );
                foreach ( $res as $row ) {
                        if ( ++ $count > $limit ) {
@@ -144,7 +145,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', $row->cl_from );
                                }
                                break;
                        }
@@ -169,6 +170,9 @@ 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;
                                }
@@ -181,14 +185,13 @@ 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', $row->cl_from );
                                        }
                                        break;
                                }
                        } else {
                                $resultPageSet->processDbRow( $row );
                        }
-                       $lastSortKey = $row->cl_sortkey; // detect duplicate sortkeys
                }
 
                if ( is_null( $resultPageSet ) ) {
@@ -197,14 +200,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
         */
@@ -213,26 +208,11 @@ 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' ? '<' : '>' );
+               $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() {
@@ -250,6 +230,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                        'ids',
                                        'title',
                                        'sortkey',
+                                       'sortkeyprefix',
                                        'type',
                                        'timestamp',
                                )
@@ -308,11 +289,12 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                        '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',
-                               ' type       - Adds the type that the page has been categorised as',
-                               ' 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 sorting in the category (may not be human-readble)',
+                               ' sortkeyprefix - Adds the sortkey prefix used for sorting in the category (human-readable part of the sortkey)',
+                               ' type          - Adds the type that the page has been categorised as (page, subcat or file)',
+                               ' 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',