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 Wikimedia\Rdbms\FakeResultWrapper
;
26 * Special handling for file pages
30 class WikiFilePage
extends WikiPage
{
32 protected $mFile = false;
34 protected $mRepo = null;
36 protected $mFileLoaded = false;
38 protected $mDupes = null;
40 public function __construct( $title ) {
41 parent
::__construct( $title );
49 public function setFile( $file ) {
51 $this->mFileLoaded
= true;
57 protected function loadFile() {
58 if ( $this->mFileLoaded
) {
61 $this->mFileLoaded
= true;
63 $this->mFile
= wfFindFile( $this->mTitle
);
64 if ( !$this->mFile
) {
65 $this->mFile
= wfLocalFile( $this->mTitle
); // always a File
67 $this->mRepo
= $this->mFile
->getRepo();
72 * @return mixed|null|Title
74 public function getRedirectTarget() {
76 if ( $this->mFile
->isLocal() ) {
77 return parent
::getRedirectTarget();
80 $from = $this->mFile
->getRedirected();
81 $to = $this->mFile
->getName();
85 $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
86 return $this->mRedirectTarget
;
90 * @return bool|mixed|Title
92 public function followRedirect() {
94 if ( $this->mFile
->isLocal() ) {
95 return parent
::followRedirect();
97 $from = $this->mFile
->getRedirected();
98 $to = $this->mFile
->getName();
102 return Title
::makeTitle( NS_FILE
, $to );
108 public function isRedirect() {
110 if ( $this->mFile
->isLocal() ) {
111 return parent
::isRedirect();
114 return (bool)$this->mFile
->getRedirected();
120 public function isLocal() {
122 return $this->mFile
->isLocal();
128 public function getFile() {
136 public function getDuplicates() {
138 if ( !is_null( $this->mDupes
) ) {
139 return $this->mDupes
;
141 $hash = $this->mFile
->getSha1();
144 return $this->mDupes
;
146 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
147 // Remove duplicates with self and non matching file sizes
148 $self = $this->mFile
->getRepoName() . ':' . $this->mFile
->getName();
149 $size = $this->mFile
->getSize();
154 foreach ( $dupes as $index => $file ) {
155 $key = $file->getRepoName() . ':' . $file->getName();
156 if ( $key == $self ) {
157 unset( $dupes[$index] );
159 if ( $file->getSize() != $size ) {
160 unset( $dupes[$index] );
163 $this->mDupes
= $dupes;
164 return $this->mDupes
;
168 * Override handling of action=purge
171 public function doPurge() {
174 if ( $this->mFile
->exists() ) {
175 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile
->getName() . "\n" );
176 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' ) );
178 wfDebug( 'ImagePage::doPurge no image for '
179 . $this->mFile
->getName() . "; limiting purge to cache only\n" );
182 // even if the file supposedly doesn't exist, force any cached information
183 // to be updated (in case the cached information is wrong)
185 // Purge current version and its thumbnails
186 $this->mFile
->purgeCache( [ 'forThumbRefresh' => true ] );
188 // Purge the old versions and their thumbnails
189 foreach ( $this->mFile
->getHistory() as $oldFile ) {
190 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
193 if ( $this->mRepo
) {
194 // Purge redirect cache
195 $this->mRepo
->invalidateImageRedirect( $this->mTitle
);
198 return parent
::doPurge();
202 * Get the categories this file is a member of on the wiki where it was uploaded.
203 * For local files, this is the same as getCategories().
204 * For foreign API files (InstantCommons), this is not supported currently.
205 * Results will include hidden categories.
207 * @return TitleArray|Title[]
210 public function getForeignCategories() {
212 $title = $this->mTitle
;
213 $file = $this->mFile
;
215 if ( !$file instanceof LocalFile
) {
216 wfDebug( __CLASS__
. '::' . __METHOD__
. " is not supported for this file\n" );
217 return TitleArray
::newFromResult( new FakeResultWrapper( [] ) );
220 /** @var LocalRepo $repo */
221 $repo = $file->getRepo();
222 $dbr = $repo->getReplicaDB();
225 [ 'page', 'categorylinks' ],
227 'page_title' => 'cl_to',
228 'page_namespace' => NS_CATEGORY
,
231 'page_namespace' => $title->getNamespace(),
232 'page_title' => $title->getDBkey(),
236 [ 'categorylinks' => [ 'INNER JOIN', 'page_id = cl_from' ] ]
239 return TitleArray
::newFromResult( $res );
246 public function getWikiDisplayName() {
247 return $this->getFile()->getRepo()->getDisplayName();
254 public function getSourceURL() {
255 return $this->getFile()->getDescriptionUrl();