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