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
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\IDatabase
;
26 use Wikimedia\Rdbms\DBError
;
27 use Wikimedia\Timestamp\ConvertibleTimestamp
;
30 * Version of FileJournal that logs to a DB table
33 class DBFileJournal
extends FileJournal
{
40 * Construct a new instance from configuration. Do not call directly, use FileJournal::factory.
42 * @param array $config Includes:
43 * domain: database domain ID of the wiki
45 public function __construct( array $config ) {
46 parent
::__construct( $config );
48 $this->domain
= $config['domain'] ??
$config['wiki']; // b/c
52 * @see FileJournal::logChangeBatch()
53 * @param array $entries
54 * @param string $batchId
57 protected function doLogChangeBatch( array $entries, $batchId ) {
58 $status = StatusValue
::newGood();
61 $dbw = $this->getMasterDB();
62 } catch ( DBError
$e ) {
63 $status->fatal( 'filejournal-fail-dbconnect', $this->backend
);
68 $now = ConvertibleTimestamp
::time();
71 foreach ( $entries as $entry ) {
73 'fj_batch_uuid' => $batchId,
74 'fj_backend' => $this->backend
,
75 'fj_op' => $entry['op'],
76 'fj_path' => $entry['path'],
77 'fj_new_sha1' => $entry['newSha1'],
78 'fj_timestamp' => $dbw->timestamp( $now )
83 $dbw->insert( 'filejournal', $data, __METHOD__
);
84 // XXX Should we do this deterministically so it's testable? Maybe look at the last two
85 // digits of a hash of a bunch of the data?
86 if ( mt_rand( 0, 99 ) == 0 ) {
87 // occasionally delete old logs
88 $this->purgeOldLogs(); // @codeCoverageIgnore
90 } catch ( DBError
$e ) {
91 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
100 * @see FileJournal::doGetCurrentPosition()
101 * @return bool|mixed The value from the field, or false on failure.
103 protected function doGetCurrentPosition() {
104 $dbw = $this->getMasterDB();
106 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
107 [ 'fj_backend' => $this->backend
],
113 * @see FileJournal::doGetPositionAtTime()
114 * @param int|string $time Timestamp
115 * @return bool|mixed The value from the field, or false on failure.
117 protected function doGetPositionAtTime( $time ) {
118 $dbw = $this->getMasterDB();
120 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
122 return $dbw->selectField( 'filejournal', 'fj_id',
123 [ 'fj_backend' => $this->backend
, "fj_timestamp <= $encTimestamp" ],
125 [ 'ORDER BY' => 'fj_timestamp DESC' ]
130 * @see FileJournal::doGetChangeEntries()
131 * @param int|null $start
135 protected function doGetChangeEntries( $start, $limit ) {
136 $dbw = $this->getMasterDB();
138 $res = $dbw->select( 'filejournal', '*',
140 'fj_backend' => $this->backend
,
141 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
143 array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
144 $limit ?
[ 'LIMIT' => $limit ] : [] )
148 foreach ( $res as $row ) {
150 foreach ( (array)$row as $key => $value ) {
151 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
160 * @see FileJournal::purgeOldLogs()
161 * @return StatusValue
164 protected function doPurgeOldLogs() {
165 $status = StatusValue
::newGood();
166 if ( $this->ttlDays
<= 0 ) {
167 return $status; // nothing to do
170 $dbw = $this->getMasterDB();
171 $dbCutoff = $dbw->timestamp( ConvertibleTimestamp
::time() - 86400 * $this->ttlDays
);
173 $dbw->delete( 'filejournal',
174 [ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
182 * Get a master connection to the logging DB
187 protected function getMasterDB() {
189 // Get a separate connection in autocommit mode
190 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory()->newMainLB();
191 $this->dbw
= $lb->getConnection( DB_MASTER
, [], $this->domain
);
192 $this->dbw
->clearFlag( DBO_TRX
);