MediaWiki  1.29.1
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 
40  public function __construct( $title ) {
41  parent::__construct( $title );
42  $this->mDupes = null;
43  $this->mRepo = null;
44  }
45 
49  public function setFile( $file ) {
50  $this->mFile = $file;
51  $this->mFileLoaded = true;
52  }
53 
57  protected function loadFile() {
58  if ( $this->mFileLoaded ) {
59  return true;
60  }
61  $this->mFileLoaded = true;
62 
63  $this->mFile = wfFindFile( $this->mTitle );
64  if ( !$this->mFile ) {
65  $this->mFile = wfLocalFile( $this->mTitle ); // always a File
66  }
67  $this->mRepo = $this->mFile->getRepo();
68  return true;
69  }
70 
74  public function getRedirectTarget() {
75  $this->loadFile();
76  if ( $this->mFile->isLocal() ) {
77  return parent::getRedirectTarget();
78  }
79  // Foreign image page
80  $from = $this->mFile->getRedirected();
81  $to = $this->mFile->getName();
82  if ( $from == $to ) {
83  return null;
84  }
85  $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
87  }
88 
92  public function followRedirect() {
93  $this->loadFile();
94  if ( $this->mFile->isLocal() ) {
95  return parent::followRedirect();
96  }
97  $from = $this->mFile->getRedirected();
98  $to = $this->mFile->getName();
99  if ( $from == $to ) {
100  return false;
101  }
102  return Title::makeTitle( NS_FILE, $to );
103  }
104 
108  public function isRedirect() {
109  $this->loadFile();
110  if ( $this->mFile->isLocal() ) {
111  return parent::isRedirect();
112  }
113 
114  return (bool)$this->mFile->getRedirected();
115  }
116 
120  public function isLocal() {
121  $this->loadFile();
122  return $this->mFile->isLocal();
123  }
124 
128  public function getFile() {
129  $this->loadFile();
130  return $this->mFile;
131  }
132 
136  public function getDuplicates() {
137  $this->loadFile();
138  if ( !is_null( $this->mDupes ) ) {
139  return $this->mDupes;
140  }
141  $hash = $this->mFile->getSha1();
142  if ( !( $hash ) ) {
143  $this->mDupes = [];
144  return $this->mDupes;
145  }
146  $dupes = RepoGroup::singleton()->findBySha1( $hash );
147  // Remove duplicates with self and non matching file sizes
148  $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
149  $size = $this->mFile->getSize();
150 
154  foreach ( $dupes as $index => $file ) {
155  $key = $file->getRepoName() . ':' . $file->getName();
156  if ( $key == $self ) {
157  unset( $dupes[$index] );
158  }
159  if ( $file->getSize() != $size ) {
160  unset( $dupes[$index] );
161  }
162  }
163  $this->mDupes = $dupes;
164  return $this->mDupes;
165  }
166 
171  public function doPurge() {
172  $this->loadFile();
173  if ( $this->mFile->exists() ) {
174  wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
175  DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->mTitle, 'imagelinks' ) );
176  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
177  } else {
178  wfDebug( 'ImagePage::doPurge no image for '
179  . $this->mFile->getName() . "; limiting purge to cache only\n" );
180  // even if the file supposedly doesn't exist, force any cached information
181  // to be updated (in case the cached information is wrong)
182  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
183  }
184  if ( $this->mRepo ) {
185  // Purge redirect cache
186  $this->mRepo->invalidateImageRedirect( $this->mTitle );
187  }
188  return parent::doPurge();
189  }
190 
200  public function getForeignCategories() {
201  $this->loadFile();
203  $file = $this->mFile;
204 
205  if ( !$file instanceof LocalFile ) {
206  wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
207  return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
208  }
209 
211  $repo = $file->getRepo();
212  $dbr = $repo->getReplicaDB();
213 
214  $res = $dbr->select(
215  [ 'page', 'categorylinks' ],
216  [
217  'page_title' => 'cl_to',
218  'page_namespace' => NS_CATEGORY,
219  ],
220  [
221  'page_namespace' => $title->getNamespace(),
222  'page_title' => $title->getDBkey(),
223  ],
224  __METHOD__,
225  [],
226  [ 'categorylinks' => [ 'INNER JOIN', 'page_id = cl_from' ] ]
227  );
228 
230  }
231 
236  public function getWikiDisplayName() {
237  return $this->getFile()->getRepo()->getDisplayName();
238  }
239 
244  public function getSourceURL() {
245  return $this->getFile()->getDescriptionUrl();
246  }
247 }
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
WikiFilePage\$mFileLoaded
bool $mFileLoaded
Definition: WikiFilePage.php:36
TitleArray\newFromResult
static newFromResult( $res)
Definition: TitleArray.php:40
WikiFilePage\$mDupes
array $mDupes
Definition: WikiFilePage.php:38
WikiFilePage\followRedirect
followRedirect()
Definition: WikiFilePage.php:92
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DeferredUpdates\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
Definition: DeferredUpdates.php:76
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:36
NS_FILE
const NS_FILE
Definition: Defines.php:68
WikiFilePage\getFile
getFile()
Definition: WikiFilePage.php:128
$res
$res
Definition: database.txt:21
WikiFilePage\getWikiDisplayName
getWikiDisplayName()
Definition: WikiFilePage.php:236
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
WikiPage\$mTitle
Title $mTitle
Definition: WikiPage.php:42
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
WikiFilePage\$mFile
File $mFile
Definition: WikiFilePage.php:32
WikiFilePage\setFile
setFile( $file)
Definition: WikiFilePage.php:49
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:51
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
WikiFilePage\$mRepo
LocalRepo $mRepo
Definition: WikiFilePage.php:34
WikiPage\$mRedirectTarget
Title $mRedirectTarget
Definition: WikiPage.php:68
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:76
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:999
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:45
HTMLCacheUpdate
Class to invalidate the HTML cache of all the pages linking to a given title.
Definition: HTMLCacheUpdate.php:29
WikiFilePage\__construct
__construct( $title)
Definition: WikiFilePage.php:40
WikiFilePage\loadFile
loadFile()
Definition: WikiFilePage.php:57
WikiFilePage\getSourceURL
getSourceURL()
Definition: WikiFilePage.php:244
wfFindFile
wfFindFile( $title, $options=[])
Find a file.
Definition: GlobalFunctions.php:3101
WikiFilePage\getDuplicates
getDuplicates()
Definition: WikiFilePage.php:136
$self
$self
Definition: doMaintenance.php:56
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
WikiFilePage\getForeignCategories
getForeignCategories()
Get the categories this file is a member of on the wiki where it was uploaded.
Definition: WikiFilePage.php:200
WikiFilePage\getRedirectTarget
getRedirectTarget()
Definition: WikiFilePage.php:74
WikiFilePage\doPurge
doPurge()
Override handling of action=purge.
Definition: WikiFilePage.php:171
WikiFilePage
Special handling for file pages.
Definition: WikiFilePage.php:30
WikiFilePage\isRedirect
isRedirect()
Definition: WikiFilePage.php:108
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:3112
WikiFilePage\isLocal
isLocal()
Definition: WikiFilePage.php:120
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:35
array
the array() calling protocol came about after MediaWiki 1.4rc1.