3 * Version of FileJournal that logs to a DB table.
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.
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.
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
21 * @ingroup FileJournal
22 * @author Aaron Schulz
26 * Version of FileJournal that logs to a DB table
29 class DBFileJournal
extends FileJournal
{
30 /** @var DatabaseBase */
33 protected $wiki = false; // string; wiki DB name
36 * Construct a new instance from configuration.
38 * 'wiki' : wiki name to use for LoadBalancer
40 * @param $config Array
42 protected function __construct( array $config ) {
43 parent
::__construct( $config );
45 $this->wiki
= $config['wiki'];
49 * @see FileJournal::logChangeBatch()
52 protected function doLogChangeBatch( array $entries, $batchId ) {
53 $status = Status
::newGood();
56 $dbw = $this->getMasterDB();
57 } catch ( DBError
$e ) {
58 $status->fatal( 'filejournal-fail-dbconnect', $this->backend
);
62 $now = wfTimestamp( TS_UNIX
);
65 foreach ( $entries as $entry ) {
67 'fj_batch_uuid' => $batchId,
68 'fj_backend' => $this->backend
,
69 'fj_op' => $entry['op'],
70 'fj_path' => $entry['path'],
71 'fj_new_sha1' => $entry['newSha1'],
72 'fj_timestamp' => $dbw->timestamp( $now )
77 $dbw->insert( 'filejournal', $data, __METHOD__
);
78 } catch ( DBError
$e ) {
79 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
87 * @see FileJournal::doGetCurrentPosition()
88 * @return integer|false
90 protected function doGetCurrentPosition() {
91 $dbw = $this->getMasterDB();
93 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
94 array( 'fj_backend' => $this->backend
),
100 * @see FileJournal::doGetChangeEntries()
104 protected function doGetChangeEntries( $start, $limit ) {
105 $dbw = $this->getMasterDB();
107 $res = $dbw->select( 'filejournal', '*',
109 'fj_backend' => $this->backend
,
110 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
112 array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
113 $limit ?
array( 'LIMIT' => $limit ) : array() )
117 foreach ( $res as $row ) {
119 foreach ( (array)$row as $key => $value ) {
120 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
129 * @see FileJournal::purgeOldLogs()
133 protected function doPurgeOldLogs() {
134 $status = Status
::newGood();
135 if ( $this->ttlDays
<= 0 ) {
136 return $status; // nothing to do
139 $dbw = $this->getMasterDB();
140 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays
);
142 $dbw->delete( 'filejournal',
143 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
151 * Get a master connection to the logging DB
153 * @return DatabaseBase
156 protected function getMasterDB() {
158 // Get a separate connection in autocommit mode
159 $lb = wfGetLBFactory()->newMainLB();
160 $this->dbw
= $lb->getConnection( DB_MASTER
, array(), $this->wiki
);
161 $this->dbw
->clearFlag( DBO_TRX
);