Merge "Revert "selenium: add new message banner test to user spec""
[lhc/web/wiklou.git] / includes / api / ApiQueryExtLinksUsage.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * @ingroup API
25 */
26 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
27
28 public function __construct( ApiQuery $query, $moduleName ) {
29 parent::__construct( $query, $moduleName, 'eu' );
30 }
31
32 public function execute() {
33 $this->run();
34 }
35
36 public function getCacheMode( $params ) {
37 return 'public';
38 }
39
40 public function executeGenerator( $resultPageSet ) {
41 $this->run( $resultPageSet );
42 }
43
44 /**
45 * @param ApiPageSet $resultPageSet
46 * @return void
47 */
48 private function run( $resultPageSet = null ) {
49 $params = $this->extractRequestParams();
50
51 $query = $params['query'];
52 $protocol = self::getProtocolPrefix( $params['protocol'] );
53
54 $this->addTables( [ 'page', 'externallinks' ] ); // must be in this order for 'USE INDEX'
55 $this->addOption( 'USE INDEX', 'el_index' );
56 $this->addWhere( 'page_id=el_from' );
57
58 $miser_ns = [];
59 if ( $this->getConfig()->get( 'MiserMode' ) ) {
60 $miser_ns = $params['namespace'] ?: [];
61 } else {
62 $this->addWhereFld( 'page_namespace', $params['namespace'] );
63 }
64
65 // Normalize query to match the normalization applied for the externallinks table
66 $query = Parser::normalizeLinkUrl( $query );
67
68 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
69
70 if ( $whereQuery !== null ) {
71 $this->addWhere( $whereQuery );
72 }
73
74 $prop = array_flip( $params['prop'] );
75 $fld_ids = isset( $prop['ids'] );
76 $fld_title = isset( $prop['title'] );
77 $fld_url = isset( $prop['url'] );
78
79 if ( is_null( $resultPageSet ) ) {
80 $this->addFields( [
81 'page_id',
82 'page_namespace',
83 'page_title'
84 ] );
85 $this->addFieldsIf( 'el_to', $fld_url );
86 } else {
87 $this->addFields( $resultPageSet->getPageTableFields() );
88 }
89
90 $limit = $params['limit'];
91 $offset = $params['offset'];
92 $this->addOption( 'LIMIT', $limit + 1 );
93 if ( isset( $offset ) ) {
94 $this->addOption( 'OFFSET', $offset );
95 }
96
97 $res = $this->select( __METHOD__ );
98
99 $result = $this->getResult();
100 $count = 0;
101 foreach ( $res as $row ) {
102 if ( ++$count > $limit ) {
103 // We've reached the one extra which shows that there are
104 // additional pages to be had. Stop here...
105 $this->setContinueEnumParameter( 'offset', $offset + $limit );
106 break;
107 }
108
109 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
110 continue;
111 }
112
113 if ( is_null( $resultPageSet ) ) {
114 $vals = [
115 ApiResult::META_TYPE => 'assoc',
116 ];
117 if ( $fld_ids ) {
118 $vals['pageid'] = intval( $row->page_id );
119 }
120 if ( $fld_title ) {
121 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
122 ApiQueryBase::addTitleInfo( $vals, $title );
123 }
124 if ( $fld_url ) {
125 $to = $row->el_to;
126 // expand protocol-relative urls
127 if ( $params['expandurl'] ) {
128 $to = wfExpandUrl( $to, PROTO_CANONICAL );
129 }
130 $vals['url'] = $to;
131 }
132 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
133 if ( !$fit ) {
134 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
135 break;
136 }
137 } else {
138 $resultPageSet->processDbRow( $row );
139 }
140 }
141
142 if ( is_null( $resultPageSet ) ) {
143 $result->addIndexedTagName( [ 'query', $this->getModuleName() ],
144 $this->getModulePrefix() );
145 }
146 }
147
148 public function getAllowedParams() {
149 $ret = [
150 'prop' => [
151 ApiBase::PARAM_ISMULTI => true,
152 ApiBase::PARAM_DFLT => 'ids|title|url',
153 ApiBase::PARAM_TYPE => [
154 'ids',
155 'title',
156 'url'
157 ],
158 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
159 ],
160 'offset' => [
161 ApiBase::PARAM_TYPE => 'integer',
162 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
163 ],
164 'protocol' => [
165 ApiBase::PARAM_TYPE => self::prepareProtocols(),
166 ApiBase::PARAM_DFLT => '',
167 ],
168 'query' => null,
169 'namespace' => [
170 ApiBase::PARAM_ISMULTI => true,
171 ApiBase::PARAM_TYPE => 'namespace'
172 ],
173 'limit' => [
174 ApiBase::PARAM_DFLT => 10,
175 ApiBase::PARAM_TYPE => 'limit',
176 ApiBase::PARAM_MIN => 1,
177 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
178 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
179 ],
180 'expandurl' => false,
181 ];
182
183 if ( $this->getConfig()->get( 'MiserMode' ) ) {
184 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
185 'api-help-param-limited-in-miser-mode',
186 ];
187 }
188
189 return $ret;
190 }
191
192 public static function prepareProtocols() {
193 global $wgUrlProtocols;
194 $protocols = [ '' ];
195 foreach ( $wgUrlProtocols as $p ) {
196 if ( $p !== '//' ) {
197 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
198 }
199 }
200
201 return $protocols;
202 }
203
204 public static function getProtocolPrefix( $protocol ) {
205 // Find the right prefix
206 global $wgUrlProtocols;
207 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
208 foreach ( $wgUrlProtocols as $p ) {
209 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
210 $protocol = $p;
211 break;
212 }
213 }
214
215 return $protocol;
216 } else {
217 return null;
218 }
219 }
220
221 protected function getExamplesMessages() {
222 return [
223 'action=query&list=exturlusage&euquery=www.mediawiki.org'
224 => 'apihelp-query+exturlusage-example-simple',
225 ];
226 }
227
228 public function getHelpUrls() {
229 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Exturlusage';
230 }
231 }