API: Add paging (i.e. limit and continue) parameters to prop={links,templatelinks...
[lhc/web/wiklou.git] / includes / api / ApiQueryLangLinks.php
index 99457ae..735f67c 100644 (file)
@@ -30,8 +30,8 @@ if (!defined('MEDIAWIKI')) {
 
 /**
  * A query module to list all langlinks (links to correspanding foreign language pages).
- * 
- * @addtogroup API
+ *
+ * @ingroup API
  */
 class ApiQueryLangLinks extends ApiQueryBase {
 
@@ -40,6 +40,10 @@ class ApiQueryLangLinks extends ApiQueryBase {
        }
 
        public function execute() {
+               if ( $this->getPageSet()->getGoodTitleCount() == 0 )
+                       return;
+
+               $params = $this->extractRequestParams();
                $this->addFields(array (
                        'll_from',
                        'll_lang',
@@ -48,14 +52,36 @@ class ApiQueryLangLinks extends ApiQueryBase {
 
                $this->addTables('langlinks');
                $this->addWhereFld('ll_from', array_keys($this->getPageSet()->getGoodTitles()));
-               $this->addOption('ORDER BY', "ll_from, ll_lang");
+               if(!is_null($params['continue'])) {
+                       $cont = explode('|', $params['continue']);
+                       if(count($cont) != 2)
+                               $this->dieUsage("Invalid continue param. You should pass the " .
+                                       "original value returned by the previous query", "_badcontinue");
+                       $llfrom = intval($cont[0]);
+                       $lllang = $this->getDb()->strencode($cont[1]);
+                       $this->addWhere("ll_from > $llfrom OR ".
+                                       "(ll_from = $llfrom AND ".
+                                       "ll_lang >= '$lllang')");
+               }
+               # 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');
+               else
+                       $this->addOption('ORDER BY', 'll_from, ll_lang');
+               $this->addOption('LIMIT', $params['limit'] + 1);
                $res = $this->select(__METHOD__);
 
                $data = array();
-               $lastId = 0;    // database has no ID 0 
+               $lastId = 0;    // database has no ID 0
+               $count = 0;
                $db = $this->getDB();
                while ($row = $db->fetchObject($res)) {
-
+                       if (++$count > $params['limit']) {
+                               // We've reached the one extra which shows that
+                               // there are additional pages to be had. Stop here...
+                               $this->setContinueEnumParameter('continue', "{$row->ll_from}|{$row->ll_lang}");
+                               break;
+                       }
                        if ($lastId != $row->ll_from) {
                                if($lastId != 0) {
                                        $this->addPageSubItems($lastId, $data);
@@ -64,7 +90,7 @@ class ApiQueryLangLinks extends ApiQueryBase {
                                $lastId = $row->ll_from;
                        }
 
-                       $entry = array('lang'=>$row->ll_lang);
+                       $entry = array('lang' => $row->ll_lang);
                        ApiResult :: setContent($entry, $row->ll_title);
                        $data[] = $entry;
                }
@@ -76,7 +102,27 @@ class ApiQueryLangLinks extends ApiQueryBase {
                $db->freeResult($res);
        }
 
-       protected function getDescription() {
+       public function getAllowedParams() {
+               return array(
+                               'limit' => array(
+                                       ApiBase :: PARAM_DFLT => 10,
+                                       ApiBase :: PARAM_TYPE => 'limit',
+                                       ApiBase :: PARAM_MIN => 1,
+                                       ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
+                                       ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
+                               ),
+                               'continue' => null,
+               );
+       }
+
+       public function getParamDescription () {
+               return array(
+                       'limit' => 'How many langlinks to return',
+                       'continue' => 'When more results are available, use this to continue',
+               );
+       }
+
+       public function getDescription() {
                return 'Returns all interlanguage links from the given page(s)';
        }
 
@@ -91,4 +137,3 @@ class ApiQueryLangLinks extends ApiQueryBase {
                return __CLASS__ . ': $Id$';
        }
 }
-