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
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
;
30 * Foreign file from a reachable database in the same wiki farm.
32 * @ingroup FileAbstraction
34 class ForeignDBFile
extends LocalFile
{
37 * @return ForeignDBRepo|false
39 public function getRepo() {
44 * @param string $srcPath
46 * @param array $options
49 public function publish( $srcPath, $flags = 0, array $options = [] ) {
50 $this->readOnlyError();
54 * @param int[] $versions
55 * @param bool $unsuppress
58 public function restore( $versions = [], $unsuppress = false ) {
59 $this->readOnlyError();
63 * @param string $reason
64 * @param UserIdentity $user
65 * @param bool $suppress
68 public function deleteFile( $reason, UserIdentity
$user, $suppress = false ) {
69 $this->readOnlyError();
73 * @param Title $target
76 public function move( $target ) {
77 $this->readOnlyError();
83 public function getDescriptionUrl() {
84 // Restore remote behavior
85 return File
::getDescriptionUrl();
89 * @param Language|null $lang Optional language to fetch description in.
90 * @return string|false
92 public function getDescriptionText( ?Language
$lang = null ) {
95 if ( !$this->repo
->fetchDescription
) {
100 $renderUrl = $this->repo
->getDescriptionRenderUrl( $this->getName(), $lang->getCode() );
105 $touched = $this->repo
->getReplicaDB()->newSelectQueryBuilder()
106 ->select( 'page_touched' )
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();
117 return $cache->getWithSetCallback(
118 $this->repo
->getLocalCacheKey(
119 'file-foreign-description',
121 md5( $this->getName() ),
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 );
130 $ttl = WANObjectCache
::TTL_UNCACHEABLE
;
139 * Get short description URL for a file based on the page ID.
141 * @return string|null
142 * @throws DBUnexpectedError
145 public function getDescriptionShortUrl() {
146 $dbr = $this->repo
->getReplicaDB();
147 $pageId = $dbr->newSelectQueryBuilder()
148 ->select( 'page_id' )
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 ) {