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
23 namespace MediaWiki\Api
;
25 use MediaWiki\ExternalLinks\LinkFilter
;
26 use MediaWiki\Parser\Parser
;
27 use MediaWiki\Title\Title
;
28 use MediaWiki\Utils\UrlUtils
;
29 use Wikimedia\ParamValidator\ParamValidator
;
30 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
31 use Wikimedia\Rdbms\IExpression
;
32 use Wikimedia\Rdbms\LikeValue
;
35 * A query module to list all external URLs found on a given set of pages.
39 class ApiQueryExternalLinks
extends ApiQueryBase
{
41 private UrlUtils
$urlUtils;
43 public function __construct( ApiQuery
$query, string $moduleName, UrlUtils
$urlUtils ) {
44 parent
::__construct( $query, $moduleName, 'el' );
46 $this->urlUtils
= $urlUtils;
49 public function execute() {
50 $pages = $this->getPageSet()->getGoodPages();
51 if ( $pages === [] ) {
55 $params = $this->extractRequestParams();
58 $query = $params['query'];
59 $protocol = LinkFilter
::getProtocolPrefix( $params['protocol'] );
61 $fields = [ 'el_from' ];
62 $fields[] = 'el_to_domain_index';
63 $fields[] = 'el_to_path';
64 $continueField = 'el_to_domain_index';
65 $this->addFields( $fields );
67 $this->addTables( 'externallinks' );
68 $this->addWhereFld( 'el_from', array_keys( $pages ) );
70 if ( $query !== null && $query !== '' ) {
71 // Normalize query to match the normalization applied for the externallinks table
72 $query = Parser
::normalizeLinkUrl( $query );
74 $conds = LinkFilter
::getQueryConditions( $query, [
75 'protocol' => $protocol,
76 'oneWildcard' => true,
80 $this->dieWithError( 'apierror-badquery' );
82 $this->addWhere( $conds );
84 if ( $protocol !== null ) {
86 $db->expr( $continueField, IExpression
::LIKE
, new LikeValue( "$protocol", $db->anyString() ) )
91 $orderBy = [ 'el_id' ];
93 $this->addOption( 'ORDER BY', $orderBy );
94 $this->addFields( $orderBy ); // Make sure
96 $this->addOption( 'LIMIT', $params['limit'] +
1 );
98 if ( $params['continue'] !== null ) {
99 $cont = $this->parseContinueParamOrDie( $params['continue'],
100 array_fill( 0, count( $orderBy ), 'string' ) );
101 $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) );
102 $this->addWhere( $db->buildComparison( '>=', $conds ) );
105 $res = $this->select( __METHOD__
);
108 foreach ( $res as $row ) {
109 if ( ++
$count > $params['limit'] ) {
110 // We've reached the one extra which shows that
111 // there are additional pages to be had. Stop here...
112 $this->setContinue( $orderBy, $row );
116 $to = LinkFilter
::reverseIndexes( $row->el_to_domain_index
) . $row->el_to_path
;
117 // expand protocol-relative urls
118 if ( $params['expandurl'] ) {
119 $to = (string)$this->urlUtils
->expand( $to, PROTO_CANONICAL
);
121 ApiResult
::setContentValue( $entry, 'url', $to );
122 $fit = $this->addPageSubItem( $row->el_from
, $entry );
124 $this->setContinue( $orderBy, $row );
130 private function setContinue( $orderBy, $row ) {
132 foreach ( $orderBy as $field ) {
133 $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
135 $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
138 public function getCacheMode( $params ) {
142 public function getAllowedParams() {
145 ParamValidator
::PARAM_DEFAULT
=> 10,
146 ParamValidator
::PARAM_TYPE
=> 'limit',
147 IntegerDef
::PARAM_MIN
=> 1,
148 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
149 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
152 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
155 ParamValidator
::PARAM_TYPE
=> LinkFilter
::prepareProtocols(),
156 ParamValidator
::PARAM_DEFAULT
=> '',
160 ParamValidator
::PARAM_TYPE
=> 'boolean',
161 ParamValidator
::PARAM_DEFAULT
=> false,
162 ParamValidator
::PARAM_DEPRECATED
=> true,
167 protected function getExamplesMessages() {
168 $title = Title
::newMainPage()->getPrefixedText();
169 $mp = rawurlencode( $title );
172 "action=query&prop=extlinks&titles={$mp}"
173 => 'apihelp-query+extlinks-example-simple',
177 public function getHelpUrls() {
178 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
182 /** @deprecated class alias since 1.43 */
183 class_alias( ApiQueryExternalLinks
::class, 'ApiQueryExternalLinks' );