ApiQueryExternalLinks.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on May 13, 2007
  6. *
  7. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. /**
  27. * A query module to list all external URLs found on a given set of pages.
  28. *
  29. * @ingroup API
  30. */
  31. class ApiQueryExternalLinks extends ApiQueryBase {
  32. public function __construct( ApiQuery $query, $moduleName ) {
  33. parent::__construct( $query, $moduleName, 'el' );
  34. }
  35. public function execute() {
  36. if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  37. return;
  38. }
  39. $params = $this->extractRequestParams();
  40. $query = $params['query'];
  41. $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
  42. $this->addFields( [
  43. 'el_from',
  44. 'el_to'
  45. ] );
  46. $this->addTables( 'externallinks' );
  47. $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  48. $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
  49. if ( $whereQuery !== null ) {
  50. $this->addWhere( $whereQuery );
  51. }
  52. // Don't order by el_from if it's constant in the WHERE clause
  53. if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
  54. $this->addOption( 'ORDER BY', 'el_from' );
  55. }
  56. // If we're querying all protocols, use DISTINCT to avoid repeating protocol-relative links twice
  57. if ( $protocol === null ) {
  58. $this->addOption( 'DISTINCT' );
  59. }
  60. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  61. $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
  62. if ( $offset ) {
  63. $this->addOption( 'OFFSET', $params['offset'] );
  64. }
  65. $res = $this->select( __METHOD__ );
  66. $count = 0;
  67. foreach ( $res as $row ) {
  68. if ( ++$count > $params['limit'] ) {
  69. // We've reached the one extra which shows that
  70. // there are additional pages to be had. Stop here...
  71. $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
  72. break;
  73. }
  74. $entry = [];
  75. $to = $row->el_to;
  76. // expand protocol-relative urls
  77. if ( $params['expandurl'] ) {
  78. $to = wfExpandUrl( $to, PROTO_CANONICAL );
  79. }
  80. ApiResult::setContentValue( $entry, 'url', $to );
  81. $fit = $this->addPageSubItem( $row->el_from, $entry );
  82. if ( !$fit ) {
  83. $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
  84. break;
  85. }
  86. }
  87. }
  88. public function getCacheMode( $params ) {
  89. return 'public';
  90. }
  91. public function getAllowedParams() {
  92. return [
  93. 'limit' => [
  94. ApiBase::PARAM_DFLT => 10,
  95. ApiBase::PARAM_TYPE => 'limit',
  96. ApiBase::PARAM_MIN => 1,
  97. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  98. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  99. ],
  100. 'offset' => [
  101. ApiBase::PARAM_TYPE => 'integer',
  102. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  103. ],
  104. 'protocol' => [
  105. ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
  106. ApiBase::PARAM_DFLT => '',
  107. ],
  108. 'query' => null,
  109. 'expandurl' => false,
  110. ];
  111. }
  112. protected function getExamplesMessages() {
  113. return [
  114. 'action=query&prop=extlinks&titles=Main%20Page'
  115. => 'apihelp-query+extlinks-example-simple',
  116. ];
  117. }
  118. public function getHelpUrls() {
  119. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
  120. }
  121. }