MediaWiki master
ExternalStoreMemory.php
Go to the documentation of this file.
1<?php
8
9use UnexpectedValueException;
10
24 private static $data = [];
26 private static $nextId = 0;
27
29 public function fetchFromURL( $url ) {
30 [ $location, $id ] = self::getURLComponents( $url );
31 if ( $id === null ) {
32 throw new UnexpectedValueException( "Missing ID in URL component." );
33 }
34
35 return self::$data[$location][$this->dbDomain][$id] ?? false;
36 }
37
39 public function batchFetchFromURLs( array $urls ) {
40 $blobs = [];
41 foreach ( $urls as $url ) {
42 $blob = $this->fetchFromURL( $url );
43 if ( $blob !== false ) {
44 $blobs[$url] = $blob;
45 }
46 }
47
48 return $blobs;
49 }
50
52 public function store( $location, $data ) {
53 $index = ++self::$nextId;
54 self::$data[$location][$this->dbDomain][$index] = $data;
55
56 return "memory://$location/$index";
57 }
58
62 public function clear() {
63 foreach ( self::$data as &$dataForLocation ) {
64 unset( $dataForLocation[$this->dbDomain] );
65 }
66 unset( $dataForLocation );
67 self::$data = array_filter( self::$data, 'count' );
68 self::$nextId = 0;
69 }
70
75 private function getURLComponents( $url ) {
76 // @phan-suppress-next-line PhanSuspiciousBinaryAddLists It's intentional
77 [ $proto, $path ] = explode( '://', $url, 2 ) + [ null, null ];
78 if ( $proto !== 'memory' ) {
79 throw new UnexpectedValueException( "Got URL of protocol '$proto', not 'memory'." );
80 } elseif ( $path === null ) {
81 throw new UnexpectedValueException( "URL is missing path component." );
82 }
83
84 $parts = explode( '/', $path );
85 if ( count( $parts ) > 2 ) {
86 throw new UnexpectedValueException( "Too components in URL '$path'." );
87 }
88
89 return [ $parts[0], $parts[1] ?? null ];
90 }
91}
92
94class_alias( ExternalStoreMemory::class, 'ExternalStoreMemory' );
string $dbDomain
Default database domain to store content under.
External storage in PHP process memory for testing.
store( $location, $data)
Insert a data item into a given location.string|bool The URL of the stored data item,...
clear()
Remove all data from memory for this domain.
fetchFromURL( $url)
Fetch data from given external store URL.string|bool The text stored or false on error
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.string[] Map of (url => text) for the URLs where data was a...