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