Move Blob class to Rdbms namespaces
[mediawiki.git] / includes / filebackend / filejournal / DBFileJournal.php
blob62e635dd23c7dbb68f0eda03d97a8ea98fc28985
1 <?php
2 /**
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
20 * @file
21 * @ingroup FileJournal
22 * @author Aaron Schulz
25 use MediaWiki\MediaWikiServices;
27 /**
28 * Version of FileJournal that logs to a DB table
29 * @since 1.20
31 class DBFileJournal extends FileJournal {
32 /** @var IDatabase */
33 protected $dbw;
35 protected $wiki = false; // string; wiki DB name
37 /**
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'];
49 /**
50 * @see FileJournal::logChangeBatch()
51 * @param array $entries
52 * @param string $batchId
53 * @return StatusValue
55 protected function doLogChangeBatch( array $entries, $batchId ) {
56 $status = StatusValue::newGood();
58 try {
59 $dbw = $this->getMasterDB();
60 } catch ( DBError $e ) {
61 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
63 return $status;
66 $now = wfTimestamp( TS_UNIX );
68 $data = [];
69 foreach ( $entries as $entry ) {
70 $data[] = [
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 )
80 try {
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 );
88 return $status;
91 return $status;
94 /**
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 ],
103 __METHOD__
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" ],
119 __METHOD__,
120 [ 'ORDER BY' => 'fj_timestamp DESC' ]
125 * @see FileJournal::doGetChangeEntries()
126 * @param int $start
127 * @param int $limit
128 * @return array
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
137 __METHOD__,
138 array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
139 $limit ? [ 'LIMIT' => $limit ] : [] )
142 $entries = [];
143 foreach ( $res as $row ) {
144 $item = [];
145 foreach ( (array)$row as $key => $value ) {
146 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
148 $entries[] = $item;
151 return $entries;
155 * @see FileJournal::purgeOldLogs()
156 * @return StatusValue
157 * @throws DBError
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 ) ],
170 __METHOD__
173 return $status;
177 * Get a master connection to the logging DB
179 * @return IDatabase
180 * @throws DBError
182 protected function getMasterDB() {
183 if ( !$this->dbw ) {
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 );
190 return $this->dbw;