42f4f1a97378a65357ef384f4d6958180cea69e5
[lhc/web/wiklou.git] / includes / filebackend / filejournal / DBFileJournal.php
1 <?php
2 /**
3 * Version of FileJournal that logs to a DB table.
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 * @ingroup FileJournal
22 * @author Aaron Schulz
23 */
24
25 /**
26 * Version of FileJournal that logs to a DB table
27 * @since 1.20
28 */
29 class DBFileJournal extends FileJournal {
30 /** @var DatabaseBase */
31 protected $dbw;
32
33 protected $wiki = false; // string; wiki DB name
34
35 /**
36 * Construct a new instance from configuration.
37 * $config includes:
38 * 'wiki' : wiki name to use for LoadBalancer
39 *
40 * @param $config Array
41 */
42 protected function __construct( array $config ) {
43 parent::__construct( $config );
44
45 $this->wiki = $config['wiki'];
46 }
47
48 /**
49 * @see FileJournal::logChangeBatch()
50 * @return Status
51 */
52 protected function doLogChangeBatch( array $entries, $batchId ) {
53 $status = Status::newGood();
54
55 try {
56 $dbw = $this->getMasterDB();
57 } catch ( DBError $e ) {
58 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
59
60 return $status;
61 }
62
63 $now = wfTimestamp( TS_UNIX );
64
65 $data = array();
66 foreach ( $entries as $entry ) {
67 $data[] = array(
68 'fj_batch_uuid' => $batchId,
69 'fj_backend' => $this->backend,
70 'fj_op' => $entry['op'],
71 'fj_path' => $entry['path'],
72 'fj_new_sha1' => $entry['newSha1'],
73 'fj_timestamp' => $dbw->timestamp( $now )
74 );
75 }
76
77 try {
78 $dbw->insert( 'filejournal', $data, __METHOD__ );
79 if ( mt_rand( 0, 99 ) == 0 ) {
80 $this->purgeOldLogs(); // occasionally delete old logs
81 }
82 } catch ( DBError $e ) {
83 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
84
85 return $status;
86 }
87
88 return $status;
89 }
90
91 /**
92 * @see FileJournal::doGetCurrentPosition()
93 * @return integer|false
94 */
95 protected function doGetCurrentPosition() {
96 $dbw = $this->getMasterDB();
97
98 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
99 array( 'fj_backend' => $this->backend ),
100 __METHOD__
101 );
102 }
103
104 /**
105 * @see FileJournal::doGetPositionAtTime()
106 * @param $time integer|string timestamp
107 * @return integer|false
108 */
109 protected function doGetPositionAtTime( $time ) {
110 $dbw = $this->getMasterDB();
111
112 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
113
114 return $dbw->selectField( 'filejournal', 'fj_id',
115 array( 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ),
116 __METHOD__,
117 array( 'ORDER BY' => 'fj_timestamp DESC' )
118 );
119 }
120
121 /**
122 * @see FileJournal::doGetChangeEntries()
123 * @return Array
124 * @throws DBError
125 */
126 protected function doGetChangeEntries( $start, $limit ) {
127 $dbw = $this->getMasterDB();
128
129 $res = $dbw->select( 'filejournal', '*',
130 array(
131 'fj_backend' => $this->backend,
132 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
133 __METHOD__,
134 array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
135 $limit ? array( 'LIMIT' => $limit ) : array() )
136 );
137
138 $entries = array();
139 foreach ( $res as $row ) {
140 $item = array();
141 foreach ( (array)$row as $key => $value ) {
142 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
143 }
144 $entries[] = $item;
145 }
146
147 return $entries;
148 }
149
150 /**
151 * @see FileJournal::purgeOldLogs()
152 * @return Status
153 * @throws DBError
154 */
155 protected function doPurgeOldLogs() {
156 $status = Status::newGood();
157 if ( $this->ttlDays <= 0 ) {
158 return $status; // nothing to do
159 }
160
161 $dbw = $this->getMasterDB();
162 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
163
164 $dbw->delete( 'filejournal',
165 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
166 __METHOD__
167 );
168
169 return $status;
170 }
171
172 /**
173 * Get a master connection to the logging DB
174 *
175 * @return DatabaseBase
176 * @throws DBError
177 */
178 protected function getMasterDB() {
179 if ( !$this->dbw ) {
180 // Get a separate connection in autocommit mode
181 $lb = wfGetLBFactory()->newMainLB();
182 $this->dbw = $lb->getConnection( DB_MASTER, array(), $this->wiki );
183 $this->dbw->clearFlag( DBO_TRX );
184 }
185
186 return $this->dbw;
187 }
188 }