ApiQueryExternalLinks.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /**
  23. * A query module to list all external URLs found on a given set of pages.
  24. *
  25. * @ingroup API
  26. */
  27. class ApiQueryExternalLinks extends ApiQueryBase {
  28. public function __construct( ApiQuery $query, $moduleName ) {
  29. parent::__construct( $query, $moduleName, 'el' );
  30. }
  31. public function execute() {
  32. if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  33. return;
  34. }
  35. $params = $this->extractRequestParams();
  36. $db = $this->getDB();
  37. $query = $params['query'];
  38. $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
  39. $this->addFields( [
  40. 'el_from',
  41. 'el_to'
  42. ] );
  43. $this->addTables( 'externallinks' );
  44. $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  45. $orderBy = [];
  46. // Don't order by el_from if it's constant in the WHERE clause
  47. if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
  48. $orderBy[] = 'el_from';
  49. }
  50. if ( $query !== null && $query !== '' ) {
  51. if ( $protocol === null ) {
  52. $protocol = 'http://';
  53. }
  54. // Normalize query to match the normalization applied for the externallinks table
  55. $query = Parser::normalizeLinkUrl( $protocol . $query );
  56. $conds = LinkFilter::getQueryConditions( $query, [
  57. 'protocol' => '',
  58. 'oneWildcard' => true,
  59. 'db' => $db
  60. ] );
  61. if ( !$conds ) {
  62. $this->dieWithError( 'apierror-badquery' );
  63. }
  64. $this->addWhere( $conds );
  65. if ( !isset( $conds['el_index_60'] ) ) {
  66. $orderBy[] = 'el_index_60';
  67. }
  68. } else {
  69. $orderBy[] = 'el_index_60';
  70. if ( $protocol !== null ) {
  71. $this->addWhere( 'el_index_60' . $db->buildLike( "$protocol", $db->anyString() ) );
  72. } else {
  73. // We're querying all protocols, filter out duplicate protocol-relative links
  74. $this->addWhere( $db->makeList( [
  75. 'el_to NOT' . $db->buildLike( '//', $db->anyString() ),
  76. 'el_index_60 ' . $db->buildLike( 'http://', $db->anyString() ),
  77. ], LIST_OR ) );
  78. }
  79. }
  80. $orderBy[] = 'el_id';
  81. $this->addOption( 'ORDER BY', $orderBy );
  82. $this->addFields( $orderBy ); // Make sure
  83. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  84. if ( $params['continue'] !== null ) {
  85. $cont = explode( '|', $params['continue'] );
  86. $this->dieContinueUsageIf( count( $cont ) !== count( $orderBy ) );
  87. $i = count( $cont ) - 1;
  88. $cond = $orderBy[$i] . ' >= ' . $db->addQuotes( rawurldecode( $cont[$i] ) );
  89. while ( $i-- > 0 ) {
  90. $field = $orderBy[$i];
  91. $v = $db->addQuotes( rawurldecode( $cont[$i] ) );
  92. $cond = "($field > $v OR ($field = $v AND $cond))";
  93. }
  94. $this->addWhere( $cond );
  95. }
  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->setContinue( $orderBy, $row );
  103. break;
  104. }
  105. $entry = [];
  106. $to = $row->el_to;
  107. // expand protocol-relative urls
  108. if ( $params['expandurl'] ) {
  109. $to = wfExpandUrl( $to, PROTO_CANONICAL );
  110. }
  111. ApiResult::setContentValue( $entry, 'url', $to );
  112. $fit = $this->addPageSubItem( $row->el_from, $entry );
  113. if ( !$fit ) {
  114. $this->setContinue( $orderBy, $row );
  115. break;
  116. }
  117. }
  118. }
  119. private function setContinue( $orderBy, $row ) {
  120. $fields = [];
  121. foreach ( $orderBy as $field ) {
  122. $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
  123. }
  124. $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
  125. }
  126. public function getCacheMode( $params ) {
  127. return 'public';
  128. }
  129. public function getAllowedParams() {
  130. return [
  131. 'limit' => [
  132. ApiBase::PARAM_DFLT => 10,
  133. ApiBase::PARAM_TYPE => 'limit',
  134. ApiBase::PARAM_MIN => 1,
  135. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  136. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  137. ],
  138. 'continue' => [
  139. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  140. ],
  141. 'protocol' => [
  142. ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
  143. ApiBase::PARAM_DFLT => '',
  144. ],
  145. 'query' => null,
  146. 'expandurl' => false,
  147. ];
  148. }
  149. protected function getExamplesMessages() {
  150. return [
  151. 'action=query&prop=extlinks&titles=Main%20Page'
  152. => 'apihelp-query+extlinks-example-simple',
  153. ];
  154. }
  155. public function getHelpUrls() {
  156. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
  157. }
  158. }