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();
42 $query = $params['query'];
43 $protocol = ApiQueryExtLinksUsage
::getProtocolPrefix( $params['protocol'] );
50 $this->addTables( 'externallinks' );
51 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
55 // Don't order by el_from if it's constant in the WHERE clause
56 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
57 $orderBy[] = 'el_from';
60 if ( $query !== null && $query !== '' ) {
61 if ( $protocol === null ) {
62 $protocol = 'http://';
65 // Normalize query to match the normalization applied for the externallinks table
66 $query = Parser
::normalizeLinkUrl( $protocol . $query );
68 $conds = LinkFilter
::getQueryConditions( $query, [
70 'oneWildcard' => true,
74 $this->dieWithError( 'apierror-badquery' );
76 $this->addWhere( $conds );
77 if ( !isset( $conds['el_index_60'] ) ) {
78 $orderBy[] = 'el_index_60';
81 $orderBy[] = 'el_index_60';
83 if ( $protocol !== null ) {
84 $this->addWhere( 'el_index_60' . $db->buildLike( "$protocol", $db->anyString() ) );
86 // We're querying all protocols, filter out duplicate protocol-relative links
87 $this->addWhere( $db->makeList( [
88 'el_to NOT' . $db->buildLike( '//', $db->anyString() ),
89 'el_index_60 ' . $db->buildLike( 'http://', $db->anyString() ),
95 $this->addOption( 'ORDER BY', $orderBy );
96 $this->addFields( $orderBy ); // Make sure
98 $this->addOption( 'LIMIT', $params['limit'] +
1 );
100 if ( $params['continue'] !== null ) {
101 $cont = explode( '|', $params['continue'] );
102 $this->dieContinueUsageIf( count( $cont ) !== count( $orderBy ) );
103 $i = count( $cont ) - 1;
104 $cond = $orderBy[$i] . ' >= ' . $db->addQuotes( rawurldecode( $cont[$i] ) );
106 $field = $orderBy[$i];
107 $v = $db->addQuotes( rawurldecode( $cont[$i] ) );
108 $cond = "($field > $v OR ($field = $v AND $cond))";
110 $this->addWhere( $cond );
113 $res = $this->select( __METHOD__
);
116 foreach ( $res as $row ) {
117 if ( ++
$count > $params['limit'] ) {
118 // We've reached the one extra which shows that
119 // there are additional pages to be had. Stop here...
120 $this->setContinue( $orderBy, $row );
125 // expand protocol-relative urls
126 if ( $params['expandurl'] ) {
127 $to = wfExpandUrl( $to, PROTO_CANONICAL
);
129 ApiResult
::setContentValue( $entry, 'url', $to );
130 $fit = $this->addPageSubItem( $row->el_from
, $entry );
132 $this->setContinue( $orderBy, $row );
138 private function setContinue( $orderBy, $row ) {
140 foreach ( $orderBy as $field ) {
141 $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
143 $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
146 public function getCacheMode( $params ) {
150 public function getAllowedParams() {
153 ApiBase
::PARAM_DFLT
=> 10,
154 ApiBase
::PARAM_TYPE
=> 'limit',
155 ApiBase
::PARAM_MIN
=> 1,
156 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
157 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
160 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
163 ApiBase
::PARAM_TYPE
=> ApiQueryExtLinksUsage
::prepareProtocols(),
164 ApiBase
::PARAM_DFLT
=> '',
167 'expandurl' => false,
171 protected function getExamplesMessages() {
173 'action=query&prop=extlinks&titles=Main%20Page'
174 => 'apihelp-query+extlinks-example-simple',
178 public function getHelpUrls() {
179 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';