Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[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 /**
36 * @param string $srcPath
37 * @param int $flags
38 * @param array $options
39 * @return Status
40 * @throws MWException
41 */
42 function publish( $srcPath, $flags = 0, array $options = [] ) {
43 $this->readOnlyError();
44 }
45
46 /**
47 * @param string $oldver
48 * @param string $desc
49 * @param string $license
50 * @param string $copyStatus
51 * @param string $source
52 * @param bool $watch
53 * @param bool|string $timestamp
54 * @param User|null $user User object or null to use $wgUser
55 * @return bool
56 * @throws MWException
57 */
58 function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
59 $watch = false, $timestamp = false, User $user = null ) {
60 $this->readOnlyError();
61 }
62
63 /**
64 * @param array $versions
65 * @param bool $unsuppress
66 * @return Status
67 * @throws MWException
68 */
69 function restore( $versions = [], $unsuppress = false ) {
70 $this->readOnlyError();
71 }
72
73 /**
74 * @param string $reason
75 * @param bool $suppress
76 * @param User|null $user
77 * @return Status
78 * @throws MWException
79 */
80 function delete( $reason, $suppress = false, $user = null ) {
81 $this->readOnlyError();
82 }
83
84 /**
85 * @param Title $target
86 * @return Status
87 * @throws MWException
88 */
89 function move( $target ) {
90 $this->readOnlyError();
91 }
92
93 /**
94 * @return string
95 */
96 function getDescriptionUrl() {
97 // Restore remote behavior
98 return File::getDescriptionUrl();
99 }
100
101 /**
102 * @param Language|null $lang Optional language to fetch description in.
103 * @return string|false
104 */
105 function getDescriptionText( Language $lang = null ) {
106 global $wgLang;
107
108 if ( !$this->repo->fetchDescription ) {
109 return false;
110 }
111
112 $lang = $lang ?? $wgLang;
113 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $lang->getCode() );
114 if ( !$renderUrl ) {
115 return false;
116 }
117
118 $touched = $this->repo->getReplicaDB()->selectField(
119 'page',
120 'page_touched',
121 [
122 'page_namespace' => NS_FILE,
123 'page_title' => $this->title->getDBkey()
124 ]
125 );
126 if ( $touched === false ) {
127 return false; // no description page
128 }
129
130 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
131 $fname = __METHOD__;
132
133 return $cache->getWithSetCallback(
134 $this->repo->getLocalCacheKey(
135 'ForeignFileDescription',
136 $lang->getCode(),
137 md5( $this->getName() ),
138 $touched
139 ),
140 $this->repo->descriptionCacheExpiry ?: $cache::TTL_UNCACHEABLE,
141 function ( $oldValue, &$ttl, array &$setOpts ) use ( $renderUrl, $fname ) {
142 wfDebug( "Fetching shared description from $renderUrl\n" );
143 $res = MediaWikiServices::getInstance()->getHttpRequestFactory()->
144 get( $renderUrl, [], $fname );
145 if ( !$res ) {
146 $ttl = WANObjectCache::TTL_UNCACHEABLE;
147 }
148
149 return $res;
150 }
151 );
152 }
153
154 /**
155 * Get short description URL for a file based on the page ID.
156 *
157 * @return string
158 * @throws DBUnexpectedError
159 * @since 1.27
160 */
161 public function getDescriptionShortUrl() {
162 $dbr = $this->repo->getReplicaDB();
163 $pageId = $dbr->selectField(
164 'page',
165 'page_id',
166 [
167 'page_namespace' => NS_FILE,
168 'page_title' => $this->title->getDBkey()
169 ]
170 );
171
172 if ( $pageId !== false ) {
173 $url = $this->repo->makeUrl( [ 'curid' => $pageId ] );
174 if ( $url !== false ) {
175 return $url;
176 }
177 }
178 return null;
179 }
180
181 }