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