ApiQueryLangLinks.php 5.6 KB

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