MediaWiki REL1_31
ExternalStoreMwstore.php
Go to the documentation of this file.
1<?php
41 public function fetchFromURL( $url ) {
42 $be = FileBackendGroup::singleton()->backendFromPath( $url );
43 if ( $be instanceof FileBackend ) {
44 // We don't need "latest" since objects are immutable and
45 // backends should at least have "read-after-create" consistency.
46 return $be->getFileContents( [ 'src' => $url ] );
47 }
48
49 return false;
50 }
51
59 public function batchFetchFromURLs( array $urls ) {
60 $pathsByBackend = [];
61 foreach ( $urls as $url ) {
62 $be = FileBackendGroup::singleton()->backendFromPath( $url );
63 if ( $be instanceof FileBackend ) {
64 $pathsByBackend[$be->getName()][] = $url;
65 }
66 }
67 $blobs = [];
68 foreach ( $pathsByBackend as $backendName => $paths ) {
69 $be = FileBackendGroup::singleton()->get( $backendName );
70 $blobs = $blobs + $be->getFileContentsMulti( [ 'srcs' => $paths ] );
71 }
72
73 return $blobs;
74 }
75
76 public function store( $backend, $data ) {
77 $be = FileBackendGroup::singleton()->get( $backend );
78 if ( $be instanceof FileBackend ) {
79 // Get three random base 36 characters to act as shard directories
80 $rand = Wikimedia\base_convert( mt_rand( 0, 46655 ), 10, 36, 3 );
81 // Make sure ID is roughly lexicographically increasing for performance
82 $id = str_pad( UIDGenerator::newTimestampedUID128( 32 ), 26, '0', STR_PAD_LEFT );
83 // Segregate items by wiki ID for the sake of bookkeeping
84 $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : wfWikiID();
85
86 $url = $be->getContainerStoragePath( 'data' ) . '/' . rawurlencode( $wiki );
87 $url .= ( $be instanceof FSFileBackend )
88 ? "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}" // keep directories small
89 : "/{$rand[0]}/{$rand[1]}/{$id}"; // container sharding is only 2-levels
90
91 $be->prepare( [ 'dir' => dirname( $url ), 'noAccess' => 1, 'noListing' => 1 ] );
92 if ( $be->create( [ 'dst' => $url, 'content' => $data ] )->isOK() ) {
93 return $url;
94 }
95 }
96
97 return false;
98 }
99
100 public function isReadOnly( $backend ) {
101 $be = FileBackendGroup::singleton()->get( $backend );
102
103 return $be ? $be->isReadOnly() : false;
104 }
105}
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Accessable external objects in a particular storage medium.
File backend accessible external objects.
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)
The URL returned is of the form of the form mwstore://backend/container/wiki/id.
Class for a file system (FS) based file backend.
Base class for all file backend classes (including multi-write backends).
static newTimestampedUID128( $base=10)
Get a statistically unique 128-bit unsigned integer ID string.