2012-03-13 01:46:33 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-07 07:11:33 +00:00
|
|
|
* Version of FileJournal that logs to a DB table.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-03-13 01:46:33 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup FileJournal
|
|
|
|
|
*/
|
|
|
|
|
|
2017-02-07 13:31:46 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-02-24 16:17:16 +00:00
|
|
|
use Wikimedia\Rdbms\DBError;
|
2017-02-07 13:31:46 +00:00
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
/**
|
|
|
|
|
* Version of FileJournal that logs to a DB table
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
class DBFileJournal extends FileJournal {
|
2015-10-06 05:39:37 +00:00
|
|
|
/** @var IDatabase */
|
2012-08-27 20:42:31 +00:00
|
|
|
protected $dbw;
|
|
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
protected $wiki = false; // string; wiki DB name
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a new instance from configuration.
|
2012-08-11 06:03:31 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $config Includes:
|
|
|
|
|
* 'wiki' : wiki name to use for LoadBalancer
|
2012-03-13 01:46:33 +00:00
|
|
|
*/
|
|
|
|
|
protected function __construct( array $config ) {
|
|
|
|
|
parent::__construct( $config );
|
|
|
|
|
|
|
|
|
|
$this->wiki = $config['wiki'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileJournal::logChangeBatch()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $entries
|
|
|
|
|
* @param string $batchId
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-13 01:46:33 +00:00
|
|
|
*/
|
|
|
|
|
protected function doLogChangeBatch( array $entries, $batchId ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = StatusValue::newGood();
|
2012-03-13 01:46:33 +00:00
|
|
|
|
2012-03-29 21:47:39 +00:00
|
|
|
try {
|
|
|
|
|
$dbw = $this->getMasterDB();
|
|
|
|
|
} catch ( DBError $e ) {
|
2012-03-13 01:46:33 +00:00
|
|
|
$status->fatal( 'filejournal-fail-dbconnect', $this->backend );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
2012-03-29 21:47:39 +00:00
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
$now = wfTimestamp( TS_UNIX );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$data = [];
|
2012-03-13 01:46:33 +00:00
|
|
|
foreach ( $entries as $entry ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$data[] = [
|
2012-03-13 01:46:33 +00:00
|
|
|
'fj_batch_uuid' => $batchId,
|
2013-04-20 17:18:13 +00:00
|
|
|
'fj_backend' => $this->backend,
|
|
|
|
|
'fj_op' => $entry['op'],
|
|
|
|
|
'fj_path' => $entry['path'],
|
|
|
|
|
'fj_new_sha1' => $entry['newSha1'],
|
|
|
|
|
'fj_timestamp' => $dbw->timestamp( $now )
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-03-13 01:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$dbw->insert( 'filejournal', $data, __METHOD__ );
|
2012-11-07 22:21:23 +00:00
|
|
|
if ( mt_rand( 0, 99 ) == 0 ) {
|
|
|
|
|
$this->purgeOldLogs(); // occasionally delete old logs
|
|
|
|
|
}
|
2012-03-13 01:46:33 +00:00
|
|
|
} catch ( DBError $e ) {
|
|
|
|
|
$status->fatal( 'filejournal-fail-dbquery', $this->backend );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-04 18:43:59 +00:00
|
|
|
/**
|
|
|
|
|
* @see FileJournal::doGetCurrentPosition()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return bool|mixed The value from the field, or false on failure.
|
2012-10-04 18:43:59 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetCurrentPosition() {
|
|
|
|
|
$dbw = $this->getMasterDB();
|
|
|
|
|
|
|
|
|
|
return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'fj_backend' => $this->backend ],
|
2012-10-04 18:43:59 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 12:45:15 +00:00
|
|
|
/**
|
|
|
|
|
* @see FileJournal::doGetPositionAtTime()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param int|string $time Timestamp
|
|
|
|
|
* @return bool|mixed The value from the field, or false on failure.
|
2013-03-03 12:45:15 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetPositionAtTime( $time ) {
|
|
|
|
|
$dbw = $this->getMasterDB();
|
|
|
|
|
|
|
|
|
|
$encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2013-03-03 12:45:15 +00:00
|
|
|
return $dbw->selectField( 'filejournal', 'fj_id',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ],
|
2013-03-03 12:45:15 +00:00
|
|
|
__METHOD__,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ORDER BY' => 'fj_timestamp DESC' ]
|
2013-03-03 12:45:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-29 21:47:39 +00:00
|
|
|
/**
|
|
|
|
|
* @see FileJournal::doGetChangeEntries()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param int $start
|
|
|
|
|
* @param int $limit
|
|
|
|
|
* @return array
|
2012-03-29 21:47:39 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetChangeEntries( $start, $limit ) {
|
|
|
|
|
$dbw = $this->getMasterDB();
|
|
|
|
|
|
|
|
|
|
$res = $dbw->select( 'filejournal', '*',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2012-03-29 21:47:39 +00:00
|
|
|
'fj_backend' => $this->backend,
|
2016-02-17 09:09:32 +00:00
|
|
|
'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
|
2012-03-29 21:47:39 +00:00
|
|
|
__METHOD__,
|
2016-02-17 09:09:32 +00:00
|
|
|
array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
|
|
|
|
|
$limit ? [ 'LIMIT' => $limit ] : [] )
|
2012-03-29 21:47:39 +00:00
|
|
|
);
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$entries = [];
|
2012-03-29 21:47:39 +00:00
|
|
|
foreach ( $res as $row ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$item = [];
|
2012-03-29 21:47:39 +00:00
|
|
|
foreach ( (array)$row as $key => $value ) {
|
|
|
|
|
$item[substr( $key, 3 )] = $value; // "fj_op" => "op"
|
|
|
|
|
}
|
|
|
|
|
$entries[] = $item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $entries;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
/**
|
|
|
|
|
* @see FileJournal::purgeOldLogs()
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-29 21:47:39 +00:00
|
|
|
* @throws DBError
|
2012-03-13 01:46:33 +00:00
|
|
|
*/
|
|
|
|
|
protected function doPurgeOldLogs() {
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = StatusValue::newGood();
|
2012-03-13 01:46:33 +00:00
|
|
|
if ( $this->ttlDays <= 0 ) {
|
|
|
|
|
return $status; // nothing to do
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dbw = $this->getMasterDB();
|
|
|
|
|
$dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
|
|
|
|
|
|
2012-03-29 21:47:39 +00:00
|
|
|
$dbw->delete( 'filejournal',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
|
2012-03-29 21:47:39 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2012-03-13 01:46:33 +00:00
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a master connection to the logging DB
|
2012-03-29 21:47:39 +00:00
|
|
|
*
|
2015-10-06 05:39:37 +00:00
|
|
|
* @return IDatabase
|
2012-03-29 21:47:39 +00:00
|
|
|
* @throws DBError
|
2012-03-13 01:46:33 +00:00
|
|
|
*/
|
|
|
|
|
protected function getMasterDB() {
|
2012-08-27 20:42:31 +00:00
|
|
|
if ( !$this->dbw ) {
|
|
|
|
|
// Get a separate connection in autocommit mode
|
2017-08-11 13:53:17 +00:00
|
|
|
$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->newMainLB();
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->dbw = $lb->getConnection( DB_MASTER, [], $this->wiki );
|
2012-08-27 20:42:31 +00:00
|
|
|
$this->dbw->clearFlag( DBO_TRX );
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-08-27 20:42:31 +00:00
|
|
|
return $this->dbw;
|
2012-03-13 01:46:33 +00:00
|
|
|
}
|
|
|
|
|
}
|