MediaWiki REL1_35
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 // @phan-suppress-next-line PhanSuspiciousBinaryAddLists It's intentional
89 list( $proto, $path ) = explode( '://', $url, 2 ) + [ null, null ];
90 if ( $proto !== 'memory' ) {
91 throw new UnexpectedValueException( "Got URL of protocol '$proto', not 'memory'." );
92 } elseif ( $path === null ) {
93 throw new UnexpectedValueException( "URL is missing path component." );
94 }
95
96 $parts = explode( '/', $path );
97 if ( count( $parts ) > 2 ) {
98 throw new UnexpectedValueException( "Too components in URL '$path'." );
99 }
100
101 return [ $parts[0], $parts[1] ?? null ];
102 }
103}
Key/value blob storage for a particular storage medium type (e.g.
array $params
Usage context options for this instance.
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.