Special case opus mime detction
[mediawiki.git] / includes / page / WikiFilePage.php
blob1fa4bfa9812fca752877343371d8b4e97747a0e7
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 /** @var File */
30 protected $mFile = false;
31 /** @var LocalRepo */
32 protected $mRepo = null;
33 /** @var bool */
34 protected $mFileLoaded = false;
35 /** @var array */
36 protected $mDupes = null;
38 public function __construct( $title ) {
39 parent::__construct( $title );
40 $this->mDupes = null;
41 $this->mRepo = null;
44 /**
45 * @param File $file
47 public function setFile( $file ) {
48 $this->mFile = $file;
49 $this->mFileLoaded = true;
52 /**
53 * @return bool
55 protected function loadFile() {
56 if ( $this->mFileLoaded ) {
57 return true;
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();
66 return true;
69 /**
70 * @return mixed|null|Title
72 public function getRedirectTarget() {
73 $this->loadFile();
74 if ( $this->mFile->isLocal() ) {
75 return parent::getRedirectTarget();
77 // Foreign image page
78 $from = $this->mFile->getRedirected();
79 $to = $this->mFile->getName();
80 if ( $from == $to ) {
81 return null;
83 $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
84 return $this->mRedirectTarget;
87 /**
88 * @return bool|mixed|Title
90 public function followRedirect() {
91 $this->loadFile();
92 if ( $this->mFile->isLocal() ) {
93 return parent::followRedirect();
95 $from = $this->mFile->getRedirected();
96 $to = $this->mFile->getName();
97 if ( $from == $to ) {
98 return false;
100 return Title::makeTitle( NS_FILE, $to );
104 * @return bool
106 public function isRedirect() {
107 $this->loadFile();
108 if ( $this->mFile->isLocal() ) {
109 return parent::isRedirect();
112 return (bool)$this->mFile->getRedirected();
116 * @return bool
118 public function isLocal() {
119 $this->loadFile();
120 return $this->mFile->isLocal();
124 * @return bool|File
126 public function getFile() {
127 $this->loadFile();
128 return $this->mFile;
132 * @return array|null
134 public function getDuplicates() {
135 $this->loadFile();
136 if ( !is_null( $this->mDupes ) ) {
137 return $this->mDupes;
139 $hash = $this->mFile->getSha1();
140 if ( !( $hash ) ) {
141 $this->mDupes = [];
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();
150 * @var $file File
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 ) {
166 $this->loadFile();
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 ] );
172 } else {
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[]
194 * @since 1.23
196 public function getForeignCategories() {
197 $this->loadFile();
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();
210 $res = $dbr->select(
211 [ 'page', 'categorylinks' ],
213 'page_title' => 'cl_to',
214 'page_namespace' => NS_CATEGORY,
217 'page_namespace' => $title->getNamespace(),
218 'page_title' => $title->getDBkey(),
220 __METHOD__,
222 [ 'categorylinks' => [ 'INNER JOIN', 'page_id = cl_from' ] ]
225 return TitleArray::newFromResult( $res );
229 * @since 1.28
230 * @return string
232 public function getWikiDisplayName() {
233 return $this->getFile()->getRepo()->getDisplayName();
237 * @since 1.28
238 * @return string
240 public function getSourceURL() {
241 return $this->getFile()->getDescriptionUrl();