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 __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 }
batchFetchFromURLs(array $urls)
__construct(array $params)
Key/value blob storage for a particular storage medium type (e.g.
static array [] $data
Map of (location => DB domain => id => value)
string $dbDomain
Default database domain to store content under.
store( $location, $data)
array $params
Usage context options for this instance.
clear()
Remove all data from memory for this domain.
Process memory based external objects for testing.