MediaWiki master
WikiFilePage.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Page;
22
31use RuntimeException;
33
39class WikiFilePage extends WikiPage {
41 protected $mFile = false;
43 protected $mRepo = null;
45 protected $mFileLoaded = false;
47 protected $mDupes = null;
48
52 public function __construct( $title ) {
53 parent::__construct( $title );
54 $this->mDupes = null;
55 $this->mRepo = null;
56 }
57
58 public function setFile( File $file ) {
59 $this->mFile = $file;
60 $this->mFileLoaded = true;
61 }
62
66 protected function loadFile() {
68 if ( $this->mFileLoaded ) {
69 return true;
70 }
71
72 $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
73 if ( !$this->mFile ) {
74 $this->mFile = $services->getRepoGroup()->getLocalRepo()
75 ->newFile( $this->mTitle );
76 }
77
78 if ( !$this->mFile instanceof File ) {
79 throw new RuntimeException( 'Expected to find file. See T250767' );
80 }
81
82 $this->mRepo = $this->mFile->getRepo();
83 $this->mFileLoaded = true;
84 return true;
85 }
86
90 public function followRedirect() {
91 $this->loadFile();
92 if ( $this->mFile->isLocal() ) {
93 return parent::followRedirect();
94 }
95 $from = $this->mFile->getRedirected();
96 $to = $this->mFile->getName();
97 if ( $from === null || $from === $to ) {
98 return false;
99 }
100 return Title::makeTitle( NS_FILE, $to );
101 }
102
106 public function isRedirect() {
107 $this->loadFile();
108 if ( $this->mFile->isLocal() ) {
109 return parent::isRedirect();
110 }
111
112 return $this->mFile->getRedirected() !== null;
113 }
114
118 public function isLocal() {
119 $this->loadFile();
120 return $this->mFile->isLocal();
121 }
122
123 public function getFile(): File {
124 $this->loadFile();
125 return $this->mFile;
126 }
127
131 public function getDuplicates() {
132 $this->loadFile();
133 if ( $this->mDupes !== null ) {
134 return $this->mDupes;
135 }
136 $hash = $this->mFile->getSha1();
137 if ( !( $hash ) ) {
138 $this->mDupes = [];
139 return $this->mDupes;
140 }
141 $dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
142 // Remove duplicates with self and non matching file sizes
143 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
144 $size = $this->mFile->getSize();
145
149 foreach ( $dupes as $index => $file ) {
150 $key = $file->getRepoName() . ':' . $file->getName();
151 if ( $key === $self || $file->getSize() != $size ) {
152 unset( $dupes[$index] );
153 }
154 }
155 $this->mDupes = $dupes;
156 return $this->mDupes;
157 }
158
163 public function doPurge() {
164 $this->loadFile();
165
166 if ( $this->mFile->exists() ) {
167 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
168 $job = HTMLCacheUpdateJob::newForBacklinks(
169 $this->mTitle,
170 'imagelinks',
171 [ 'causeAction' => 'file-purge' ]
172 );
173 MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $job );
174 } else {
175 wfDebug( 'ImagePage::doPurge no image for '
176 . $this->mFile->getName() . "; limiting purge to cache only" );
177 }
178
179 // even if the file supposedly doesn't exist, force any cached information
180 // to be updated (in case the cached information is wrong)
181
182 // Purge current version and its thumbnails
183 $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
184
185 // Purge the old versions and their thumbnails
186 foreach ( $this->mFile->getHistory() as $oldFile ) {
187 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
188 }
189
190 if ( $this->mRepo ) {
191 // Purge redirect cache
192 $this->mRepo->invalidateImageRedirect( $this->mTitle );
193 }
194
195 return parent::doPurge();
196 }
197
207 public function getForeignCategories() {
208 $this->loadFile();
209 $title = $this->mTitle;
210 $file = $this->mFile;
211 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
212
213 if ( !$file instanceof LocalFile ) {
214 wfDebug( __METHOD__ . " is not supported for this file" );
215 return $titleFactory->newTitleArrayFromResult( new FakeResultWrapper( [] ) );
216 }
217
219 $repo = $file->getRepo();
220 $dbr = $repo->getReplicaDB();
221
222 $res = $dbr->newSelectQueryBuilder()
223 ->select( [ 'page_title' => 'cl_to', 'page_namespace' => (string)NS_CATEGORY ] )
224 ->from( 'page' )
225 ->join( 'categorylinks', null, 'page_id = cl_from' )
226 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey(), ] )
227 ->caller( __METHOD__ )->fetchResultSet();
228
229 return $titleFactory->newTitleArrayFromResult( $res );
230 }
231
236 public function getWikiDisplayName() {
237 return $this->getFile()->getRepo()->getDisplayName();
238 }
239
244 public function getSourceURL() {
245 return $this->getFile()->getDescriptionUrl();
246 }
247
251 public function getActionOverrides() {
252 $file = $this->getFile();
253 if ( $file->exists() && $file->isLocal() && !$file->getRedirected() ) {
254 // Would be an actual file deletion
255 return [ 'delete' => FileDeleteAction::class ] + parent::getActionOverrides();
256 }
257 // It should use the normal article deletion interface
258 return parent::getActionOverrides();
259 }
260}
261
263class_alias( WikiFilePage::class, 'WikiFilePage' );
const NS_FILE
Definition Defines.php:71
const NS_CATEGORY
Definition Defines.php:79
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:93
Local file in the wiki's own database.
Definition LocalFile.php:93
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:57
Job to purge the HTML/file cache for all pages that link to or use another page or file.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Special handling for representing file pages.
doPurge()
Override handling of action=purge.
getForeignCategories()
Get the categories this file is a member of on the wiki where it was uploaded.
getActionOverrides()
Move this UI stuff somewhere elseContentHandler::getActionOverrides array
Base representation for an editable wiki page.
Definition WikiPage.php:94
Represents a title within MediaWiki.
Definition Title.php:78
Overloads the relevant methods of the real ResultWrapper so it doesn't go anywhere near an actual dat...
if(count( $args)< 1) $job