ApiQueryLangLinks.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on May 13, 2007
  6. *
  7. * Copyright © 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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. /**
  27. * A query module to list all langlinks (links to corresponding foreign language pages).
  28. *
  29. * @ingroup API
  30. */
  31. class ApiQueryLangLinks extends ApiQueryBase {
  32. public function __construct( ApiQuery $query, $moduleName ) {
  33. parent::__construct( $query, $moduleName, 'll' );
  34. }
  35. public function execute() {
  36. if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  37. return;
  38. }
  39. $params = $this->extractRequestParams();
  40. $prop = array_flip( (array)$params['prop'] );
  41. if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
  42. $this->dieWithError(
  43. [
  44. 'apierror-invalidparammix-mustusewith',
  45. $this->encodeParamName( 'title' ),
  46. $this->encodeParamName( 'lang' ),
  47. ],
  48. 'invalidparammix'
  49. );
  50. }
  51. // Handle deprecated param
  52. $this->requireMaxOneParameter( $params, 'url', 'prop' );
  53. if ( $params['url'] ) {
  54. $prop = [ 'url' => 1 ];
  55. }
  56. $this->addFields( [
  57. 'll_from',
  58. 'll_lang',
  59. 'll_title'
  60. ] );
  61. $this->addTables( 'langlinks' );
  62. $this->addWhereFld( 'll_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  63. if ( !is_null( $params['continue'] ) ) {
  64. $cont = explode( '|', $params['continue'] );
  65. $this->dieContinueUsageIf( count( $cont ) != 2 );
  66. $op = $params['dir'] == 'descending' ? '<' : '>';
  67. $llfrom = intval( $cont[0] );
  68. $lllang = $this->getDB()->addQuotes( $cont[1] );
  69. $this->addWhere(
  70. "ll_from $op $llfrom OR " .
  71. "(ll_from = $llfrom AND " .
  72. "ll_lang $op= $lllang)"
  73. );
  74. }
  75. // FIXME: (follow-up) To allow extensions to add to the language links, we need
  76. // to load them all, add the extra links, then apply paging.
  77. // Should not be terrible, it's not going to be more than a few hundred links.
  78. // Note that, since (ll_from, ll_lang) is a unique key, we don't need
  79. // to sort by ll_title to ensure deterministic ordering.
  80. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  81. if ( isset( $params['lang'] ) ) {
  82. $this->addWhereFld( 'll_lang', $params['lang'] );
  83. if ( isset( $params['title'] ) ) {
  84. $this->addWhereFld( 'll_title', $params['title'] );
  85. }
  86. $this->addOption( 'ORDER BY', 'll_from' . $sort );
  87. } else {
  88. // Don't order by ll_from if it's constant in the WHERE clause
  89. if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
  90. $this->addOption( 'ORDER BY', 'll_lang' . $sort );
  91. } else {
  92. $this->addOption( 'ORDER BY', [
  93. 'll_from' . $sort,
  94. 'll_lang' . $sort
  95. ] );
  96. }
  97. }
  98. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  99. $res = $this->select( __METHOD__ );
  100. $count = 0;
  101. foreach ( $res as $row ) {
  102. if ( ++$count > $params['limit'] ) {
  103. // We've reached the one extra which shows that
  104. // there are additional pages to be had. Stop here...
  105. $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
  106. break;
  107. }
  108. $entry = [ 'lang' => $row->ll_lang ];
  109. if ( isset( $prop['url'] ) ) {
  110. $title = Title::newFromText( "{$row->ll_lang}:{$row->ll_title}" );
  111. if ( $title ) {
  112. $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
  113. }
  114. }
  115. if ( isset( $prop['langname'] ) ) {
  116. $entry['langname'] = Language::fetchLanguageName( $row->ll_lang, $params['inlanguagecode'] );
  117. }
  118. if ( isset( $prop['autonym'] ) ) {
  119. $entry['autonym'] = Language::fetchLanguageName( $row->ll_lang );
  120. }
  121. ApiResult::setContentValue( $entry, 'title', $row->ll_title );
  122. $fit = $this->addPageSubItem( $row->ll_from, $entry );
  123. if ( !$fit ) {
  124. $this->setContinueEnumParameter( 'continue', "{$row->ll_from}|{$row->ll_lang}" );
  125. break;
  126. }
  127. }
  128. }
  129. public function getCacheMode( $params ) {
  130. return 'public';
  131. }
  132. public function getAllowedParams() {
  133. global $wgContLang;
  134. return [
  135. 'prop' => [
  136. ApiBase::PARAM_ISMULTI => true,
  137. ApiBase::PARAM_TYPE => [
  138. 'url',
  139. 'langname',
  140. 'autonym',
  141. ],
  142. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  143. ],
  144. 'lang' => null,
  145. 'title' => null,
  146. 'dir' => [
  147. ApiBase::PARAM_DFLT => 'ascending',
  148. ApiBase::PARAM_TYPE => [
  149. 'ascending',
  150. 'descending'
  151. ]
  152. ],
  153. 'inlanguagecode' => $wgContLang->getCode(),
  154. 'limit' => [
  155. ApiBase::PARAM_DFLT => 10,
  156. ApiBase::PARAM_TYPE => 'limit',
  157. ApiBase::PARAM_MIN => 1,
  158. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  159. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  160. ],
  161. 'continue' => [
  162. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  163. ],
  164. 'url' => [
  165. ApiBase::PARAM_DFLT => false,
  166. ApiBase::PARAM_DEPRECATED => true,
  167. ],
  168. ];
  169. }
  170. protected function getExamplesMessages() {
  171. return [
  172. 'action=query&prop=langlinks&titles=Main%20Page&redirects='
  173. => 'apihelp-query+langlinks-example-simple',
  174. ];
  175. }
  176. public function getHelpUrls() {
  177. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langlinks';
  178. }
  179. }