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
{
33 protected $wiki = false; // string; wiki DB name
36 * Construct a new instance from configuration.
38 * @param array $config Includes:
39 * 'wiki' : wiki name to use for LoadBalancer
41 protected function __construct( array $config ) {
42 parent
::__construct( $config );
44 $this->wiki
= $config['wiki'];
48 * @see FileJournal::logChangeBatch()
49 * @param array $entries
50 * @param string $batchId
53 protected function doLogChangeBatch( array $entries, $batchId ) {
54 $status = StatusValue
::newGood();
57 $dbw = $this->getMasterDB();
58 } catch ( DBError
$e ) {
59 $status->fatal( 'filejournal-fail-dbconnect', $this->backend
);
64 $now = wfTimestamp( TS_UNIX
);
67 foreach ( $entries as $entry ) {
69 'fj_batch_uuid' => $batchId,
70 'fj_backend' => $this->backend
,
71 'fj_op' => $entry['op'],
72 'fj_path' => $entry['path'],
73 'fj_new_sha1' => $entry['newSha1'],
74 'fj_timestamp' => $dbw->timestamp( $now )
79 $dbw->insert( 'filejournal', $data, __METHOD__
);
80 if ( mt_rand( 0, 99 ) == 0 ) {
81 $this->purgeOldLogs(); // occasionally delete old logs
83 } catch ( DBError
$e ) {
84 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
93 * @see FileJournal::doGetCurrentPosition()
94 * @return bool|mixed The value from the field, or false on failure.
96 protected function doGetCurrentPosition() {
97 $dbw = $this->getMasterDB();
99 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
100 [ 'fj_backend' => $this->backend
],
106 * @see FileJournal::doGetPositionAtTime()
107 * @param int|string $time Timestamp
108 * @return bool|mixed The value from the field, or false on failure.
110 protected function doGetPositionAtTime( $time ) {
111 $dbw = $this->getMasterDB();
113 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
115 return $dbw->selectField( 'filejournal', 'fj_id',
116 [ 'fj_backend' => $this->backend
, "fj_timestamp <= $encTimestamp" ],
118 [ 'ORDER BY' => 'fj_timestamp DESC' ]
123 * @see FileJournal::doGetChangeEntries()
128 protected function doGetChangeEntries( $start, $limit ) {
129 $dbw = $this->getMasterDB();
131 $res = $dbw->select( 'filejournal', '*',
133 'fj_backend' => $this->backend
,
134 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
136 array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
137 $limit ?
[ 'LIMIT' => $limit ] : [] )
141 foreach ( $res as $row ) {
143 foreach ( (array)$row as $key => $value ) {
144 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
153 * @see FileJournal::purgeOldLogs()
154 * @return StatusValue
157 protected function doPurgeOldLogs() {
158 $status = StatusValue
::newGood();
159 if ( $this->ttlDays
<= 0 ) {
160 return $status; // nothing to do
163 $dbw = $this->getMasterDB();
164 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays
);
166 $dbw->delete( 'filejournal',
167 [ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
175 * Get a master connection to the logging DB
180 protected function getMasterDB() {
182 // Get a separate connection in autocommit mode
183 $lb = wfGetLBFactory()->newMainLB();
184 $this->dbw
= $lb->getConnection( DB_MASTER
, [], $this->wiki
);
185 $this->dbw
->clearFlag( DBO_TRX
);