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
{
33 protected $mFile = false;
35 protected $mRepo = null;
37 protected $mFileLoaded = false;
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 DeferredUpdates
::addUpdate(
180 new HTMLCacheUpdate( $this->mTitle
, 'imagelinks', 'file-purge' )
183 wfDebug( 'ImagePage::doPurge no image for '
184 . $this->mFile
->getName() . "; limiting purge to cache only\n" );
187 // even if the file supposedly doesn't exist, force any cached information
188 // to be updated (in case the cached information is wrong)
190 // Purge current version and its thumbnails
191 $this->mFile
->purgeCache( [ 'forThumbRefresh' => true ] );
193 // Purge the old versions and their thumbnails
194 foreach ( $this->mFile
->getHistory() as $oldFile ) {
195 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
198 if ( $this->mRepo
) {
199 // Purge redirect cache
200 $this->mRepo
->invalidateImageRedirect( $this->mTitle
);
203 return parent
::doPurge();
207 * Get the categories this file is a member of on the wiki where it was uploaded.
208 * For local files, this is the same as getCategories().
209 * For foreign API files (InstantCommons), this is not supported currently.
210 * Results will include hidden categories.
212 * @return TitleArray|Title[]
215 public function getForeignCategories() {
217 $title = $this->mTitle
;
218 $file = $this->mFile
;
220 if ( !$file instanceof LocalFile
) {
221 wfDebug( __CLASS__
. '::' . __METHOD__
. " is not supported for this file\n" );
222 return TitleArray
::newFromResult( new FakeResultWrapper( [] ) );
225 /** @var LocalRepo $repo */
226 $repo = $file->getRepo();
227 $dbr = $repo->getReplicaDB();
230 [ 'page', 'categorylinks' ],
232 'page_title' => 'cl_to',
233 'page_namespace' => NS_CATEGORY
,
236 'page_namespace' => $title->getNamespace(),
237 'page_title' => $title->getDBkey(),
241 [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
244 return TitleArray
::newFromResult( $res );
251 public function getWikiDisplayName() {
252 return $this->getFile()->getRepo()->getDisplayName();
259 public function getSourceURL() {
260 return $this->getFile()->getDescriptionUrl();