+ $this->addOption( 'USE INDEX', 'cl_timestamp' );
+ } else {
+ if ( $params['continue'] ) {
+ $cont = explode( '|', $params['continue'], 3 );
+ if ( count( $cont ) != 3 ) {
+ $this->dieUsage( 'Invalid continue param. You should pass the original value returned '.
+ 'by the previous query', '_badcontinue'
+ );
+ }
+
+ // Remove the types to skip from $queryTypes
+ $contTypeIndex = array_search( $cont[0], $queryTypes );
+ $queryTypes = array_slice( $queryTypes, $contTypeIndex );
+
+ // Add a WHERE clause for sortkey and from
+ // pack( "H*", $foo ) is used to convert hex back to binary
+ $escSortkey = $this->getDB()->addQuotes( pack( "H*", $cont[1] ) );
+ $from = intval( $cont[2] );
+ $op = $dir == 'newer' ? '>' : '<';
+ // $contWhere is used further down
+ $contWhere = "cl_sortkey $op $escSortkey OR " .
+ "(cl_sortkey = $escSortkey AND " .
+ "cl_from $op= $from)";
+
+ } else {
+ // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
+ $this->addWhereRange( 'cl_sortkey',
+ $dir,
+ $params['startsortkey'],
+ $params['endsortkey'] );
+ $this->addWhereRange( 'cl_from', $dir, null, null );
+ }
+ $this->addOption( 'USE INDEX', 'cl_sortkey' );
+ }
+
+ $this->addWhere( 'cl_from=page_id' );
+
+ $limit = $params['limit'];
+ $this->addOption( 'LIMIT', $limit + 1 );
+
+ if ( $params['sort'] == 'sortkey' ) {
+ // Run a separate SELECT query for each value of cl_type.
+ // This is needed because cl_type is an enum, and MySQL has
+ // inconsistencies between ORDER BY cl_type and
+ // WHERE cl_type >= 'foo' making proper paging impossible
+ // and unindexed.
+ $rows = array();
+ $first = true;
+ foreach ( $queryTypes as $type ) {
+ $extraConds = array( 'cl_type' => $type );
+ if ( $first && $contWhere ) {
+ // Continuation condition. Only added to the
+ // first query, otherwise we'll skip things
+ $extraConds[] = $contWhere;
+ }
+ $res = $this->select( __METHOD__, array( 'where' => $extraConds ) );
+ $rows = array_merge( $rows, iterator_to_array( $res ) );
+ if ( count( $rows ) >= $limit + 1 ) {
+ break;
+ }
+ $first = false;
+ }
+ } else {
+ // Sorting by timestamp
+ // No need to worry about per-type queries because we
+ // aren't sorting or filtering by type anyway
+ $res = $this->select( __METHOD__ );
+ $rows = iterator_to_array( $res );
+ }