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
;
26 use Wikimedia\Rdbms\IDatabase
;
27 use Wikimedia\Rdbms\DBError
;
30 * Version of FileJournal that logs to a DB table
33 class DBFileJournal
extends FileJournal
{
37 protected $wiki = false; // string; wiki DB name
40 * Construct a new instance from configuration.
42 * @param array $config Includes:
43 * 'wiki' : wiki name to use for LoadBalancer
45 protected function __construct( array $config ) {
46 parent
::__construct( $config );
48 $this->wiki
= $config['wiki'];
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 = wfTimestamp( TS_UNIX
);
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 if ( mt_rand( 0, 99 ) == 0 ) {
85 $this->purgeOldLogs(); // occasionally delete old logs
87 } catch ( DBError
$e ) {
88 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
97 * @see FileJournal::doGetCurrentPosition()
98 * @return bool|mixed The value from the field, or false on failure.
100 protected function doGetCurrentPosition() {
101 $dbw = $this->getMasterDB();
103 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
104 [ 'fj_backend' => $this->backend
],
110 * @see FileJournal::doGetPositionAtTime()
111 * @param int|string $time Timestamp
112 * @return bool|mixed The value from the field, or false on failure.
114 protected function doGetPositionAtTime( $time ) {
115 $dbw = $this->getMasterDB();
117 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
119 return $dbw->selectField( 'filejournal', 'fj_id',
120 [ 'fj_backend' => $this->backend
, "fj_timestamp <= $encTimestamp" ],
122 [ 'ORDER BY' => 'fj_timestamp DESC' ]
127 * @see FileJournal::doGetChangeEntries()
132 protected function doGetChangeEntries( $start, $limit ) {
133 $dbw = $this->getMasterDB();
135 $res = $dbw->select( 'filejournal', '*',
137 'fj_backend' => $this->backend
,
138 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
140 array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
141 $limit ?
[ 'LIMIT' => $limit ] : [] )
145 foreach ( $res as $row ) {
147 foreach ( (array)$row as $key => $value ) {
148 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
157 * @see FileJournal::purgeOldLogs()
158 * @return StatusValue
161 protected function doPurgeOldLogs() {
162 $status = StatusValue
::newGood();
163 if ( $this->ttlDays
<= 0 ) {
164 return $status; // nothing to do
167 $dbw = $this->getMasterDB();
168 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays
);
170 $dbw->delete( 'filejournal',
171 [ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
179 * Get a master connection to the logging DB
184 protected function getMasterDB() {
186 // Get a separate connection in autocommit mode
187 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory()->newMainLB();
188 $this->dbw
= $lb->getConnection( DB_MASTER
, [], $this->wiki
);
189 $this->dbw
->clearFlag( DBO_TRX
);