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  MediaWikiServices::getInstance()->getJobQueueGroup()->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();
230  $file = $this->mFile;
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 
256  return TitleArray::newFromResult( $res );
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 }
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:68
static newForBacklinks(PageReference $page, $table, $params=[])
Local file in the wiki's own database.
Definition: LocalFile.php:61
Service locator for MediaWiki core services.
The TitleArray class only exists to provide the newFromResult method at pre- sent.
Definition: TitleArray.php:40
Represents a title within MediaWiki.
Definition: Title.php:82
Special handling for representing file pages.
File false $mFile
array null $mDupes
getActionOverrides()
Move this UI stuff somewhere elseContentHandler::getActionOverrides array
__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
Base representation for an editable wiki page.
Definition: WikiPage.php:75
Title null $mRedirectTarget
The cache of the redirect target.
Definition: WikiPage.php:113
Overloads the relevant methods of the real ResultWrapper so it doesn't go anywhere near an actual dat...
$self
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