Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / page / WikiFilePage.php
1 <?php
2 /**
3 * Special handling for file pages.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24 use Wikimedia\Rdbms\FakeResultWrapper;
25
26 /**
27 * Special handling for file pages
28 *
29 * @ingroup Media
30 */
31 class WikiFilePage extends WikiPage {
32 /** @var File|false */
33 protected $mFile = false;
34 /** @var LocalRepo|null */
35 protected $mRepo = null;
36 /** @var bool */
37 protected $mFileLoaded = false;
38 /** @var array|null */
39 protected $mDupes = null;
40
41 public function __construct( $title ) {
42 parent::__construct( $title );
43 $this->mDupes = null;
44 $this->mRepo = null;
45 }
46
47 /**
48 * @param File $file
49 */
50 public function setFile( $file ) {
51 $this->mFile = $file;
52 $this->mFileLoaded = true;
53 }
54
55 /**
56 * @return bool
57 */
58 protected function loadFile() {
59 $services = MediaWikiServices::getInstance();
60 if ( $this->mFileLoaded ) {
61 return true;
62 }
63 $this->mFileLoaded = true;
64
65 $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
66 if ( !$this->mFile ) {
67 $this->mFile = $services->getRepoGroup()->getLocalRepo()
68 ->newFile( $this->mTitle ); // always a File
69 }
70 $this->mRepo = $this->mFile->getRepo();
71 return true;
72 }
73
74 /**
75 * @return mixed|null|Title
76 */
77 public function getRedirectTarget() {
78 $this->loadFile();
79 if ( $this->mFile->isLocal() ) {
80 return parent::getRedirectTarget();
81 }
82 // Foreign image page
83 $from = $this->mFile->getRedirected();
84 $to = $this->mFile->getName();
85 if ( $from == $to ) {
86 return null;
87 }
88 $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
89 return $this->mRedirectTarget;
90 }
91
92 /**
93 * @return bool|mixed|Title
94 */
95 public function followRedirect() {
96 $this->loadFile();
97 if ( $this->mFile->isLocal() ) {
98 return parent::followRedirect();
99 }
100 $from = $this->mFile->getRedirected();
101 $to = $this->mFile->getName();
102 if ( $from == $to ) {
103 return false;
104 }
105 return Title::makeTitle( NS_FILE, $to );
106 }
107
108 /**
109 * @return bool
110 */
111 public function isRedirect() {
112 $this->loadFile();
113 if ( $this->mFile->isLocal() ) {
114 return parent::isRedirect();
115 }
116
117 return (bool)$this->mFile->getRedirected();
118 }
119
120 /**
121 * @return bool
122 */
123 public function isLocal() {
124 $this->loadFile();
125 return $this->mFile->isLocal();
126 }
127
128 /**
129 * @return bool|File
130 */
131 public function getFile() {
132 $this->loadFile();
133 return $this->mFile;
134 }
135
136 /**
137 * @return array|null
138 */
139 public function getDuplicates() {
140 $this->loadFile();
141 if ( !is_null( $this->mDupes ) ) {
142 return $this->mDupes;
143 }
144 $hash = $this->mFile->getSha1();
145 if ( !( $hash ) ) {
146 $this->mDupes = [];
147 return $this->mDupes;
148 }
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();
153
154 /**
155 * @var File $file
156 */
157 foreach ( $dupes as $index => $file ) {
158 $key = $file->getRepoName() . ':' . $file->getName();
159 if ( $key == $self ) {
160 unset( $dupes[$index] );
161 }
162 if ( $file->getSize() != $size ) {
163 unset( $dupes[$index] );
164 }
165 }
166 $this->mDupes = $dupes;
167 return $this->mDupes;
168 }
169
170 /**
171 * Override handling of action=purge
172 * @return bool
173 */
174 public function doPurge() {
175 $this->loadFile();
176
177 if ( $this->mFile->exists() ) {
178 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
179 DeferredUpdates::addUpdate(
180 new HTMLCacheUpdate( $this->mTitle, 'imagelinks', 'file-purge' )
181 );
182 } else {
183 wfDebug( 'ImagePage::doPurge no image for '
184 . $this->mFile->getName() . "; limiting purge to cache only\n" );
185 }
186
187 // even if the file supposedly doesn't exist, force any cached information
188 // to be updated (in case the cached information is wrong)
189
190 // Purge current version and its thumbnails
191 $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
192
193 // Purge the old versions and their thumbnails
194 foreach ( $this->mFile->getHistory() as $oldFile ) {
195 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
196 }
197
198 if ( $this->mRepo ) {
199 // Purge redirect cache
200 $this->mRepo->invalidateImageRedirect( $this->mTitle );
201 }
202
203 return parent::doPurge();
204 }
205
206 /**
207 * Get the categories this file is a member of on the wiki where it was uploaded.
208 * For local files, this is the same as getCategories().
209 * For foreign API files (InstantCommons), this is not supported currently.
210 * Results will include hidden categories.
211 *
212 * @return TitleArray|Title[]
213 * @since 1.23
214 */
215 public function getForeignCategories() {
216 $this->loadFile();
217 $title = $this->mTitle;
218 $file = $this->mFile;
219
220 if ( !$file instanceof LocalFile ) {
221 wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
222 return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
223 }
224
225 /** @var LocalRepo $repo */
226 $repo = $file->getRepo();
227 $dbr = $repo->getReplicaDB();
228
229 $res = $dbr->select(
230 [ 'page', 'categorylinks' ],
231 [
232 'page_title' => 'cl_to',
233 'page_namespace' => NS_CATEGORY,
234 ],
235 [
236 'page_namespace' => $title->getNamespace(),
237 'page_title' => $title->getDBkey(),
238 ],
239 __METHOD__,
240 [],
241 [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
242 );
243
244 return TitleArray::newFromResult( $res );
245 }
246
247 /**
248 * @since 1.28
249 * @return string
250 */
251 public function getWikiDisplayName() {
252 return $this->getFile()->getRepo()->getDisplayName();
253 }
254
255 /**
256 * @since 1.28
257 * @return string
258 */
259 public function getSourceURL() {
260 return $this->getFile()->getDescriptionUrl();
261 }
262 }