Merge "NameTableStore: ensure consistency upon rollback." into REL1_34
[lhc/web/wiklou.git] / includes / externalstore / ExternalStoreMwstore.php
index 30c742d..77c23ee 100644 (file)
  * @since 1.21
  */
 class ExternalStoreMwstore extends ExternalStoreMedium {
+       /** @var FileBackendGroup */
+       private $fbGroup;
+
+       /**
+        * @see ExternalStoreMedium::__construct()
+        * @param array $params Additional parameters include:
+        *   - fbGroup: a FileBackendGroup instance
+        */
+       public function __construct( array $params ) {
+               parent::__construct( $params );
+               if ( !isset( $params['fbGroup'] ) || !( $params['fbGroup'] instanceof FileBackendGroup ) ) {
+                       throw new InvalidArgumentException( "FileBackendGroup required in 'fbGroup' field." );
+               }
+               $this->fbGroup = $params['fbGroup'];
+       }
+
        /**
         * The URL returned is of the form of the form mwstore://backend/container/wiki/id
         *
@@ -39,7 +55,7 @@ class ExternalStoreMwstore extends ExternalStoreMedium {
         * @return bool
         */
        public function fetchFromURL( $url ) {
-               $be = FileBackendGroup::singleton()->backendFromPath( $url );
+               $be = $this->fbGroup->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.
@@ -59,47 +75,56 @@ class ExternalStoreMwstore extends ExternalStoreMedium {
        public function batchFetchFromURLs( array $urls ) {
                $pathsByBackend = [];
                foreach ( $urls as $url ) {
-                       $be = FileBackendGroup::singleton()->backendFromPath( $url );
+                       $be = $this->fbGroup->backendFromPath( $url );
                        if ( $be instanceof FileBackend ) {
                                $pathsByBackend[$be->getName()][] = $url;
                        }
                }
                $blobs = [];
                foreach ( $pathsByBackend as $backendName => $paths ) {
-                       $be = FileBackendGroup::singleton()->get( $backendName );
+                       $be = $this->fbGroup->get( $backendName );
                        $blobs += $be->getFileContentsMulti( [ 'srcs' => $paths ] );
                }
 
                return $blobs;
        }
 
+       /**
+        * @inheritDoc
+        */
        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 = Wikimedia\base_convert( mt_rand( 0, 46655 ), 10, 36, 3 );
-                       // Make sure ID is roughly lexicographically increasing for performance
-                       $id = str_pad( UIDGenerator::newTimestampedUID128( 32 ), 26, '0', STR_PAD_LEFT );
-                       // Segregate items by wiki ID for the sake of bookkeeping
-                       // @FIXME: this does not include the domain for b/c but it ideally should
-                       $wiki = $this->params['wiki'] ?? wfWikiID();
+               $be = $this->fbGroup->get( $backend );
+               // Get three random base 36 characters to act as shard directories
+               $rand = Wikimedia\base_convert( mt_rand( 0, 46655 ), 10, 36, 3 );
+               // Make sure ID is roughly lexicographically increasing for performance
+               $id = str_pad( UIDGenerator::newTimestampedUID128( 32 ), 26, '0', STR_PAD_LEFT );
+               // Segregate items by DB domain ID for the sake of bookkeeping
+               $domain = $this->isDbDomainExplicit
+                       ? $this->dbDomain
+                       // @FIXME: this does not include the schema for b/c but it ideally should
+                       : WikiMap::getWikiIdFromDbDomain( $this->dbDomain );
+               $url = $be->getContainerStoragePath( 'data' ) . '/' . rawurlencode( $domain );
+               // Use directory/container sharding
+               $url .= ( $be instanceof FSFileBackend )
+                       ? "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}" // keep directories small
+                       : "/{$rand[0]}/{$rand[1]}/{$id}"; // container sharding is only 2-levels
 
-                       $url = $be->getContainerStoragePath( 'data' ) . '/' . rawurlencode( $wiki );
-                       $url .= ( $be instanceof FSFileBackend )
-                               ? "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}" // keep directories small
-                               : "/{$rand[0]}/{$rand[1]}/{$id}"; // container sharding is only 2-levels
+               $be->prepare( [ 'dir' => dirname( $url ), 'noAccess' => 1, 'noListing' => 1 ] );
+               $status = $be->create( [ 'dst' => $url, 'content' => $data ] );
 
-                       $be->prepare( [ 'dir' => dirname( $url ), 'noAccess' => 1, 'noListing' => 1 ] );
-                       if ( $be->create( [ 'dst' => $url, 'content' => $data ] )->isOK() ) {
-                               return $url;
-                       }
+               if ( $status->isOK() ) {
+                       return $url;
                }
 
-               return false;
+               throw new MWException( __METHOD__ . ": operation failed: $status" );
        }
 
        public function isReadOnly( $backend ) {
-               $be = FileBackendGroup::singleton()->get( $backend );
+               if ( parent::isReadOnly( $backend ) ) {
+                       return true;
+               }
+
+               $be = $this->fbGroup->get( $backend );
 
                return $be ? $be->isReadOnly() : false;
        }