MediaWiki  1.34.0
ExternalStoreMwstore.php
Go to the documentation of this file.
1 <?php
35  private $fbGroup;
36 
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 
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 
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 
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 }
ExternalStoreMedium
Key/value blob storage for a particular storage medium type (e.g.
Definition: ExternalStoreMedium.php:38
ExternalStoreMwstore\__construct
__construct(array $params)
Definition: ExternalStoreMwstore.php:42
ExternalStoreMedium\$params
array $params
Usage context options for this instance.
Definition: ExternalStoreMedium.php:40
UIDGenerator\newTimestampedUID128
static newTimestampedUID128( $base=10)
Get a statistically unique 128-bit unsigned integer ID string.
Definition: UIDGenerator.php:170
FileBackendGroup
Class to handle file backend registration.
Definition: FileBackendGroup.php:33
FileBackend
Base class for all file backend classes (including multi-write backends).
Definition: FileBackend.php:94
MWException
MediaWiki exception.
Definition: MWException.php:26
WikiMap\getWikiIdFromDbDomain
static getWikiIdFromDbDomain( $domain)
Get the wiki ID of a database domain.
Definition: WikiMap.php:268
ExternalStoreMwstore\fetchFromURL
fetchFromURL( $url)
The URL returned is of the form of the form mwstore://backend/container/wiki/id.
Definition: ExternalStoreMwstore.php:57
ExternalStoreMwstore
File backend accessible external objects.
Definition: ExternalStoreMwstore.php:33
ExternalStoreMwstore\$fbGroup
FileBackendGroup $fbGroup
Definition: ExternalStoreMwstore.php:35
ExternalStoreMwstore\isReadOnly
isReadOnly( $backend)
Check if a given location is read-only.
Definition: ExternalStoreMwstore.php:122
$status
return $status
Definition: SyntaxHighlight.php:347
ExternalStoreMwstore\batchFetchFromURLs
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
Definition: ExternalStoreMwstore.php:75
FSFileBackend
Class for a file system (FS) based file backend.
Definition: FSFileBackend.php:62
ExternalStoreMwstore\store
store( $backend, $data)
Insert a data item into a given location.The location name The data item string|bool The URL of the s...
Definition: ExternalStoreMwstore.php:95