Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 90
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryExternalLinks
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 7
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 1
132
 setContinue
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Api;
10
11use MediaWiki\Deferred\LinksUpdate\ExternalLinksTable;
12use MediaWiki\ExternalLinks\LinkFilter;
13use MediaWiki\Parser\Parser;
14use MediaWiki\Title\Title;
15use MediaWiki\Utils\UrlUtils;
16use Wikimedia\ParamValidator\ParamValidator;
17use Wikimedia\ParamValidator\TypeDef\IntegerDef;
18use Wikimedia\Rdbms\IExpression;
19use Wikimedia\Rdbms\LikeValue;
20
21/**
22 * A query module to list all external URLs found on a given set of pages.
23 *
24 * @ingroup API
25 */
26class ApiQueryExternalLinks extends ApiQueryBase {
27
28    public function __construct(
29        ApiQuery $query,
30        string $moduleName,
31        private readonly UrlUtils $urlUtils,
32    ) {
33        parent::__construct( $query, $moduleName, 'el' );
34    }
35
36    public function execute() {
37        $pages = $this->getPageSet()->getGoodPages();
38        if ( $pages === [] ) {
39            return;
40        }
41
42        $params = $this->extractRequestParams();
43        $db = $this->getDB();
44
45        $query = $params['query'];
46        $protocol = LinkFilter::getProtocolPrefix( $params['protocol'] );
47
48        $fields = [ 'el_from' ];
49        $fields[] = 'el_to_domain_index';
50        $fields[] = 'el_to_path';
51        $continueField = 'el_to_domain_index';
52        $this->addFields( $fields );
53
54        $this->addTables( 'externallinks' );
55        $this->addWhereFld( 'el_from', array_keys( $pages ) );
56
57        if ( $query !== null && $query !== '' ) {
58            // Normalize query to match the normalization applied for the externallinks table
59            $query = Parser::normalizeLinkUrl( $query );
60
61            $conds = LinkFilter::getQueryConditions( $query, [
62                'protocol' => $protocol,
63                'oneWildcard' => true,
64                'db' => $db
65            ] );
66            if ( !$conds ) {
67                $this->dieWithError( 'apierror-badquery' );
68            }
69            $this->addWhere( $conds );
70        } else {
71            if ( $protocol !== null ) {
72                $this->addWhere(
73                    $db->expr( $continueField, IExpression::LIKE, new LikeValue( "$protocol", $db->anyString() ) )
74                );
75            }
76        }
77
78        $orderBy = [ 'el_id' ];
79
80        $this->addOption( 'ORDER BY', $orderBy );
81        $this->addFields( $orderBy ); // Make sure
82
83        $this->addOption( 'LIMIT', $params['limit'] + 1 );
84
85        if ( $params['continue'] !== null ) {
86            $cont = $this->parseContinueParamOrDie( $params['continue'],
87                array_fill( 0, count( $orderBy ), 'string' ) );
88            $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) );
89            $this->addWhere( $db->buildComparison( '>=', $conds ) );
90        }
91
92        $this->setVirtualDomain( ExternalLinksTable::VIRTUAL_DOMAIN );
93        $res = $this->select( __METHOD__ );
94        $this->resetVirtualDomain();
95
96        $count = 0;
97        foreach ( $res as $row ) {
98            if ( ++$count > $params['limit'] ) {
99                // We've reached the one extra which shows that
100                // there are additional pages to be had. Stop here...
101                $this->setContinue( $orderBy, $row );
102                break;
103            }
104            $entry = [];
105            $to = LinkFilter::reverseIndexes( $row->el_to_domain_index ) . $row->el_to_path;
106            // expand protocol-relative urls
107            if ( $params['expandurl'] ) {
108                $to = (string)$this->urlUtils->expand( $to, PROTO_CANONICAL );
109            }
110            ApiResult::setContentValue( $entry, 'url', $to );
111            $fit = $this->addPageSubItem( $row->el_from, $entry );
112            if ( !$fit ) {
113                $this->setContinue( $orderBy, $row );
114                break;
115            }
116        }
117    }
118
119    private function setContinue( array $orderBy, \stdClass $row ) {
120        $fields = [];
121        foreach ( $orderBy as $field ) {
122            $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
123        }
124        $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
125    }
126
127    /** @inheritDoc */
128    public function getCacheMode( $params ) {
129        return 'public';
130    }
131
132    /** @inheritDoc */
133    public function getAllowedParams() {
134        return [
135            'limit' => [
136                ParamValidator::PARAM_DEFAULT => 10,
137                ParamValidator::PARAM_TYPE => 'limit',
138                IntegerDef::PARAM_MIN => 1,
139                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
140                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
141            ],
142            'continue' => [
143                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
144            ],
145            'protocol' => [
146                ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(),
147                ParamValidator::PARAM_DEFAULT => '',
148            ],
149            'query' => null,
150            'expandurl' => [
151                ParamValidator::PARAM_TYPE => 'boolean',
152                ParamValidator::PARAM_DEFAULT => false,
153                ParamValidator::PARAM_DEPRECATED => true,
154            ],
155        ];
156    }
157
158    /** @inheritDoc */
159    protected function getExamplesMessages() {
160        $title = Title::newMainPage()->getPrefixedText();
161        $mp = rawurlencode( $title );
162
163        return [
164            "action=query&prop=extlinks&titles={$mp}"
165                => 'apihelp-query+extlinks-example-simple',
166        ];
167    }
168
169    /** @inheritDoc */
170    public function getHelpUrls() {
171        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
172    }
173}
174
175/** @deprecated class alias since 1.43 */
176class_alias( ApiQueryExternalLinks::class, 'ApiQueryExternalLinks' );