MediaWiki 1.42.0
WikiFilePage.php
Go to the documentation of this file.
1<?php
26
32class WikiFilePage extends WikiPage {
34 protected $mFile = false;
36 protected $mRepo = null;
38 protected $mFileLoaded = false;
40 protected $mDupes = null;
41
45 public function __construct( $title ) {
46 parent::__construct( $title );
47 $this->mDupes = null;
48 $this->mRepo = null;
49 }
50
54 public function setFile( File $file ) {
55 $this->mFile = $file;
56 $this->mFileLoaded = true;
57 }
58
62 protected function loadFile() {
63 $services = MediaWikiServices::getInstance();
64 if ( $this->mFileLoaded ) {
65 return true;
66 }
67
68 $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
69 if ( !$this->mFile ) {
70 $this->mFile = $services->getRepoGroup()->getLocalRepo()
71 ->newFile( $this->mTitle );
72 }
73
74 if ( !$this->mFile instanceof File ) {
75 throw new RuntimeException( 'Expected to find file. See T250767' );
76 }
77
78 $this->mRepo = $this->mFile->getRepo();
79 $this->mFileLoaded = true;
80 return true;
81 }
82
86 public function getRedirectTarget() {
87 $this->loadFile();
88 if ( $this->mFile->isLocal() ) {
89 return parent::getRedirectTarget();
90 }
91 // Foreign image page
92 $from = $this->mFile->getRedirected();
93 $to = $this->mFile->getName();
94 if ( $from === null || $from === $to ) {
95 return null;
96 }
97 $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
98 return $this->mRedirectTarget;
99 }
100
104 public function followRedirect() {
105 $this->loadFile();
106 if ( $this->mFile->isLocal() ) {
107 return parent::followRedirect();
108 }
109 $from = $this->mFile->getRedirected();
110 $to = $this->mFile->getName();
111 if ( $from === null || $from === $to ) {
112 return false;
113 }
114 return Title::makeTitle( NS_FILE, $to );
115 }
116
120 public function isRedirect() {
121 $this->loadFile();
122 if ( $this->mFile->isLocal() ) {
123 return parent::isRedirect();
124 }
125
126 return $this->mFile->getRedirected() !== null;
127 }
128
132 public function isLocal() {
133 $this->loadFile();
134 return $this->mFile->isLocal();
135 }
136
140 public function getFile(): File {
141 $this->loadFile();
142 return $this->mFile;
143 }
144
148 public function getDuplicates() {
149 $this->loadFile();
150 if ( $this->mDupes !== null ) {
151 return $this->mDupes;
152 }
153 $hash = $this->mFile->getSha1();
154 if ( !( $hash ) ) {
155 $this->mDupes = [];
156 return $this->mDupes;
157 }
158 $dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
159 // Remove duplicates with self and non matching file sizes
160 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
161 $size = $this->mFile->getSize();
162
166 foreach ( $dupes as $index => $file ) {
167 $key = $file->getRepoName() . ':' . $file->getName();
168 if ( $key === $self || $file->getSize() != $size ) {
169 unset( $dupes[$index] );
170 }
171 }
172 $this->mDupes = $dupes;
173 return $this->mDupes;
174 }
175
180 public function doPurge() {
181 $this->loadFile();
182
183 if ( $this->mFile->exists() ) {
184 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
185 $job = HTMLCacheUpdateJob::newForBacklinks(
186 $this->mTitle,
187 'imagelinks',
188 [ 'causeAction' => 'file-purge' ]
189 );
190 MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $job );
191 } else {
192 wfDebug( 'ImagePage::doPurge no image for '
193 . $this->mFile->getName() . "; limiting purge to cache only" );
194 }
195
196 // even if the file supposedly doesn't exist, force any cached information
197 // to be updated (in case the cached information is wrong)
198
199 // Purge current version and its thumbnails
200 $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
201
202 // Purge the old versions and their thumbnails
203 foreach ( $this->mFile->getHistory() as $oldFile ) {
204 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
205 }
206
207 if ( $this->mRepo ) {
208 // Purge redirect cache
209 $this->mRepo->invalidateImageRedirect( $this->mTitle );
210 }
211
212 return parent::doPurge();
213 }
214
224 public function getForeignCategories() {
225 $this->loadFile();
226 $title = $this->mTitle;
227 $file = $this->mFile;
228 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
229
230 if ( !$file instanceof LocalFile ) {
231 wfDebug( __METHOD__ . " is not supported for this file" );
232 return $titleFactory->newTitleArrayFromResult( new FakeResultWrapper( [] ) );
233 }
234
236 $repo = $file->getRepo();
237 $dbr = $repo->getReplicaDB();
238
239 $res = $dbr->newSelectQueryBuilder()
240 ->select( [ 'page_title' => 'cl_to', 'page_namespace' => (string)NS_CATEGORY ] )
241 ->from( 'page' )
242 ->join( 'categorylinks', null, 'page_id = cl_from' )
243 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey(), ] )
244 ->caller( __METHOD__ )->fetchResultSet();
245
246 return $titleFactory->newTitleArrayFromResult( $res );
247 }
248
253 public function getWikiDisplayName() {
254 return $this->getFile()->getRepo()->getDisplayName();
255 }
256
261 public function getSourceURL() {
262 return $this->getFile()->getDescriptionUrl();
263 }
264
268 public function getActionOverrides() {
269 $file = $this->getFile();
270 if ( $file->exists() && $file->isLocal() && !$file->getRedirected() ) {
271 // Would be an actual file deletion
272 return [ 'delete' => FileDeleteAction::class ] + parent::getActionOverrides();
273 }
274 // It should use the normal article deletion interface
275 return parent::getActionOverrides();
276 }
277}
const NS_FILE
Definition Defines.php:70
const NS_CATEGORY
Definition Defines.php:78
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Title null $mTitle
getFile()
Get the file for this page, if one exists.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:73
Local file in the wiki's own database.
Definition LocalFile.php:68
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:49
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:78
Special handling for representing file pages.
File false $mFile
array null $mDupes
getActionOverrides()
Move this UI stuff somewhere elseContentHandler::getActionOverrides array
__construct( $title)
doPurge()
Override handling of action=purge.
getForeignCategories()
Get the categories this file is a member of on the wiki where it was uploaded.
setFile(File $file)
LocalRepo null $mRepo
Base representation for an editable wiki page.
Definition WikiPage.php:79
Overloads the relevant methods of the real ResultWrapper so it doesn't go anywhere near an actual dat...
if(count( $args)< 1) $job