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