MediaWiki master
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
51 public function setFile( File $file ) {
52 $this->mFile = $file;
53 $this->mFileLoaded = true;
54 }
55
59 protected function loadFile() {
60 $services = MediaWikiServices::getInstance();
61 if ( $this->mFileLoaded ) {
62 return true;
63 }
64
65 $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
66 if ( !$this->mFile ) {
67 $this->mFile = $services->getRepoGroup()->getLocalRepo()
68 ->newFile( $this->mTitle );
69 }
70
71 if ( !$this->mFile instanceof File ) {
72 throw new RuntimeException( 'Expected to find file. See T250767' );
73 }
74
75 $this->mRepo = $this->mFile->getRepo();
76 $this->mFileLoaded = true;
77 return true;
78 }
79
83 public function followRedirect() {
84 $this->loadFile();
85 if ( $this->mFile->isLocal() ) {
86 return parent::followRedirect();
87 }
88 $from = $this->mFile->getRedirected();
89 $to = $this->mFile->getName();
90 if ( $from === null || $from === $to ) {
91 return false;
92 }
93 return Title::makeTitle( NS_FILE, $to );
94 }
95
99 public function isRedirect() {
100 $this->loadFile();
101 if ( $this->mFile->isLocal() ) {
102 return parent::isRedirect();
103 }
104
105 return $this->mFile->getRedirected() !== null;
106 }
107
111 public function isLocal() {
112 $this->loadFile();
113 return $this->mFile->isLocal();
114 }
115
116 public function getFile(): File {
117 $this->loadFile();
118 return $this->mFile;
119 }
120
124 public function getDuplicates() {
125 $this->loadFile();
126 if ( $this->mDupes !== null ) {
127 return $this->mDupes;
128 }
129 $hash = $this->mFile->getSha1();
130 if ( !( $hash ) ) {
131 $this->mDupes = [];
132 return $this->mDupes;
133 }
134 $dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
135 // Remove duplicates with self and non matching file sizes
136 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
137 $size = $this->mFile->getSize();
138
142 foreach ( $dupes as $index => $file ) {
143 $key = $file->getRepoName() . ':' . $file->getName();
144 if ( $key === $self || $file->getSize() != $size ) {
145 unset( $dupes[$index] );
146 }
147 }
148 $this->mDupes = $dupes;
149 return $this->mDupes;
150 }
151
156 public function doPurge() {
157 $this->loadFile();
158
159 if ( $this->mFile->exists() ) {
160 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
161 $job = HTMLCacheUpdateJob::newForBacklinks(
162 $this->mTitle,
163 'imagelinks',
164 [ 'causeAction' => 'file-purge' ]
165 );
166 MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $job );
167 } else {
168 wfDebug( 'ImagePage::doPurge no image for '
169 . $this->mFile->getName() . "; limiting purge to cache only" );
170 }
171
172 // even if the file supposedly doesn't exist, force any cached information
173 // to be updated (in case the cached information is wrong)
174
175 // Purge current version and its thumbnails
176 $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
177
178 // Purge the old versions and their thumbnails
179 foreach ( $this->mFile->getHistory() as $oldFile ) {
180 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
181 }
182
183 if ( $this->mRepo ) {
184 // Purge redirect cache
185 $this->mRepo->invalidateImageRedirect( $this->mTitle );
186 }
187
188 return parent::doPurge();
189 }
190
200 public function getForeignCategories() {
201 $this->loadFile();
202 $title = $this->mTitle;
203 $file = $this->mFile;
204 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
205
206 if ( !$file instanceof LocalFile ) {
207 wfDebug( __METHOD__ . " is not supported for this file" );
208 return $titleFactory->newTitleArrayFromResult( new FakeResultWrapper( [] ) );
209 }
210
212 $repo = $file->getRepo();
213 $dbr = $repo->getReplicaDB();
214
215 $res = $dbr->newSelectQueryBuilder()
216 ->select( [ 'page_title' => 'cl_to', 'page_namespace' => (string)NS_CATEGORY ] )
217 ->from( 'page' )
218 ->join( 'categorylinks', null, 'page_id = cl_from' )
219 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey(), ] )
220 ->caller( __METHOD__ )->fetchResultSet();
221
222 return $titleFactory->newTitleArrayFromResult( $res );
223 }
224
229 public function getWikiDisplayName() {
230 return $this->getFile()->getRepo()->getDisplayName();
231 }
232
237 public function getSourceURL() {
238 return $this->getFile()->getDescriptionUrl();
239 }
240
244 public function getActionOverrides() {
245 $file = $this->getFile();
246 if ( $file->exists() && $file->isLocal() && !$file->getRedirected() ) {
247 // Would be an actual file deletion
248 return [ 'delete' => FileDeleteAction::class ] + parent::getActionOverrides();
249 }
250 // It should use the normal article deletion interface
251 return parent::getActionOverrides();
252 }
253}
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:79
Local file in the wiki's own database.
Definition LocalFile.php:76
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:84
Overloads the relevant methods of the real ResultWrapper so it doesn't go anywhere near an actual dat...
if(count( $args)< 1) $job