Reverted r113177 per CR
[lhc/web/wiklou.git] / includes / WikiFilePage.php
1 <?php
2 /**
3 * Special handling for file pages
4 *
5 * @ingroup Media
6 */
7 class WikiFilePage extends WikiPage {
8 /**
9 * @var File
10 */
11 protected $mFile = false; // !< File object
12 protected $mRepo = null; // !<
13 protected $mFileLoaded = false; // !<
14 protected $mDupes = null; // !<
15
16 public function __construct( $title ) {
17 parent::__construct( $title );
18 $this->mDupes = null;
19 $this->mRepo = null;
20 }
21
22 public function getActionOverrides() {
23 return array( 'revert' => 'RevertFileAction' );
24 }
25
26 /**
27 * @param $file File:
28 */
29 public function setFile( $file ) {
30 $this->mFile = $file;
31 $this->mFileLoaded = true;
32 }
33
34 /**
35 * @return bool
36 */
37 protected function loadFile() {
38 if ( $this->mFileLoaded ) {
39 return true;
40 }
41 $this->mFileLoaded = true;
42
43 $this->mFile = wfFindFile( $this->mTitle );
44 if ( !$this->mFile ) {
45 $this->mFile = wfLocalFile( $this->mTitle ); // always a File
46 }
47 $this->mRepo = $this->mFile->getRepo();
48 return true;
49 }
50
51 /**
52 * @return mixed|null|Title
53 */
54 public function getRedirectTarget() {
55 $this->loadFile();
56 if ( $this->mFile->isLocal() ) {
57 return parent::getRedirectTarget();
58 }
59 // Foreign image page
60 $from = $this->mFile->getRedirected();
61 $to = $this->mFile->getName();
62 if ( $from == $to ) {
63 return null;
64 }
65 return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
66 }
67
68 /**
69 * @return bool|mixed|Title
70 */
71 public function followRedirect() {
72 $this->loadFile();
73 if ( $this->mFile->isLocal() ) {
74 return parent::followRedirect();
75 }
76 $from = $this->mFile->getRedirected();
77 $to = $this->mFile->getName();
78 if ( $from == $to ) {
79 return false;
80 }
81 return Title::makeTitle( NS_FILE, $to );
82 }
83
84 /**
85 * @param bool $text
86 * @return bool
87 */
88 public function isRedirect( $text = false ) {
89 $this->loadFile();
90 if ( $this->mFile->isLocal() ) {
91 return parent::isRedirect( $text );
92 }
93
94 return (bool)$this->mFile->getRedirected();
95 }
96
97 /**
98 * @return bool
99 */
100 public function isLocal() {
101 $this->loadFile();
102 return $this->mFile->isLocal();
103 }
104
105 /**
106 * @return bool|File
107 */
108 public function getFile() {
109 $this->loadFile();
110 return $this->mFile;
111 }
112
113 /**
114 * @return array|null
115 */
116 public function getDuplicates() {
117 $this->loadFile();
118 if ( !is_null( $this->mDupes ) ) {
119 return $this->mDupes;
120 }
121 $hash = $this->mFile->getSha1();
122 if ( !( $hash ) ) {
123 return $this->mDupes = array();
124 }
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();
129
130 /**
131 * @var $file File
132 */
133 foreach ( $dupes as $index => $file ) {
134 $key = $file->getRepoName() . ':' . $file->getName();
135 if ( $key == $self ) {
136 unset( $dupes[$index] );
137 }
138 if ( $file->getSize() != $size ) {
139 unset( $dupes[$index] );
140 }
141 }
142 $this->mDupes = $dupes;
143 return $this->mDupes;
144 }
145
146 /**
147 * Override handling of action=purge
148 * @return bool
149 */
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 ) );
163 }
164 return parent::doPurge();
165 }
166 }