Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / externalstore / ExternalStoreMemory.php
blobd7c2a8da995e5684ae69e01dc46fbc0d0b507391
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 /**
22 * External storage in PHP process memory for testing.
24 * In this system, each store "location" is separate PHP array.
25 * URLs are of the form "memory://location/id". The id/value pairs
26 * at each location are segregated by DB domain ID.
28 * @see ExternalStoreAccess
29 * @ingroup ExternalStorage
30 * @since 1.33
32 class ExternalStoreMemory extends ExternalStoreMedium {
33 /** @var array[] Map of (location => DB domain => id => value) */
34 private static $data = [];
35 /** @var int */
36 private static $nextId = 0;
38 public function fetchFromURL( $url ) {
39 [ $location, $id ] = self::getURLComponents( $url );
40 if ( $id === null ) {
41 throw new UnexpectedValueException( "Missing ID in URL component." );
44 return self::$data[$location][$this->dbDomain][$id] ?? false;
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;
56 return $blobs;
59 public function store( $location, $data ) {
60 $index = ++self::$nextId;
61 self::$data[$location][$this->dbDomain][$index] = $data;
63 return "memory://$location/$index";
66 /**
67 * Remove all data from memory for this domain
69 public function clear() {
70 foreach ( self::$data as &$dataForLocation ) {
71 unset( $dataForLocation[$this->dbDomain] );
73 unset( $dataForLocation );
74 self::$data = array_filter( self::$data, 'count' );
75 self::$nextId = 0;
78 /**
79 * @param string $url
80 * @return array (location, ID or null)
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." );
91 $parts = explode( '/', $path );
92 if ( count( $parts ) > 2 ) {
93 throw new UnexpectedValueException( "Too components in URL '$path'." );
96 return [ $parts[0], $parts[1] ?? null ];