ApiQueryLangBacklinks.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * API for MediaWiki 1.17+
  4. *
  5. * Copyright © 2011 Sam Reed
  6. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. */
  25. /**
  26. * This gives links pointing to the given interwiki
  27. * @ingroup API
  28. */
  29. class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
  30. public function __construct( ApiQuery $query, $moduleName ) {
  31. parent::__construct( $query, $moduleName, 'lbl' );
  32. }
  33. public function execute() {
  34. $this->run();
  35. }
  36. public function executeGenerator( $resultPageSet ) {
  37. $this->run( $resultPageSet );
  38. }
  39. /**
  40. * @param ApiPageSet|null $resultPageSet
  41. * @return void
  42. */
  43. public function run( $resultPageSet = null ) {
  44. $params = $this->extractRequestParams();
  45. if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
  46. $this->dieWithError(
  47. [
  48. 'apierror-invalidparammix-mustusewith',
  49. $this->encodeParamName( 'title' ),
  50. $this->encodeParamName( 'lang' )
  51. ],
  52. 'nolang'
  53. );
  54. }
  55. if ( !is_null( $params['continue'] ) ) {
  56. $cont = explode( '|', $params['continue'] );
  57. $this->dieContinueUsageIf( count( $cont ) != 3 );
  58. $db = $this->getDB();
  59. $op = $params['dir'] == 'descending' ? '<' : '>';
  60. $prefix = $db->addQuotes( $cont[0] );
  61. $title = $db->addQuotes( $cont[1] );
  62. $from = (int)$cont[2];
  63. $this->addWhere(
  64. "ll_lang $op $prefix OR " .
  65. "(ll_lang = $prefix AND " .
  66. "(ll_title $op $title OR " .
  67. "(ll_title = $title AND " .
  68. "ll_from $op= $from)))"
  69. );
  70. }
  71. $prop = array_flip( $params['prop'] );
  72. $lllang = isset( $prop['lllang'] );
  73. $lltitle = isset( $prop['lltitle'] );
  74. $this->addTables( [ 'langlinks', 'page' ] );
  75. $this->addWhere( 'll_from = page_id' );
  76. $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
  77. 'll_from', 'll_lang', 'll_title' ] );
  78. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  79. if ( isset( $params['lang'] ) ) {
  80. $this->addWhereFld( 'll_lang', $params['lang'] );
  81. if ( isset( $params['title'] ) ) {
  82. $this->addWhereFld( 'll_title', $params['title'] );
  83. $this->addOption( 'ORDER BY', 'll_from' . $sort );
  84. } else {
  85. $this->addOption( 'ORDER BY', [
  86. 'll_title' . $sort,
  87. 'll_from' . $sort
  88. ] );
  89. }
  90. } else {
  91. $this->addOption( 'ORDER BY', [
  92. 'll_lang' . $sort,
  93. 'll_title' . $sort,
  94. 'll_from' . $sort
  95. ] );
  96. }
  97. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  98. $res = $this->select( __METHOD__ );
  99. $pages = [];
  100. $count = 0;
  101. $result = $this->getResult();
  102. foreach ( $res as $row ) {
  103. if ( ++$count > $params['limit'] ) {
  104. // We've reached the one extra which shows that there are
  105. // additional pages to be had. Stop here... Continue string
  106. // preserved in case the redirect query doesn't pass the limit.
  107. $this->setContinueEnumParameter(
  108. 'continue',
  109. "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
  110. );
  111. break;
  112. }
  113. if ( !is_null( $resultPageSet ) ) {
  114. $pages[] = Title::newFromRow( $row );
  115. } else {
  116. $entry = [ 'pageid' => (int)$row->page_id ];
  117. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  118. ApiQueryBase::addTitleInfo( $entry, $title );
  119. if ( $row->page_is_redirect ) {
  120. $entry['redirect'] = true;
  121. }
  122. if ( $lllang ) {
  123. $entry['lllang'] = $row->ll_lang;
  124. }
  125. if ( $lltitle ) {
  126. $entry['lltitle'] = $row->ll_title;
  127. }
  128. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
  129. if ( !$fit ) {
  130. $this->setContinueEnumParameter(
  131. 'continue',
  132. "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}"
  133. );
  134. break;
  135. }
  136. }
  137. }
  138. if ( is_null( $resultPageSet ) ) {
  139. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'll' );
  140. } else {
  141. $resultPageSet->populateFromTitles( $pages );
  142. }
  143. }
  144. public function getCacheMode( $params ) {
  145. return 'public';
  146. }
  147. public function getAllowedParams() {
  148. return [
  149. 'lang' => null,
  150. 'title' => null,
  151. 'continue' => [
  152. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  153. ],
  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. 'prop' => [
  162. ApiBase::PARAM_ISMULTI => true,
  163. ApiBase::PARAM_DFLT => '',
  164. ApiBase::PARAM_TYPE => [
  165. 'lllang',
  166. 'lltitle',
  167. ],
  168. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  169. ],
  170. 'dir' => [
  171. ApiBase::PARAM_DFLT => 'ascending',
  172. ApiBase::PARAM_TYPE => [
  173. 'ascending',
  174. 'descending'
  175. ]
  176. ],
  177. ];
  178. }
  179. protected function getExamplesMessages() {
  180. return [
  181. 'action=query&list=langbacklinks&lbltitle=Test&lbllang=fr'
  182. => 'apihelp-query+langbacklinks-example-simple',
  183. 'action=query&generator=langbacklinks&glbltitle=Test&glbllang=fr&prop=info'
  184. => 'apihelp-query+langbacklinks-example-generator',
  185. ];
  186. }
  187. public function getHelpUrls() {
  188. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Langbacklinks';
  189. }
  190. }