MediaWiki master
WikiFilePage.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Page;
8
17use RuntimeException;
19
25class WikiFilePage extends WikiPage {
27 protected $mFile = false;
29 protected $mRepo = null;
31 protected $mFileLoaded = false;
33 protected $mDupes = null;
34
38 public function __construct( $title ) {
39 parent::__construct( $title );
40 $this->mDupes = null;
41 $this->mRepo = null;
42 }
43
44 public function setFile( File $file ) {
45 $this->mFile = $file;
46 $this->mFileLoaded = true;
47 }
48
52 protected function loadFile() {
54 if ( $this->mFileLoaded ) {
55 return true;
56 }
57
58 $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
59 if ( !$this->mFile ) {
60 $this->mFile = $services->getRepoGroup()->getLocalRepo()
61 ->newFile( $this->mTitle );
62 }
63
64 if ( !$this->mFile instanceof File ) {
65 throw new RuntimeException( 'Expected to find file. See T250767' );
66 }
67
68 $this->mRepo = $this->mFile->getRepo();
69 $this->mFileLoaded = true;
70 return true;
71 }
72
76 public function followRedirect() {
77 $this->loadFile();
78 if ( $this->mFile->isLocal() ) {
79 return parent::followRedirect();
80 }
81 $from = $this->mFile->getRedirected();
82 $to = $this->mFile->getName();
83 if ( $from === null || $from === $to ) {
84 return false;
85 }
86 return Title::makeTitle( NS_FILE, $to );
87 }
88
92 public function isRedirect() {
93 $this->loadFile();
94 if ( $this->mFile->isLocal() ) {
95 return parent::isRedirect();
96 }
97
98 return $this->mFile->getRedirected() !== null;
99 }
100
104 public function isLocal() {
105 $this->loadFile();
106 return $this->mFile->isLocal();
107 }
108
109 public function getFile(): File {
110 $this->loadFile();
111 return $this->mFile;
112 }
113
117 public function getDuplicates() {
118 $this->loadFile();
119 if ( $this->mDupes !== null ) {
120 return $this->mDupes;
121 }
122 $hash = $this->mFile->getSha1();
123 if ( !( $hash ) ) {
124 $this->mDupes = [];
125 return $this->mDupes;
126 }
127 $dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
128 // Remove duplicates with self and non matching file sizes
129 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
130 $size = $this->mFile->getSize();
131
135 foreach ( $dupes as $index => $file ) {
136 $key = $file->getRepoName() . ':' . $file->getName();
137 if ( $key === $self || $file->getSize() != $size ) {
138 unset( $dupes[$index] );
139 }
140 }
141 $this->mDupes = $dupes;
142 return $this->mDupes;
143 }
144
149 public function doPurge() {
150 $this->loadFile();
151
152 if ( $this->mFile->exists() ) {
153 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
154 $job = HTMLCacheUpdateJob::newForBacklinks(
155 $this->mTitle,
156 'imagelinks',
157 [ 'causeAction' => 'file-purge' ]
158 );
159 MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $job );
160 } else {
161 wfDebug( 'ImagePage::doPurge no image for '
162 . $this->mFile->getName() . "; limiting purge to cache only" );
163 }
164
165 // even if the file supposedly doesn't exist, force any cached information
166 // to be updated (in case the cached information is wrong)
167
168 // Purge current version and its thumbnails
169 $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
170
171 // Purge the old versions and their thumbnails
172 foreach ( $this->mFile->getHistory() as $oldFile ) {
173 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
174 }
175
176 if ( $this->mRepo ) {
177 // Purge redirect cache
178 $this->mRepo->invalidateImageRedirect( $this->mTitle );
179 }
180
181 return parent::doPurge();
182 }
183
193 public function getForeignCategories() {
194 $this->loadFile();
195 $title = $this->mTitle;
196 $file = $this->mFile;
197 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
198
199 if ( !$file instanceof LocalFile ) {
200 wfDebug( __METHOD__ . " is not supported for this file" );
201 return $titleFactory->newTitleArrayFromResult( new FakeResultWrapper( [] ) );
202 }
203
205 $repo = $file->getRepo();
206 $dbr = $repo->getReplicaDB();
207 $res = $dbr->newSelectQueryBuilder()
208 ->select( [ 'page_title' => 'lt_title', 'page_namespace' => (string)NS_CATEGORY ] )
209 ->from( 'page' )
210 ->join( 'categorylinks', null, 'page_id = cl_from' )
211 ->join( 'linktarget', null, 'cl_target_id = lt_id' )
212 ->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey(), ] )
213 ->caller( __METHOD__ )
214 ->fetchResultSet();
215
216 return $titleFactory->newTitleArrayFromResult( $res );
217 }
218
223 public function getWikiDisplayName() {
224 return $this->getFile()->getRepo()->getDisplayName();
225 }
226
231 public function getSourceURL() {
232 return $this->getFile()->getDescriptionUrl();
233 }
234
238 public function getActionOverrides() {
239 $file = $this->getFile();
240 if ( $file->exists() && $file->isLocal() && !$file->getRedirected() ) {
241 // Would be an actual file deletion
242 return [ 'delete' => FileDeleteAction::class ] + parent::getActionOverrides();
243 }
244 // It should use the normal article deletion interface
245 return parent::getActionOverrides();
246 }
247}
248
250class_alias( WikiFilePage::class, 'WikiFilePage' );
const NS_FILE
Definition Defines.php:57
const NS_CATEGORY
Definition Defines.php:65
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
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:79
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:45
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:81
Represents a title within MediaWiki.
Definition Title.php:69
Overloads the relevant methods of the real ResultWrapper so it doesn't go anywhere near an actual dat...
if(count( $args)< 1) $job