Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryGlobalUsage
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 6
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
182
 setIndexedTagName
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Created on November 8, 2009
4 *
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2009 Bryan Tong Minh <bryan.tongminh@gmail.com>
8 *
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.
13 *
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.
18 *
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
23 */
24
25namespace MediaWiki\Extension\GlobalUsage;
26
27use ApiBase;
28use ApiQueryBase;
29use MediaWiki\MediaWikiServices;
30use MediaWiki\WikiMap\WikiMap;
31use Wikimedia\ParamValidator\ParamValidator;
32use Wikimedia\ParamValidator\TypeDef\IntegerDef;
33
34class ApiQueryGlobalUsage extends ApiQueryBase {
35    public function __construct( $query, $moduleName ) {
36        parent::__construct( $query, $moduleName, 'gu' );
37    }
38
39    public function execute() {
40        $params = $this->extractRequestParams();
41        $prop = array_flip( $params['prop'] );
42
43        $allPageIds = $this->getPageSet()->getAllTitlesByNamespace();
44        if ( !empty( $allPageIds[NS_FILE] ) ) {
45            # Create a query and set parameters
46            $pageIds = $allPageIds[NS_FILE];
47            $query = new GlobalUsageQuery( array_map( 'strval', array_keys( $pageIds ) ) );
48            if ( $params['continue'] !== null ) {
49                $this->dieContinueUsageIf( !$query->setOffset( $params['continue'] ) );
50            }
51            $query->setLimit( $params['limit'] );
52            $query->filterLocal( $params['filterlocal'] );
53            $query->filterNamespaces( $params['namespace'] );
54            if ( $params['site'] ) {
55                $query->filterSites( $params['site'] );
56            }
57
58            # Execute the query
59            $query->execute();
60
61            # Create the result
62            $apiResult = $this->getResult();
63
64            foreach ( $query->getResult() as $image => $wikis ) {
65                $pageId = intval( $pageIds[$image] );
66                foreach ( $wikis as $wiki => $result ) {
67                    foreach ( $result as $item ) {
68                        if ( $item['namespace'] ) {
69                            $title = "{$item['namespace']}:{$item['title']}";
70                        } else {
71                            $title = $item['title'];
72                        }
73                        $result = [
74                            'title' => $title,
75                            'wiki' => WikiMap::getWikiName( $wiki )
76                        ];
77                        if ( isset( $prop['url'] ) ) {
78                            // We expand the url because we don't want protocol relative urls
79                            // in API results
80                            $result['url'] = wfExpandUrl(
81                                WikiMap::getForeignUrl( $item['wiki'], $title ), PROTO_CURRENT );
82                        }
83                        if ( isset( $prop['pageid'] ) ) {
84                            $result['pageid'] = $item['id'];
85                        }
86                        if ( isset( $prop['namespace'] ) ) {
87                            $result['ns'] = $item['namespace_id'];
88                        }
89
90                        $fit = $apiResult->addValue(
91                            [ 'query', 'pages', $pageId, 'globalusage' ],
92                            null,
93                            $result
94                        );
95
96                        if ( !$fit ) {
97                            $continue = "{$item['image']}|{$item['wiki']}|{$item['id']}";
98                            $this->setIndexedTagName();
99                            $this->setContinueEnumParameter( 'continue', $continue );
100                            return;
101                        }
102                    }
103                }
104            }
105            $this->setIndexedTagName();
106
107            if ( $query->hasMore() ) {
108                $this->setContinueEnumParameter( 'continue', $query->getContinueString() );
109            }
110        }
111    }
112
113    private function setIndexedTagName() {
114        $result = $this->getResult();
115        $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
116        foreach ( $pageIds[NS_FILE] as $id ) {
117            $result->addIndexedTagName(
118                [ 'query', 'pages', $id, 'globalusage' ],
119                'gu'
120            );
121        }
122    }
123
124    public function getAllowedParams() {
125        $sites = MediaWikiServices::getInstance()->getSiteLookup()->getSites()
126            ->getGlobalIdentifiers();
127        return [
128            'prop' => [
129                ParamValidator::PARAM_DEFAULT => 'url',
130                ParamValidator::PARAM_TYPE => [
131                    'url',
132                    'pageid',
133                    'namespace',
134                ],
135                ParamValidator::PARAM_ISMULTI => true,
136                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
137            ],
138            'limit' => [
139                ParamValidator::PARAM_DEFAULT => 10,
140                ParamValidator::PARAM_TYPE => 'limit',
141                IntegerDef::PARAM_MIN => 1,
142                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
143                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
144            ],
145            'namespace' => [
146                ParamValidator::PARAM_TYPE => 'namespace',
147                ParamValidator::PARAM_DEFAULT => '*',
148                ParamValidator::PARAM_ISMULTI => true,
149            ],
150            'site' => [
151                ParamValidator::PARAM_TYPE => $sites,
152                ParamValidator::PARAM_ISMULTI => true
153            ],
154            'continue' => [
155                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
156            ],
157            'filterlocal' => false,
158        ];
159    }
160
161    /**
162     * @see ApiBase::getExamplesMessages()
163     * @return array
164     */
165    protected function getExamplesMessages() {
166        return [
167            'action=query&prop=globalusage&titles=File:Example.jpg'
168                => 'apihelp-query+globalusage-example-1',
169        ];
170    }
171
172    public function getCacheMode( $params ) {
173        return 'public';
174    }
175}