MediaWiki REL1_34
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 ) {
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 $this->mFileLoaded = true;
64
65 $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
66 if ( !$this->mFile ) {
67 $this->mFile = $services->getRepoGroup()->getLocalRepo()
68 ->newFile( $this->mTitle ); // always a File
69 }
70 $this->mRepo = $this->mFile->getRepo();
71 return true;
72 }
73
77 public function getRedirectTarget() {
78 $this->loadFile();
79 if ( $this->mFile->isLocal() ) {
80 return parent::getRedirectTarget();
81 }
82 // Foreign image page
83 $from = $this->mFile->getRedirected();
84 $to = $this->mFile->getName();
85 if ( $from == $to ) {
86 return null;
87 }
88 $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
90 }
91
95 public function followRedirect() {
96 $this->loadFile();
97 if ( $this->mFile->isLocal() ) {
98 return parent::followRedirect();
99 }
100 $from = $this->mFile->getRedirected();
101 $to = $this->mFile->getName();
102 if ( $from == $to ) {
103 return false;
104 }
105 return Title::makeTitle( NS_FILE, $to );
106 }
107
111 public function isRedirect() {
112 $this->loadFile();
113 if ( $this->mFile->isLocal() ) {
114 return parent::isRedirect();
115 }
116
117 return (bool)$this->mFile->getRedirected();
118 }
119
123 public function isLocal() {
124 $this->loadFile();
125 return $this->mFile->isLocal();
126 }
127
131 public function getFile() {
132 $this->loadFile();
133 return $this->mFile;
134 }
135
139 public function getDuplicates() {
140 $this->loadFile();
141 if ( !is_null( $this->mDupes ) ) {
142 return $this->mDupes;
143 }
144 $hash = $this->mFile->getSha1();
145 if ( !( $hash ) ) {
146 $this->mDupes = [];
147 return $this->mDupes;
148 }
149 $dupes = RepoGroup::singleton()->findBySha1( $hash );
150 // Remove duplicates with self and non matching file sizes
151 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
152 $size = $this->mFile->getSize();
153
157 foreach ( $dupes as $index => $file ) {
158 $key = $file->getRepoName() . ':' . $file->getName();
159 if ( $key == $self ) {
160 unset( $dupes[$index] );
161 }
162 if ( $file->getSize() != $size ) {
163 unset( $dupes[$index] );
164 }
165 }
166 $this->mDupes = $dupes;
167 return $this->mDupes;
168 }
169
174 public function doPurge() {
175 $this->loadFile();
176
177 if ( $this->mFile->exists() ) {
178 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
180 $this->mTitle,
181 'imagelinks',
182 [ 'causeAction' => 'file-purge' ]
183 );
184 JobQueueGroup::singleton()->lazyPush( $job );
185 } else {
186 wfDebug( 'ImagePage::doPurge no image for '
187 . $this->mFile->getName() . "; limiting purge to cache only\n" );
188 }
189
190 // even if the file supposedly doesn't exist, force any cached information
191 // to be updated (in case the cached information is wrong)
192
193 // Purge current version and its thumbnails
194 $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
195
196 // Purge the old versions and their thumbnails
197 foreach ( $this->mFile->getHistory() as $oldFile ) {
198 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
199 }
200
201 if ( $this->mRepo ) {
202 // Purge redirect cache
203 $this->mRepo->invalidateImageRedirect( $this->mTitle );
204 }
205
206 return parent::doPurge();
207 }
208
218 public function getForeignCategories() {
219 $this->loadFile();
222
223 if ( !$file instanceof LocalFile ) {
224 wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
226 }
227
229 $repo = $file->getRepo();
230 $dbr = $repo->getReplicaDB();
231
232 $res = $dbr->select(
233 [ 'page', 'categorylinks' ],
234 [
235 'page_title' => 'cl_to',
236 'page_namespace' => NS_CATEGORY,
237 ],
238 [
239 'page_namespace' => $title->getNamespace(),
240 'page_title' => $title->getDBkey(),
241 ],
242 __METHOD__,
243 [],
244 [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
245 );
246
248 }
249
254 public function getWikiDisplayName() {
255 return $this->getFile()->getRepo()->getDisplayName();
256 }
257
262 public function getSourceURL() {
263 return $this->getFile()->getDescriptionUrl();
264 }
265}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:61
static newForBacklinks(Title $title, $table, $params=[])
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:56
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition LocalRepo.php:37
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
setFile( $file)
__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.
LocalRepo null $mRepo
Class representing a MediaWiki article and history.
Definition WikiPage.php:47
Title $mTitle
Definition WikiPage.php:53
Title $mRedirectTarget
Definition WikiPage.php:91
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
const NS_FILE
Definition Defines.php:75
const NS_CATEGORY
Definition Defines.php:83
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