MediaWiki  1.23.2
ApiQueryExternalLinks.php
Go to the documentation of this file.
1 <?php
33 
34  public function __construct( $query, $moduleName ) {
35  parent::__construct( $query, $moduleName, 'el' );
36  }
37 
38  public function execute() {
39  if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
40  return;
41  }
42 
43  $params = $this->extractRequestParams();
44 
45  $query = $params['query'];
46  $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
47 
48  $this->addFields( array(
49  'el_from',
50  'el_to'
51  ) );
52 
53  $this->addTables( 'externallinks' );
54  $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
55 
56  $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
57 
58  if ( $whereQuery !== null ) {
59  $this->addWhere( $whereQuery );
60  }
61 
62  // Don't order by el_from if it's constant in the WHERE clause
63  if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
64  $this->addOption( 'ORDER BY', 'el_from' );
65  }
66 
67  // If we're querying all protocols, use DISTINCT to avoid repeating protocol-relative links twice
68  if ( $protocol === null ) {
69  $this->addOption( 'DISTINCT' );
70  }
71 
72  $this->addOption( 'LIMIT', $params['limit'] + 1 );
73  $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
74  if ( $offset ) {
75  $this->addOption( 'OFFSET', $params['offset'] );
76  }
77 
78  $res = $this->select( __METHOD__ );
79 
80  $count = 0;
81  foreach ( $res as $row ) {
82  if ( ++$count > $params['limit'] ) {
83  // We've reached the one extra which shows that
84  // there are additional pages to be had. Stop here...
85  $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
86  break;
87  }
88  $entry = array();
89  $to = $row->el_to;
90  // expand protocol-relative urls
91  if ( $params['expandurl'] ) {
92  $to = wfExpandUrl( $to, PROTO_CANONICAL );
93  }
94  ApiResult::setContent( $entry, $to );
95  $fit = $this->addPageSubItem( $row->el_from, $entry );
96  if ( !$fit ) {
97  $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
98  break;
99  }
100  }
101  }
102 
103  public function getCacheMode( $params ) {
104  return 'public';
105  }
106 
107  public function getAllowedParams() {
108  return array(
109  'limit' => array(
110  ApiBase::PARAM_DFLT => 10,
111  ApiBase::PARAM_TYPE => 'limit',
112  ApiBase::PARAM_MIN => 1,
115  ),
116  'offset' => array(
117  ApiBase::PARAM_TYPE => 'integer'
118  ),
119  'protocol' => array(
121  ApiBase::PARAM_DFLT => '',
122  ),
123  'query' => null,
124  'expandurl' => false,
125  );
126  }
127 
128  public function getParamDescription() {
129  $p = $this->getModulePrefix();
130 
131  return array(
132  'limit' => 'How many links to return',
133  'offset' => 'When more results are available, use this to continue',
134  'protocol' => array(
135  "Protocol of the URL. If empty and {$p}query set, the protocol is http.",
136  "Leave both this and {$p}query empty to list all external links"
137  ),
138  'query' => 'Search string without protocol. Useful for checking ' .
139  'whether a certain page contains a certain external url',
140  'expandurl' => 'Expand protocol-relative URLs with the canonical protocol',
141  );
142  }
143 
144  public function getResultProperties() {
145  return array(
146  '' => array(
147  '*' => 'string'
148  )
149  );
150  }
151 
152  public function getDescription() {
153  return 'Returns all external URLs (not interwikis) from the given page(s).';
154  }
155 
156  public function getPossibleErrors() {
157  return array_merge( parent::getPossibleErrors(), array(
158  array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
159  ) );
160  }
161 
162  public function getExamples() {
163  return array(
164  'api.php?action=query&prop=extlinks&titles=Main%20Page'
165  => 'Get a list of external links on the [[Main Page]]',
166  );
167  }
168 
169  public function getHelpUrls() {
170  return 'https://www.mediawiki.org/wiki/API:Properties#extlinks_.2F_el';
171  }
172 }
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:117
PROTO_CANONICAL
const PROTO_CANONICAL
Definition: Defines.php:271
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiResult\setContent
static setContent(&$arr, $value, $subElemName=null)
Adds a content element to an array.
Definition: ApiResult.php:201
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiQueryBase\select
select( $method, $extraQuery=array())
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:274
$params
$params
Definition: styleTest.css.php:40
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:252
ApiQueryExtLinksUsage\prepareProtocols
static prepareProtocols()
Definition: ApiQueryExtLinksUsage.php:182
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:56
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:34
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Definition: ApiBase.php:78
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:52
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:82
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:165
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
Definition: ApiQueryBase.php:185
ApiQueryBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryBase.php:441
$count
$count
Definition: UtfNormalTest2.php:96
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Definition: ApiBase.php:79
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
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
Definition: ApiBase.php:54
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:152
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:404
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
ApiQueryBase\prepareUrlQuerySearchString
prepareUrlQuerySearchString( $query=null, $protocol=null)
Definition: ApiQueryBase.php:537
$res
$res
Definition: database.txt:21
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:383
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:497
ApiQueryExtLinksUsage\getProtocolPrefix
static getProtocolPrefix( $protocol)
Definition: ApiQueryExtLinksUsage.php:194