Merge "Revert "Use display name in category page subheadings if provided""
[lhc/web/wiklou.git] / includes / page / 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 /** @var File */
30 protected $mFile = false;
31 /** @var LocalRepo */
32 protected $mRepo = null;
33 /** @var bool */
34 protected $mFileLoaded = false;
35 /** @var array */
36 protected $mDupes = null;
37
38 public function __construct( $title ) {
39 parent::__construct( $title );
40 $this->mDupes = null;
41 $this->mRepo = null;
42 }
43
44 /**
45 * @param File $file
46 */
47 public function setFile( $file ) {
48 $this->mFile = $file;
49 $this->mFileLoaded = true;
50 }
51
52 /**
53 * @return bool
54 */
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
69 /**
70 * @return mixed|null|Title
71 */
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 );
84 return $this->mRedirectTarget;
85 }
86
87 /**
88 * @return bool|mixed|Title
89 */
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
103 /**
104 * @return bool
105 */
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
115 /**
116 * @return bool
117 */
118 public function isLocal() {
119 $this->loadFile();
120 return $this->mFile->isLocal();
121 }
122
123 /**
124 * @return bool|File
125 */
126 public function getFile() {
127 $this->loadFile();
128 return $this->mFile;
129 }
130
131 /**
132 * @return array|null
133 */
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
149 /**
150 * @var $file File
151 */
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
187 /**
188 * Get the categories this file is a member of on the wiki where it was uploaded.
189 * For local files, this is the same as getCategories().
190 * For foreign API files (InstantCommons), this is not supported currently.
191 * Results will include hidden categories.
192 *
193 * @return TitleArray|Title[]
194 * @since 1.23
195 */
196 public function getForeignCategories() {
197 $this->loadFile();
198 $title = $this->mTitle;
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
206 /** @var LocalRepo $repo */
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
225 return TitleArray::newFromResult( $res );
226 }
227 }