MediaWiki master
ApiQueryExternalLinks.php
Go to the documentation of this file.
1<?php
31
38
39 private UrlUtils $urlUtils;
40
46 public function __construct( ApiQuery $query, $moduleName, UrlUtils $urlUtils ) {
47 parent::__construct( $query, $moduleName, 'el' );
48
49 $this->urlUtils = $urlUtils;
50 }
51
52 public function execute() {
53 $pages = $this->getPageSet()->getGoodPages();
54 if ( $pages === [] ) {
55 return;
56 }
57
59 $db = $this->getDB();
60
61 $query = $params['query'];
62 $protocol = LinkFilter::getProtocolPrefix( $params['protocol'] );
63
64 $fields = [ 'el_from' ];
65 $fields[] = 'el_to_domain_index';
66 $fields[] = 'el_to_path';
67 $continueField = 'el_to_domain_index';
68 $this->addFields( $fields );
69
70 $this->addTables( 'externallinks' );
71 $this->addWhereFld( 'el_from', array_keys( $pages ) );
72
73 if ( $query !== null && $query !== '' ) {
74 // Normalize query to match the normalization applied for the externallinks table
75 $query = Parser::normalizeLinkUrl( $query );
76
77 $conds = LinkFilter::getQueryConditions( $query, [
78 'protocol' => $protocol,
79 'oneWildcard' => true,
80 'db' => $db
81 ] );
82 if ( !$conds ) {
83 $this->dieWithError( 'apierror-badquery' );
84 }
85 $this->addWhere( $conds );
86 } else {
87 if ( $protocol !== null ) {
88 $this->addWhere(
89 $db->expr( $continueField, IExpression::LIKE, new LikeValue( "$protocol", $db->anyString() ) )
90 );
91 }
92 }
93
94 $orderBy = [ 'el_id' ];
95
96 $this->addOption( 'ORDER BY', $orderBy );
97 $this->addFields( $orderBy ); // Make sure
98
99 $this->addOption( 'LIMIT', $params['limit'] + 1 );
100
101 if ( $params['continue'] !== null ) {
102 $cont = $this->parseContinueParamOrDie( $params['continue'],
103 array_fill( 0, count( $orderBy ), 'string' ) );
104 $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) );
105 $this->addWhere( $db->buildComparison( '>=', $conds ) );
106 }
107
108 $res = $this->select( __METHOD__ );
109
110 $count = 0;
111 foreach ( $res as $row ) {
112 if ( ++$count > $params['limit'] ) {
113 // We've reached the one extra which shows that
114 // there are additional pages to be had. Stop here...
115 $this->setContinue( $orderBy, $row );
116 break;
117 }
118 $entry = [];
119 $to = LinkFilter::reverseIndexes( $row->el_to_domain_index ) . $row->el_to_path;
120 // expand protocol-relative urls
121 if ( $params['expandurl'] ) {
122 $to = (string)$this->urlUtils->expand( $to, PROTO_CANONICAL );
123 }
124 ApiResult::setContentValue( $entry, 'url', $to );
125 $fit = $this->addPageSubItem( $row->el_from, $entry );
126 if ( !$fit ) {
127 $this->setContinue( $orderBy, $row );
128 break;
129 }
130 }
131 }
132
133 private function setContinue( $orderBy, $row ) {
134 $fields = [];
135 foreach ( $orderBy as $field ) {
136 $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
137 }
138 $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
139 }
140
141 public function getCacheMode( $params ) {
142 return 'public';
143 }
144
145 public function getAllowedParams() {
146 return [
147 'limit' => [
148 ParamValidator::PARAM_DEFAULT => 10,
149 ParamValidator::PARAM_TYPE => 'limit',
150 IntegerDef::PARAM_MIN => 1,
151 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
152 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
153 ],
154 'continue' => [
155 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
156 ],
157 'protocol' => [
158 ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(),
159 ParamValidator::PARAM_DEFAULT => '',
160 ],
161 'query' => null,
162 'expandurl' => [
163 ParamValidator::PARAM_TYPE => 'boolean',
164 ParamValidator::PARAM_DEFAULT => false,
165 ParamValidator::PARAM_DEPRECATED => true,
166 ],
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:208
array $params
The job parameters.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1533
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1725
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:236
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:171
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:238
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:43
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:156
Represents a title within MediaWiki.
Definition Title.php:78
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16
Service for formatting and validating API parameters.
Type definition for integer types.
Content of like value.
Definition LikeValue.php:14