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