Minor bugfix to IP::prettifyIP.
[lhc/web/wiklou.git] / includes / WikiFilePage.php
1 <?php
2 /**
3 * Special handling for file pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Special handling for file pages
25 *
26 * @ingroup Media
27 */
28 class WikiFilePage extends WikiPage {
29 /**
30 * @var File
31 */
32 protected $mFile = false; // !< File object
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
43 public function getActionOverrides() {
44 $overrides = parent::getActionOverrides();
45 $overrides['revert'] = 'RevertFileAction';
46 return $overrides;
47 }
48
49 /**
50 * @param $file File:
51 */
52 public function setFile( $file ) {
53 $this->mFile = $file;
54 $this->mFileLoaded = true;
55 }
56
57 /**
58 * @return bool
59 */
60 protected function loadFile() {
61 if ( $this->mFileLoaded ) {
62 return true;
63 }
64 $this->mFileLoaded = true;
65
66 $this->mFile = wfFindFile( $this->mTitle );
67 if ( !$this->mFile ) {
68 $this->mFile = wfLocalFile( $this->mTitle ); // always a File
69 }
70 $this->mRepo = $this->mFile->getRepo();
71 return true;
72 }
73
74 /**
75 * @return mixed|null|Title
76 */
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 return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
89 }
90
91 /**
92 * @return bool|mixed|Title
93 */
94 public function followRedirect() {
95 $this->loadFile();
96 if ( $this->mFile->isLocal() ) {
97 return parent::followRedirect();
98 }
99 $from = $this->mFile->getRedirected();
100 $to = $this->mFile->getName();
101 if ( $from == $to ) {
102 return false;
103 }
104 return Title::makeTitle( NS_FILE, $to );
105 }
106
107 /**
108 * @return bool
109 */
110 public function isRedirect() {
111 $this->loadFile();
112 if ( $this->mFile->isLocal() ) {
113 return parent::isRedirect();
114 }
115
116 return (bool)$this->mFile->getRedirected();
117 }
118
119 /**
120 * @return bool
121 */
122 public function isLocal() {
123 $this->loadFile();
124 return $this->mFile->isLocal();
125 }
126
127 /**
128 * @return bool|File
129 */
130 public function getFile() {
131 $this->loadFile();
132 return $this->mFile;
133 }
134
135 /**
136 * @return array|null
137 */
138 public function getDuplicates() {
139 $this->loadFile();
140 if ( !is_null( $this->mDupes ) ) {
141 return $this->mDupes;
142 }
143 $hash = $this->mFile->getSha1();
144 if ( !( $hash ) ) {
145 return $this->mDupes = array();
146 }
147 $dupes = RepoGroup::singleton()->findBySha1( $hash );
148 // Remove duplicates with self and non matching file sizes
149 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
150 $size = $this->mFile->getSize();
151
152 /**
153 * @var $file File
154 */
155 foreach ( $dupes as $index => $file ) {
156 $key = $file->getRepoName() . ':' . $file->getName();
157 if ( $key == $self ) {
158 unset( $dupes[$index] );
159 }
160 if ( $file->getSize() != $size ) {
161 unset( $dupes[$index] );
162 }
163 }
164 $this->mDupes = $dupes;
165 return $this->mDupes;
166 }
167
168 /**
169 * Override handling of action=purge
170 * @return bool
171 */
172 public function doPurge() {
173 $this->loadFile();
174 if ( $this->mFile->exists() ) {
175 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
176 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
177 $update->doUpdate();
178 $this->mFile->upgradeRow();
179 $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
180 } else {
181 wfDebug( 'ImagePage::doPurge no image for ' . $this->mFile->getName() . "; limiting purge to cache only\n" );
182 // even if the file supposedly doesn't exist, force any cached information
183 // to be updated (in case the cached information is wrong)
184 $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
185 }
186 if ( $this->mRepo ) {
187 // Purge redirect cache
188 $this->mRepo->invalidateImageRedirect( $this->mTitle );
189 }
190 return parent::doPurge();
191 }
192 }