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