(bug 26909) Add dir parameter for prop= API modules. Modified patch by Umherirrender
authorRoan Kattouw <catrope@users.mediawiki.org>
Mon, 14 Nov 2011 08:19:55 +0000 (08:19 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Mon, 14 Nov 2011 08:19:55 +0000 (08:19 +0000)
includes/api/ApiQueryBase.php
includes/api/ApiQueryCategories.php
includes/api/ApiQueryDuplicateFiles.php
includes/api/ApiQueryIWLinks.php
includes/api/ApiQueryImages.php
includes/api/ApiQueryLangLinks.php
includes/api/ApiQueryLinks.php

index daadcaa..49db0c2 100644 (file)
@@ -214,11 +214,10 @@ abstract class ApiQueryBase extends ApiBase {
 
                if ( $sort ) {
                        $order = $field . ( $isDirNewer ? '' : ' DESC' );
-                       if ( !isset( $this->options['ORDER BY'] ) ) {
-                               $this->addOption( 'ORDER BY', $order );
-                       } else {
-                               $this->addOption( 'ORDER BY', $this->options['ORDER BY'] . ', ' . $order );
-                       }
+                       // Append ORDER BY
+                       $optionOrderBy = isset( $this->options['ORDER BY'] ) ? (array)$this->options['ORDER BY'] : array();
+                       $optionOrderBy[] = $order;
+                       $this->addOption( 'ORDER BY', $optionOrderBy );
                }
        }
 
index 8ed4846..823eef9 100644 (file)
@@ -127,11 +127,16 @@ class ApiQueryCategories extends ApiQueryGeneratorBase {
                }
 
                $this->addOption( 'USE INDEX', array( 'categorylinks' => 'cl_from' ) );
+
+               $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
                // Don't order by cl_from if it's constant in the WHERE clause
                if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
-                       $this->addOption( 'ORDER BY', 'cl_to' );
+                       $this->addOption( 'ORDER BY', 'cl_to' . $dir );
                } else {
-                       $this->addOption( 'ORDER BY', "cl_from, cl_to" );
+                       $this->addOption( 'ORDER BY', array(
+                                               'cl_from' . $dir,
+                                               'cl_to' . $dir
+                       ));
                }
 
                $res = $this->select( __METHOD__ );
@@ -213,6 +218,13 @@ class ApiQueryCategories extends ApiQueryGeneratorBase {
                        'categories' => array(
                                ApiBase::PARAM_ISMULTI => true,
                        ),
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               )
+                       ),
                );
        }
 
@@ -228,6 +240,7 @@ class ApiQueryCategories extends ApiQueryGeneratorBase {
                        'show' => 'Which kind of categories to show',
                        'continue' => 'When more results are available, use this to continue',
                        'categories' => 'Only list these categories. Useful for checking whether a certain page is in a certain category',
+                       'dir' => 'The direction in which to list',
                );
        }
 
index 7193675..90c345c 100644 (file)
@@ -94,7 +94,8 @@ class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
                        );
                }
 
-               $this->addOption( 'ORDER BY', 'i1.img_name' );
+               $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
+               $this->addOption( 'ORDER BY', 'i1.img_name' . $dir );
                $this->addOption( 'LIMIT', $params['limit'] + 1 );
 
                $res = $this->select( __METHOD__ );
@@ -141,6 +142,13 @@ class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
                        ),
                        'continue' => null,
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               )
+                       ),
                );
        }
 
@@ -148,6 +156,7 @@ class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
                return array(
                        'limit' => 'How many files to return',
                        'continue' => 'When more results are available, use this to continue',
+                       'dir' => 'The direction in which to list',
                );
        }
 
index 569f36f..a0cff72 100644 (file)
@@ -79,20 +79,27 @@ class ApiQueryIWLinks extends ApiQueryBase {
                        );
                }
 
+               $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
                if ( isset( $params['prefix'] ) ) {
                        $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
                        if ( isset( $params['title'] ) ) {
                                $this->addWhereFld( 'iwl_title', $params['title'] );
-                               $this->addOption( 'ORDER BY', 'iwl_from' );
+                               $this->addOption( 'ORDER BY', 'iwl_from' . $dir );
                        } else {
-                               $this->addOption( 'ORDER BY', 'iwl_title, iwl_from' );
+                               $this->addOption( 'ORDER BY', array(
+                                               'iwl_title' . $dir,
+                                               'iwl_from' . $dir
+                               ));
                        }
                } else {
                        // Don't order by iwl_from if it's constant in the WHERE clause
                        if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
-                               $this->addOption( 'ORDER BY', 'iwl_prefix' );
+                               $this->addOption( 'ORDER BY', 'iwl_prefix' . $dir );
                        } else {
-                               $this->addOption( 'ORDER BY', 'iwl_from, iwl_prefix' );
+                               $this->addOption( 'ORDER BY', array (
+                                               'iwl_from' . $dir,
+                                               'iwl_prefix' . $dir
+                               ));
                        }
                }
 
@@ -142,6 +149,13 @@ class ApiQueryIWLinks extends ApiQueryBase {
                        'continue' => null,
                        'prefix' => null,
                        'title' => null,
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               )
+                       ),
                );
        }
 
@@ -152,6 +166,7 @@ class ApiQueryIWLinks extends ApiQueryBase {
                        'continue' => 'When more results are available, use this to continue',
                        'prefix' => 'Prefix for the interwiki',
                        'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
+                       'dir' => 'The direction in which to list',
                );
        }
 
index c69ea93..dfd5722 100644 (file)
@@ -79,11 +79,15 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                        );
                }
 
+               $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
                // Don't order by il_from if it's constant in the WHERE clause
                if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
-                       $this->addOption( 'ORDER BY', 'il_to' );
+                       $this->addOption( 'ORDER BY', 'il_to' . $dir );
                } else {
-                       $this->addOption( 'ORDER BY', 'il_from, il_to' );
+                       $this->addOption( 'ORDER BY', array(
+                                               'il_from' . $dir,
+                                               'il_to' . $dir
+                       ));
                }
                $this->addOption( 'LIMIT', $params['limit'] + 1 );
 
@@ -154,7 +158,14 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                        'continue' => null,
                        'images' => array(
                                ApiBase::PARAM_ISMULTI => true,
-                       )
+                       ),
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               )
+                       ),
                );
        }
 
@@ -163,6 +174,7 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                        'limit' => 'How many images to return',
                        'continue' => 'When more results are available, use this to continue',
                        'images' => 'Only list these images. Useful for checking whether a certain page has a certain Image.',
+                       'dir' => 'The direction in which to list',
                );
        }
 
index b0880bb..aafa294 100644 (file)
@@ -74,20 +74,27 @@ class ApiQueryLangLinks extends ApiQueryBase {
                        );
                }
 
+           $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
            if ( isset( $params['lang'] ) ) {
                        $this->addWhereFld( 'll_lang', $params['lang'] );
                        if ( isset( $params['title'] ) ) {
                                $this->addWhereFld( 'll_title', $params['title'] );
-                               $this->addOption( 'ORDER BY', 'll_from' );
+                               $this->addOption( 'ORDER BY', 'll_from' . $dir );
                        } else {
-                               $this->addOption( 'ORDER BY', 'll_title, ll_from' );
+                               $this->addOption( 'ORDER BY', array(
+                                                       'll_title' . $dir,
+                                                       'll_from' . $dir
+                               ));
                        }
                } else {
                        // Don't order by ll_from if it's constant in the WHERE clause
                        if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
-                               $this->addOption( 'ORDER BY', 'll_lang' );
+                               $this->addOption( 'ORDER BY', 'll_lang' . $dir );
                        } else {
-                               $this->addOption( 'ORDER BY', 'll_from, ll_lang' );
+                               $this->addOption( 'ORDER BY', array(
+                                                       'll_from' . $dir,
+                                                       'll_lang' . $dir
+                               ));
                        }
                }
 
@@ -135,6 +142,13 @@ class ApiQueryLangLinks extends ApiQueryBase {
                        'url' => false,
                        'lang' => null,
                        'title' => null,
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               )
+                       ),
                );
        }
 
@@ -145,6 +159,7 @@ class ApiQueryLangLinks extends ApiQueryBase {
                        'url' => 'Whether to get the full URL',
                        'lang' => 'Language code',
                        'title' => "Link to search for. Must be used with {$this->getModulePrefix()}lang",
+                       'dir' => 'The direction in which to list',
                );
        }
 
index 8090daf..e2524a5 100644 (file)
@@ -48,6 +48,7 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                                $this->prefix = 'pl';
                                $this->description = 'link';
                                $this->titlesParam = 'titles';
+                               $this->titlesParamDescription = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
                                $this->helpUrl = 'http://www.mediawiki.org/wiki/API:Properties#links_.2F_pl';
                                break;
                        case self::TEMPLATES:
@@ -55,6 +56,7 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                                $this->prefix = 'tl';
                                $this->description = 'template';
                                $this->titlesParam = 'templates';
+                               $this->titlesParamDescription = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
                                $this->helpUrl = 'http://www.mediawiki.org/wiki/API:Properties#templates_.2F_tl';
                                break;
                        default:
@@ -131,6 +133,7 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                        );
                }
 
+               $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
                // Here's some MySQL craziness going on: if you use WHERE foo='bar'
                // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
                // but instead goes and filesorts, because the index for foo was used
@@ -138,15 +141,15 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                // clause from the ORDER BY clause
                $order = array();
                if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
-                       $order[] = "{$this->prefix}_from";
+                       $order[] = $this->prefix . '_from' . $dir;
                }
                if ( count( $params['namespace'] ) != 1 ) {
-                       $order[] = "{$this->prefix}_namespace";
+                       $order[] = $this->prefix . '_namespace' . $dir;
                }
 
-               $order[] = "{$this->prefix}_title";
-               $this->addOption( 'ORDER BY', implode( ', ', $order ) );
-               $this->addOption( 'USE INDEX', "{$this->prefix}_from" );
+               $order[] = $this->prefix . "_title" . $dir;
+               $this->addOption( 'ORDER BY', $order );
+               $this->addOption( 'USE INDEX', $this->prefix . '_from' );
                $this->addOption( 'LIMIT', $params['limit'] + 1 );
 
                $res = $this->select( __METHOD__ );
@@ -207,22 +210,25 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                        $this->titlesParam => array(
                                ApiBase::PARAM_ISMULTI => true,
                        ),
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               )
+                       ),
                );
        }
 
        public function getParamDescription() {
                $desc = $this->description;
-               $arr = array(
+               return array(
                        'namespace' => "Show {$desc}s in this namespace(s) only",
                        'limit' => "How many {$desc}s to return",
                        'continue' => 'When more results are available, use this to continue',
+                       $this->titlesParam => $this->titlesParamDescription,
+                       'dir' => 'The direction in which to list',
                );
-               if ( $this->getModuleName() == self::LINKS ) {
-                       $arr[$this->titlesParam] = 'Only list links to these titles. Useful for checking whether a certain page links to a certain title.';
-               } elseif ( $this->getModuleName() == self::TEMPLATES ) {
-                       $arr[$this->titlesParam] = 'Only list these templates. Useful for checking whether a certain page uses a certain template.';
-               }
-               return $arr;
        }
 
        public function getDescription() {