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  $this->addOption( 'ORDER BY', $orderBy );
100  $this->addFields( $orderBy ); // Make sure
101 
102  $this->addOption( 'LIMIT', $params['limit'] + 1 );
103 
104  if ( $params['continue'] !== null ) {
105  $cont = explode( '|', $params['continue'] );
106  $this->dieContinueUsageIf( count( $cont ) !== count( $orderBy ) );
107  $i = count( $cont ) - 1;
108  $cond = $orderBy[$i] . ' >= ' . $db->addQuotes( rawurldecode( $cont[$i] ) );
109  while ( $i-- > 0 ) {
110  $field = $orderBy[$i];
111  $v = $db->addQuotes( rawurldecode( $cont[$i] ) );
112  $cond = "($field > $v OR ($field = $v AND $cond))";
113  }
114  $this->addWhere( $cond );
115  }
116 
117  $res = $this->select( __METHOD__ );
118 
119  $count = 0;
120  foreach ( $res as $row ) {
121  if ( ++$count > $params['limit'] ) {
122  // We've reached the one extra which shows that
123  // there are additional pages to be had. Stop here...
124  $this->setContinue( $orderBy, $row );
125  break;
126  }
127  $entry = [];
128  $to = $row->el_to;
129  // expand protocol-relative urls
130  if ( $params['expandurl'] ) {
131  $to = wfExpandUrl( $to, PROTO_CANONICAL );
132  }
133  ApiResult::setContentValue( $entry, 'url', $to );
134  $fit = $this->addPageSubItem( $row->el_from, $entry );
135  if ( !$fit ) {
136  $this->setContinue( $orderBy, $row );
137  break;
138  }
139  }
140  }
141 
142  private function setContinue( $orderBy, $row ) {
143  $fields = [];
144  foreach ( $orderBy as $field ) {
145  $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
146  }
147  $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
148  }
149 
150  public function getCacheMode( $params ) {
151  return 'public';
152  }
153 
154  public function getAllowedParams() {
155  return [
156  'limit' => [
157  ParamValidator::PARAM_DEFAULT => 10,
158  ParamValidator::PARAM_TYPE => 'limit',
159  IntegerDef::PARAM_MIN => 1,
160  IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
161  IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
162  ],
163  'continue' => [
164  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
165  ],
166  'protocol' => [
167  ParamValidator::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
168  ParamValidator::PARAM_DEFAULT => '',
169  ],
170  'query' => null,
171  'expandurl' => false,
172  ];
173  }
174 
175  protected function getExamplesMessages() {
176  return [
177  'action=query&prop=extlinks&titles=Main%20Page'
178  => 'apihelp-query+extlinks-example-simple',
179  ];
180  }
181 
182  public function getHelpUrls() {
183  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
184  }
185 }
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:1453
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1645
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:221
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:163
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:223
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:252
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition: Parser.php:2316
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23