Merge branch 'Wikidata', remote-tracking branch 'origin/Wikidata' into Wikidata
[mediawiki.git] / includes / WikiFilePage.php
blob52d8e6d87b5357adbba787026712860852476b90
1 <?php
2 /**
3 * Special handling for file pages
5 * @ingroup Media
6 */
7 class WikiFilePage extends WikiPage {
8 /**
9 * @var File
11 protected $mFile = false; // !< File object
12 protected $mRepo = null; // !<
13 protected $mFileLoaded = false; // !<
14 protected $mDupes = null; // !<
16 public function __construct( $title ) {
17 parent::__construct( $title );
18 $this->mDupes = null;
19 $this->mRepo = null;
22 public function getActionOverrides() {
23 return array( 'revert' => 'RevertFileAction' );
26 /**
27 * @param $file File:
29 public function setFile( $file ) {
30 $this->mFile = $file;
31 $this->mFileLoaded = true;
34 /**
35 * @return bool
37 protected function loadFile() {
38 if ( $this->mFileLoaded ) {
39 return true;
41 $this->mFileLoaded = true;
43 $this->mFile = wfFindFile( $this->mTitle );
44 if ( !$this->mFile ) {
45 $this->mFile = wfLocalFile( $this->mTitle ); // always a File
47 $this->mRepo = $this->mFile->getRepo();
48 return true;
51 /**
52 * @return mixed|null|Title
54 public function getRedirectTarget() {
55 $this->loadFile();
56 if ( $this->mFile->isLocal() ) {
57 return parent::getRedirectTarget();
59 // Foreign image page
60 $from = $this->mFile->getRedirected();
61 $to = $this->mFile->getName();
62 if ( $from == $to ) {
63 return null;
65 return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
68 /**
69 * @return bool|mixed|Title
71 public function followRedirect() {
72 $this->loadFile();
73 if ( $this->mFile->isLocal() ) {
74 return parent::followRedirect();
76 $from = $this->mFile->getRedirected();
77 $to = $this->mFile->getName();
78 if ( $from == $to ) {
79 return false;
81 return Title::makeTitle( NS_FILE, $to );
84 /**
85 * @param bool $text
86 * @return bool
88 public function isRedirect( $text = false ) {
89 $this->loadFile();
90 if ( $this->mFile->isLocal() ) {
91 return parent::isRedirect( $text );
94 return (bool)$this->mFile->getRedirected();
97 /**
98 * @return bool
100 public function isLocal() {
101 $this->loadFile();
102 return $this->mFile->isLocal();
106 * @return bool|File
108 public function getFile() {
109 $this->loadFile();
110 return $this->mFile;
114 * @return array|null
116 public function getDuplicates() {
117 $this->loadFile();
118 if ( !is_null( $this->mDupes ) ) {
119 return $this->mDupes;
121 $hash = $this->mFile->getSha1();
122 if ( !( $hash ) ) {
123 return $this->mDupes = array();
125 $dupes = RepoGroup::singleton()->findBySha1( $hash );
126 // Remove duplicates with self and non matching file sizes
127 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
128 $size = $this->mFile->getSize();
131 * @var $file File
133 foreach ( $dupes as $index => $file ) {
134 $key = $file->getRepoName() . ':' . $file->getName();
135 if ( $key == $self ) {
136 unset( $dupes[$index] );
138 if ( $file->getSize() != $size ) {
139 unset( $dupes[$index] );
142 $this->mDupes = $dupes;
143 return $this->mDupes;
147 * Override handling of action=purge
148 * @return bool
150 public function doPurge() {
151 $this->loadFile();
152 if ( $this->mFile->exists() ) {
153 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
154 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
155 $update->doUpdate();
156 $this->mFile->upgradeRow();
157 $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
158 } else {
159 wfDebug( 'ImagePage::doPurge no image for ' . $this->mFile->getName() . "; limiting purge to cache only\n" );
160 // even if the file supposedly doesn't exist, force any cached information
161 // to be updated (in case the cached information is wrong)
162 $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
164 return parent::doPurge();