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