ApiQueryLangLinks.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * Created on May 13, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ("ApiQueryBase.php");
  27. }
  28. /**
  29. * A query module to list all langlinks (links to correspanding foreign language pages).
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryLangLinks extends ApiQueryBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'll');
  36. }
  37. public function execute() {
  38. if ( $this->getPageSet()->getGoodTitleCount() == 0 )
  39. return;
  40. $params = $this->extractRequestParams();
  41. $this->addFields(array (
  42. 'll_from',
  43. 'll_lang',
  44. 'll_title'
  45. ));
  46. $this->addTables('langlinks');
  47. $this->addWhereFld('ll_from', array_keys($this->getPageSet()->getGoodTitles()));
  48. if(!is_null($params['continue'])) {
  49. $cont = explode('|', $params['continue']);
  50. if(count($cont) != 2)
  51. $this->dieUsage("Invalid continue param. You should pass the " .
  52. "original value returned by the previous query", "_badcontinue");
  53. $llfrom = intval($cont[0]);
  54. $lllang = $this->getDB()->strencode($cont[1]);
  55. $this->addWhere("ll_from > $llfrom OR ".
  56. "(ll_from = $llfrom AND ".
  57. "ll_lang >= '$lllang')");
  58. }
  59. # Don't order by ll_from if it's constant in the WHERE clause
  60. if(count($this->getPageSet()->getGoodTitles()) == 1)
  61. $this->addOption('ORDER BY', 'll_lang');
  62. else
  63. $this->addOption('ORDER BY', 'll_from, ll_lang');
  64. $this->addOption('LIMIT', $params['limit'] + 1);
  65. $res = $this->select(__METHOD__);
  66. $count = 0;
  67. $db = $this->getDB();
  68. while ($row = $db->fetchObject($res)) {
  69. if (++$count > $params['limit']) {
  70. // We've reached the one extra which shows that
  71. // there are additional pages to be had. Stop here...
  72. $this->setContinueEnumParameter('continue', "{$row->ll_from}|{$row->ll_lang}");
  73. break;
  74. }
  75. $entry = array('lang' => $row->ll_lang);
  76. ApiResult :: setContent($entry, $row->ll_title);
  77. $fit = $this->addPageSubItem($row->ll_from, $entry);
  78. if(!$fit)
  79. {
  80. $this->setContinueEnumParameter('continue', "{$row->ll_from}|{$row->ll_lang}");
  81. break;
  82. }
  83. }
  84. $db->freeResult($res);
  85. }
  86. public function getAllowedParams() {
  87. return array(
  88. 'limit' => array(
  89. ApiBase :: PARAM_DFLT => 10,
  90. ApiBase :: PARAM_TYPE => 'limit',
  91. ApiBase :: PARAM_MIN => 1,
  92. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  93. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  94. ),
  95. 'continue' => null,
  96. );
  97. }
  98. public function getParamDescription () {
  99. return array(
  100. 'limit' => 'How many langlinks to return',
  101. 'continue' => 'When more results are available, use this to continue',
  102. );
  103. }
  104. public function getDescription() {
  105. return 'Returns all interlanguage links from the given page(s)';
  106. }
  107. protected function getExamples() {
  108. return array (
  109. "Get interlanguage links from the [[Main Page]]:",
  110. " api.php?action=query&prop=langlinks&titles=Main%20Page&redirects",
  111. );
  112. }
  113. public function getVersion() {
  114. return __CLASS__ . ': $Id: ApiQueryLangLinks.php 46845 2009-02-05 14:30:59Z catrope $';
  115. }
  116. }