MediaWiki REL1_28
MemoryFileBackend.php
Go to the documentation of this file.
1<?php
36 protected $files = [];
37
38 public function getFeatures() {
40 }
41
42 public function isPathUsableInternal( $storagePath ) {
43 return true;
44 }
45
46 protected function doCreateInternal( array $params ) {
47 $status = $this->newStatus();
48
49 $dst = $this->resolveHashKey( $params['dst'] );
50 if ( $dst === null ) {
51 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
52
53 return $status;
54 }
55
56 $this->files[$dst] = [
57 'data' => $params['content'],
58 'mtime' => wfTimestamp( TS_MW, time() )
59 ];
60
61 return $status;
62 }
63
64 protected function doStoreInternal( array $params ) {
65 $status = $this->newStatus();
66
67 $dst = $this->resolveHashKey( $params['dst'] );
68 if ( $dst === null ) {
69 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
70
71 return $status;
72 }
73
74 MediaWiki\suppressWarnings();
75 $data = file_get_contents( $params['src'] );
76 MediaWiki\restoreWarnings();
77 if ( $data === false ) { // source doesn't exist?
78 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
79
80 return $status;
81 }
82
83 $this->files[$dst] = [
84 'data' => $data,
85 'mtime' => wfTimestamp( TS_MW, time() )
86 ];
87
88 return $status;
89 }
90
91 protected function doCopyInternal( array $params ) {
92 $status = $this->newStatus();
93
94 $src = $this->resolveHashKey( $params['src'] );
95 if ( $src === null ) {
96 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
97
98 return $status;
99 }
100
101 $dst = $this->resolveHashKey( $params['dst'] );
102 if ( $dst === null ) {
103 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
104
105 return $status;
106 }
107
108 if ( !isset( $this->files[$src] ) ) {
109 if ( empty( $params['ignoreMissingSource'] ) ) {
110 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
111 }
112
113 return $status;
114 }
115
116 $this->files[$dst] = [
117 'data' => $this->files[$src]['data'],
118 'mtime' => wfTimestamp( TS_MW, time() )
119 ];
120
121 return $status;
122 }
123
124 protected function doDeleteInternal( array $params ) {
125 $status = $this->newStatus();
126
127 $src = $this->resolveHashKey( $params['src'] );
128 if ( $src === null ) {
129 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
130
131 return $status;
132 }
133
134 if ( !isset( $this->files[$src] ) ) {
135 if ( empty( $params['ignoreMissingSource'] ) ) {
136 $status->fatal( 'backend-fail-delete', $params['src'] );
137 }
138
139 return $status;
140 }
141
142 unset( $this->files[$src] );
143
144 return $status;
145 }
146
147 protected function doGetFileStat( array $params ) {
148 $src = $this->resolveHashKey( $params['src'] );
149 if ( $src === null ) {
150 return null;
151 }
152
153 if ( isset( $this->files[$src] ) ) {
154 return [
155 'mtime' => $this->files[$src]['mtime'],
156 'size' => strlen( $this->files[$src]['data'] ),
157 ];
158 }
159
160 return false;
161 }
162
163 protected function doGetLocalCopyMulti( array $params ) {
164 $tmpFiles = []; // (path => TempFSFile)
165 foreach ( $params['srcs'] as $srcPath ) {
166 $src = $this->resolveHashKey( $srcPath );
167 if ( $src === null || !isset( $this->files[$src] ) ) {
168 $fsFile = null;
169 } else {
170 // Create a new temporary file with the same extension...
172 $fsFile = TempFSFile::factory( 'localcopy_', $ext, $this->tmpDirectory );
173 if ( $fsFile ) {
174 $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] );
175 if ( $bytes !== strlen( $this->files[$src]['data'] ) ) {
176 $fsFile = null;
177 }
178 }
179 }
180 $tmpFiles[$srcPath] = $fsFile;
181 }
182
183 return $tmpFiles;
184 }
185
186 protected function doDirectoryExists( $container, $dir, array $params ) {
187 $prefix = rtrim( "$container/$dir", '/' ) . '/';
188 foreach ( $this->files as $path => $data ) {
189 if ( strpos( $path, $prefix ) === 0 ) {
190 return true;
191 }
192 }
193
194 return false;
195 }
196
197 public function getDirectoryListInternal( $container, $dir, array $params ) {
198 $dirs = [];
199 $prefix = rtrim( "$container/$dir", '/' ) . '/';
200 $prefixLen = strlen( $prefix );
201 foreach ( $this->files as $path => $data ) {
202 if ( strpos( $path, $prefix ) === 0 ) {
203 $relPath = substr( $path, $prefixLen );
204 if ( $relPath === false ) {
205 continue;
206 } elseif ( strpos( $relPath, '/' ) === false ) {
207 continue; // just a file
208 }
209 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
210 if ( !empty( $params['topOnly'] ) ) {
211 $dirs[$parts[0]] = 1; // top directory
212 } else {
213 $current = '';
214 foreach ( $parts as $part ) { // all directories
215 $dir = ( $current === '' ) ? $part : "$current/$part";
216 $dirs[$dir] = 1;
217 $current = $dir;
218 }
219 }
220 }
221 }
222
223 return array_keys( $dirs );
224 }
225
226 public function getFileListInternal( $container, $dir, array $params ) {
227 $files = [];
228 $prefix = rtrim( "$container/$dir", '/' ) . '/';
229 $prefixLen = strlen( $prefix );
230 foreach ( $this->files as $path => $data ) {
231 if ( strpos( $path, $prefix ) === 0 ) {
232 $relPath = substr( $path, $prefixLen );
233 if ( $relPath === false ) {
234 continue;
235 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
236 continue;
237 }
238 $files[] = $relPath;
239 }
240 }
241
242 return $files;
243 }
244
245 protected function directoriesAreVirtual() {
246 return true;
247 }
248
255 protected function resolveHashKey( $storagePath ) {
256 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
257 if ( $relPath === null ) {
258 return null; // invalid
259 }
260
261 return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont;
262 }
263}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Base class for all backends using particular storage medium.
resolveStoragePathReal( $storagePath)
Like resolveStoragePath() except null values are returned if the container is sharded and the shard c...
const ATTR_UNICODE_PATHS
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
newStatus()
Yields the result of the status wrapper callback on either:
Simulation of a backend storage in memory.
doGetLocalCopyMulti(array $params)
doDirectoryExists( $container, $dir, array $params)
doCreateInternal(array $params)
getFileListInternal( $container, $dir, array $params)
Do not call this function from places outside FileBackend.
directoriesAreVirtual()
Is this a key/value store where directories are just virtual? Virtual directories exists in so much a...
getFeatures()
Get the a bitfield of extra features supported by the backend medium.
getDirectoryListInternal( $container, $dir, array $params)
Do not call this function from places outside FileBackend.
doStoreInternal(array $params)
doDeleteInternal(array $params)
array $files
Map of (file path => (data,mtime)
doCopyInternal(array $params)
doGetFileStat(array $params)
isPathUsableInternal( $storagePath)
Check if a file can be created or changed at a given storage path.
resolveHashKey( $storagePath)
Get the absolute file system path for a storage path.
static factory( $prefix, $extension='', $tmpDirectory=null)
Make a new temporary file on the file system.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition hooks.txt:1049
the array() calling protocol came about after MediaWiki 1.4rc1.
if(count( $args)==0) $dir
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation files(the "Software")
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$params
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition defines.php:11