MediaWiki master
ExternalStoreMwstore.php
Go to the documentation of this file.
1<?php
22
36 private $fbGroup;
37
43 public function __construct( array $params ) {
44 parent::__construct( $params );
45 if ( !isset( $params['fbGroup'] ) || !( $params['fbGroup'] instanceof FileBackendGroup ) ) {
46 throw new InvalidArgumentException( "FileBackendGroup required in 'fbGroup' field." );
47 }
48 $this->fbGroup = $params['fbGroup'];
49 }
50
58 public function fetchFromURL( $url ) {
59 $be = $this->fbGroup->backendFromPath( $url );
60 if ( $be instanceof FileBackend ) {
61 // We don't need "latest" since objects are immutable and
62 // backends should at least have "read-after-create" consistency.
63 return $be->getFileContents( [ 'src' => $url ] );
64 }
65
66 return false;
67 }
68
76 public function batchFetchFromURLs( array $urls ) {
77 $pathsByBackend = [];
78 foreach ( $urls as $url ) {
79 $be = $this->fbGroup->backendFromPath( $url );
80 if ( $be instanceof FileBackend ) {
81 $pathsByBackend[$be->getName()][] = $url;
82 }
83 }
84 $blobs = [];
85 foreach ( $pathsByBackend as $backendName => $paths ) {
86 $be = $this->fbGroup->get( $backendName );
87 $blobs += $be->getFileContentsMulti( [ 'srcs' => $paths ] );
88 }
89
90 return $blobs;
91 }
92
93 public function store( $backend, $data ) {
94 $be = $this->fbGroup->get( $backend );
95 // Get three random base 36 characters to act as shard directories
96 $rand = Wikimedia\base_convert( (string)mt_rand( 0, 46655 ), 10, 36, 3 );
97 // Make sure ID is roughly lexicographically increasing for performance
98 $id = str_pad( UIDGenerator::newTimestampedUID128( 32 ), 26, '0', STR_PAD_LEFT );
99 // Segregate items by DB domain ID for the sake of bookkeeping
100 $domain = $this->isDbDomainExplicit
101 ? $this->dbDomain
102 // @FIXME: this does not include the schema for b/c but it ideally should
103 : WikiMap::getWikiIdFromDbDomain( $this->dbDomain );
104 $url = $be->getContainerStoragePath( 'data' ) . '/' . rawurlencode( $domain );
105 // Use directory/container sharding
106 $url .= ( $be instanceof FSFileBackend )
107 ? "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}" // keep directories small
108 : "/{$rand[0]}/{$rand[1]}/{$id}"; // container sharding is only 2-levels
109
110 $be->prepare( [ 'dir' => dirname( $url ), 'noAccess' => 1, 'noListing' => 1 ] );
111 $status = $be->create( [ 'dst' => $url, 'content' => $data ] );
112
113 if ( $status->isOK() ) {
114 return $url;
115 }
116
117 throw new ExternalStoreException( __METHOD__ . ": operation failed: $status" );
118 }
119
120 public function isReadOnly( $backend ) {
121 if ( parent::isReadOnly( $backend ) ) {
122 return true;
123 }
124
125 $be = $this->fbGroup->get( $backend );
126
127 return $be->isReadOnly();
128 }
129}
Base class for external storage.
array $params
Usage context options for this instance.
External storage in a FileBackend.
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
isReadOnly( $backend)
Check if a given location is read-only.
store( $backend, $data)
Insert a data item into a given location.
fetchFromURL( $url)
Fetch data from a given external store URL.
Class for a file system (FS) based file backend.
Class to handle file backend registration.
Base class for all file backend classes (including multi-write backends).
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
static newTimestampedUID128( $base=10)
Get a statistically unique 128-bit unsigned integer ID string.