MediaWiki REL1_32
WikiFilePage.php
Go to the documentation of this file.
1<?php
24
30class WikiFilePage extends WikiPage {
32 protected $mFile = false;
34 protected $mRepo = null;
36 protected $mFileLoaded = false;
38 protected $mDupes = null;
39
40 public function __construct( $title ) {
41 parent::__construct( $title );
42 $this->mDupes = null;
43 $this->mRepo = null;
44 }
45
49 public function setFile( $file ) {
50 $this->mFile = $file;
51 $this->mFileLoaded = true;
52 }
53
57 protected function loadFile() {
58 if ( $this->mFileLoaded ) {
59 return true;
60 }
61 $this->mFileLoaded = true;
62
63 $this->mFile = wfFindFile( $this->mTitle );
64 if ( !$this->mFile ) {
65 $this->mFile = wfLocalFile( $this->mTitle ); // always a File
66 }
67 $this->mRepo = $this->mFile->getRepo();
68 return true;
69 }
70
74 public function getRedirectTarget() {
75 $this->loadFile();
76 if ( $this->mFile->isLocal() ) {
77 return parent::getRedirectTarget();
78 }
79 // Foreign image page
80 $from = $this->mFile->getRedirected();
81 $to = $this->mFile->getName();
82 if ( $from == $to ) {
83 return null;
84 }
85 $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
87 }
88
92 public function followRedirect() {
93 $this->loadFile();
94 if ( $this->mFile->isLocal() ) {
95 return parent::followRedirect();
96 }
97 $from = $this->mFile->getRedirected();
98 $to = $this->mFile->getName();
99 if ( $from == $to ) {
100 return false;
101 }
102 return Title::makeTitle( NS_FILE, $to );
103 }
104
108 public function isRedirect() {
109 $this->loadFile();
110 if ( $this->mFile->isLocal() ) {
111 return parent::isRedirect();
112 }
113
114 return (bool)$this->mFile->getRedirected();
115 }
116
120 public function isLocal() {
121 $this->loadFile();
122 return $this->mFile->isLocal();
123 }
124
128 public function getFile() {
129 $this->loadFile();
130 return $this->mFile;
131 }
132
136 public function getDuplicates() {
137 $this->loadFile();
138 if ( !is_null( $this->mDupes ) ) {
139 return $this->mDupes;
140 }
141 $hash = $this->mFile->getSha1();
142 if ( !( $hash ) ) {
143 $this->mDupes = [];
144 return $this->mDupes;
145 }
146 $dupes = RepoGroup::singleton()->findBySha1( $hash );
147 // Remove duplicates with self and non matching file sizes
148 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
149 $size = $this->mFile->getSize();
150
154 foreach ( $dupes as $index => $file ) {
155 $key = $file->getRepoName() . ':' . $file->getName();
156 if ( $key == $self ) {
157 unset( $dupes[$index] );
158 }
159 if ( $file->getSize() != $size ) {
160 unset( $dupes[$index] );
161 }
162 }
163 $this->mDupes = $dupes;
164 return $this->mDupes;
165 }
166
171 public function doPurge() {
172 $this->loadFile();
173
174 if ( $this->mFile->exists() ) {
175 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
176 DeferredUpdates::addUpdate(
177 new HTMLCacheUpdate( $this->mTitle, 'imagelinks', 'file-purge' )
178 );
179 } else {
180 wfDebug( 'ImagePage::doPurge no image for '
181 . $this->mFile->getName() . "; limiting purge to cache only\n" );
182 }
183
184 // even if the file supposedly doesn't exist, force any cached information
185 // to be updated (in case the cached information is wrong)
186
187 // Purge current version and its thumbnails
188 $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
189
190 // Purge the old versions and their thumbnails
191 foreach ( $this->mFile->getHistory() as $oldFile ) {
192 $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
193 }
194
195 if ( $this->mRepo ) {
196 // Purge redirect cache
197 $this->mRepo->invalidateImageRedirect( $this->mTitle );
198 }
199
200 return parent::doPurge();
201 }
202
212 public function getForeignCategories() {
213 $this->loadFile();
214 $title = $this->mTitle;
215 $file = $this->mFile;
216
217 if ( !$file instanceof LocalFile ) {
218 wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
220 }
221
223 $repo = $file->getRepo();
224 $dbr = $repo->getReplicaDB();
225
226 $res = $dbr->select(
227 [ 'page', 'categorylinks' ],
228 [
229 'page_title' => 'cl_to',
230 'page_namespace' => NS_CATEGORY,
231 ],
232 [
233 'page_namespace' => $title->getNamespace(),
234 'page_title' => $title->getDBkey(),
235 ],
236 __METHOD__,
237 [],
238 [ 'categorylinks' => [ 'INNER JOIN', 'page_id = cl_from' ] ]
239 );
240
242 }
243
248 public function getWikiDisplayName() {
249 return $this->getFile()->getRepo()->getDisplayName();
250 }
251
256 public function getSourceURL() {
257 return $this->getFile()->getDescriptionUrl();
258 }
259}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfLocalFile( $title)
Get an object referring to a locally registered file.
wfFindFile( $title, $options=[])
Find a file.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:51
Class to invalidate the HTML cache of all the pages linking to a given title.
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:46
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition LocalRepo.php:36
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
static newFromResult( $res)
Special handling for file pages.
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 $mRepo
Class representing a MediaWiki article and history.
Definition WikiPage.php:44
Title $mTitle
Definition WikiPage.php:50
Title $mRedirectTarget
Definition WikiPage.php:76
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
$res
Definition database.txt:21
const NS_FILE
Definition Defines.php:70
const NS_CATEGORY
Definition Defines.php:78
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))