MediaWiki  REL1_28
WikiFilePage.php
Go to the documentation of this file.
1 <?php
28 class WikiFilePage extends WikiPage {
30  protected $mFile = false;
32  protected $mRepo = null;
34  protected $mFileLoaded = false;
36  protected $mDupes = null;
37 
38  public function __construct( $title ) {
39  parent::__construct( $title );
40  $this->mDupes = null;
41  $this->mRepo = null;
42  }
43 
47  public function setFile( $file ) {
48  $this->mFile = $file;
49  $this->mFileLoaded = true;
50  }
51 
55  protected function loadFile() {
56  if ( $this->mFileLoaded ) {
57  return true;
58  }
59  $this->mFileLoaded = true;
60 
61  $this->mFile = wfFindFile( $this->mTitle );
62  if ( !$this->mFile ) {
63  $this->mFile = wfLocalFile( $this->mTitle ); // always a File
64  }
65  $this->mRepo = $this->mFile->getRepo();
66  return true;
67  }
68 
72  public function getRedirectTarget() {
73  $this->loadFile();
74  if ( $this->mFile->isLocal() ) {
75  return parent::getRedirectTarget();
76  }
77  // Foreign image page
78  $from = $this->mFile->getRedirected();
79  $to = $this->mFile->getName();
80  if ( $from == $to ) {
81  return null;
82  }
83  $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
85  }
86 
90  public function followRedirect() {
91  $this->loadFile();
92  if ( $this->mFile->isLocal() ) {
93  return parent::followRedirect();
94  }
95  $from = $this->mFile->getRedirected();
96  $to = $this->mFile->getName();
97  if ( $from == $to ) {
98  return false;
99  }
100  return Title::makeTitle( NS_FILE, $to );
101  }
102 
106  public function isRedirect() {
107  $this->loadFile();
108  if ( $this->mFile->isLocal() ) {
109  return parent::isRedirect();
110  }
111 
112  return (bool)$this->mFile->getRedirected();
113  }
114 
118  public function isLocal() {
119  $this->loadFile();
120  return $this->mFile->isLocal();
121  }
122 
126  public function getFile() {
127  $this->loadFile();
128  return $this->mFile;
129  }
130 
134  public function getDuplicates() {
135  $this->loadFile();
136  if ( !is_null( $this->mDupes ) ) {
137  return $this->mDupes;
138  }
139  $hash = $this->mFile->getSha1();
140  if ( !( $hash ) ) {
141  $this->mDupes = [];
142  return $this->mDupes;
143  }
144  $dupes = RepoGroup::singleton()->findBySha1( $hash );
145  // Remove duplicates with self and non matching file sizes
146  $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
147  $size = $this->mFile->getSize();
148 
152  foreach ( $dupes as $index => $file ) {
153  $key = $file->getRepoName() . ':' . $file->getName();
154  if ( $key == $self ) {
155  unset( $dupes[$index] );
156  }
157  if ( $file->getSize() != $size ) {
158  unset( $dupes[$index] );
159  }
160  }
161  $this->mDupes = $dupes;
162  return $this->mDupes;
163  }
164 
165  public function doPurge( $flags = self::PURGE_ALL ) {
166  $this->loadFile();
167 
168  if ( $this->mFile->exists() ) {
169  wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
170  DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->mTitle, 'imagelinks' ) );
171  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
172  } else {
173  wfDebug( 'ImagePage::doPurge no image for '
174  . $this->mFile->getName() . "; limiting purge to cache only\n" );
175  // even if the file supposedly doesn't exist, force any cached information
176  // to be updated (in case the cached information is wrong)
177  $this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
178  }
179  if ( $this->mRepo ) {
180  // Purge redirect cache
181  $this->mRepo->invalidateImageRedirect( $this->mTitle );
182  }
183 
184  return parent::doPurge( $flags );
185  }
186 
196  public function getForeignCategories() {
197  $this->loadFile();
199  $file = $this->mFile;
200 
201  if ( !$file instanceof LocalFile ) {
202  wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
203  return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
204  }
205 
207  $repo = $file->getRepo();
208  $dbr = $repo->getSlaveDB();
209 
210  $res = $dbr->select(
211  [ 'page', 'categorylinks' ],
212  [
213  'page_title' => 'cl_to',
214  'page_namespace' => NS_CATEGORY,
215  ],
216  [
217  'page_namespace' => $title->getNamespace(),
218  'page_title' => $title->getDBkey(),
219  ],
220  __METHOD__,
221  [],
222  [ 'categorylinks' => [ 'INNER JOIN', 'page_id = cl_from' ] ]
223  );
224 
226  }
227 }
FakeResultWrapper
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Definition: FakeResultWrapper.php:6
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
WikiFilePage\$mFileLoaded
bool $mFileLoaded
Definition: WikiFilePage.php:34
TitleArray\newFromResult
static newFromResult( $res)
Definition: TitleArray.php:38
array
the array() calling protocol came about after MediaWiki 1.4rc1.
WikiFilePage\$mDupes
array $mDupes
Definition: WikiFilePage.php:36
WikiFilePage\followRedirect
followRedirect()
Definition: WikiFilePage.php:90
$from
$from
Definition: importImages.php:98
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:73
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:32
NS_FILE
const NS_FILE
Definition: Defines.php:62
WikiFilePage\getFile
getFile()
Definition: WikiFilePage.php:126
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:956
$res
$res
Definition: database.txt:21
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2706
WikiPage\$mTitle
Title $mTitle
Definition: WikiPage.php:38
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
$dbr
$dbr
Definition: testCompression.php:50
WikiFilePage\$mFile
File $mFile
Definition: WikiFilePage.php:30
WikiFilePage\setFile
setFile( $file)
Definition: WikiFilePage.php:47
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
WikiFilePage\doPurge
doPurge( $flags=self::PURGE_ALL)
Perform the actions of a page purging.
Definition: WikiFilePage.php:165
WikiFilePage\$mRepo
LocalRepo $mRepo
Definition: WikiFilePage.php:32
WikiPage\$mRedirectTarget
Title $mRedirectTarget
Definition: WikiPage.php:64
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:70
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:997
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:43
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:38
WikiFilePage\loadFile
loadFile()
Definition: WikiFilePage.php:55
wfFindFile
wfFindFile( $title, $options=[])
Find a file.
Definition: GlobalFunctions.php:3114
WikiFilePage\getDuplicates
getDuplicates()
Definition: WikiFilePage.php:134
$self
$self
Definition: doMaintenance.php:56
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:196
WikiFilePage\getRedirectTarget
getRedirectTarget()
Definition: WikiFilePage.php:72
WikiFilePage
Special handling for file pages.
Definition: WikiFilePage.php:28
WikiFilePage\isRedirect
isRedirect()
Definition: WikiFilePage.php:106
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:3125
WikiFilePage\isLocal
isLocal()
Definition: WikiFilePage.php:118
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:31