Merge "Improve the shell cgroup feature"
[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 */
32 class ExternalStoreMwstore extends ExternalStoreMedium {
33 /**
34 * The URL returned is of the form of the form mwstore://backend/container/wiki/id
35 *
36 * @see ExternalStoreMedium::fetchFromURL()
37 */
38 public function fetchFromURL( $url ) {
39 $be = FileBackendGroup::singleton()->backendFromPath( $url );
40 if ( $be instanceof FileBackend ) {
41 // We don't need "latest" since objects are immutable and
42 // backends should at least have "read-after-create" consistency.
43 return $be->getFileContents( array( 'src' => $url ) );
44 }
45 return false;
46 }
47
48 /**
49 * @see ExternalStoreMedium::store()
50 */
51 public function store( $backend, $data ) {
52 $be = FileBackendGroup::singleton()->get( $backend );
53 if ( $be instanceof FileBackend ) {
54 // Get three random base 36 characters to act as shard directories
55 $rand = wfBaseConvert( mt_rand( 0, 46655 ), 10, 36, 3 );
56 // Make sure ID is roughly lexicographically increasing for performance
57 $id = str_pad( UIDGenerator::getTimestampedID128( 32 ), 26, '0', STR_PAD_LEFT );
58 // Segregate items by wiki ID for the sake of book keeping
59 $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : wfWikiID();
60
61 $url = $be->getContainerStoragePath( 'data' ) . '/' .
62 rawurlencode( $wiki ) . "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}";
63
64 $be->prepare( array( 'dir' => dirname( $url ) ) );
65 if ( $be->create( array( 'dst' => $url, 'content' => $data ) )->isOK() ) {
66 return $url;
67 }
68 }
69 return false;
70 }
71 }