MediaWiki master
ExternalStoreMemory.php
Go to the documentation of this file.
1<?php
20 private static $data = [];
22 private static $nextId = 0;
23
25 public function fetchFromURL( $url ) {
26 [ $location, $id ] = self::getURLComponents( $url );
27 if ( $id === null ) {
28 throw new UnexpectedValueException( "Missing ID in URL component." );
29 }
30
31 return self::$data[$location][$this->dbDomain][$id] ?? false;
32 }
33
35 public function batchFetchFromURLs( array $urls ) {
36 $blobs = [];
37 foreach ( $urls as $url ) {
38 $blob = $this->fetchFromURL( $url );
39 if ( $blob !== false ) {
40 $blobs[$url] = $blob;
41 }
42 }
43
44 return $blobs;
45 }
46
48 public function store( $location, $data ) {
49 $index = ++self::$nextId;
50 self::$data[$location][$this->dbDomain][$index] = $data;
51
52 return "memory://$location/$index";
53 }
54
58 public function clear() {
59 foreach ( self::$data as &$dataForLocation ) {
60 unset( $dataForLocation[$this->dbDomain] );
61 }
62 unset( $dataForLocation );
63 self::$data = array_filter( self::$data, 'count' );
64 self::$nextId = 0;
65 }
66
71 private function getURLComponents( $url ) {
72 // @phan-suppress-next-line PhanSuspiciousBinaryAddLists It's intentional
73 [ $proto, $path ] = explode( '://', $url, 2 ) + [ null, null ];
74 if ( $proto !== 'memory' ) {
75 throw new UnexpectedValueException( "Got URL of protocol '$proto', not 'memory'." );
76 } elseif ( $path === null ) {
77 throw new UnexpectedValueException( "URL is missing path component." );
78 }
79
80 $parts = explode( '/', $path );
81 if ( count( $parts ) > 2 ) {
82 throw new UnexpectedValueException( "Too components in URL '$path'." );
83 }
84
85 return [ $parts[0], $parts[1] ?? null ];
86 }
87}
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.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...
store( $location, $data)
Insert a data item into a given location.string|bool The URL of the stored data item,...