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
{
32 protected $mFile = false; // !< File object
33 protected $mRepo = null; // !<
34 protected $mFileLoaded = false; // !<
35 protected $mDupes = null; // !<
37 public function __construct( $title ) {
38 parent
::__construct( $title );
43 public function getActionOverrides() {
44 $overrides = parent
::getActionOverrides();
45 $overrides['revert'] = 'RevertFileAction';
52 public function setFile( $file ) {
54 $this->mFileLoaded
= true;
60 protected function loadFile() {
61 if ( $this->mFileLoaded
) {
64 $this->mFileLoaded
= true;
66 $this->mFile
= wfFindFile( $this->mTitle
);
67 if ( !$this->mFile
) {
68 $this->mFile
= wfLocalFile( $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();
146 $this->mDupes
= array();
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() {
176 if ( $this->mFile
->exists() ) {
177 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile
->getName() . "\n" );
178 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
180 $this->mFile
->upgradeRow();
181 $this->mFile
->purgeCache( array( 'forThumbRefresh' => true ) );
183 wfDebug( 'ImagePage::doPurge no image for ' . $this->mFile
->getName() . "; limiting purge to cache only\n" );
184 // even if the file supposedly doesn't exist, force any cached information
185 // to be updated (in case the cached information is wrong)
186 $this->mFile
->purgeCache( array( 'forThumbRefresh' => true ) );
188 if ( $this->mRepo
) {
189 // Purge redirect cache
190 $this->mRepo
->invalidateImageRedirect( $this->mTitle
);
192 return parent
::doPurge();
196 * Get the categories this file is a member of on the wiki where it was uploaded.
197 * For local files, this is the same as getCategories().
198 * For foreign API files (InstantCommons), this is not supported currently.
199 * Results will include hidden categories.
201 * @return TitleArray|Title[]
204 public function getForeignCategories() {
206 $title = $this->mTitle
;
207 $file = $this->mFile
;
209 if ( ! $file instanceof LocalFile
) {
210 wfDebug( __CLASS__
. '::' . __METHOD__
. " is not supported for this file\n" );
211 return TitleArray
::newFromResult( new FakeResultWrapper( array() ) );
214 /** @var LocalRepo $repo */
215 $repo = $file->getRepo();
216 $dbr = $repo->getSlaveDB();
219 array( 'page', 'categorylinks' ),
221 'page_title' => 'cl_to',
222 'page_namespace' => NS_CATEGORY
,
225 'page_namespace' => $title->getNamespace(),
226 'page_title' => $title->getDBkey(),
230 array( 'categorylinks' => array( 'INNER JOIN', 'page_id = cl_from' ) )
233 return TitleArray
::newFromResult( $res );