Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ForeignDBFile
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 8
240
0.00% covered (danger)
0.00%
0 / 1
 getRepo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 publish
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 restore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deleteFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 move
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescriptionUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescriptionText
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
42
 getDescriptionShortUrl
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21use MediaWiki\MediaWikiServices;
22use MediaWiki\Status\Status;
23use MediaWiki\Title\Title;
24use MediaWiki\User\UserIdentity;
25use Wikimedia\Rdbms\DBUnexpectedError;
26
27/**
28 * Foreign file from a reachable database in the same wiki farm.
29 *
30 * @ingroup FileAbstraction
31 */
32class ForeignDBFile extends LocalFile {
33
34    /**
35     * @return ForeignDBRepo|false
36     */
37    public function getRepo() {
38        return $this->repo;
39    }
40
41    /**
42     * @param string $srcPath
43     * @param int $flags
44     * @param array $options
45     * @return Status
46     */
47    public function publish( $srcPath, $flags = 0, array $options = [] ) {
48        $this->readOnlyError();
49    }
50
51    /**
52     * @param int[] $versions
53     * @param bool $unsuppress
54     * @return Status
55     */
56    public function restore( $versions = [], $unsuppress = false ) {
57        $this->readOnlyError();
58    }
59
60    /**
61     * @param string $reason
62     * @param UserIdentity $user
63     * @param bool $suppress
64     * @return Status
65     */
66    public function deleteFile( $reason, UserIdentity $user, $suppress = false ) {
67        $this->readOnlyError();
68    }
69
70    /**
71     * @param Title $target
72     * @return Status
73     */
74    public function move( $target ) {
75        $this->readOnlyError();
76    }
77
78    /**
79     * @return string
80     */
81    public function getDescriptionUrl() {
82        // Restore remote behavior
83        return File::getDescriptionUrl();
84    }
85
86    /**
87     * @param Language|null $lang Optional language to fetch description in.
88     * @return string|false
89     */
90    public function getDescriptionText( Language $lang = null ) {
91        global $wgLang;
92
93        if ( !$this->repo->fetchDescription ) {
94            return false;
95        }
96
97        $lang ??= $wgLang;
98        $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $lang->getCode() );
99        if ( !$renderUrl ) {
100            return false;
101        }
102
103        $touched = $this->repo->getReplicaDB()->newSelectQueryBuilder()
104            ->select( 'page_touched' )
105            ->from( 'page' )
106            ->where( [ 'page_namespace' => NS_FILE, 'page_title' => $this->title->getDBkey() ] )
107            ->caller( __METHOD__ )->fetchField();
108        if ( $touched === false ) {
109            return false; // no description page
110        }
111
112        $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
113        $fname = __METHOD__;
114
115        return $cache->getWithSetCallback(
116            $this->repo->getLocalCacheKey(
117                'file-foreign-description',
118                $lang->getCode(),
119                md5( $this->getName() ),
120                $touched
121            ),
122            $this->repo->descriptionCacheExpiry ?: $cache::TTL_UNCACHEABLE,
123            static function ( $oldValue, &$ttl, array &$setOpts ) use ( $renderUrl, $fname ) {
124                wfDebug( "Fetching shared description from $renderUrl" );
125                $res = MediaWikiServices::getInstance()->getHttpRequestFactory()->
126                    get( $renderUrl, [], $fname );
127                if ( !$res ) {
128                    $ttl = WANObjectCache::TTL_UNCACHEABLE;
129                }
130
131                return $res;
132            }
133        );
134    }
135
136    /**
137     * Get short description URL for a file based on the page ID.
138     *
139     * @return string|null
140     * @throws DBUnexpectedError
141     * @since 1.27
142     */
143    public function getDescriptionShortUrl() {
144        $dbr = $this->repo->getReplicaDB();
145        $pageId = $dbr->newSelectQueryBuilder()
146            ->select( 'page_id' )
147            ->from( 'page' )
148            ->where( [ 'page_namespace' => NS_FILE, 'page_title' => $this->title->getDBkey() ] )
149            ->caller( __METHOD__ )->fetchField();
150
151        if ( $pageId !== false ) {
152            $url = $this->repo->makeUrl( [ 'curid' => $pageId ] );
153            if ( $url !== false ) {
154                return $url;
155            }
156        }
157        return null;
158    }
159
160}