MediaWiki  master
WikiFilePage.php
Go to the documentation of this file.
1 <?php
26 
32 class 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 
54  public function setFile( File $file ) {
55  $this->mFile = $file;
56  $this->mFileLoaded = true;
57  }
58 
62  protected function loadFile() {
63  $services = MediaWikiServices::getInstance();
64  if ( $this->mFileLoaded ) {
65  return true;
66  }
67 
68  $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
69  if ( !$this->mFile ) {
70  $this->mFile = $services->getRepoGroup()->getLocalRepo()
71  ->newFile( $this->mTitle );
72  }
73 
74  if ( !$this->mFile instanceof File ) {
75  throw new RuntimeException( 'Expected to find file. See T250767' );
76  }
77 
78  $this->mRepo = $this->mFile->getRepo();
79  $this->mFileLoaded = true;
80  return true;
81  }
82 
86  public function getRedirectTarget() {
87  $this->loadFile();
88  if ( $this->mFile->isLocal() ) {
89  return parent::getRedirectTarget();
90  }
91  // Foreign image page
92  $from = $this->mFile->getRedirected();
93  $to = $this->mFile->getName();
94  if ( $from == $to ) {
95  return null;
96  }
97  $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
99  }
100 
104  public function followRedirect() {
105  $this->loadFile();
106  if ( $this->mFile->isLocal() ) {
107  return parent::followRedirect();
108  }
109  $from = $this->mFile->getRedirected();
110  $to = $this->mFile->getName();
111  if ( $from == $to ) {
112  return false;
113  }
114  return Title::makeTitle( NS_FILE, $to );
115  }
116 
120  public function isRedirect() {
121  $this->loadFile();
122  if ( $this->mFile->isLocal() ) {
123  return parent::isRedirect();
124  }
125 
126  return (bool)$this->mFile->getRedirected();
127  }
128 
132  public function isLocal() {
133  $this->loadFile();
134  return $this->mFile->isLocal();
135  }
136 
140  public function getFile(): File {
141  $this->loadFile();
142  return $this->mFile;
143  }
144 
148  public function getDuplicates() {
149  $this->loadFile();
150  if ( $this->mDupes !== null ) {
151  return $this->mDupes;
152  }
153  $hash = $this->mFile->getSha1();
154  if ( !( $hash ) ) {
155  $this->mDupes = [];
156  return $this->mDupes;
157  }
158  $dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
159  // Remove duplicates with self and non matching file sizes
160  $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
161  $size = $this->mFile->getSize();
162 
166  foreach ( $dupes as $index => $file ) {
167  $key = $file->getRepoName() . ':' . $file->getName();
168  if ( $key == $self ) {
169  unset( $dupes[$index] );
170  }
171  if ( $file->getSize() != $size ) {
172  unset( $dupes[$index] );
173  }
174  }
175  $this->mDupes = $dupes;
176  return $this->mDupes;
177  }
178 
183  public function doPurge() {
184  $this->loadFile();
185 
186  if ( $this->mFile->exists() ) {
187  wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
189  $this->mTitle,
190  'imagelinks',
191  [ 'causeAction' => 'file-purge' ]
192  );
193  JobQueueGroup::singleton()->lazyPush( $job );
194  } else {
195  wfDebug( 'ImagePage::doPurge no image for '
196  . $this->mFile->getName() . "; limiting purge to cache only" );
197  }
198 
199  // even if the file supposedly doesn't exist, force any cached information
200  // to be updated (in case the cached information is wrong)
201 
202  // Purge current version and its thumbnails
203  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
204 
205  // Purge the old versions and their thumbnails
206  foreach ( $this->mFile->getHistory() as $oldFile ) {
207  $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
208  }
209 
210  if ( $this->mRepo ) {
211  // Purge redirect cache
212  $this->mRepo->invalidateImageRedirect( $this->mTitle );
213  }
214 
215  return parent::doPurge();
216  }
217 
227  public function getForeignCategories() {
228  $this->loadFile();
231 
232  if ( !$file instanceof LocalFile ) {
233  wfDebug( __METHOD__ . " is not supported for this file" );
234  return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
235  }
236 
238  $repo = $file->getRepo();
239  $dbr = $repo->getReplicaDB();
240 
241  $res = $dbr->select(
242  [ 'page', 'categorylinks' ],
243  [
244  'page_title' => 'cl_to',
245  'page_namespace' => NS_CATEGORY,
246  ],
247  [
248  'page_namespace' => $title->getNamespace(),
249  'page_title' => $title->getDBkey(),
250  ],
251  __METHOD__,
252  [],
253  [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
254  );
255 
257  }
258 
263  public function getWikiDisplayName() {
264  return $this->getFile()->getRepo()->getDisplayName();
265  }
266 
271  public function getSourceURL() {
272  return $this->getFile()->getDescriptionUrl();
273  }
274 
278  public function getActionOverrides() {
279  $file = $this->getFile();
280  if ( $file->exists() && $file->isLocal() && !$file->getRedirected() ) {
281  // Would be an actual file deletion
282  return [ 'delete' => FileDeleteAction::class ] + parent::getActionOverrides();
283  }
284  // It should use the normal article deletion interface
285  return parent::getActionOverrides();
286  }
287 }
WikiFilePage\$mFileLoaded
bool $mFileLoaded
Definition: WikiFilePage.php:38
MediaWiki\Actions\FileDeleteAction
Handle file deletion.
Definition: FileDeleteAction.php:41
TitleArray\newFromResult
static newFromResult( $res)
Definition: TitleArray.php:44
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
WikiFilePage\followRedirect
followRedirect()
Definition: WikiFilePage.php:104
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:63
WikiFilePage\getFile
getFile()
Definition: WikiFilePage.php:140
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
WikiFilePage\getWikiDisplayName
getWikiDisplayName()
Definition: WikiFilePage.php:263
$res
$res
Definition: testCompression.php:57
Wikimedia\Rdbms\FakeResultWrapper
Overloads the relevant methods of the real ResultWrapper so it doesn't go anywhere near an actual dat...
Definition: FakeResultWrapper.php:12
WikiPage\$mTitle
Title $mTitle
Definition: WikiPage.php:75
$dbr
$dbr
Definition: testCompression.php:54
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:67
WikiFilePage\$mFile
File false $mFile
Definition: WikiFilePage.php:34
WikiPage\$mRedirectTarget
Title $mRedirectTarget
The cache of the redirect target.
Definition: WikiPage.php:103
WikiFilePage\setFile
setFile(File $file)
Definition: WikiFilePage.php:54
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:648
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:63
WikiFilePage\__construct
__construct( $title)
Definition: WikiFilePage.php:45
WikiFilePage\loadFile
loadFile()
Definition: WikiFilePage.php:62
WikiFilePage\$mDupes
array null $mDupes
Definition: WikiFilePage.php:40
WikiFilePage\getSourceURL
getSourceURL()
Definition: WikiFilePage.php:271
HTMLCacheUpdateJob\newForBacklinks
static newForBacklinks(PageReference $page, $table, $params=[])
Definition: HTMLCacheUpdateJob.php:61
WikiFilePage\getDuplicates
getDuplicates()
Definition: WikiFilePage.php:148
$self
$self
Definition: doMaintenance.php:63
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:114
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:49
WikiFilePage\getForeignCategories
getForeignCategories()
Get the categories this file is a member of on the wiki where it was uploaded.
Definition: WikiFilePage.php:227
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
WikiFilePage\getRedirectTarget
getRedirectTarget()
Definition: WikiFilePage.php:86
WikiFilePage\doPurge
doPurge()
Override handling of action=purge.
Definition: WikiFilePage.php:183
WikiFilePage
Special handling for file pages.
Definition: WikiFilePage.php:32
WikiFilePage\getActionOverrides
getActionOverrides()
Move this UI stuff somewhere elseContentHandler::getActionOverrides array
Definition: WikiFilePage.php:278
NS_FILE
const NS_FILE
Definition: Defines.php:70
WikiFilePage\$mRepo
LocalRepo null $mRepo
Definition: WikiFilePage.php:36
WikiFilePage\isRedirect
isRedirect()
Definition: WikiFilePage.php:120
WikiFilePage\isLocal
isLocal()
Definition: WikiFilePage.php:132
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:41