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
24 * Special handling for file pages
28 class WikiFilePage
extends WikiPage
{
30 protected $mFile = false;
32 protected $mRepo = null;
34 protected $mFileLoaded = false;
36 protected $mDupes = null;
38 public function __construct( $title ) {
39 parent
::__construct( $title );
47 public function setFile( $file ) {
49 $this->mFileLoaded
= true;
55 protected function loadFile() {
56 if ( $this->mFileLoaded
) {
59 $this->mFileLoaded
= true;
61 $this->mFile
= wfFindFile( $this->mTitle
);
62 if ( !$this->mFile
) {
63 $this->mFile
= wfLocalFile( $this->mTitle
); // always a File
65 $this->mRepo
= $this->mFile
->getRepo();
70 * @return mixed|null|Title
72 public function getRedirectTarget() {
74 if ( $this->mFile
->isLocal() ) {
75 return parent
::getRedirectTarget();
78 $from = $this->mFile
->getRedirected();
79 $to = $this->mFile
->getName();
83 $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
84 return $this->mRedirectTarget
;
88 * @return bool|mixed|Title
90 public function followRedirect() {
92 if ( $this->mFile
->isLocal() ) {
93 return parent
::followRedirect();
95 $from = $this->mFile
->getRedirected();
96 $to = $this->mFile
->getName();
100 return Title
::makeTitle( NS_FILE
, $to );
106 public function isRedirect() {
108 if ( $this->mFile
->isLocal() ) {
109 return parent
::isRedirect();
112 return (bool)$this->mFile
->getRedirected();
118 public function isLocal() {
120 return $this->mFile
->isLocal();
126 public function getFile() {
134 public function getDuplicates() {
136 if ( !is_null( $this->mDupes
) ) {
137 return $this->mDupes
;
139 $hash = $this->mFile
->getSha1();
142 return $this->mDupes
;
144 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
145 // Remove duplicates with self and non matching file sizes
146 $self = $this->mFile
->getRepoName() . ':' . $this->mFile
->getName();
147 $size = $this->mFile
->getSize();
152 foreach ( $dupes as $index => $file ) {
153 $key = $file->getRepoName() . ':' . $file->getName();
154 if ( $key == $self ) {
155 unset( $dupes[$index] );
157 if ( $file->getSize() != $size ) {
158 unset( $dupes[$index] );
161 $this->mDupes
= $dupes;
162 return $this->mDupes
;
165 public function doPurge( $flags = self
::PURGE_ALL
) {
168 if ( $this->mFile
->exists() ) {
169 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile
->getName() . "\n" );
170 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' ) );
171 $this->mFile
->purgeCache( [ 'forThumbRefresh' => true ] );
173 wfDebug( 'ImagePage::doPurge no image for '
174 . $this->mFile
->getName() . "; limiting purge to cache only\n" );
175 // even if the file supposedly doesn't exist, force any cached information
176 // to be updated (in case the cached information is wrong)
177 $this->mFile
->purgeCache( [ 'forThumbRefresh' => true ] );
179 if ( $this->mRepo
) {
180 // Purge redirect cache
181 $this->mRepo
->invalidateImageRedirect( $this->mTitle
);
184 return parent
::doPurge( $flags );
188 * Get the categories this file is a member of on the wiki where it was uploaded.
189 * For local files, this is the same as getCategories().
190 * For foreign API files (InstantCommons), this is not supported currently.
191 * Results will include hidden categories.
193 * @return TitleArray|Title[]
196 public function getForeignCategories() {
198 $title = $this->mTitle
;
199 $file = $this->mFile
;
201 if ( !$file instanceof LocalFile
) {
202 wfDebug( __CLASS__
. '::' . __METHOD__
. " is not supported for this file\n" );
203 return TitleArray
::newFromResult( new FakeResultWrapper( [] ) );
206 /** @var LocalRepo $repo */
207 $repo = $file->getRepo();
208 $dbr = $repo->getReplicaDB();
211 [ 'page', 'categorylinks' ],
213 'page_title' => 'cl_to',
214 'page_namespace' => NS_CATEGORY
,
217 'page_namespace' => $title->getNamespace(),
218 'page_title' => $title->getDBkey(),
222 [ 'categorylinks' => [ 'INNER JOIN', 'page_id = cl_from' ] ]
225 return TitleArray
::newFromResult( $res );
232 public function getWikiDisplayName() {
233 return $this->getFile()->getRepo()->getDisplayName();
240 public function getSourceURL() {
241 return $this->getFile()->getDescriptionUrl();