ApiQueryIWLinks.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * API for MediaWiki 1.17+
  4. *
  5. * Copyright © 2010 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. * A query module to list all interwiki links on a page
  27. *
  28. * @ingroup API
  29. */
  30. class ApiQueryIWLinks extends ApiQueryBase {
  31. public function __construct( ApiQuery $query, $moduleName ) {
  32. parent::__construct( $query, $moduleName, 'iw' );
  33. }
  34. public function execute() {
  35. if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  36. return;
  37. }
  38. $params = $this->extractRequestParams();
  39. $prop = array_flip( (array)$params['prop'] );
  40. if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
  41. $this->dieWithError(
  42. [
  43. 'apierror-invalidparammix-mustusewith',
  44. $this->encodeParamName( 'title' ),
  45. $this->encodeParamName( 'prefix' ),
  46. ],
  47. 'invalidparammix'
  48. );
  49. }
  50. // Handle deprecated param
  51. $this->requireMaxOneParameter( $params, 'url', 'prop' );
  52. if ( $params['url'] ) {
  53. $prop = [ 'url' => 1 ];
  54. }
  55. $this->addFields( [
  56. 'iwl_from',
  57. 'iwl_prefix',
  58. 'iwl_title'
  59. ] );
  60. $this->addTables( 'iwlinks' );
  61. $this->addWhereFld( 'iwl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  62. if ( !is_null( $params['continue'] ) ) {
  63. $cont = explode( '|', $params['continue'] );
  64. $this->dieContinueUsageIf( count( $cont ) != 3 );
  65. $op = $params['dir'] == 'descending' ? '<' : '>';
  66. $db = $this->getDB();
  67. $iwlfrom = (int)$cont[0];
  68. $iwlprefix = $db->addQuotes( $cont[1] );
  69. $iwltitle = $db->addQuotes( $cont[2] );
  70. $this->addWhere(
  71. "iwl_from $op $iwlfrom OR " .
  72. "(iwl_from = $iwlfrom AND " .
  73. "(iwl_prefix $op $iwlprefix OR " .
  74. "(iwl_prefix = $iwlprefix AND " .
  75. "iwl_title $op= $iwltitle)))"
  76. );
  77. }
  78. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  79. if ( isset( $params['prefix'] ) ) {
  80. $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
  81. if ( isset( $params['title'] ) ) {
  82. $this->addWhereFld( 'iwl_title', $params['title'] );
  83. $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
  84. } else {
  85. $this->addOption( 'ORDER BY', [
  86. 'iwl_from' . $sort,
  87. 'iwl_title' . $sort
  88. ] );
  89. }
  90. } else {
  91. // Don't order by iwl_from if it's constant in the WHERE clause
  92. if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
  93. $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort );
  94. } else {
  95. $this->addOption( 'ORDER BY', [
  96. 'iwl_from' . $sort,
  97. 'iwl_prefix' . $sort,
  98. 'iwl_title' . $sort
  99. ] );
  100. }
  101. }
  102. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  103. $res = $this->select( __METHOD__ );
  104. $count = 0;
  105. foreach ( $res as $row ) {
  106. if ( ++$count > $params['limit'] ) {
  107. // We've reached the one extra which shows that
  108. // there are additional pages to be had. Stop here...
  109. $this->setContinueEnumParameter(
  110. 'continue',
  111. "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
  112. );
  113. break;
  114. }
  115. $entry = [ 'prefix' => $row->iwl_prefix ];
  116. if ( isset( $prop['url'] ) ) {
  117. $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
  118. if ( $title ) {
  119. $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
  120. }
  121. }
  122. ApiResult::setContentValue( $entry, 'title', $row->iwl_title );
  123. $fit = $this->addPageSubItem( $row->iwl_from, $entry );
  124. if ( !$fit ) {
  125. $this->setContinueEnumParameter(
  126. 'continue',
  127. "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
  128. );
  129. break;
  130. }
  131. }
  132. }
  133. public function getCacheMode( $params ) {
  134. return 'public';
  135. }
  136. public function getAllowedParams() {
  137. return [
  138. 'prop' => [
  139. ApiBase::PARAM_ISMULTI => true,
  140. ApiBase::PARAM_TYPE => [
  141. 'url',
  142. ],
  143. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  144. ],
  145. 'prefix' => null,
  146. 'title' => null,
  147. 'dir' => [
  148. ApiBase::PARAM_DFLT => 'ascending',
  149. ApiBase::PARAM_TYPE => [
  150. 'ascending',
  151. 'descending'
  152. ]
  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. '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=iwlinks&titles=Main%20Page'
  173. => 'apihelp-query+iwlinks-example-simple',
  174. ];
  175. }
  176. public function getHelpUrls() {
  177. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwlinks';
  178. }
  179. }