ApiQueryIWBacklinks.php 5.8 KB

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