MediaWiki  master
ApiQueryExternalLinks.php
Go to the documentation of this file.
1 <?php
25 
32 
33  public function __construct( ApiQuery $query, $moduleName ) {
34  parent::__construct( $query, $moduleName, 'el' );
35  }
36 
37  public function execute() {
38  $pages = $this->getPageSet()->getGoodPages();
39  if ( $pages === [] ) {
40  return;
41  }
42 
43  $params = $this->extractRequestParams();
44  $db = $this->getDB();
45 
46  $query = $params['query'];
47  $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
48 
49  $this->addFields( [
50  'el_from',
51  'el_to'
52  ] );
53 
54  $this->addTables( 'externallinks' );
55  $this->addWhereFld( 'el_from', array_keys( $pages ) );
56 
57  $orderBy = [];
58 
59  // Don't order by el_from if it's constant in the WHERE clause
60  if ( count( $pages ) !== 1 ) {
61  $orderBy[] = 'el_from';
62  }
63 
64  if ( $query !== null && $query !== '' ) {
65  if ( $protocol === null ) {
66  $protocol = 'http://';
67  }
68 
69  // Normalize query to match the normalization applied for the externallinks table
70  $query = Parser::normalizeLinkUrl( $protocol . $query );
71 
72  $conds = LinkFilter::getQueryConditions( $query, [
73  'protocol' => '',
74  'oneWildcard' => true,
75  'db' => $db
76  ] );
77  if ( !$conds ) {
78  $this->dieWithError( 'apierror-badquery' );
79  }
80  $this->addWhere( $conds );
81  if ( !isset( $conds['el_index_60'] ) ) {
82  $orderBy[] = 'el_index_60';
83  }
84  } else {
85  $orderBy[] = 'el_index_60';
86 
87  if ( $protocol !== null ) {
88  $this->addWhere( 'el_index_60' . $db->buildLike( "$protocol", $db->anyString() ) );
89  } else {
90  // We're querying all protocols, filter out duplicate protocol-relative links
91  $this->addWhere( $db->makeList( [
92  'el_to NOT' . $db->buildLike( '//', $db->anyString() ),
93  'el_index_60 ' . $db->buildLike( 'http://', $db->anyString() ),
94  ], LIST_OR ) );
95  }
96  }
97 
98  $orderBy[] = 'el_id';
99 
100  $this->addOption( 'ORDER BY', $orderBy );
101  $this->addFields( $orderBy ); // Make sure
102 
103  $this->addOption( 'LIMIT', $params['limit'] + 1 );
104 
105  if ( $params['continue'] !== null ) {
106  $cont = $this->parseContinueParamOrDie( $params['continue'],
107  array_fill( 0, count( $orderBy ), 'string' ) );
108  $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) );
109  $this->addWhere( $db->buildComparison( '>=', $conds ) );
110  }
111 
112  $res = $this->select( __METHOD__ );
113 
114  $count = 0;
115  foreach ( $res as $row ) {
116  if ( ++$count > $params['limit'] ) {
117  // We've reached the one extra which shows that
118  // there are additional pages to be had. Stop here...
119  $this->setContinue( $orderBy, $row );
120  break;
121  }
122  $entry = [];
123  $to = $row->el_to;
124  // expand protocol-relative urls
125  if ( $params['expandurl'] ) {
126  $to = wfExpandUrl( $to, PROTO_CANONICAL );
127  }
128  ApiResult::setContentValue( $entry, 'url', $to );
129  $fit = $this->addPageSubItem( $row->el_from, $entry );
130  if ( !$fit ) {
131  $this->setContinue( $orderBy, $row );
132  break;
133  }
134  }
135  }
136 
137  private function setContinue( $orderBy, $row ) {
138  $fields = [];
139  foreach ( $orderBy as $field ) {
140  $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
141  }
142  $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
143  }
144 
145  public function getCacheMode( $params ) {
146  return 'public';
147  }
148 
149  public function getAllowedParams() {
150  return [
151  'limit' => [
152  ParamValidator::PARAM_DEFAULT => 10,
153  ParamValidator::PARAM_TYPE => 'limit',
154  IntegerDef::PARAM_MIN => 1,
155  IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
156  IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
157  ],
158  'continue' => [
159  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
160  ],
161  'protocol' => [
162  ParamValidator::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
163  ParamValidator::PARAM_DEFAULT => '',
164  ],
165  'query' => null,
166  'expandurl' => false,
167  ];
168  }
169 
170  protected function getExamplesMessages() {
171  return [
172  'action=query&prop=extlinks&titles=Main%20Page'
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 }
const PROTO_CANONICAL
Definition: Defines.php:199
const LIST_OR
Definition: Defines.php:46
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1455
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition: ApiBase.php:1644
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:222
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:766
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:164
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:224
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.
static getProtocolPrefix( $protocol)
This is the main query class.
Definition: ApiQuery.php:41
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
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
Definition: LinkFilter.php:253
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition: Parser.php:2305
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23