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
{
30 protected $wiki = false; // string; wiki DB name
33 * Construct a new instance from configuration.
35 * 'wiki' : wiki name to use for LoadBalancer
37 * @param $config Array
39 protected function __construct( array $config ) {
40 parent
::__construct( $config );
42 $this->wiki
= $config['wiki'];
46 * @see FileJournal::logChangeBatch()
49 protected function doLogChangeBatch( array $entries, $batchId ) {
50 $status = Status
::newGood();
52 $dbw = $this->getMasterDB();
54 $status->fatal( 'filejournal-fail-dbconnect', $this->backend
);
57 $now = wfTimestamp( TS_UNIX
);
60 foreach ( $entries as $entry ) {
62 'fj_batch_uuid' => $batchId,
63 'fj_backend' => $this->backend
,
64 'fj_op' => $entry['op'],
65 'fj_path' => $entry['path'],
66 'fj_path_sha1' => wfBaseConvert( sha1( $entry['path'] ), 16, 36, 31 ),
67 'fj_new_sha1' => $entry['newSha1'],
68 'fj_timestamp' => $dbw->timestamp( $now )
74 $dbw->insert( 'filejournal', $data, __METHOD__
);
76 } catch ( DBError
$e ) {
77 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
85 * @see FileJournal::purgeOldLogs()
88 protected function doPurgeOldLogs() {
89 $status = Status
::newGood();
90 if ( $this->ttlDays
<= 0 ) {
91 return $status; // nothing to do
94 $dbw = $this->getMasterDB();
96 $status->fatal( 'filejournal-fail-dbconnect', $this->backend
);
99 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays
);
103 $dbw->delete( 'filejournal',
104 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
108 } catch ( DBError
$e ) {
109 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
117 * Get a master connection to the logging DB
119 * @return DatabaseBase|null
121 protected function getMasterDB() {
123 $lb = wfGetLBFactory()->newMainLB();
124 return $lb->getConnection( DB_MASTER
, array(), $this->wiki
);
125 } catch ( DBConnectionError
$e ) {