Remove 4 unused live preview messages
[mediawiki.git] / includes / WikiFilePage.php
blob44f05995915c5667382ddd3f9a013049dfdb22d1
1 <?php
2 /**
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
20 * @file
23 /**
24 * Special handling for file pages
26 * @ingroup Media
28 class WikiFilePage extends WikiPage {
29 /**
30 * @var File
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 );
39 $this->mDupes = null;
40 $this->mRepo = null;
43 public function getActionOverrides() {
44 $overrides = parent::getActionOverrides();
45 $overrides['revert'] = 'RevertFileAction';
46 return $overrides;
49 /**
50 * @param File $file
52 public function setFile( $file ) {
53 $this->mFile = $file;
54 $this->mFileLoaded = true;
57 /**
58 * @return bool
60 protected function loadFile() {
61 if ( $this->mFileLoaded ) {
62 return true;
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();
71 return true;
74 /**
75 * @return mixed|null|Title
77 public function getRedirectTarget() {
78 $this->loadFile();
79 if ( $this->mFile->isLocal() ) {
80 return parent::getRedirectTarget();
82 // Foreign image page
83 $from = $this->mFile->getRedirected();
84 $to = $this->mFile->getName();
85 if ( $from == $to ) {
86 return null;
88 $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
89 return $this->mRedirectTarget;
92 /**
93 * @return bool|mixed|Title
95 public function followRedirect() {
96 $this->loadFile();
97 if ( $this->mFile->isLocal() ) {
98 return parent::followRedirect();
100 $from = $this->mFile->getRedirected();
101 $to = $this->mFile->getName();
102 if ( $from == $to ) {
103 return false;
105 return Title::makeTitle( NS_FILE, $to );
109 * @return bool
111 public function isRedirect() {
112 $this->loadFile();
113 if ( $this->mFile->isLocal() ) {
114 return parent::isRedirect();
117 return (bool)$this->mFile->getRedirected();
121 * @return bool
123 public function isLocal() {
124 $this->loadFile();
125 return $this->mFile->isLocal();
129 * @return bool|File
131 public function getFile() {
132 $this->loadFile();
133 return $this->mFile;
137 * @return array|null
139 public function getDuplicates() {
140 $this->loadFile();
141 if ( !is_null( $this->mDupes ) ) {
142 return $this->mDupes;
144 $hash = $this->mFile->getSha1();
145 if ( !( $hash ) ) {
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();
155 * @var $file File
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
172 * @return bool
174 public function doPurge() {
175 $this->loadFile();
176 if ( $this->mFile->exists() ) {
177 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
178 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
179 $update->doUpdate();
180 $this->mFile->upgradeRow();
181 $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
182 } else {
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[]
202 * @since 1.23
204 public function getForeignCategories() {
205 $this->loadFile();
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();
218 $res = $dbr->select(
219 array( 'page', 'categorylinks' ),
220 array(
221 'page_title' => 'cl_to',
222 'page_namespace' => NS_CATEGORY,
224 array(
225 'page_namespace' => $title->getNamespace(),
226 'page_title' => $title->getDBkey(),
228 __METHOD__,
229 array(),
230 array( 'categorylinks' => array( 'INNER JOIN', 'page_id = cl_from' ) )
233 return TitleArray::newFromResult( $res );