MediaWiki  1.34.4
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 ) {
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  $this->mFileLoaded = true;
64 
65  $this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
66  if ( !$this->mFile ) {
67  $this->mFile = $services->getRepoGroup()->getLocalRepo()
68  ->newFile( $this->mTitle ); // always a File
69  }
70  $this->mRepo = $this->mFile->getRepo();
71  return true;
72  }
73 
77  public function getRedirectTarget() {
78  $this->loadFile();
79  if ( $this->mFile->isLocal() ) {
80  return parent::getRedirectTarget();
81  }
82  // Foreign image page
83  $from = $this->mFile->getRedirected();
84  $to = $this->mFile->getName();
85  if ( $from == $to ) {
86  return null;
87  }
88  $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
90  }
91 
95  public function followRedirect() {
96  $this->loadFile();
97  if ( $this->mFile->isLocal() ) {
98  return parent::followRedirect();
99  }
100  $from = $this->mFile->getRedirected();
101  $to = $this->mFile->getName();
102  if ( $from == $to ) {
103  return false;
104  }
105  return Title::makeTitle( NS_FILE, $to );
106  }
107 
111  public function isRedirect() {
112  $this->loadFile();
113  if ( $this->mFile->isLocal() ) {
114  return parent::isRedirect();
115  }
116 
117  return (bool)$this->mFile->getRedirected();
118  }
119 
123  public function isLocal() {
124  $this->loadFile();
125  return $this->mFile->isLocal();
126  }
127 
131  public function getFile() {
132  $this->loadFile();
133  return $this->mFile;
134  }
135 
139  public function getDuplicates() {
140  $this->loadFile();
141  if ( !is_null( $this->mDupes ) ) {
142  return $this->mDupes;
143  }
144  $hash = $this->mFile->getSha1();
145  if ( !( $hash ) ) {
146  $this->mDupes = [];
147  return $this->mDupes;
148  }
149  $dupes = RepoGroup::singleton()->findBySha1( $hash );
150  // Remove duplicates with self and non matching file sizes
151  $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
152  $size = $this->mFile->getSize();
153 
157  foreach ( $dupes as $index => $file ) {
158  $key = $file->getRepoName() . ':' . $file->getName();
159  if ( $key == $self ) {
160  unset( $dupes[$index] );
161  }
162  if ( $file->getSize() != $size ) {
163  unset( $dupes[$index] );
164  }
165  }
166  $this->mDupes = $dupes;
167  return $this->mDupes;
168  }
169 
174  public function doPurge() {
175  $this->loadFile();
176 
177  if ( $this->mFile->exists() ) {
178  wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
180  $this->mTitle,
181  'imagelinks',
182  [ 'causeAction' => 'file-purge' ]
183  );
184  JobQueueGroup::singleton()->lazyPush( $job );
185  } else {
186  wfDebug( 'ImagePage::doPurge no image for '
187  . $this->mFile->getName() . "; limiting purge to cache only\n" );
188  }
189 
190  // even if the file supposedly doesn't exist, force any cached information
191  // to be updated (in case the cached information is wrong)
192 
193  // Purge current version and its thumbnails
194  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
195 
196  // Purge the old versions and their thumbnails
197  foreach ( $this->mFile->getHistory() as $oldFile ) {
198  $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
199  }
200 
201  if ( $this->mRepo ) {
202  // Purge redirect cache
203  $this->mRepo->invalidateImageRedirect( $this->mTitle );
204  }
205 
206  return parent::doPurge();
207  }
208 
218  public function getForeignCategories() {
219  $this->loadFile();
222 
223  if ( !$file instanceof LocalFile ) {
224  wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
225  return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
226  }
227 
229  $repo = $file->getRepo();
230  $dbr = $repo->getReplicaDB();
231 
232  $res = $dbr->select(
233  [ 'page', 'categorylinks' ],
234  [
235  'page_title' => 'cl_to',
236  'page_namespace' => NS_CATEGORY,
237  ],
238  [
239  'page_namespace' => $title->getNamespace(),
240  'page_title' => $title->getDBkey(),
241  ],
242  __METHOD__,
243  [],
244  [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
245  );
246 
248  }
249 
254  public function getWikiDisplayName() {
255  return $this->getFile()->getRepo()->getDisplayName();
256  }
257 
262  public function getSourceURL() {
263  return $this->getFile()->getDescriptionUrl();
264  }
265 }
RepoGroup\singleton
static singleton()
Definition: RepoGroup.php:60
WikiFilePage\$mFileLoaded
bool $mFileLoaded
Definition: WikiFilePage.php:37
TitleArray\newFromResult
static newFromResult( $res)
Definition: TitleArray.php:42
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
WikiFilePage\followRedirect
followRedirect()
Definition: WikiFilePage.php:95
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:47
NS_FILE
const NS_FILE
Definition: Defines.php:75
WikiFilePage\getFile
getFile()
Definition: WikiFilePage.php:131
$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:254
$res
$res
Definition: testCompression.php:52
Wikimedia\Rdbms\FakeResultWrapper
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Definition: FakeResultWrapper.php:11
HTMLCacheUpdateJob\newForBacklinks
static newForBacklinks(Title $title, $table, $params=[])
Definition: HTMLCacheUpdateJob.php:59
WikiPage\$mTitle
Title $mTitle
Definition: WikiPage.php:53
$dbr
$dbr
Definition: testCompression.php:50
WikiFilePage\setFile
setFile( $file)
Definition: WikiFilePage.php:50
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
WikiFilePage\$mFile
File false $mFile
Definition: WikiFilePage.php:33
WikiPage\$mRedirectTarget
Title $mRedirectTarget
Definition: WikiPage.php:91
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:83
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:913
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:56
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:262
WikiFilePage\getDuplicates
getDuplicates()
Definition: WikiFilePage.php:139
$self
$self
Definition: doMaintenance.php:55
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:50
WikiFilePage\getForeignCategories
getForeignCategories()
Get the categories this file is a member of on the wiki where it was uploaded.
Definition: WikiFilePage.php:218
WikiFilePage\getRedirectTarget
getRedirectTarget()
Definition: WikiFilePage.php:77
WikiFilePage\doPurge
doPurge()
Override handling of action=purge.
Definition: WikiFilePage.php:174
WikiFilePage
Special handling for file pages.
Definition: WikiFilePage.php:31
WikiFilePage\$mRepo
LocalRepo null $mRepo
Definition: WikiFilePage.php:35
WikiFilePage\isRedirect
isRedirect()
Definition: WikiFilePage.php:111
WikiFilePage\isLocal
isLocal()
Definition: WikiFilePage.php:123
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:37