MediaWiki  master
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( 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 }
ExternalStoreMedium
Base class for external storage.
Definition: ExternalStoreMedium.php:36
ExternalStoreMwstore\__construct
__construct(array $params)
Definition: ExternalStoreMwstore.php:41
ExternalStoreMedium\$params
array $params
Usage context options for this instance.
Definition: ExternalStoreMedium.php:38
UIDGenerator\newTimestampedUID128
static newTimestampedUID128( $base=10)
Get a statistically unique 128-bit unsigned integer ID string.
Definition: UIDGenerator.php:72
FileBackendGroup
Class to handle file backend registration.
Definition: FileBackendGroup.php:36
FileBackend
Base class for all file backend classes (including multi-write backends).
Definition: FileBackend.php:99
MWException
MediaWiki exception.
Definition: MWException.php:29
WikiMap\getWikiIdFromDbDomain
static getWikiIdFromDbDomain( $domain)
Get the wiki ID of a database domain.
Definition: WikiMap.php:269
ExternalStoreMwstore\fetchFromURL
fetchFromURL( $url)
Fetch data from a given external store URL.
Definition: ExternalStoreMwstore.php:56
ExternalStoreMwstore
External storage in a FileBackend.
Definition: ExternalStoreMwstore.php:32
ExternalStoreMwstore\$fbGroup
FileBackendGroup $fbGroup
Definition: ExternalStoreMwstore.php:34
ExternalStoreMwstore\isReadOnly
isReadOnly( $backend)
Check if a given location is read-only.
Definition: ExternalStoreMwstore.php:118
ExternalStoreMwstore\batchFetchFromURLs
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
Definition: ExternalStoreMwstore.php:74
FSFileBackend
Class for a file system (FS) based file backend.
Definition: FSFileBackend.php:64
ExternalStoreMwstore\store
store( $backend, $data)
Insert a data item into a given location.
Definition: ExternalStoreMwstore.php:91