r12537 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r12536 | r12537 (on ViewVC) | r12538 >
Date:03:16, 9 January 2006
Author:timstarling
Status:new
Tags:
Comment:Option to write text directly to external storage.
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/Revision.php
===================================================================
--- trunk/phase3/includes/Revision.php	(revision 12536)
+++ trunk/phase3/includes/Revision.php	(revision 12537)
@@ -517,19 +517,36 @@
 	 * @return int
 	 */
 	function insertOn( &$dbw ) {
+		global $wgDefaultExternalStore;
+		
 		$fname = 'Revision::insertOn';
 		wfProfileIn( $fname );
 
-		$mungedText = $this->mText;
-		$flags = Revision::compressRevisionText( $mungedText );
+		$data = $this->mText;
+		$flags = Revision::compressRevisionText( $data );
 
-		# Record the text to the text table
+		# Write to external storage if required
+		if ( $wgDefaultExternalStore ) {
+			require_once('ExternalStore.php');
+			// Store and get the URL
+			$data = ExternalStore::insert( $wgDefaultExternalStore, $data );
+			if ( !$data ) {
+				# This should only happen in the case of a configuration error, where the external store is not valid
+				wfDebugDieBacktrace( "Unable to store text to external storage $wgDefaultExternalStore" );
+			}
+			if ( $flags ) {
+				$flags .= ',';
+			}
+			$flags .= 'external';
+		}
+
+		# Record the text (or external storage URL) to the text table
 		if( !isset( $this->mTextId ) ) {
 			$old_id = $dbw->nextSequenceValue( 'text_old_id_val' );
 			$dbw->insert( 'text',
 				array(
 					'old_id'    => $old_id,
-					'old_text'  => $mungedText,
+					'old_text'  => $data,
 					'old_flags' => $flags,
 				), $fname
 			);
Index: trunk/phase3/includes/ExternalStore.php
===================================================================
--- trunk/phase3/includes/ExternalStore.php	(revision 12536)
+++ trunk/phase3/includes/ExternalStore.php	(revision 12537)
@@ -23,6 +23,20 @@
 		/* Bad URL */
 		if ($path=="")
 			return false;
+
+		$store =& ExternalStore::getStoreObject( $proto );
+		if ( $store === false )
+			return false;
+		return $store->fetchFromURL($url);
+	}
+
+	/**
+	 * Get an external store object of the given type
+	 */
+	function &getStoreObject( $proto ) {
+		global $wgExternalStores;
+		if (!$wgExternalStores)
+			return false;
 		/* Protocol not enabled */
 		if (!in_array( $proto, $wgExternalStores ))
 			return false;
@@ -34,11 +48,23 @@
 				return false;
 		}
 		$store=new $class();
-		return $store->fetchFromURL($url);
+		return $store;
 	}
 
-	/* XXX: may require other methods, for store, delete,
-	 * whatever, for initial ext storage
+	/**
+	 * Store a data item to an external store, identified by a partial URL
+	 * The protocol part is used to identify the class, the rest is passed to the 
+	 * class itself as a parameter. 
+	 * Returns the URL of the stored data item, or false on error
 	 */
+	function insert( $url, $data ) {
+		list( $proto, $params ) = explode( '://', $url, 2 );
+		$store =& ExternalStore::getStoreObject( $proto );
+		if ( $store === false ) {
+			return false;
+		} else {
+			return $store->store( $params, $data );
+		}
+	}
 }
 ?>
Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 12536)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 12537)
@@ -1749,6 +1749,12 @@
 $wgExternalServers = array();
 
 /**
+ * The place to put new revisions, false to put them in the local text table.
+ * Part of a URL, e.g. DB://cluster1
+ */
+$wgDefaultExternalStore = false;
+
+/**
 * list of trusted media-types and mime types.
 * Use the MEDIATYPE_xxx constants to represent media types.
 * This list is used by Image::isSafeFile
Index: trunk/phase3/includes/ExternalStoreDB.php
===================================================================
--- trunk/phase3/includes/ExternalStoreDB.php	(revision 12536)
+++ trunk/phase3/includes/ExternalStoreDB.php	(revision 12537)
@@ -128,7 +128,11 @@
 
 		$id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
 		$dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
-		return "DB://$cluster/" . $dbw->insertId();
+		$id = $dbw->insertId();
+		if ( $dbw->getFlag( DBO_TRX ) ) {
+			$dbw->immediateCommit();
+		}
+		return "DB://$cluster/$id";
 	}
 }
 ?>
Views
Toolbox