Merge "Special:Newpages feed now shows first revision instead of latest revision"
[lhc/web/wiklou.git] / includes / filerepo / file / ForeignDBFile.php
1 <?php
2 /**
3 * Foreign file with an accessible MediaWiki database.
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 * @ingroup FileAbstraction
22 */
23
24 use Wikimedia\Rdbms\DBUnexpectedError;
25
26 /**
27 * Foreign file with an accessible MediaWiki database
28 *
29 * @ingroup FileAbstraction
30 */
31 class ForeignDBFile extends LocalFile {
32 /**
33 * @param Title $title
34 * @param FileRepo $repo
35 * @param null $unused
36 * @return ForeignDBFile
37 */
38 static function newFromTitle( $title, $repo, $unused = null ) {
39 return new self( $title, $repo );
40 }
41
42 /**
43 * Create a ForeignDBFile from a title
44 * Do not call this except from inside a repo class.
45 *
46 * @param stdClass $row
47 * @param FileRepo $repo
48 * @return ForeignDBFile
49 */
50 static function newFromRow( $row, $repo ) {
51 $title = Title::makeTitle( NS_FILE, $row->img_name );
52 $file = new self( $title, $repo );
53 $file->loadFromRow( $row );
54
55 return $file;
56 }
57
58 /**
59 * @param string $srcPath
60 * @param int $flags
61 * @param array $options
62 * @return Status
63 * @throws MWException
64 */
65 function publish( $srcPath, $flags = 0, array $options = [] ) {
66 $this->readOnlyError();
67 }
68
69 /**
70 * @param string $oldver
71 * @param string $desc
72 * @param string $license
73 * @param string $copyStatus
74 * @param string $source
75 * @param bool $watch
76 * @param bool|string $timestamp
77 * @param User $user User object or null to use $wgUser
78 * @return bool
79 * @throws MWException
80 */
81 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
82 $watch = false, $timestamp = false, User $user = null ) {
83 $this->readOnlyError();
84 }
85
86 /**
87 * @param array $versions
88 * @param bool $unsuppress
89 * @return Status
90 * @throws MWException
91 */
92 function restore( $versions = [], $unsuppress = false ) {
93 $this->readOnlyError();
94 }
95
96 /**
97 * @param string $reason
98 * @param bool $suppress
99 * @param User|null $user
100 * @return Status
101 * @throws MWException
102 */
103 function delete( $reason, $suppress = false, $user = null ) {
104 $this->readOnlyError();
105 }
106
107 /**
108 * @param Title $target
109 * @return Status
110 * @throws MWException
111 */
112 function move( $target ) {
113 $this->readOnlyError();
114 }
115
116 /**
117 * @return string
118 */
119 function getDescriptionUrl() {
120 // Restore remote behavior
121 return File::getDescriptionUrl();
122 }
123
124 /**
125 * @param Language|null $lang Optional language to fetch description in.
126 * @return string|false
127 */
128 function getDescriptionText( $lang = null ) {
129 global $wgLang;
130
131 if ( !$this->repo->fetchDescription ) {
132 return false;
133 }
134
135 $lang = $lang ?: $wgLang;
136 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $lang->getCode() );
137 if ( !$renderUrl ) {
138 return false;
139 }
140
141 $touched = $this->repo->getReplicaDB()->selectField(
142 'page',
143 'page_touched',
144 [
145 'page_namespace' => NS_FILE,
146 'page_title' => $this->title->getDBkey()
147 ]
148 );
149 if ( $touched === false ) {
150 return false; // no description page
151 }
152
153 $cache = ObjectCache::getMainWANInstance();
154
155 return $cache->getWithSetCallback(
156 $this->repo->getLocalCacheKey(
157 'RemoteFileDescription',
158 'url',
159 $lang->getCode(),
160 $this->getName(),
161 $touched
162 ),
163 $this->repo->descriptionCacheExpiry ?: $cache::TTL_UNCACHEABLE,
164 function ( $oldValue, &$ttl, array &$setOpts ) use ( $renderUrl ) {
165 wfDebug( "Fetching shared description from $renderUrl\n" );
166 $res = Http::get( $renderUrl, [], __METHOD__ );
167 if ( !$res ) {
168 $ttl = WANObjectCache::TTL_UNCACHEABLE;
169 }
170
171 return $res;
172 }
173 );
174 }
175
176 /**
177 * Get short description URL for a file based on the page ID.
178 *
179 * @return string
180 * @throws DBUnexpectedError
181 * @since 1.27
182 */
183 public function getDescriptionShortUrl() {
184 $dbr = $this->repo->getReplicaDB();
185 $pageId = $dbr->selectField(
186 'page',
187 'page_id',
188 [
189 'page_namespace' => NS_FILE,
190 'page_title' => $this->title->getDBkey()
191 ]
192 );
193
194 if ( $pageId !== false ) {
195 $url = $this->repo->makeUrl( [ 'curid' => $pageId ] );
196 if ( $url !== false ) {
197 return $url;
198 }
199 }
200 return null;
201 }
202
203 }