Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / externalstore / ExternalStoreMwstore.php
1 <?php
2 /**
3 * External storage in a file backend.
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 */
22
23 /**
24 * File backend accessible external objects.
25 *
26 * In this system, each store "location" maps to the name of a file backend.
27 * The file backends must be defined in $wgFileBackends and must be global
28 * and fully qualified with a global "wikiId" prefix in the configuration.
29 *
30 * @ingroup ExternalStorage
31 * @since 1.21
32 */
33 class ExternalStoreMwstore extends ExternalStoreMedium {
34 /** @var FileBackendGroup */
35 private $fbGroup;
36
37 /**
38 * @see ExternalStoreMedium::__construct()
39 * @param array $params Additional parameters include:
40 * - fbGroup: a FileBackendGroup instance
41 */
42 public function __construct( array $params ) {
43 parent::__construct( $params );
44 if ( !isset( $params['fbGroup'] ) || !( $params['fbGroup'] instanceof FileBackendGroup ) ) {
45 throw new InvalidArgumentException( "FileBackendGroup required in 'fbGroup' field." );
46 }
47 $this->fbGroup = $params['fbGroup'];
48 }
49
50 /**
51 * The URL returned is of the form of the form mwstore://backend/container/wiki/id
52 *
53 * @see ExternalStoreMedium::fetchFromURL()
54 * @param string $url
55 * @return bool
56 */
57 public function fetchFromURL( $url ) {
58 $be = $this->fbGroup->backendFromPath( $url );
59 if ( $be instanceof FileBackend ) {
60 // We don't need "latest" since objects are immutable and
61 // backends should at least have "read-after-create" consistency.
62 return $be->getFileContents( [ 'src' => $url ] );
63 }
64
65 return false;
66 }
67
68 /**
69 * Fetch data from given external store URLs.
70 * The URL returned is of the form of the form mwstore://backend/container/wiki/id
71 *
72 * @param array $urls An array of external store URLs
73 * @return array A map from url to stored content. Failed results are not represented.
74 */
75 public function batchFetchFromURLs( array $urls ) {
76 $pathsByBackend = [];
77 foreach ( $urls as $url ) {
78 $be = $this->fbGroup->backendFromPath( $url );
79 if ( $be instanceof FileBackend ) {
80 $pathsByBackend[$be->getName()][] = $url;
81 }
82 }
83 $blobs = [];
84 foreach ( $pathsByBackend as $backendName => $paths ) {
85 $be = $this->fbGroup->get( $backendName );
86 $blobs += $be->getFileContentsMulti( [ 'srcs' => $paths ] );
87 }
88
89 return $blobs;
90 }
91
92 /**
93 * @inheritDoc
94 */
95 public function store( $backend, $data ) {
96 $be = $this->fbGroup->get( $backend );
97 // Get three random base 36 characters to act as shard directories
98 $rand = Wikimedia\base_convert( mt_rand( 0, 46655 ), 10, 36, 3 );
99 // Make sure ID is roughly lexicographically increasing for performance
100 $id = str_pad( UIDGenerator::newTimestampedUID128( 32 ), 26, '0', STR_PAD_LEFT );
101 // Segregate items by DB domain ID for the sake of bookkeeping
102 $domain = $this->isDbDomainExplicit
103 ? $this->dbDomain
104 // @FIXME: this does not include the schema for b/c but it ideally should
105 : WikiMap::getWikiIdFromDbDomain( $this->dbDomain );
106 $url = $be->getContainerStoragePath( 'data' ) . '/' . rawurlencode( $domain );
107 // Use directory/container sharding
108 $url .= ( $be instanceof FSFileBackend )
109 ? "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}" // keep directories small
110 : "/{$rand[0]}/{$rand[1]}/{$id}"; // container sharding is only 2-levels
111
112 $be->prepare( [ 'dir' => dirname( $url ), 'noAccess' => 1, 'noListing' => 1 ] );
113 $status = $be->create( [ 'dst' => $url, 'content' => $data ] );
114
115 if ( $status->isOK() ) {
116 return $url;
117 }
118
119 throw new MWException( __METHOD__ . ": operation failed: $status" );
120 }
121
122 public function isReadOnly( $backend ) {
123 if ( parent::isReadOnly( $backend ) ) {
124 return true;
125 }
126
127 $be = $this->fbGroup->get( $backend );
128
129 return $be ? $be->isReadOnly() : false;
130 }
131 }