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