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