MediaWiki REL1_37
WikiFilePage.php
Go to the documentation of this file.
1<?php
25
31class WikiFilePage extends WikiPage {
33 protected $mFile = false;
35 protected $mRepo = null;
37 protected $mFileLoaded = false;
39 protected $mDupes = null;
40
41 public function __construct( $title ) {
42 parent::__construct( $title );
43 $this->mDupes = null;
44 $this->mRepo = null;
45 }
46
50 public function setFile( File $file ) {
51 $this->mFile = $file;
52 $this->mFileLoaded = true;
53 }
54
58 protected function loadFile() {
59 $services = MediaWikiServices::getInstance();
60 if ( $this->mFileLoaded ) {
61 return true;
62 }
63
64 $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
65 if ( !$this->mFile ) {
66 $this->mFile = $services->getRepoGroup()->getLocalRepo()
67 ->newFile( $this->mTitle );
68 }
69
70 if ( !$this->mFile instanceof File ) {
71 throw new RuntimeException( 'Expected to find file. See T250767' );
72 }
73
74 $this->mRepo = $this->mFile->getRepo();
75 $this->mFileLoaded = true;
76 return true;
77 }
78
82 public function getRedirectTarget() {
83 $this->loadFile();
84 if ( $this->mFile->isLocal() ) {
85 return parent::getRedirectTarget();
86 }
87 // Foreign image page
88 $from = $this->mFile->getRedirected();
89 $to = $this->mFile->getName();
90 if ( $from == $to ) {
91 return null;
92 }
93 $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
94 return $this->mRedirectTarget;
95 }
96
100 public function followRedirect() {
101 $this->loadFile();
102 if ( $this->mFile->isLocal() ) {
103 return parent::followRedirect();
104 }
105 $from = $this->mFile->getRedirected();
106 $to = $this->mFile->getName();
107 if ( $from == $to ) {
108 return false;
109 }
110 return Title::makeTitle( NS_FILE, $to );
111 }
112
116 public function isRedirect() {
117 $this->loadFile();
118 if ( $this->mFile->isLocal() ) {
119 return parent::isRedirect();
120 }
121
122 return (bool)$this->mFile->getRedirected();
123 }
124
128 public function isLocal() {
129 $this->loadFile();
130 return $this->mFile->isLocal();
131 }
132
136 public function getFile() {
137 $this->loadFile();
138 return $this->mFile;
139 }
140
144 public function getDuplicates() {
145 $this->loadFile();
146 if ( $this->mDupes !== null ) {
147 return $this->mDupes;
148 }
149 $hash = $this->mFile->getSha1();
150 if ( !( $hash ) ) {
151 $this->mDupes = [];
152 return $this->mDupes;
153 }
154 $dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
155 // Remove duplicates with self and non matching file sizes
156 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
157 $size = $this->mFile->getSize();
158
162 foreach ( $dupes as $index => $file ) {
163 $key = $file->getRepoName() . ':' . $file->getName();
164 if ( $key == $self ) {
165 unset( $dupes[$index] );
166 }
167 if ( $file->getSize() != $size ) {
168 unset( $dupes[$index] );
169 }
170 }
171 $this->mDupes = $dupes;
172 return $this->mDupes;
173 }
174
179 public function doPurge() {
180 $this->loadFile();
181
182 if ( $this->mFile->exists() ) {
183 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
185 $this->mTitle,
186 'imagelinks',
187 [ 'causeAction' => 'file-purge' ]
188 );
189 JobQueueGroup::singleton()->lazyPush( $job );
190 } else {
191 wfDebug( 'ImagePage::doPurge no image for '
192 . $this->mFile->getName() . "; limiting purge to cache only" );
193 }
194
195 // even if the file supposedly doesn't exist, force any cached information
196 // to be updated (in case the cached information is wrong)
197
198 // Purge current version and its thumbnails
199 $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
200
201 // Purge the old versions and their thumbnails
202 foreach ( $this->mFile->getHistory() as $oldFile ) {
203 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
204 }
205
206 if ( $this->mRepo ) {
207 // Purge redirect cache
208 $this->mRepo->invalidateImageRedirect( $this->mTitle );
209 }
210
211 return parent::doPurge();
212 }
213
223 public function getForeignCategories() {
224 $this->loadFile();
226 $file = $this->mFile;
227
228 if ( !$file instanceof LocalFile ) {
229 wfDebug( __METHOD__ . " is not supported for this file" );
231 }
232
234 $repo = $file->getRepo();
235 $dbr = $repo->getReplicaDB();
236
237 $res = $dbr->select(
238 [ 'page', 'categorylinks' ],
239 [
240 'page_title' => 'cl_to',
241 'page_namespace' => NS_CATEGORY,
242 ],
243 [
244 'page_namespace' => $title->getNamespace(),
245 'page_title' => $title->getDBkey(),
246 ],
247 __METHOD__,
248 [],
249 [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
250 );
251
253 }
254
259 public function getWikiDisplayName() {
260 return $this->getFile()->getRepo()->getDisplayName();
261 }
262
267 public function getSourceURL() {
268 return $this->getFile()->getDescriptionUrl();
269 }
270}
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:66
static newForBacklinks(PageReference $page, $table, $params=[])
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:63
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition LocalRepo.php:41
MediaWikiServices is the service locator for the application scope of MediaWiki.
static newFromResult( $res)
Special handling for file pages.
File false $mFile
array null $mDupes
__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
Class representing a MediaWiki article and history.
Definition WikiPage.php:60
Overloads the relevant methods of the real ResultWrapper so it doesn't go anywhere near an actual dat...
if(count( $args)< 1) $job
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42