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