MediaWiki  1.33.0
ApiQueryExternalLinks.php
Go to the documentation of this file.
1 <?php
29 
30  public function __construct( ApiQuery $query, $moduleName ) {
31  parent::__construct( $query, $moduleName, 'el' );
32  }
33 
34  public function execute() {
35  if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
36  return;
37  }
38 
39  $params = $this->extractRequestParams();
40  $db = $this->getDB();
41 
42  $query = $params['query'];
43  $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
44 
45  $this->addFields( [
46  'el_from',
47  'el_to'
48  ] );
49 
50  $this->addTables( 'externallinks' );
51  $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
52 
53  $orderBy = [];
54 
55  // Don't order by el_from if it's constant in the WHERE clause
56  if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
57  $orderBy[] = 'el_from';
58  }
59 
60  if ( $query !== null && $query !== '' ) {
61  if ( $protocol === null ) {
62  $protocol = 'http://';
63  }
64 
65  // Normalize query to match the normalization applied for the externallinks table
66  $query = Parser::normalizeLinkUrl( $protocol . $query );
67 
69  'protocol' => '',
70  'oneWildcard' => true,
71  'db' => $db
72  ] );
73  if ( !$conds ) {
74  $this->dieWithError( 'apierror-badquery' );
75  }
76  $this->addWhere( $conds );
77  if ( !isset( $conds['el_index_60'] ) ) {
78  $orderBy[] = 'el_index_60';
79  }
80  } else {
81  $orderBy[] = 'el_index_60';
82 
83  if ( $protocol !== null ) {
84  $this->addWhere( 'el_index_60' . $db->buildLike( "$protocol", $db->anyString() ) );
85  } else {
86  // We're querying all protocols, filter out duplicate protocol-relative links
87  $this->addWhere( $db->makeList( [
88  'el_to NOT' . $db->buildLike( '//', $db->anyString() ),
89  'el_index_60 ' . $db->buildLike( 'http://', $db->anyString() ),
90  ], LIST_OR ) );
91  }
92  }
93 
94  $orderBy[] = 'el_id';
95  $this->addOption( 'ORDER BY', $orderBy );
96  $this->addFields( $orderBy ); // Make sure
97 
98  $this->addOption( 'LIMIT', $params['limit'] + 1 );
99 
100  if ( $params['continue'] !== null ) {
101  $cont = explode( '|', $params['continue'] );
102  $this->dieContinueUsageIf( count( $cont ) !== count( $orderBy ) );
103  $i = count( $cont ) - 1;
104  $cond = $orderBy[$i] . ' >= ' . $db->addQuotes( rawurldecode( $cont[$i] ) );
105  while ( $i-- > 0 ) {
106  $field = $orderBy[$i];
107  $v = $db->addQuotes( rawurldecode( $cont[$i] ) );
108  $cond = "($field > $v OR ($field = $v AND $cond))";
109  }
110  $this->addWhere( $cond );
111  }
112 
113  $res = $this->select( __METHOD__ );
114 
115  $count = 0;
116  foreach ( $res as $row ) {
117  if ( ++$count > $params['limit'] ) {
118  // We've reached the one extra which shows that
119  // there are additional pages to be had. Stop here...
120  $this->setContinue( $orderBy, $row );
121  break;
122  }
123  $entry = [];
124  $to = $row->el_to;
125  // expand protocol-relative urls
126  if ( $params['expandurl'] ) {
127  $to = wfExpandUrl( $to, PROTO_CANONICAL );
128  }
129  ApiResult::setContentValue( $entry, 'url', $to );
130  $fit = $this->addPageSubItem( $row->el_from, $entry );
131  if ( !$fit ) {
132  $this->setContinue( $orderBy, $row );
133  break;
134  }
135  }
136  }
137 
138  private function setContinue( $orderBy, $row ) {
139  $fields = [];
140  foreach ( $orderBy as $field ) {
141  $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
142  }
143  $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
144  }
145 
146  public function getCacheMode( $params ) {
147  return 'public';
148  }
149 
150  public function getAllowedParams() {
151  return [
152  'limit' => [
153  ApiBase::PARAM_DFLT => 10,
154  ApiBase::PARAM_TYPE => 'limit',
155  ApiBase::PARAM_MIN => 1,
158  ],
159  'continue' => [
160  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
161  ],
162  'protocol' => [
164  ApiBase::PARAM_DFLT => '',
165  ],
166  'query' => null,
167  'expandurl' => false,
168  ];
169  }
170 
171  protected function getExamplesMessages() {
172  return [
173  'action=query&prop=extlinks&titles=Main%20Page'
174  => 'apihelp-query+extlinks-example-simple',
175  ];
176  }
177 
178  public function getHelpUrls() {
179  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
180  }
181 }
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:190
ApiQuery
This is the main query class.
Definition: ApiQuery.php:36
PROTO_CANONICAL
const PROTO_CANONICAL
Definition: Defines.php:223
LinkFilter\getQueryConditions
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
Definition: LinkFilter.php:254
captcha-old.count
count
Definition: captcha-old.py:249
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1990
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:124
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:87
ApiQueryBase\$fields
$fields
Definition: ApiQueryBase.php:35
$params
$params
Definition: styleTest.css.php:44
$res
$res
Definition: database.txt:21
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:347
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiResult\setContentValue
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:478
ApiQueryExtLinksUsage\prepareProtocols
static prepareProtocols()
Definition: ApiQueryExtLinksUsage.php:239
$query
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1588
ApiBase\PARAM_MIN
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:99
LIST_OR
const LIST_OR
Definition: Defines.php:46
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:33
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:252
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:105
ApiBase\PARAM_MAX
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:90
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:158
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:372
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:743
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:2176
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
Definition: ApiQueryBase.php:258
ApiQueryBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryBase.php:130
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:254
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:48
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiBase\PARAM_MAX2
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition: ApiBase.php:96
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:225
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:559
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:538
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:515
ApiQueryExtLinksUsage\getProtocolPrefix
static getProtocolPrefix( $protocol)
Definition: ApiQueryExtLinksUsage.php:251