Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / filerepo / file / ForeignDBFile.php
blobae4ac58b04d47b2075017991060ba562ea30499d
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Language\Language;
22 use MediaWiki\MediaWikiServices;
23 use MediaWiki\Status\Status;
24 use MediaWiki\Title\Title;
25 use MediaWiki\User\UserIdentity;
26 use Wikimedia\ObjectCache\WANObjectCache;
27 use Wikimedia\Rdbms\DBUnexpectedError;
29 /**
30 * Foreign file from a reachable database in the same wiki farm.
32 * @ingroup FileAbstraction
34 class ForeignDBFile extends LocalFile {
36 /**
37 * @return ForeignDBRepo|false
39 public function getRepo() {
40 return $this->repo;
43 /**
44 * @param string $srcPath
45 * @param int $flags
46 * @param array $options
47 * @return Status
49 public function publish( $srcPath, $flags = 0, array $options = [] ) {
50 $this->readOnlyError();
53 /**
54 * @param int[] $versions
55 * @param bool $unsuppress
56 * @return Status
58 public function restore( $versions = [], $unsuppress = false ) {
59 $this->readOnlyError();
62 /**
63 * @param string $reason
64 * @param UserIdentity $user
65 * @param bool $suppress
66 * @return Status
68 public function deleteFile( $reason, UserIdentity $user, $suppress = false ) {
69 $this->readOnlyError();
72 /**
73 * @param Title $target
74 * @return Status
76 public function move( $target ) {
77 $this->readOnlyError();
80 /**
81 * @return string
83 public function getDescriptionUrl() {
84 // Restore remote behavior
85 return File::getDescriptionUrl();
88 /**
89 * @param Language|null $lang Optional language to fetch description in.
90 * @return string|false
92 public function getDescriptionText( ?Language $lang = null ) {
93 global $wgLang;
95 if ( !$this->repo->fetchDescription ) {
96 return false;
99 $lang ??= $wgLang;
100 $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $lang->getCode() );
101 if ( !$renderUrl ) {
102 return false;
105 $touched = $this->repo->getReplicaDB()->newSelectQueryBuilder()
106 ->select( 'page_touched' )
107 ->from( 'page' )
108 ->where( [ 'page_namespace' => NS_FILE, 'page_title' => $this->title->getDBkey() ] )
109 ->caller( __METHOD__ )->fetchField();
110 if ( $touched === false ) {
111 return false; // no description page
114 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
115 $fname = __METHOD__;
117 return $cache->getWithSetCallback(
118 $this->repo->getLocalCacheKey(
119 'file-foreign-description',
120 $lang->getCode(),
121 md5( $this->getName() ),
122 $touched
124 $this->repo->descriptionCacheExpiry ?: $cache::TTL_UNCACHEABLE,
125 static function ( $oldValue, &$ttl, array &$setOpts ) use ( $renderUrl, $fname ) {
126 wfDebug( "Fetching shared description from $renderUrl" );
127 $res = MediaWikiServices::getInstance()->getHttpRequestFactory()->
128 get( $renderUrl, [], $fname );
129 if ( !$res ) {
130 $ttl = WANObjectCache::TTL_UNCACHEABLE;
133 return $res;
139 * Get short description URL for a file based on the page ID.
141 * @return string|null
142 * @throws DBUnexpectedError
143 * @since 1.27
145 public function getDescriptionShortUrl() {
146 $dbr = $this->repo->getReplicaDB();
147 $pageId = $dbr->newSelectQueryBuilder()
148 ->select( 'page_id' )
149 ->from( 'page' )
150 ->where( [ 'page_namespace' => NS_FILE, 'page_title' => $this->title->getDBkey() ] )
151 ->caller( __METHOD__ )->fetchField();
153 if ( $pageId !== false ) {
154 $url = $this->repo->makeUrl( [ 'curid' => $pageId ] );
155 if ( $url !== false ) {
156 return $url;
159 return null;