[ExternalStore] Added ExternalStoreMwstore class that uses a file backend.
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 3 Feb 2013 01:09:16 +0000 (17:09 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 7 Feb 2013 17:52:03 +0000 (09:52 -0800)
Change-Id: I7785a621fe32f549b2e7c636f2b6e7b18be367f4

includes/AutoLoader.php
includes/externalstore/ExternalStoreMwstore.php [new file with mode: 0644]

index df17f28..75eadab 100644 (file)
@@ -95,6 +95,7 @@ $wgAutoloadLocalClasses = array(
        'ExternalStoreDB' => 'includes/externalstore/ExternalStoreDB.php',
        'ExternalStoreHttp' => 'includes/externalstore/ExternalStoreHttp.php',
        'ExternalStoreMedium' => 'includes/externalstore/ExternalStoreMedium.php',
+       'ExternalStoreMwstore' => 'includes/externalstore/ExternalStoreMwstore.php',
        'ExternalUser' => 'includes/ExternalUser.php',
        'FakeTitle' => 'includes/FakeTitle.php',
        'Fallback' => 'includes/Fallback.php',
diff --git a/includes/externalstore/ExternalStoreMwstore.php b/includes/externalstore/ExternalStoreMwstore.php
new file mode 100644 (file)
index 0000000..35b3494
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+/**
+ * External storage in a file backend.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * File backend accessable external objects.
+ *
+ * In this system, each store "location" maps to the name of a file backend.
+ * The file backends must be defined in $wgFileBackends and must be global
+ * and fully qualified with a global "wikiId" prefix in the configuration.
+ *
+ * @ingroup ExternalStorage
+ */
+class ExternalStoreMwstore extends ExternalStoreMedium {
+       /**
+        * The URL returned is of the form of the form mwstore://backend/container/wiki/id
+        *
+        * @see ExternalStoreMedium::fetchFromURL()
+        */
+       public function fetchFromURL( $url ) {
+               $be = FileBackendGroup::singleton()->backendFromPath( $url );
+               if ( $be instanceof FileBackend ) {
+                       // We don't need "latest" since objects are immutable and
+                       // backends should at least have "read-after-create" consistency.
+                       return $be->getFileContents( array( 'src' => $url ) );
+               }
+               return false;
+       }
+
+       /**
+        * @see ExternalStoreMedium::store()
+        */
+       public function store( $backend, $data ) {
+               $be = FileBackendGroup::singleton()->get( $backend );
+               if ( $be instanceof FileBackend ) {
+                       // Get three random base 36 characters to act as shard directories
+                       $rand = wfBaseConvert( mt_rand( 0, 46655 ), 10, 36, 3 );
+                       // Make sure ID is roughly lexicographically increasing for performance
+                       $id = str_pad( UIDGenerator::getTimestampedID128( 32 ), 26, '0', STR_PAD_LEFT );
+                       // Segregate items by wiki ID for the sake of book keeping
+                       $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : wfWikiID();
+
+                       $url = $be->getContainerStoragePath( 'data' ) . '/' .
+                               rawurlencode( $wiki ) . "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}";
+
+                       $be->prepare( array( 'dir' => dirname( $url ) ) );
+                       if ( $be->create( array( 'dst' => $url, 'content' => $data ) )->isOK() ) {
+                               return $url;
+                       }
+               }
+               return false;
+       }
+}