3 * Special handling for file pages.
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.
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.
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
23 use MediaWiki\MediaWikiServices
;
24 use Wikimedia\Rdbms\FakeResultWrapper
;
27 * Special handling for file pages
31 class WikiFilePage
extends WikiPage
{
32 /** @var File|false */
33 protected $mFile = false;
34 /** @var LocalRepo|null */
35 protected $mRepo = null;
37 protected $mFileLoaded = false;
38 /** @var array|null */
39 protected $mDupes = null;
41 public function __construct( $title ) {
42 parent
::__construct( $title );
50 public function setFile( $file ) {
52 $this->mFileLoaded
= true;
58 protected function loadFile() {
59 $services = MediaWikiServices
::getInstance();
60 if ( $this->mFileLoaded
) {
63 $this->mFileLoaded
= true;
65 $this->mFile
= $services->getRepoGroup()->findFile( $this->mTitle
);
66 if ( !$this->mFile
) {
67 $this->mFile
= $services->getRepoGroup()->getLocalRepo()
68 ->newFile( $this->mTitle
); // always a File
70 $this->mRepo
= $this->mFile
->getRepo();
75 * @return mixed|null|Title
77 public function getRedirectTarget() {
79 if ( $this->mFile
->isLocal() ) {
80 return parent
::getRedirectTarget();
83 $from = $this->mFile
->getRedirected();
84 $to = $this->mFile
->getName();
88 $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
89 return $this->mRedirectTarget
;
93 * @return bool|mixed|Title
95 public function followRedirect() {
97 if ( $this->mFile
->isLocal() ) {
98 return parent
::followRedirect();
100 $from = $this->mFile
->getRedirected();
101 $to = $this->mFile
->getName();
102 if ( $from == $to ) {
105 return Title
::makeTitle( NS_FILE
, $to );
111 public function isRedirect() {
113 if ( $this->mFile
->isLocal() ) {
114 return parent
::isRedirect();
117 return (bool)$this->mFile
->getRedirected();
123 public function isLocal() {
125 return $this->mFile
->isLocal();
131 public function getFile() {
139 public function getDuplicates() {
141 if ( !is_null( $this->mDupes
) ) {
142 return $this->mDupes
;
144 $hash = $this->mFile
->getSha1();
147 return $this->mDupes
;
149 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
150 // Remove duplicates with self and non matching file sizes
151 $self = $this->mFile
->getRepoName() . ':' . $this->mFile
->getName();
152 $size = $this->mFile
->getSize();
157 foreach ( $dupes as $index => $file ) {
158 $key = $file->getRepoName() . ':' . $file->getName();
159 if ( $key == $self ) {
160 unset( $dupes[$index] );
162 if ( $file->getSize() != $size ) {
163 unset( $dupes[$index] );
166 $this->mDupes
= $dupes;
167 return $this->mDupes
;
171 * Override handling of action=purge
174 public function doPurge() {
177 if ( $this->mFile
->exists() ) {
178 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile
->getName() . "\n" );
179 $job = HTMLCacheUpdateJob
::newForBacklinks(
182 [ 'causeAction' => 'file-purge' ]
184 JobQueueGroup
::singleton()->lazyPush( $job );
186 wfDebug( 'ImagePage::doPurge no image for '
187 . $this->mFile
->getName() . "; limiting purge to cache only\n" );
190 // even if the file supposedly doesn't exist, force any cached information
191 // to be updated (in case the cached information is wrong)
193 // Purge current version and its thumbnails
194 $this->mFile
->purgeCache( [ 'forThumbRefresh' => true ] );
196 // Purge the old versions and their thumbnails
197 foreach ( $this->mFile
->getHistory() as $oldFile ) {
198 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
201 if ( $this->mRepo
) {
202 // Purge redirect cache
203 $this->mRepo
->invalidateImageRedirect( $this->mTitle
);
206 return parent
::doPurge();
210 * Get the categories this file is a member of on the wiki where it was uploaded.
211 * For local files, this is the same as getCategories().
212 * For foreign API files (InstantCommons), this is not supported currently.
213 * Results will include hidden categories.
215 * @return TitleArray|Title[]
218 public function getForeignCategories() {
220 $title = $this->mTitle
;
221 $file = $this->mFile
;
223 if ( !$file instanceof LocalFile
) {
224 wfDebug( __CLASS__
. '::' . __METHOD__
. " is not supported for this file\n" );
225 return TitleArray
::newFromResult( new FakeResultWrapper( [] ) );
228 /** @var LocalRepo $repo */
229 $repo = $file->getRepo();
230 $dbr = $repo->getReplicaDB();
233 [ 'page', 'categorylinks' ],
235 'page_title' => 'cl_to',
236 'page_namespace' => NS_CATEGORY
,
239 'page_namespace' => $title->getNamespace(),
240 'page_title' => $title->getDBkey(),
244 [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
247 return TitleArray
::newFromResult( $res );
254 public function getWikiDisplayName() {
255 return $this->getFile()->getRepo()->getDisplayName();
262 public function getSourceURL() {
263 return $this->getFile()->getDescriptionUrl();