MediaWiki  1.34.0
ExternalStoreMemory.php
Go to the documentation of this file.
1 <?php
35  private static $data = [];
37  private static $nextId = 0;
38 
39  public function __construct( array $params ) {
40  parent::__construct( $params );
41  }
42 
43  public function fetchFromURL( $url ) {
44  list( $location, $id ) = self::getURLComponents( $url );
45  if ( $id === null ) {
46  throw new UnexpectedValueException( "Missing ID in URL component." );
47  }
48 
49  return self::$data[$location][$this->dbDomain][$id] ?? false;
50  }
51 
52  public function batchFetchFromURLs( array $urls ) {
53  $blobs = [];
54  foreach ( $urls as $url ) {
55  $blob = $this->fetchFromURL( $url );
56  if ( $blob !== false ) {
57  $blobs[$url] = $blob;
58  }
59  }
60 
61  return $blobs;
62  }
63 
64  public function store( $location, $data ) {
65  $index = ++self::$nextId;
66  self::$data[$location][$this->dbDomain][$index] = $data;
67 
68  return "memory://$location/$index";
69  }
70 
74  public function clear() {
75  foreach ( self::$data as &$dataForLocation ) {
76  unset( $dataForLocation[$this->dbDomain] );
77  }
78  unset( $dataForLocation );
79  self::$data = array_filter( self::$data, 'count' );
80  self::$nextId = 0;
81  }
82 
87  private function getURLComponents( $url ) {
88  list( $proto, $path ) = explode( '://', $url, 2 ) + [ null, null ];
89  if ( $proto !== 'memory' ) {
90  throw new UnexpectedValueException( "Got URL of protocol '$proto', not 'memory'." );
91  } elseif ( $path === null ) {
92  throw new UnexpectedValueException( "URL is missing path component." );
93  }
94 
95  $parts = explode( '/', $path );
96  if ( count( $parts ) > 2 ) {
97  throw new UnexpectedValueException( "Too components in URL '$path'." );
98  }
99 
100  return [ $parts[0], $parts[1] ?? null ];
101  }
102 }
ExternalStoreMedium
Key/value blob storage for a particular storage medium type (e.g.
Definition: ExternalStoreMedium.php:38
ExternalStoreMemory\__construct
__construct(array $params)
Definition: ExternalStoreMemory.php:39
ExternalStoreMedium\$params
array $params
Usage context options for this instance.
Definition: ExternalStoreMedium.php:40
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:65
ExternalStoreMemory\store
store( $location, $data)
Insert a data item into a given location.
Definition: ExternalStoreMemory.php:64
ExternalStoreMemory\fetchFromURL
fetchFromURL( $url)
Fetch data from given external store URL.
Definition: ExternalStoreMemory.php:43
ExternalStoreMemory\batchFetchFromURLs
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
Definition: ExternalStoreMemory.php:52
ExternalStoreMemory\clear
clear()
Remove all data from memory for this domain.
Definition: ExternalStoreMemory.php:74
ExternalStoreMemory\getURLComponents
getURLComponents( $url)
Definition: ExternalStoreMemory.php:87
$path
$path
Definition: NoLocalSettings.php:25
ExternalStoreMemory
Process memory based external objects for testing.
Definition: ExternalStoreMemory.php:33