MediaWiki REL1_37
ExternalStoreMemory.php
Go to the documentation of this file.
1<?php
35 private static $data = [];
37 private static $nextId = 0;
38
39 public function fetchFromURL( $url ) {
40 list( $location, $id ) = self::getURLComponents( $url );
41 if ( $id === null ) {
42 throw new UnexpectedValueException( "Missing ID in URL component." );
43 }
44
45 return self::$data[$location][$this->dbDomain][$id] ?? false;
46 }
47
48 public function batchFetchFromURLs( array $urls ) {
49 $blobs = [];
50 foreach ( $urls as $url ) {
51 $blob = $this->fetchFromURL( $url );
52 if ( $blob !== false ) {
53 $blobs[$url] = $blob;
54 }
55 }
56
57 return $blobs;
58 }
59
60 public function store( $location, $data ) {
61 $index = ++self::$nextId;
62 self::$data[$location][$this->dbDomain][$index] = $data;
63
64 return "memory://$location/$index";
65 }
66
70 public function clear() {
71 foreach ( self::$data as &$dataForLocation ) {
72 unset( $dataForLocation[$this->dbDomain] );
73 }
74 unset( $dataForLocation );
75 self::$data = array_filter( self::$data, 'count' );
76 self::$nextId = 0;
77 }
78
83 private function getURLComponents( $url ) {
84 // @phan-suppress-next-line PhanSuspiciousBinaryAddLists It's intentional
85 list( $proto, $path ) = explode( '://', $url, 2 ) + [ null, null ];
86 if ( $proto !== 'memory' ) {
87 throw new UnexpectedValueException( "Got URL of protocol '$proto', not 'memory'." );
88 } elseif ( $path === null ) {
89 throw new UnexpectedValueException( "URL is missing path component." );
90 }
91
92 $parts = explode( '/', $path );
93 if ( count( $parts ) > 2 ) {
94 throw new UnexpectedValueException( "Too components in URL '$path'." );
95 }
96
97 return [ $parts[0], $parts[1] ?? null ];
98 }
99}
Key/value blob storage for a particular storage medium type (e.g.
string $dbDomain
Default database domain to store content under.
Process memory based external objects for testing.
clear()
Remove all data from memory for this domain.
static array[] $data
Map of (location => DB domain => id => value)
fetchFromURL( $url)
Fetch data from given external store URL.
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
store( $location, $data)
Insert a data item into a given location.