Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / externalstore / ExternalStoreMwstore.php
1 <?php
2 /**
3 * External storage in a file backend.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * File backend accessable external objects.
25 *
26 * In this system, each store "location" maps to the name of a file backend.
27 * The file backends must be defined in $wgFileBackends and must be global
28 * and fully qualified with a global "wikiId" prefix in the configuration.
29 *
30 * @ingroup ExternalStorage
31 * @since 1.21
32 */
33 class ExternalStoreMwstore extends ExternalStoreMedium {
34 /**
35 * The URL returned is of the form of the form mwstore://backend/container/wiki/id
36 *
37 * @see ExternalStoreMedium::fetchFromURL()
38 */
39 public function fetchFromURL( $url ) {
40 $be = FileBackendGroup::singleton()->backendFromPath( $url );
41 if ( $be instanceof FileBackend ) {
42 // We don't need "latest" since objects are immutable and
43 // backends should at least have "read-after-create" consistency.
44 return $be->getFileContents( [ 'src' => $url ] );
45 }
46
47 return false;
48 }
49
50 /**
51 * Fetch data from given external store URLs.
52 * The URL returned is of the form of the form mwstore://backend/container/wiki/id
53 *
54 * @param array $urls An array of external store URLs
55 * @return array A map from url to stored content. Failed results are not represented.
56 */
57 public function batchFetchFromURLs( array $urls ) {
58 $pathsByBackend = [];
59 foreach ( $urls as $url ) {
60 $be = FileBackendGroup::singleton()->backendFromPath( $url );
61 if ( $be instanceof FileBackend ) {
62 $pathsByBackend[$be->getName()][] = $url;
63 }
64 }
65 $blobs = [];
66 foreach ( $pathsByBackend as $backendName => $paths ) {
67 $be = FileBackendGroup::singleton()->get( $backendName );
68 $blobs = $blobs + $be->getFileContentsMulti( [ 'srcs' => $paths ] );
69 }
70
71 return $blobs;
72 }
73
74 /**
75 * @see ExternalStoreMedium::store()
76 */
77 public function store( $backend, $data ) {
78 $be = FileBackendGroup::singleton()->get( $backend );
79 if ( $be instanceof FileBackend ) {
80 // Get three random base 36 characters to act as shard directories
81 $rand = Wikimedia\base_convert( mt_rand( 0, 46655 ), 10, 36, 3 );
82 // Make sure ID is roughly lexicographically increasing for performance
83 $id = str_pad( UIDGenerator::newTimestampedUID128( 32 ), 26, '0', STR_PAD_LEFT );
84 // Segregate items by wiki ID for the sake of bookkeeping
85 $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : wfWikiID();
86
87 $url = $be->getContainerStoragePath( 'data' ) . '/' . rawurlencode( $wiki );
88 $url .= ( $be instanceof FSFileBackend )
89 ? "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}" // keep directories small
90 : "/{$rand[0]}/{$rand[1]}/{$id}"; // container sharding is only 2-levels
91
92 $be->prepare( [ 'dir' => dirname( $url ), 'noAccess' => 1, 'noListing' => 1 ] );
93 if ( $be->create( [ 'dst' => $url, 'content' => $data ] )->isOK() ) {
94 return $url;
95 }
96 }
97
98 return false;
99 }
100 }