MediaWiki master
ExternalStoreMwstore.php
Go to the documentation of this file.
1<?php
24
38 private $fbGroup;
39
45 public function __construct( array $params ) {
46 parent::__construct( $params );
47 if ( !isset( $params['fbGroup'] ) || !( $params['fbGroup'] instanceof FileBackendGroup ) ) {
48 throw new InvalidArgumentException( "FileBackendGroup required in 'fbGroup' field." );
49 }
50 $this->fbGroup = $params['fbGroup'];
51 }
52
60 public function fetchFromURL( $url ) {
61 $be = $this->fbGroup->backendFromPath( $url );
62 if ( $be instanceof FileBackend ) {
63 // We don't need "latest" since objects are immutable and
64 // backends should at least have "read-after-create" consistency.
65 return $be->getFileContents( [ 'src' => $url ] );
66 }
67
68 return false;
69 }
70
78 public function batchFetchFromURLs( array $urls ) {
79 $pathsByBackend = [];
80 foreach ( $urls as $url ) {
81 $be = $this->fbGroup->backendFromPath( $url );
82 if ( $be instanceof FileBackend ) {
83 $pathsByBackend[$be->getName()][] = $url;
84 }
85 }
86 $blobs = [];
87 foreach ( $pathsByBackend as $backendName => $paths ) {
88 $be = $this->fbGroup->get( $backendName );
89 $blobs += $be->getFileContentsMulti( [ 'srcs' => $paths ] );
90 }
91
92 return $blobs;
93 }
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( (string)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 ExternalStoreException( __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->isReadOnly();
130 }
131}
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.
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.
Base class for all file backend classes (including multi-write backends).