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 
41  public function __construct( $title ) {
42  parent::__construct( $title );
43  $this->mDupes = null;
44  $this->mRepo = null;
45  }
46 
50  public function setFile( File $file ) {
51  $this->mFile = $file;
52  $this->mFileLoaded = true;
53  }
54 
58  protected function loadFile() {
59  $services = MediaWikiServices::getInstance();
60  if ( $this->mFileLoaded ) {
61  return true;
62  }
63 
64  $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
65  if ( !$this->mFile ) {
66  $this->mFile = $services->getRepoGroup()->getLocalRepo()
67  ->newFile( $this->mTitle );
68  }
69 
70  if ( !$this->mFile instanceof File ) {
71  throw new RuntimeException( 'Expected to find file. See T250767' );
72  }
73 
74  $this->mRepo = $this->mFile->getRepo();
75  $this->mFileLoaded = true;
76  return true;
77  }
78 
82  public function getRedirectTarget() {
83  $this->loadFile();
84  if ( $this->mFile->isLocal() ) {
85  return parent::getRedirectTarget();
86  }
87  // Foreign image page
88  $from = $this->mFile->getRedirected();
89  $to = $this->mFile->getName();
90  if ( $from == $to ) {
91  return null;
92  }
93  $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
95  }
96 
100  public function followRedirect() {
101  $this->loadFile();
102  if ( $this->mFile->isLocal() ) {
103  return parent::followRedirect();
104  }
105  $from = $this->mFile->getRedirected();
106  $to = $this->mFile->getName();
107  if ( $from == $to ) {
108  return false;
109  }
110  return Title::makeTitle( NS_FILE, $to );
111  }
112 
116  public function isRedirect() {
117  $this->loadFile();
118  if ( $this->mFile->isLocal() ) {
119  return parent::isRedirect();
120  }
121 
122  return (bool)$this->mFile->getRedirected();
123  }
124 
128  public function isLocal() {
129  $this->loadFile();
130  return $this->mFile->isLocal();
131  }
132 
136  public function getFile() {
137  $this->loadFile();
138  return $this->mFile;
139  }
140 
144  public function getDuplicates() {
145  $this->loadFile();
146  if ( $this->mDupes !== null ) {
147  return $this->mDupes;
148  }
149  $hash = $this->mFile->getSha1();
150  if ( !( $hash ) ) {
151  $this->mDupes = [];
152  return $this->mDupes;
153  }
154  $dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
155  // Remove duplicates with self and non matching file sizes
156  $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
157  $size = $this->mFile->getSize();
158 
162  foreach ( $dupes as $index => $file ) {
163  $key = $file->getRepoName() . ':' . $file->getName();
164  if ( $key == $self ) {
165  unset( $dupes[$index] );
166  }
167  if ( $file->getSize() != $size ) {
168  unset( $dupes[$index] );
169  }
170  }
171  $this->mDupes = $dupes;
172  return $this->mDupes;
173  }
174 
179  public function doPurge() {
180  $this->loadFile();
181 
182  if ( $this->mFile->exists() ) {
183  wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
185  $this->mTitle,
186  'imagelinks',
187  [ 'causeAction' => 'file-purge' ]
188  );
189  JobQueueGroup::singleton()->lazyPush( $job );
190  } else {
191  wfDebug( 'ImagePage::doPurge no image for '
192  . $this->mFile->getName() . "; limiting purge to cache only" );
193  }
194 
195  // even if the file supposedly doesn't exist, force any cached information
196  // to be updated (in case the cached information is wrong)
197 
198  // Purge current version and its thumbnails
199  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
200 
201  // Purge the old versions and their thumbnails
202  foreach ( $this->mFile->getHistory() as $oldFile ) {
203  $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
204  }
205 
206  if ( $this->mRepo ) {
207  // Purge redirect cache
208  $this->mRepo->invalidateImageRedirect( $this->mTitle );
209  }
210 
211  return parent::doPurge();
212  }
213 
223  public function getForeignCategories() {
224  $this->loadFile();
227 
228  if ( !$file instanceof LocalFile ) {
229  wfDebug( __METHOD__ . " is not supported for this file" );
230  return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
231  }
232 
234  $repo = $file->getRepo();
235  $dbr = $repo->getReplicaDB();
236 
237  $res = $dbr->select(
238  [ 'page', 'categorylinks' ],
239  [
240  'page_title' => 'cl_to',
241  'page_namespace' => NS_CATEGORY,
242  ],
243  [
244  'page_namespace' => $title->getNamespace(),
245  'page_title' => $title->getDBkey(),
246  ],
247  __METHOD__,
248  [],
249  [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
250  );
251 
253  }
254 
259  public function getWikiDisplayName() {
260  return $this->getFile()->getRepo()->getDisplayName();
261  }
262 
267  public function getSourceURL() {
268  return $this->getFile()->getDescriptionUrl();
269  }
270 }
WikiFilePage\$mFileLoaded
bool $mFileLoaded
Definition: WikiFilePage.php:37
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:193
WikiFilePage\followRedirect
followRedirect()
Definition: WikiFilePage.php:100
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:60
WikiFilePage\getFile
getFile()
Definition: WikiFilePage.php:136
$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:259
$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:72
$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:66
WikiFilePage\$mFile
File false $mFile
Definition: WikiFilePage.php:33
WikiPage\$mRedirectTarget
Title $mRedirectTarget
The cache of the redirect target.
Definition: WikiPage.php:100
WikiFilePage\setFile
setFile(File $file)
Definition: WikiFilePage.php:50
$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:651
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:41
WikiFilePage\loadFile
loadFile()
Definition: WikiFilePage.php:58
WikiFilePage\$mDupes
array null $mDupes
Definition: WikiFilePage.php:39
WikiFilePage\getSourceURL
getSourceURL()
Definition: WikiFilePage.php:267
HTMLCacheUpdateJob\newForBacklinks
static newForBacklinks(PageReference $page, $table, $params=[])
Definition: HTMLCacheUpdateJob.php:61
WikiFilePage\getDuplicates
getDuplicates()
Definition: WikiFilePage.php:144
$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:223
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
WikiFilePage\getRedirectTarget
getRedirectTarget()
Definition: WikiFilePage.php:82
WikiFilePage\doPurge
doPurge()
Override handling of action=purge.
Definition: WikiFilePage.php:179
WikiFilePage
Special handling for file pages.
Definition: WikiFilePage.php:31
NS_FILE
const NS_FILE
Definition: Defines.php:70
WikiFilePage\$mRepo
LocalRepo null $mRepo
Definition: WikiFilePage.php:35
WikiFilePage\isRedirect
isRedirect()
Definition: WikiFilePage.php:116
WikiFilePage\isLocal
isLocal()
Definition: WikiFilePage.php:128
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:41