MediaWiki 1.40.4
ApiQueryExternalLinks.php
Go to the documentation of this file.
1<?php
27
34
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'el' );
37 }
38
39 public function execute() {
40 $pages = $this->getPageSet()->getGoodPages();
41 if ( $pages === [] ) {
42 return;
43 }
44
45 $params = $this->extractRequestParams();
46 $db = $this->getDB();
47
48 $query = $params['query'];
49 $protocol = LinkFilter::getProtocolPrefix( $params['protocol'] );
50
51 $this->addFields( [
52 'el_from',
53 'el_to'
54 ] );
55
56 $this->addTables( 'externallinks' );
57 $this->addWhereFld( 'el_from', array_keys( $pages ) );
58
59 $orderBy = [];
60
61 // Don't order by el_from if it's constant in the WHERE clause
62 if ( count( $pages ) !== 1 ) {
63 $orderBy[] = 'el_from';
64 }
65
66 if ( $query !== null && $query !== '' ) {
67 $protocol ??= 'http://';
68
69 // Normalize query to match the normalization applied for the externallinks table
70 $query = Parser::normalizeLinkUrl( $protocol . $query );
71
72 $conds = LinkFilter::getQueryConditions( $query, [
73 'protocol' => '',
74 'oneWildcard' => true,
75 'db' => $db
76 ] );
77 if ( !$conds ) {
78 $this->dieWithError( 'apierror-badquery' );
79 }
80 $this->addWhere( $conds );
81 if ( !isset( $conds['el_index_60'] ) ) {
82 $orderBy[] = 'el_index_60';
83 }
84 } else {
85 $orderBy[] = 'el_index_60';
86
87 if ( $protocol !== null ) {
88 $this->addWhere( 'el_index_60' . $db->buildLike( "$protocol", $db->anyString() ) );
89 } else {
90 // We're querying all protocols, filter out duplicate protocol-relative links
91 $this->addWhere( $db->makeList( [
92 'el_to NOT' . $db->buildLike( '//', $db->anyString() ),
93 'el_index_60 ' . $db->buildLike( 'http://', $db->anyString() ),
94 ], LIST_OR ) );
95 }
96 }
97
98 $orderBy[] = 'el_id';
99
100 $this->addOption( 'ORDER BY', $orderBy );
101 $this->addFields( $orderBy ); // Make sure
102
103 $this->addOption( 'LIMIT', $params['limit'] + 1 );
104
105 if ( $params['continue'] !== null ) {
106 $cont = $this->parseContinueParamOrDie( $params['continue'],
107 array_fill( 0, count( $orderBy ), 'string' ) );
108 $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) );
109 $this->addWhere( $db->buildComparison( '>=', $conds ) );
110 }
111
112 $res = $this->select( __METHOD__ );
113
114 $count = 0;
115 foreach ( $res as $row ) {
116 if ( ++$count > $params['limit'] ) {
117 // We've reached the one extra which shows that
118 // there are additional pages to be had. Stop here...
119 $this->setContinue( $orderBy, $row );
120 break;
121 }
122 $entry = [];
123 $to = $row->el_to;
124 // expand protocol-relative urls
125 if ( $params['expandurl'] ) {
126 $to = wfExpandUrl( $to, PROTO_CANONICAL );
127 }
128 ApiResult::setContentValue( $entry, 'url', $to );
129 $fit = $this->addPageSubItem( $row->el_from, $entry );
130 if ( !$fit ) {
131 $this->setContinue( $orderBy, $row );
132 break;
133 }
134 }
135 }
136
137 private function setContinue( $orderBy, $row ) {
138 $fields = [];
139 foreach ( $orderBy as $field ) {
140 $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
141 }
142 $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
143 }
144
145 public function getCacheMode( $params ) {
146 return 'public';
147 }
148
149 public function getAllowedParams() {
150 return [
151 'limit' => [
152 ParamValidator::PARAM_DEFAULT => 10,
153 ParamValidator::PARAM_TYPE => 'limit',
154 IntegerDef::PARAM_MIN => 1,
155 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
156 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
157 ],
158 'continue' => [
159 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
160 ],
161 'protocol' => [
162 ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(),
163 ParamValidator::PARAM_DEFAULT => '',
164 ],
165 'query' => null,
166 'expandurl' => false,
167 ];
168 }
169
170 protected function getExamplesMessages() {
171 $title = Title::newMainPage()->getPrefixedText();
172 $mp = rawurlencode( $title );
173
174 return [
175 "action=query&prop=extlinks&titles={$mp}"
176 => 'apihelp-query+extlinks-example-simple',
177 ];
178 }
179
180 public function getHelpUrls() {
181 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
182 }
183}
const PROTO_CANONICAL
Definition Defines.php:199
const LIST_OR
Definition Defines.php:46
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1460
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1649
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:229
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:773
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:231
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addFields( $value)
Add a set of fields to select to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
getPageSet()
Get the PageSet object to work on.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
This is the main query class.
Definition ApiQuery.php:42
Represents a title within MediaWiki.
Definition Title.php:82
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition Parser.php:2302
Service for formatting and validating API parameters.
Type definition for integer types.