r100253 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100252‎ | r100253 | r100254 >
Date:18:46, 19 October 2011
Author:nelson
Status:deferred
Tags:
Comment:
Added TempLocalFile and added rewinding of ops
Modified paths:
  • /branches/FileBackend/phase3/includes/filerepo/FileBackend.php (modified) (history)

Diff [purge]

Index: branches/FileBackend/phase3/includes/filerepo/FileBackend.php
@@ -69,34 +69,75 @@
7070 * @param Array $ops - Array of arrays containing N operations to execute IN ORDER
7171 * @return Status
7272 */
73 - public function doOps($ops) {
 73+ public function doOps( $ops, $reversed = 0 ) {
7474 if(!is_array($ops)) {
7575 throw new MWException(__METHOD__ . " not provided with an operations array");
7676 }
7777
7878 $statusObject = ''; // some sort of status object that can hold other status objects
7979
80 - foreach($ops AS $op) {
81 - switch ($op['operation']) {
 80+ foreach( $ops AS $i => $op ) {
 81+ switch ( $op['operation'] ) {
8282 case 'move':
83 - $statusObject->append($this->commonCaller('move', $op));
 83+ $st = $this->commonCaller('move', $op);
8484 break;
8585 case 'delete':
86 - $statusObject->append($this->commonCaller('delete', $op));
 86+ $st = $this->commonCaller('delete', $op);
8787 break;
8888 case 'copy':
89 - $statusObject->append($this->copy());
 89+ $st = $this->copy();
9090 break;
9191 case 'getFileProps':
9292 $tmpFile = $this->getLocalCopy();
93 - $statusObject->append($this->getFileProps($tmpFile));
 93+ $st = $this->getFileProps( $tmpFile );
9494 default:
95 - $statusObject->append('Unknown data store operation ' . $op['operation']);
96 -
 95+ $st = 'Unknown data store operation ' . $op['operation']);
9796 }
98 - }
 97+ $statusObject->append( $st );
 98+ if ( $st && $reversed) {
 99+ // oh noes! Something went wrong AGAIN.
 100+ // pray to gods.
 101+ return 'STATUS OBJECT';
 102+ elseif ( $st ) {
 103+ // oh crap, something went wrong. Try to unwind.
 104+ return $this->doOps( $this->unwind( $ops, $i ), 1);
 105+ }
 106+ }
99107
100 - return 'STATUS OBJECT';
 108+ return 'STATUS OBJECT';
 109+ }
 110+
 111+ /**
 112+ * Unwinds an array of operations, attempting to reverse their action.
 113+ * @param Array $ops - Array of arrays containing N operations to execute IN ORDER
 114+ * @param Integer $i - index of first operation that failed.
 115+ * @return Array
 116+ */
 117+ protected function unwind( $ops, $i ) {
 118+ $outops = array();
 119+
 120+ foreach( $ops AS $k => $op ) {
 121+ $newop = null;
 122+ switch ( $op['operation'] ) {
 123+ case 'move':
 124+ $newop = $op;
 125+ $newop['source'] = $op['source'];
 126+ $newop['dest'] = $op['dest'];
 127+ break;
 128+ case 'delete':
 129+ // sigh.
 130+ break;
 131+ case 'copy':
 132+ $newop = $op;
 133+ $newop['operation'] = 'delete';
 134+ $newop['source'] = $op['dest'];
 135+ break;
 136+ }
 137+ if ($newop) {
 138+ array_unshift($outops, $newop);
 139+ }
 140+ }
 141+ return $outops;
101142 }
102143
103144 /**
@@ -219,7 +260,7 @@
220261 // * Please overload this class inside your storage module
221262 // *
222263 // * @param String $file - Name of file to retreive thumbnail listing for
223 -// * @retrun Array
 264+// * @return Array
224265 // */
225266 // abstract function getThumbnailList( $file );
226267 //
@@ -229,4 +270,44 @@
230271 // public function concatenateChunks() { throw new MWException( __METHOD__ . ' not yet implemented.' ); }
231272
232273
233 -} // end class
\ No newline at end of file
 274+} // end class
 275+
 276+
 277+/**
 278+ * A helper class for FileBackend. Some filestores won't have files in the filesystem,
 279+ * so we can't count on being able to hand out the filename. Instead, we hand out an
 280+ * instance of this class, which will DTRT for all filestores.
 281+ */
 282+class TempLocalFile {
 283+
 284+ /**
 285+ * @param String $file - mwrepo:// url for the file to hand out.
 286+ */
 287+ public function __construct( $file ) {
 288+ $this->tempPath = $file;
 289+ }
 290+
 291+ /**
 292+ * Returns a file path pointing to our file or a copy thereof.
 293+ * @return String
 294+ */
 295+ public function path() {
 296+ return $this->tempPath;
 297+ }
 298+
 299+ /**
 300+ * we don't have a close() method. Just let the instance go out of scope.
 301+ */
 302+
 303+ /**
 304+ * if we need to delete the file, do so now.
 305+ */
 306+ public function __destruct() {
 307+ if ( $this->tempPath ) {
 308+ // Clean up temporary data.
 309+ // Only if we actually made a local copy!! unlink( $this->tempPath );
 310+ $this->tempPath = null;
 311+ }
 312+ }
 313+
 314+} // end class

Status & tagging log