Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialGloballyUnusedFiles
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 12
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 isOnGlobalUsageDatabase
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 execute
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 isCacheable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isListed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isExpensive
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sortDescending
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isSyndicated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getQueryInfo
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 usesTimestamps
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Implements Special:GloballyUnusedFiles, the global equivalent to
4 * Special:UnusedFiles
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup SpecialPage
23 */
24
25namespace MediaWiki\Extension\GlobalUsage;
26
27use ErrorPageError;
28use MediaWiki\SpecialPage\ImageQueryPage;
29use MediaWiki\WikiMap\WikiMap;
30use RuntimeException;
31use Wikimedia\Rdbms\IConnectionProvider;
32
33/**
34 * A special page that lists globally unused files
35 *
36 * @ingroup SpecialPage
37 */
38class SpecialGloballyUnusedFiles extends ImageQueryPage {
39    public function __construct(
40        IConnectionProvider $dbProvider
41    ) {
42        parent::__construct( 'GloballyUnusedFiles' );
43        $this->setDatabaseProvider( $dbProvider );
44    }
45
46    /**
47     * Check if we are on wiki with globalimagelinks table in database.
48     * @return bool
49     */
50    private function isOnGlobalUsageDatabase() {
51        $globalUsageDatabase = $this->getConfig()->get( 'GlobalUsageDatabase' );
52        return !$globalUsageDatabase || $globalUsageDatabase === WikiMap::getCurrentWikiId();
53    }
54
55    /**
56     * Main execution function. Use the parent if we're on the right wiki.
57     * @param string $par
58     * @throws ErrorPageError if we are not on a wiki with GlobalUsage database
59     */
60    public function execute( $par ) {
61        if ( $this->isOnGlobalUsageDatabase() ) {
62            parent::execute( $par );
63        } else {
64            throw new ErrorPageError( 'globallyunusedfiles', 'globallyunusedfiles-error-nonsharedrepo' );
65        }
66    }
67
68    /**
69     * Allow to cache only if globalimagelinks table exists in database.
70     * @return bool
71     */
72    public function isCacheable() {
73        return $this->isOnGlobalUsageDatabase();
74    }
75
76    /**
77     * Only list this special page on the wiki that has globalimagelinks table.
78     * @return bool Should this be listed in Special:SpecialPages
79     */
80    public function isListed() {
81        return $this->isOnGlobalUsageDatabase();
82    }
83
84    /** @inheritDoc */
85    public function isExpensive() {
86        return true;
87    }
88
89    /** @inheritDoc */
90    public function sortDescending() {
91        return false;
92    }
93
94    /** @inheritDoc */
95    public function isSyndicated() {
96        return false;
97    }
98
99    /** @inheritDoc */
100    public function getQueryInfo() {
101        if ( !$this->isOnGlobalUsageDatabase() ) {
102            throw new RuntimeException( "This wiki is not on shared repo" );
103        }
104
105        $retval = [
106            'tables' => [ 'image', 'globalimagelinks' ],
107            'fields' => [
108                'namespace' => NS_FILE,
109                'title' => 'img_name',
110                'value' => 'img_timestamp',
111            ],
112            'conds' => [ 'gil_to' => null ],
113            'join_conds' => [ 'globalimagelinks' => [ 'LEFT JOIN', 'gil_to = img_name' ] ]
114        ];
115
116        if ( $this->getConfig()->get( 'CountCategorizedImagesAsUsed' ) ) {
117            // Order is significant
118            $retval['tables'] = [ 'image', 'page', 'categorylinks',
119                'globalimagelinks' ];
120            $retval['conds']['page_namespace'] = NS_FILE;
121            $retval['conds']['cl_from'] = null;
122            $retval['conds'][] = 'img_name = page_title';
123            $retval['join_conds']['categorylinks'] = [
124                'LEFT JOIN', 'cl_from = page_id' ];
125            $retval['join_conds']['globalimagelinks'] = [
126                'LEFT JOIN', 'gil_to = page_title' ];
127        }
128
129        return $retval;
130    }
131
132    /** @inheritDoc */
133    public function usesTimestamps() {
134        return true;
135    }
136
137    /** @inheritDoc */
138    public function getPageHeader() {
139        return $this->msg( 'globallyunusedfilestext' )->parseAsBlock();
140    }
141
142    /** @inheritDoc */
143    protected function getGroupName() {
144        return 'maintenance';
145    }
146}