MediaWiki  1.27.2
WikiFilePage.php
Go to the documentation of this file.
1 <?php
28 class WikiFilePage extends WikiPage {
32  protected $mFile = false;
33  protected $mRepo = null;
34  protected $mFileLoaded = false;
35  protected $mDupes = null;
36 
37  public function __construct( $title ) {
38  parent::__construct( $title );
39  $this->mDupes = null;
40  $this->mRepo = null;
41  }
42 
46  public function setFile( $file ) {
47  $this->mFile = $file;
48  $this->mFileLoaded = true;
49  }
50 
54  protected function loadFile() {
55  if ( $this->mFileLoaded ) {
56  return true;
57  }
58  $this->mFileLoaded = true;
59 
60  $this->mFile = wfFindFile( $this->mTitle );
61  if ( !$this->mFile ) {
62  $this->mFile = wfLocalFile( $this->mTitle ); // always a File
63  }
64  $this->mRepo = $this->mFile->getRepo();
65  return true;
66  }
67 
71  public function getRedirectTarget() {
72  $this->loadFile();
73  if ( $this->mFile->isLocal() ) {
74  return parent::getRedirectTarget();
75  }
76  // Foreign image page
77  $from = $this->mFile->getRedirected();
78  $to = $this->mFile->getName();
79  if ( $from == $to ) {
80  return null;
81  }
82  $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
84  }
85 
89  public function followRedirect() {
90  $this->loadFile();
91  if ( $this->mFile->isLocal() ) {
92  return parent::followRedirect();
93  }
94  $from = $this->mFile->getRedirected();
95  $to = $this->mFile->getName();
96  if ( $from == $to ) {
97  return false;
98  }
99  return Title::makeTitle( NS_FILE, $to );
100  }
101 
105  public function isRedirect() {
106  $this->loadFile();
107  if ( $this->mFile->isLocal() ) {
108  return parent::isRedirect();
109  }
110 
111  return (bool)$this->mFile->getRedirected();
112  }
113 
117  public function isLocal() {
118  $this->loadFile();
119  return $this->mFile->isLocal();
120  }
121 
125  public function getFile() {
126  $this->loadFile();
127  return $this->mFile;
128  }
129 
133  public function getDuplicates() {
134  $this->loadFile();
135  if ( !is_null( $this->mDupes ) ) {
136  return $this->mDupes;
137  }
138  $hash = $this->mFile->getSha1();
139  if ( !( $hash ) ) {
140  $this->mDupes = [];
141  return $this->mDupes;
142  }
143  $dupes = RepoGroup::singleton()->findBySha1( $hash );
144  // Remove duplicates with self and non matching file sizes
145  $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
146  $size = $this->mFile->getSize();
147 
151  foreach ( $dupes as $index => $file ) {
152  $key = $file->getRepoName() . ':' . $file->getName();
153  if ( $key == $self ) {
154  unset( $dupes[$index] );
155  }
156  if ( $file->getSize() != $size ) {
157  unset( $dupes[$index] );
158  }
159  }
160  $this->mDupes = $dupes;
161  return $this->mDupes;
162  }
163 
168  public function doPurge() {
169  $this->loadFile();
170  if ( $this->mFile->exists() ) {
171  wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
172  DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->mTitle, 'imagelinks' ) );
173  $this->mFile->upgradeRow();
174  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
175  } else {
176  wfDebug( 'ImagePage::doPurge no image for '
177  . $this->mFile->getName() . "; limiting purge to cache only\n" );
178  // even if the file supposedly doesn't exist, force any cached information
179  // to be updated (in case the cached information is wrong)
180  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
181  }
182  if ( $this->mRepo ) {
183  // Purge redirect cache
184  $this->mRepo->invalidateImageRedirect( $this->mTitle );
185  }
186  return parent::doPurge();
187  }
188 
198  public function getForeignCategories() {
199  $this->loadFile();
201  $file = $this->mFile;
202 
203  if ( !$file instanceof LocalFile ) {
204  wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
205  return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
206  }
207 
209  $repo = $file->getRepo();
210  $dbr = $repo->getSlaveDB();
211 
212  $res = $dbr->select(
213  [ 'page', 'categorylinks' ],
214  [
215  'page_title' => 'cl_to',
216  'page_namespace' => NS_CATEGORY,
217  ],
218  [
219  'page_namespace' => $title->getNamespace(),
220  'page_title' => $title->getDBkey(),
221  ],
222  __METHOD__,
223  [],
224  [ 'categorylinks' => [ 'INNER JOIN', 'page_id = cl_from' ] ]
225  );
226 
228  }
229 }
__construct($title)
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
Title $mTitle
Definition: WikiPage.php:35
getForeignCategories()
Get the categories this file is a member of on the wiki where it was uploaded.
setFile($file)
wfLocalFile($title)
Get an object referring to a locally registered file.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
doPurge()
Override handling of action=purge.
Class to invalidate the HTML cache of all the pages linking to a given title.
$self
$res
Definition: database.txt:21
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
const NS_CATEGORY
Definition: Defines.php:83
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
static addUpdate(DeferrableUpdate $update, $type=self::POSTSEND)
Add an update to the deferred list.
const NS_FILE
Definition: Defines.php:75
static newFromResult($res)
Definition: TitleArray.php:38
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.
Class representing a MediaWiki article and history.
Definition: WikiPage.php:29
$from
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
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:46
Title $mRedirectTarget
Definition: WikiPage.php:61
wfFindFile($title, $options=[])
Find a file.
static & makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:524