MediaWiki  master
ApiQueryExternalLinks.php
Go to the documentation of this file.
1 <?php
28 
35 
36  private UrlUtils $urlUtils;
37 
43  public function __construct( ApiQuery $query, $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 
55  $params = $this->extractRequestParams();
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( $continueField . $db->buildLike( "$protocol", $db->anyString() ) );
86  }
87  }
88 
89  $orderBy = [ 'el_id' ];
90 
91  $this->addOption( 'ORDER BY', $orderBy );
92  $this->addFields( $orderBy ); // Make sure
93 
94  $this->addOption( 'LIMIT', $params['limit'] + 1 );
95 
96  if ( $params['continue'] !== null ) {
97  $cont = $this->parseContinueParamOrDie( $params['continue'],
98  array_fill( 0, count( $orderBy ), 'string' ) );
99  $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) );
100  $this->addWhere( $db->buildComparison( '>=', $conds ) );
101  }
102 
103  $res = $this->select( __METHOD__ );
104 
105  $count = 0;
106  foreach ( $res as $row ) {
107  if ( ++$count > $params['limit'] ) {
108  // We've reached the one extra which shows that
109  // there are additional pages to be had. Stop here...
110  $this->setContinue( $orderBy, $row );
111  break;
112  }
113  $entry = [];
114  $to = LinkFilter::reverseIndexes( $row->el_to_domain_index ) . $row->el_to_path;
115  // expand protocol-relative urls
116  if ( $params['expandurl'] ) {
117  $to = (string)$this->urlUtils->expand( $to, PROTO_CANONICAL );
118  }
119  ApiResult::setContentValue( $entry, 'url', $to );
120  $fit = $this->addPageSubItem( $row->el_from, $entry );
121  if ( !$fit ) {
122  $this->setContinue( $orderBy, $row );
123  break;
124  }
125  }
126  }
127 
128  private function setContinue( $orderBy, $row ) {
129  $fields = [];
130  foreach ( $orderBy as $field ) {
131  $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
132  }
133  $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
134  }
135 
136  public function getCacheMode( $params ) {
137  return 'public';
138  }
139 
140  public function getAllowedParams() {
141  return [
142  'limit' => [
143  ParamValidator::PARAM_DEFAULT => 10,
144  ParamValidator::PARAM_TYPE => 'limit',
145  IntegerDef::PARAM_MIN => 1,
146  IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
147  IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
148  ],
149  'continue' => [
150  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
151  ],
152  'protocol' => [
153  ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(),
154  ParamValidator::PARAM_DEFAULT => '',
155  ],
156  'query' => null,
157  'expandurl' => [
158  ParamValidator::PARAM_TYPE => 'boolean',
159  ParamValidator::PARAM_DEFAULT => false,
160  ParamValidator::PARAM_DEPRECATED => true,
161  ],
162  ];
163  }
164 
165  protected function getExamplesMessages() {
166  $title = Title::newMainPage()->getPrefixedText();
167  $mp = rawurlencode( $title );
168 
169  return [
170  "action=query&prop=extlinks&titles={$mp}"
171  => 'apihelp-query+extlinks-example-simple',
172  ];
173  }
174 
175  public function getHelpUrls() {
176  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
177  }
178 }
const PROTO_CANONICAL
Definition: Defines.php:197
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1516
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition: ApiBase.php:1718
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:235
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:808
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:170
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:237
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
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
Definition: ApiResult.php:467
Represents a title within MediaWiki.
Definition: Title.php:76
A service to expand, parse, and otherwise manipulate URLs.
Definition: UrlUtils.php:17
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition: Parser.php:2248
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23