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