9 * Version of FileJournal that logs to a DB table
12 class DBFileJournal
extends FileJournal
{
13 protected $wiki = false; // string; wiki DB name
16 * Construct a new instance from configuration.
18 * 'wiki' : wiki name to use for LoadBalancer
20 * @param $config Array
22 protected function __construct( array $config ) {
23 parent
::__construct( $config );
25 $this->wiki
= $config['wiki'];
29 * @see FileJournal::logChangeBatch()
32 protected function doLogChangeBatch( array $entries, $batchId ) {
33 $status = Status
::newGood();
36 $dbw = $this->getMasterDB();
37 } catch ( DBError
$e ) {
38 $status->fatal( 'filejournal-fail-dbconnect', $this->backend
);
42 $now = wfTimestamp( TS_UNIX
);
45 foreach ( $entries as $entry ) {
47 'fj_batch_uuid' => $batchId,
48 'fj_backend' => $this->backend
,
49 'fj_op' => $entry['op'],
50 'fj_path' => $entry['path'],
51 'fj_path_sha1' => wfBaseConvert( sha1( $entry['path'] ), 16, 36, 31 ),
52 'fj_new_sha1' => $entry['newSha1'],
53 'fj_timestamp' => $dbw->timestamp( $now )
59 $dbw->insert( 'filejournal', $data, __METHOD__
);
61 } catch ( DBError
$e ) {
62 $status->fatal( 'filejournal-fail-dbquery', $this->backend
);
70 * @see FileJournal::doGetChangeEntries()
74 protected function doGetChangeEntries( $start, $limit ) {
75 $dbw = $this->getMasterDB();
77 $res = $dbw->select( 'filejournal', '*',
79 'fj_backend' => $this->backend
,
80 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
82 array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
83 $limit ?
array( 'LIMIT' => $limit ) : array() )
87 foreach ( $res as $row ) {
89 foreach ( (array)$row as $key => $value ) {
90 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
99 * @see FileJournal::purgeOldLogs()
103 protected function doPurgeOldLogs() {
104 $status = Status
::newGood();
105 if ( $this->ttlDays
<= 0 ) {
106 return $status; // nothing to do
109 $dbw = $this->getMasterDB();
110 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays
);
113 $dbw->delete( 'filejournal',
114 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
123 * Get a master connection to the logging DB
125 * @return DatabaseBase
128 protected function getMasterDB() {
129 $lb = wfGetLBFactory()->newMainLB();
130 return $lb->getConnection( DB_MASTER
, array(), $this->wiki
);