3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * A query module to list all external URLs found on a given set of pages.
28 class ApiQueryExternalLinks
extends ApiQueryBase
{
30 public function __construct( ApiQuery
$query, $moduleName ) {
31 parent
::__construct( $query, $moduleName, 'el' );
34 public function execute() {
35 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
39 $params = $this->extractRequestParams();
41 $query = $params['query'];
42 $protocol = ApiQueryExtLinksUsage
::getProtocolPrefix( $params['protocol'] );
49 $this->addTables( 'externallinks' );
50 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
52 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
54 if ( $whereQuery !== null ) {
55 $this->addWhere( $whereQuery );
58 // Don't order by el_from if it's constant in the WHERE clause
59 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
60 $this->addOption( 'ORDER BY', 'el_from' );
63 // If we're querying all protocols, use DISTINCT to avoid repeating protocol-relative links twice
64 if ( $protocol === null ) {
65 $this->addOption( 'DISTINCT' );
68 $this->addOption( 'LIMIT', $params['limit'] +
1 );
69 $offset = $params['offset'] ??
0;
71 $this->addOption( 'OFFSET', $params['offset'] );
74 $res = $this->select( __METHOD__
);
77 foreach ( $res as $row ) {
78 if ( ++
$count > $params['limit'] ) {
79 // We've reached the one extra which shows that
80 // there are additional pages to be had. Stop here...
81 $this->setContinueEnumParameter( 'offset', $offset +
$params['limit'] );
86 // expand protocol-relative urls
87 if ( $params['expandurl'] ) {
88 $to = wfExpandUrl( $to, PROTO_CANONICAL
);
90 ApiResult
::setContentValue( $entry, 'url', $to );
91 $fit = $this->addPageSubItem( $row->el_from
, $entry );
93 $this->setContinueEnumParameter( 'offset', $offset +
$count - 1 );
99 public function getCacheMode( $params ) {
103 public function getAllowedParams() {
106 ApiBase
::PARAM_DFLT
=> 10,
107 ApiBase
::PARAM_TYPE
=> 'limit',
108 ApiBase
::PARAM_MIN
=> 1,
109 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
110 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
113 ApiBase
::PARAM_TYPE
=> 'integer',
114 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
117 ApiBase
::PARAM_TYPE
=> ApiQueryExtLinksUsage
::prepareProtocols(),
118 ApiBase
::PARAM_DFLT
=> '',
121 'expandurl' => false,
125 protected function getExamplesMessages() {
127 'action=query&prop=extlinks&titles=Main%20Page'
128 => 'apihelp-query+extlinks-example-simple',
132 public function getHelpUrls() {
133 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';