Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 89
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 / 88
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 / 52
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 * 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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23namespace MediaWiki\Api;
24
25use MediaWiki\ExternalLinks\LinkFilter;
26use MediaWiki\Parser\Parser;
27use MediaWiki\Title\Title;
28use MediaWiki\Utils\UrlUtils;
29use Wikimedia\ParamValidator\ParamValidator;
30use Wikimedia\ParamValidator\TypeDef\IntegerDef;
31use Wikimedia\Rdbms\IExpression;
32use Wikimedia\Rdbms\LikeValue;
33
34/**
35 * A query module to list all external URLs found on a given set of pages.
36 *
37 * @ingroup API
38 */
39class ApiQueryExternalLinks extends ApiQueryBase {
40
41    private UrlUtils $urlUtils;
42
43    public function __construct( ApiQuery $query, string $moduleName, UrlUtils $urlUtils ) {
44        parent::__construct( $query, $moduleName, 'el' );
45
46        $this->urlUtils = $urlUtils;
47    }
48
49    public function execute() {
50        $pages = $this->getPageSet()->getGoodPages();
51        if ( $pages === [] ) {
52            return;
53        }
54
55        $params = $this->extractRequestParams();
56        $db = $this->getDB();
57
58        $query = $params['query'];
59        $protocol = LinkFilter::getProtocolPrefix( $params['protocol'] );
60
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 );
66
67        $this->addTables( 'externallinks' );
68        $this->addWhereFld( 'el_from', array_keys( $pages ) );
69
70        if ( $query !== null && $query !== '' ) {
71            // Normalize query to match the normalization applied for the externallinks table
72            $query = Parser::normalizeLinkUrl( $query );
73
74            $conds = LinkFilter::getQueryConditions( $query, [
75                'protocol' => $protocol,
76                'oneWildcard' => true,
77                'db' => $db
78            ] );
79            if ( !$conds ) {
80                $this->dieWithError( 'apierror-badquery' );
81            }
82            $this->addWhere( $conds );
83        } else {
84            if ( $protocol !== null ) {
85                $this->addWhere(
86                    $db->expr( $continueField, IExpression::LIKE, new LikeValue( "$protocol", $db->anyString() ) )
87                );
88            }
89        }
90
91        $orderBy = [ 'el_id' ];
92
93        $this->addOption( 'ORDER BY', $orderBy );
94        $this->addFields( $orderBy ); // Make sure
95
96        $this->addOption( 'LIMIT', $params['limit'] + 1 );
97
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 ) );
103        }
104
105        $res = $this->select( __METHOD__ );
106
107        $count = 0;
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 );
113                break;
114            }
115            $entry = [];
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 );
120            }
121            ApiResult::setContentValue( $entry, 'url', $to );
122            $fit = $this->addPageSubItem( $row->el_from, $entry );
123            if ( !$fit ) {
124                $this->setContinue( $orderBy, $row );
125                break;
126            }
127        }
128    }
129
130    private function setContinue( array $orderBy, \stdClass $row ) {
131        $fields = [];
132        foreach ( $orderBy as $field ) {
133            $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
134        }
135        $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
136    }
137
138    public function getCacheMode( $params ) {
139        return 'public';
140    }
141
142    public function getAllowedParams() {
143        return [
144            'limit' => [
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
150            ],
151            'continue' => [
152                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
153            ],
154            'protocol' => [
155                ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(),
156                ParamValidator::PARAM_DEFAULT => '',
157            ],
158            'query' => null,
159            'expandurl' => [
160                ParamValidator::PARAM_TYPE => 'boolean',
161                ParamValidator::PARAM_DEFAULT => false,
162                ParamValidator::PARAM_DEPRECATED => true,
163            ],
164        ];
165    }
166
167    protected function getExamplesMessages() {
168        $title = Title::newMainPage()->getPrefixedText();
169        $mp = rawurlencode( $title );
170
171        return [
172            "action=query&prop=extlinks&titles={$mp}"
173                => 'apihelp-query+extlinks-example-simple',
174        ];
175    }
176
177    public function getHelpUrls() {
178        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
179    }
180}
181
182/** @deprecated class alias since 1.43 */
183class_alias( ApiQueryExternalLinks::class, 'ApiQueryExternalLinks' );