MediaWiki master
ExternalStoreMwstore.php
Go to the documentation of this file.
1<?php
12
26 private $fbGroup;
27
33 public function __construct( array $params ) {
34 parent::__construct( $params );
35 if ( !isset( $params['fbGroup'] ) || !( $params['fbGroup'] instanceof FileBackendGroup ) ) {
36 throw new InvalidArgumentException( "FileBackendGroup required in 'fbGroup' field." );
37 }
38 $this->fbGroup = $params['fbGroup'];
39 }
40
48 public function fetchFromURL( $url ) {
49 $be = $this->fbGroup->backendFromPath( $url );
50 if ( $be instanceof FileBackend ) {
51 // We don't need "latest" since objects are immutable and
52 // backends should at least have "read-after-create" consistency.
53 return $be->getFileContents( [ 'src' => $url ] );
54 }
55
56 return false;
57 }
58
66 public function batchFetchFromURLs( array $urls ) {
67 $pathsByBackend = [];
68 foreach ( $urls as $url ) {
69 $be = $this->fbGroup->backendFromPath( $url );
70 if ( $be instanceof FileBackend ) {
71 $pathsByBackend[$be->getName()][] = $url;
72 }
73 }
74 $blobs = [];
75 foreach ( $pathsByBackend as $backendName => $paths ) {
76 $be = $this->fbGroup->get( $backendName );
77 $blobs += $be->getFileContentsMulti( [ 'srcs' => $paths ] );
78 }
79
80 return $blobs;
81 }
82
84 public function store( $backend, $data ) {
85 $be = $this->fbGroup->get( $backend );
86 // Get three random base 36 characters to act as shard directories
87 $rand = Wikimedia\base_convert( (string)mt_rand( 0, 46655 ), 10, 36, 3 );
88 // Make sure ID is roughly lexicographically increasing for performance
89 $gen = MediaWikiServices::getInstance()->getGlobalIdGenerator();
90 $id = str_pad( $gen->newTimestampedUID128( 32 ), 26, '0', STR_PAD_LEFT );
91 // Segregate items by DB domain ID for the sake of bookkeeping
92 $domain = $this->isDbDomainExplicit
93 ? $this->dbDomain
94 // @FIXME: this does not include the schema for b/c but it ideally should
95 : WikiMap::getWikiIdFromDbDomain( $this->dbDomain );
96 $url = $be->getContainerStoragePath( 'data' ) . '/' . rawurlencode( $domain );
97 // Use directory/container sharding
98 $url .= ( $be instanceof FSFileBackend )
99 ? "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}" // keep directories small
100 : "/{$rand[0]}/{$rand[1]}/{$id}"; // container sharding is only 2-levels
101
102 $be->prepare( [ 'dir' => dirname( $url ), 'noAccess' => 1, 'noListing' => 1 ] );
103 $status = $be->create( [ 'dst' => $url, 'content' => $data ] );
104
105 if ( $status->isOK() ) {
106 return $url;
107 }
108
109 throw new ExternalStoreException( __METHOD__ . ": operation failed: $status" );
110 }
111
113 public function isReadOnly( $backend ) {
114 if ( parent::isReadOnly( $backend ) ) {
115 return true;
116 }
117
118 $be = $this->fbGroup->get( $backend );
119
120 return $be->isReadOnly();
121 }
122}
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.bool Whether this location is read-only 1.31
store( $backend, $data)
Insert a data item into a given location.string|bool The URL of the stored data item,...
fetchFromURL( $url)
Fetch data from a given external store URL.
Class to handle file backend registration.
Service locator for MediaWiki core services.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:19
Class for a file system (FS) based file backend.
Base class for all file backend classes (including multi-write backends).