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
25 use MediaWiki\MediaWikiServices
;
28 * Version of FileJournal that logs to a DB table
31 class DBFileJournal
extends FileJournal
{
35 protected $wiki = false; // string; wiki DB name
38 * Construct a new instance from configuration.
40 * @param array $config Includes:
41 * 'wiki' : wiki name to use for LoadBalancer
43 protected function __construct( array $config ) {
44 parent
::__construct( $config );
46 $this->wiki
= $config['wiki'];
50 * @see FileJournal::logChangeBatch()
51 * @param array $entries
52 * @param string $batchId
55 protected function doLogChangeBatch( array $entries, $batchId ) {
56 $status = StatusValue
::newGood();
59 $dbw = $this->getMasterDB();
60 } catch ( DBError
$e ) {
61 $status->fatal( 'filejournal-fail-dbconnect', $this->backend
);
66 $now = wfTimestamp( TS_UNIX
);
69 foreach ( $entries as $entry ) {
71 'fj_batch_uuid' => $batchId,
72 'fj_backend' => $this->backend
,
73 'fj_op' => $entry['op'],
74 'fj_path' => $entry['path'],
75 'fj_new_sha1' => $entry['newSha1'],
76 'fj_timestamp' => $dbw->timestamp( $now )
81 $dbw->insert( 'filejournal', $data, __METHOD__
);
82 if ( mt_rand( 0, 99 ) == 0 ) {
83 $this->purgeOldLogs(); // occasionally delete old logs
85 } catch ( DBError
$e ) {
86 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
95 * @see FileJournal::doGetCurrentPosition()
96 * @return bool|mixed The value from the field, or false on failure.
98 protected function doGetCurrentPosition() {
99 $dbw = $this->getMasterDB();
101 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
102 [ 'fj_backend' => $this->backend
],
108 * @see FileJournal::doGetPositionAtTime()
109 * @param int|string $time Timestamp
110 * @return bool|mixed The value from the field, or false on failure.
112 protected function doGetPositionAtTime( $time ) {
113 $dbw = $this->getMasterDB();
115 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
117 return $dbw->selectField( 'filejournal', 'fj_id',
118 [ 'fj_backend' => $this->backend
, "fj_timestamp <= $encTimestamp" ],
120 [ 'ORDER BY' => 'fj_timestamp DESC' ]
125 * @see FileJournal::doGetChangeEntries()
130 protected function doGetChangeEntries( $start, $limit ) {
131 $dbw = $this->getMasterDB();
133 $res = $dbw->select( 'filejournal', '*',
135 'fj_backend' => $this->backend
,
136 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
138 array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
139 $limit ?
[ 'LIMIT' => $limit ] : [] )
143 foreach ( $res as $row ) {
145 foreach ( (array)$row as $key => $value ) {
146 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
155 * @see FileJournal::purgeOldLogs()
156 * @return StatusValue
159 protected function doPurgeOldLogs() {
160 $status = StatusValue
::newGood();
161 if ( $this->ttlDays
<= 0 ) {
162 return $status; // nothing to do
165 $dbw = $this->getMasterDB();
166 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays
);
168 $dbw->delete( 'filejournal',
169 [ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
177 * Get a master connection to the logging DB
182 protected function getMasterDB() {
184 // Get a separate connection in autocommit mode
185 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory()->newMainLB();
186 $this->dbw
= $lb->getConnection( DB_MASTER
, [], $this->wiki
);
187 $this->dbw
->clearFlag( DBO_TRX
);