MediaWiki master
ApiQueryExternalLinks.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
33
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
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( $orderBy, $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
183class_alias( ApiQueryExternalLinks::class, 'ApiQueryExternalLinks' );
const PROTO_CANONICAL
Definition Defines.php:237
array $params
The job parameters.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1707
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:181
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:248
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:246
This is a base class for all Query modules.
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.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
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.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
getPageSet()
Get the PageSet object to work on.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addFields( $value)
Add a set of fields to select to the internal array.
This is the main query class.
Definition ApiQuery.php:48
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:147
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