ApiQueryIWBacklinks.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * This gives links pointing to the given interwiki
  27. * @ingroup API
  28. */
  29. class ApiQueryIWBacklinks extends ApiQueryGeneratorBase {
  30. public function __construct( ApiQuery $query, $moduleName ) {
  31. parent::__construct( $query, $moduleName, 'iwbl' );
  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['prefix'] ) ) {
  46. $this->dieWithError(
  47. [
  48. 'apierror-invalidparammix-mustusewith',
  49. $this->encodeParamName( 'title' ),
  50. $this->encodeParamName( 'prefix' ),
  51. ],
  52. 'invalidparammix'
  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. "iwl_prefix $op $prefix OR " .
  65. "(iwl_prefix = $prefix AND " .
  66. "(iwl_title $op $title OR " .
  67. "(iwl_title = $title AND " .
  68. "iwl_from $op= $from)))"
  69. );
  70. }
  71. $prop = array_flip( $params['prop'] );
  72. $iwprefix = isset( $prop['iwprefix'] );
  73. $iwtitle = isset( $prop['iwtitle'] );
  74. $this->addTables( [ 'iwlinks', 'page' ] );
  75. $this->addWhere( 'iwl_from = page_id' );
  76. $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
  77. 'iwl_from', 'iwl_prefix', 'iwl_title' ] );
  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_title' . $sort,
  87. 'iwl_from' . $sort
  88. ] );
  89. }
  90. } else {
  91. $this->addOption( 'ORDER BY', [
  92. 'iwl_prefix' . $sort,
  93. 'iwl_title' . $sort,
  94. 'iwl_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...
  106. // Continue string preserved in case the redirect query doesn't
  107. // pass the limit
  108. $this->setContinueEnumParameter(
  109. 'continue',
  110. "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
  111. );
  112. break;
  113. }
  114. if ( !is_null( $resultPageSet ) ) {
  115. $pages[] = Title::newFromRow( $row );
  116. } else {
  117. $entry = [ 'pageid' => (int)$row->page_id ];
  118. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  119. ApiQueryBase::addTitleInfo( $entry, $title );
  120. if ( $row->page_is_redirect ) {
  121. $entry['redirect'] = true;
  122. }
  123. if ( $iwprefix ) {
  124. $entry['iwprefix'] = $row->iwl_prefix;
  125. }
  126. if ( $iwtitle ) {
  127. $entry['iwtitle'] = $row->iwl_title;
  128. }
  129. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
  130. if ( !$fit ) {
  131. $this->setContinueEnumParameter(
  132. 'continue',
  133. "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}"
  134. );
  135. break;
  136. }
  137. }
  138. }
  139. if ( is_null( $resultPageSet ) ) {
  140. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'iw' );
  141. } else {
  142. $resultPageSet->populateFromTitles( $pages );
  143. }
  144. }
  145. public function getCacheMode( $params ) {
  146. return 'public';
  147. }
  148. public function getAllowedParams() {
  149. return [
  150. 'prefix' => null,
  151. 'title' => null,
  152. 'continue' => [
  153. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  154. ],
  155. 'limit' => [
  156. ApiBase::PARAM_DFLT => 10,
  157. ApiBase::PARAM_TYPE => 'limit',
  158. ApiBase::PARAM_MIN => 1,
  159. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  160. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  161. ],
  162. 'prop' => [
  163. ApiBase::PARAM_ISMULTI => true,
  164. ApiBase::PARAM_DFLT => '',
  165. ApiBase::PARAM_TYPE => [
  166. 'iwprefix',
  167. 'iwtitle',
  168. ],
  169. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  170. ],
  171. 'dir' => [
  172. ApiBase::PARAM_DFLT => 'ascending',
  173. ApiBase::PARAM_TYPE => [
  174. 'ascending',
  175. 'descending'
  176. ]
  177. ],
  178. ];
  179. }
  180. protected function getExamplesMessages() {
  181. return [
  182. 'action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks'
  183. => 'apihelp-query+iwbacklinks-example-simple',
  184. 'action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info'
  185. => 'apihelp-query+iwbacklinks-example-generator',
  186. ];
  187. }
  188. public function getHelpUrls() {
  189. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwbacklinks';
  190. }
  191. }