No need to wrap the result of DatabaseBase::select() in a ResultWrapper object.
[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 return array( 'revert' => 'RevertFileAction' );
45 }
46
47 /**
48 * @param $file File:
49 */
50 public function setFile( $file ) {
51 $this->mFile = $file;
52 $this->mFileLoaded = true;
53 }
54
55 /**
56 * @return bool
57 */
58 protected function loadFile() {
59 if ( $this->mFileLoaded ) {
60 return true;
61 }
62 $this->mFileLoaded = true;
63
64 $this->mFile = wfFindFile( $this->mTitle );
65 if ( !$this->mFile ) {
66 $this->mFile = wfLocalFile( $this->mTitle ); // always a File
67 }
68 $this->mRepo = $this->mFile->getRepo();
69 return true;
70 }
71
72 /**
73 * @return mixed|null|Title
74 */
75 public function getRedirectTarget() {
76 $this->loadFile();
77 if ( $this->mFile->isLocal() ) {
78 return parent::getRedirectTarget();
79 }
80 // Foreign image page
81 $from = $this->mFile->getRedirected();
82 $to = $this->mFile->getName();
83 if ( $from == $to ) {
84 return null;
85 }
86 return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
87 }
88
89 /**
90 * @return bool|mixed|Title
91 */
92 public function followRedirect() {
93 $this->loadFile();
94 if ( $this->mFile->isLocal() ) {
95 return parent::followRedirect();
96 }
97 $from = $this->mFile->getRedirected();
98 $to = $this->mFile->getName();
99 if ( $from == $to ) {
100 return false;
101 }
102 return Title::makeTitle( NS_FILE, $to );
103 }
104
105 /**
106 * @param bool $text
107 * @return bool
108 */
109 public function isRedirect( $text = false ) {
110 $this->loadFile();
111 if ( $this->mFile->isLocal() ) {
112 return parent::isRedirect( $text );
113 }
114
115 return (bool)$this->mFile->getRedirected();
116 }
117
118 /**
119 * @return bool
120 */
121 public function isLocal() {
122 $this->loadFile();
123 return $this->mFile->isLocal();
124 }
125
126 /**
127 * @return bool|File
128 */
129 public function getFile() {
130 $this->loadFile();
131 return $this->mFile;
132 }
133
134 /**
135 * @return array|null
136 */
137 public function getDuplicates() {
138 $this->loadFile();
139 if ( !is_null( $this->mDupes ) ) {
140 return $this->mDupes;
141 }
142 $hash = $this->mFile->getSha1();
143 if ( !( $hash ) ) {
144 return $this->mDupes = array();
145 }
146 $dupes = RepoGroup::singleton()->findBySha1( $hash );
147 // Remove duplicates with self and non matching file sizes
148 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
149 $size = $this->mFile->getSize();
150
151 /**
152 * @var $file File
153 */
154 foreach ( $dupes as $index => $file ) {
155 $key = $file->getRepoName() . ':' . $file->getName();
156 if ( $key == $self ) {
157 unset( $dupes[$index] );
158 }
159 if ( $file->getSize() != $size ) {
160 unset( $dupes[$index] );
161 }
162 }
163 $this->mDupes = $dupes;
164 return $this->mDupes;
165 }
166
167 /**
168 * Override handling of action=purge
169 * @return bool
170 */
171 public function doPurge() {
172 $this->loadFile();
173 if ( $this->mFile->exists() ) {
174 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
175 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
176 $update->doUpdate();
177 $this->mFile->upgradeRow();
178 $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
179 } else {
180 wfDebug( 'ImagePage::doPurge no image for ' . $this->mFile->getName() . "; limiting purge to cache only\n" );
181 // even if the file supposedly doesn't exist, force any cached information
182 // to be updated (in case the cached information is wrong)
183 $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
184 }
185 return parent::doPurge();
186 }
187 }