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 ) {
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  new HTMLCacheUpdate( $this->mTitle, 'imagelinks', 'file-purge' )
181  );
182  } else {
183  wfDebug( 'ImagePage::doPurge no image for '
184  . $this->mFile->getName() . "; limiting purge to cache only\n" );
185  }
186 
187  // even if the file supposedly doesn't exist, force any cached information
188  // to be updated (in case the cached information is wrong)
189 
190  // Purge current version and its thumbnails
191  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
192 
193  // Purge the old versions and their thumbnails
194  foreach ( $this->mFile->getHistory() as $oldFile ) {
195  $oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
196  }
197 
198  if ( $this->mRepo ) {
199  // Purge redirect cache
200  $this->mRepo->invalidateImageRedirect( $this->mTitle );
201  }
202 
203  return parent::doPurge();
204  }
205 
215  public function getForeignCategories() {
216  $this->loadFile();
219 
220  if ( !$file instanceof LocalFile ) {
221  wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
222  return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
223  }
224 
226  $repo = $file->getRepo();
227  $dbr = $repo->getReplicaDB();
228 
229  $res = $dbr->select(
230  [ 'page', 'categorylinks' ],
231  [
232  'page_title' => 'cl_to',
233  'page_namespace' => NS_CATEGORY,
234  ],
235  [
236  'page_namespace' => $title->getNamespace(),
237  'page_title' => $title->getDBkey(),
238  ],
239  __METHOD__,
240  [],
241  [ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
242  );
243 
245  }
246 
251  public function getWikiDisplayName() {
252  return $this->getFile()->getRepo()->getDisplayName();
253  }
254 
259  public function getSourceURL() {
260  return $this->getFile()->getDescriptionUrl();
261  }
262 }
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
Title $mTitle
Definition: WikiPage.php:51
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 MediaWikiServices
Definition: injection.txt:23
getForeignCategories()
Get the categories this file is a member of on the wiki where it was uploaded.
doPurge()
Override handling of action=purge.
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2205
Class to invalidate the HTML cache of all the pages linking to a given title.
$self
$res
Definition: database.txt:21
static singleton()
Definition: RepoGroup.php:60
LocalRepo null $mRepo
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
const NS_CATEGORY
Definition: Defines.php:74
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
static newFromResult( $res)
Definition: TitleArray.php:40
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
const NS_FILE
Definition: Defines.php:66
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
Special handling for file pages.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
__construct( $title)
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
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
array null $mDupes
Overloads the relevant methods of the real ResultsWrapper so it doesn&#39;t go anywhere near an actual da...
Class to represent a local file in the wiki&#39;s own database.
Definition: LocalFile.php:56
setFile( $file)
File false $mFile
Title $mRedirectTarget
Definition: WikiPage.php:87