MediaWiki REL1_32
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
40
41 $query = $params['query'];
42 $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
43
44 $this->addFields( [
45 'el_from',
46 'el_to'
47 ] );
48
49 $this->addTables( 'externallinks' );
50 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
51
52 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
53
54 if ( $whereQuery !== null ) {
55 $this->addWhere( $whereQuery );
56 }
57
58 // Don't order by el_from if it's constant in the WHERE clause
59 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
60 $this->addOption( 'ORDER BY', 'el_from' );
61 }
62
63 // If we're querying all protocols, use DISTINCT to avoid repeating protocol-relative links twice
64 if ( $protocol === null ) {
65 $this->addOption( 'DISTINCT' );
66 }
67
68 $this->addOption( 'LIMIT', $params['limit'] + 1 );
69 $offset = $params['offset'] ?? 0;
70 if ( $offset ) {
71 $this->addOption( 'OFFSET', $params['offset'] );
72 }
73
74 $res = $this->select( __METHOD__ );
75
76 $count = 0;
77 foreach ( $res as $row ) {
78 if ( ++$count > $params['limit'] ) {
79 // We've reached the one extra which shows that
80 // there are additional pages to be had. Stop here...
81 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
82 break;
83 }
84 $entry = [];
85 $to = $row->el_to;
86 // expand protocol-relative urls
87 if ( $params['expandurl'] ) {
88 $to = wfExpandUrl( $to, PROTO_CANONICAL );
89 }
90 ApiResult::setContentValue( $entry, 'url', $to );
91 $fit = $this->addPageSubItem( $row->el_from, $entry );
92 if ( !$fit ) {
93 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
94 break;
95 }
96 }
97 }
98
99 public function getCacheMode( $params ) {
100 return 'public';
101 }
102
103 public function getAllowedParams() {
104 return [
105 'limit' => [
107 ApiBase::PARAM_TYPE => 'limit',
111 ],
112 'offset' => [
113 ApiBase::PARAM_TYPE => 'integer',
114 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
115 ],
116 'protocol' => [
119 ],
120 'query' => null,
121 'expandurl' => false,
122 ];
123 }
124
125 protected function getExamplesMessages() {
126 return [
127 'action=query&prop=extlinks&titles=Main%20Page'
128 => 'apihelp-query+extlinks-example-simple',
129 ];
130 }
131
132 public function getHelpUrls() {
133 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
134 }
135}
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition ApiBase.php:96
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:90
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:87
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:48
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:99
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:252
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:770
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:124
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:254
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
prepareUrlQuerySearchString( $query=null, $protocol=null)
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.
addWhereFld( $field, $value)
Equivalent to addWhere(array($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:36
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like select() and insert() are usually more convenient. They take care of things like table prefixes and escaping for you. If you really need to make your own SQL
$res
Definition database.txt:21
null for the local 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:1656
const PROTO_CANONICAL
Definition Defines.php:223
$params