MediaWiki  master
WikiFilePage.php
Go to the documentation of this file.
1 <?php
24 
30 class WikiFilePage extends WikiPage {
32  protected $mFile = false;
34  protected $mRepo = null;
36  protected $mFileLoaded = false;
38  protected $mDupes = null;
39 
43  public function __construct( $title ) {
44  parent::__construct( $title );
45  $this->mDupes = null;
46  $this->mRepo = null;
47  }
48 
52  public function setFile( File $file ) {
53  $this->mFile = $file;
54  $this->mFileLoaded = true;
55  }
56 
60  protected function loadFile() {
61  $services = MediaWikiServices::getInstance();
62  if ( $this->mFileLoaded ) {
63  return true;
64  }
65 
66  $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
67  if ( !$this->mFile ) {
68  $this->mFile = $services->getRepoGroup()->getLocalRepo()
69  ->newFile( $this->mTitle );
70  }
71 
72  if ( !$this->mFile instanceof File ) {
73  throw new RuntimeException( 'Expected to find file. See T250767' );
74  }
75 
76  $this->mRepo = $this->mFile->getRepo();
77  $this->mFileLoaded = true;
78  return true;
79  }
80 
84  public function getRedirectTarget() {
85  $this->loadFile();
86  if ( $this->mFile->isLocal() ) {
87  return parent::getRedirectTarget();
88  }
89  // Foreign image page
90  $from = $this->mFile->getRedirected();
91  $to = $this->mFile->getName();
92  if ( $from == $to ) {
93  return null;
94  }
95  $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
97  }
98 
102  public function followRedirect() {
103  $this->loadFile();
104  if ( $this->mFile->isLocal() ) {
105  return parent::followRedirect();
106  }
107  $from = $this->mFile->getRedirected();
108  $to = $this->mFile->getName();
109  if ( $from == $to ) {
110  return false;
111  }
112  return Title::makeTitle( NS_FILE, $to );
113  }
114 
118  public function isRedirect() {
119  $this->loadFile();
120  if ( $this->mFile->isLocal() ) {
121  return parent::isRedirect();
122  }
123 
124  return (bool)$this->mFile->getRedirected();
125  }
126 
130  public function isLocal() {
131  $this->loadFile();
132  return $this->mFile->isLocal();
133  }
134 
138  public function getFile(): File {
139  $this->loadFile();
140  return $this->mFile;
141  }
142 
146  public function getDuplicates() {
147  $this->loadFile();
148  if ( $this->mDupes !== null ) {
149  return $this->mDupes;
150  }
151  $hash = $this->mFile->getSha1();
152  if ( !( $hash ) ) {
153  $this->mDupes = [];
154  return $this->mDupes;
155  }
156  $dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
157  // Remove duplicates with self and non matching file sizes
158  $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
159  $size = $this->mFile->getSize();
160 
164  foreach ( $dupes as $index => $file ) {
165  $key = $file->getRepoName() . ':' . $file->getName();
166  if ( $key == $self ) {
167  unset( $dupes[$index] );
168  }
169  if ( $file->getSize() != $size ) {
170  unset( $dupes[$index] );
171  }
172  }
173  $this->mDupes = $dupes;
174  return $this->mDupes;
175  }
176 
181  public function doPurge() {
182  $this->loadFile();
183 
184  if ( $this->mFile->exists() ) {
185  wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
187  $this->mTitle,
188  'imagelinks',
189  [ 'causeAction' => 'file-purge' ]
190  );
191  MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $job );
192  } else {
193  wfDebug( 'ImagePage::doPurge no image for '
194  . $this->mFile->getName() . "; limiting purge to cache only" );
195  }
196 
197  // even if the file supposedly doesn't exist, force any cached information
198  // to be updated (in case the cached information is wrong)
199 
200  // Purge current version and its thumbnails
201  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
202 
203  // Purge the old versions and their thumbnails
204  foreach ( $this->mFile->getHistory() as $oldFile ) {
205  $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
206  }
207 
208  if ( $this->mRepo ) {
209  // Purge redirect cache
210  $this->mRepo->invalidateImageRedirect( $this->mTitle );
211  }
212 
213  return parent::doPurge();
214  }
215 
225  public function getForeignCategories() {
226  $this->loadFile();
228  $file = $this->mFile;
229 
230  if ( !$file instanceof LocalFile ) {
231  wfDebug( __METHOD__ . " is not supported for this file" );
232  return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
233  }
234 
236  $repo = $file->getRepo();
237  $dbr = $repo->getReplicaDB();
238 
239  $res = $dbr->select(
240  [ 'page', 'categorylinks' ],
241  [
242  'page_title' => 'cl_to',
243  'page_namespace' => NS_CATEGORY,
244  ],
245  [
246  'page_namespace' => $title->getNamespace(),
247  'page_title' => $title->getDBkey(),
248  ],
249  __METHOD__,
250  [],
251  [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
252  );
253 
255  }
256 
261  public function getWikiDisplayName() {
262  return $this->getFile()->getRepo()->getDisplayName();
263  }
264 
269  public function getSourceURL() {
270  return $this->getFile()->getDescriptionUrl();
271  }
272 
276  public function getActionOverrides() {
277  $file = $this->getFile();
278  if ( $file->exists() && $file->isLocal() && !$file->getRedirected() ) {
279  // Would be an actual file deletion
280  return [ 'delete' => FileDeleteAction::class ] + parent::getActionOverrides();
281  }
282  // It should use the normal article deletion interface
283  return parent::getActionOverrides();
284  }
285 }
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:60
Service locator for MediaWiki core services.
static newFromResult( $res)
Definition: TitleArray.php:44
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:638
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:62
Title null $mRedirectTarget
The cache of the redirect target.
Definition: WikiPage.php:100
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