[FileBackend] Syncing from journal support.
[lhc/web/wiklou.git] / includes / filerepo / backend / filejournal / DBFileJournal.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileJournal
5 * @author Aaron Schulz
6 */
7
8 /**
9 * Version of FileJournal that logs to a DB table
10 * @since 1.20
11 */
12 class DBFileJournal extends FileJournal {
13 protected $wiki = false; // string; wiki DB name
14
15 /**
16 * Construct a new instance from configuration.
17 * $config includes:
18 * 'wiki' : wiki name to use for LoadBalancer
19 *
20 * @param $config Array
21 */
22 protected function __construct( array $config ) {
23 parent::__construct( $config );
24
25 $this->wiki = $config['wiki'];
26 }
27
28 /**
29 * @see FileJournal::logChangeBatch()
30 * @return Status
31 */
32 protected function doLogChangeBatch( array $entries, $batchId ) {
33 $status = Status::newGood();
34
35 try {
36 $dbw = $this->getMasterDB();
37 } catch ( DBError $e ) {
38 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
39 return $status;
40 }
41
42 $now = wfTimestamp( TS_UNIX );
43
44 $data = array();
45 foreach ( $entries as $entry ) {
46 $data[] = array(
47 'fj_batch_uuid' => $batchId,
48 'fj_backend' => $this->backend,
49 'fj_op' => $entry['op'],
50 'fj_path' => $entry['path'],
51 'fj_path_sha1' => wfBaseConvert( sha1( $entry['path'] ), 16, 36, 31 ),
52 'fj_new_sha1' => $entry['newSha1'],
53 'fj_timestamp' => $dbw->timestamp( $now )
54 );
55 }
56
57 try {
58 $dbw->begin();
59 $dbw->insert( 'filejournal', $data, __METHOD__ );
60 $dbw->commit();
61 } catch ( DBError $e ) {
62 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
63 return $status;
64 }
65
66 return $status;
67 }
68
69 /**
70 * @see FileJournal::doGetChangeEntries()
71 * @return Array
72 * @throws DBError
73 */
74 protected function doGetChangeEntries( $start, $limit ) {
75 $dbw = $this->getMasterDB();
76
77 $res = $dbw->select( 'filejournal', '*',
78 array(
79 'fj_backend' => $this->backend,
80 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
81 __METHOD__,
82 array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
83 $limit ? array( 'LIMIT' => $limit ) : array() )
84 );
85
86 $entries = array();
87 foreach ( $res as $row ) {
88 $item = array();
89 foreach ( (array)$row as $key => $value ) {
90 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
91 }
92 $entries[] = $item;
93 }
94
95 return $entries;
96 }
97
98 /**
99 * @see FileJournal::purgeOldLogs()
100 * @return Status
101 * @throws DBError
102 */
103 protected function doPurgeOldLogs() {
104 $status = Status::newGood();
105 if ( $this->ttlDays <= 0 ) {
106 return $status; // nothing to do
107 }
108
109 $dbw = $this->getMasterDB();
110 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
111
112 $dbw->begin();
113 $dbw->delete( 'filejournal',
114 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
115 __METHOD__
116 );
117 $dbw->commit();
118
119 return $status;
120 }
121
122 /**
123 * Get a master connection to the logging DB
124 *
125 * @return DatabaseBase
126 * @throws DBError
127 */
128 protected function getMasterDB() {
129 $lb = wfGetLBFactory()->newMainLB();
130 return $lb->getConnection( DB_MASTER, array(), $this->wiki );
131 }
132 }