MediaWiki  master
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 }
ExternalStoreMedium
Key/value blob storage for a particular storage medium type (e.g.
Definition: ExternalStoreMedium.php:38
ExternalStoreMedium\$dbDomain
string $dbDomain
Default database domain to store content under.
Definition: ExternalStoreMedium.php:42
ExternalStoreMemory\$data
static array[] $data
Map of (location => DB domain => id => value)
Definition: ExternalStoreMemory.php:35
ExternalStoreMemory\$nextId
static int $nextId
Definition: ExternalStoreMemory.php:37
$blob
$blob
Definition: testCompression.php:70
ExternalStoreMemory\store
store( $location, $data)
Insert a data item into a given location.
Definition: ExternalStoreMemory.php:60
ExternalStoreMemory\fetchFromURL
fetchFromURL( $url)
Fetch data from given external store URL.
Definition: ExternalStoreMemory.php:39
ExternalStoreMemory\batchFetchFromURLs
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
Definition: ExternalStoreMemory.php:48
ExternalStoreMemory\clear
clear()
Remove all data from memory for this domain.
Definition: ExternalStoreMemory.php:70
ExternalStoreMemory\getURLComponents
getURLComponents( $url)
Definition: ExternalStoreMemory.php:83
$path
$path
Definition: NoLocalSettings.php:25
ExternalStoreMemory
Process memory based external objects for testing.
Definition: ExternalStoreMemory.php:33