Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / filebackend / filejournal / DBFileJournal.php
blob0ade6164f25e43b2abc7558816d3a85d91ee468f
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 /**
26 * Version of FileJournal that logs to a DB table
27 * @since 1.20
29 class DBFileJournal extends FileJournal {
30 /** @var IDatabase */
31 protected $dbw;
33 protected $wiki = false; // string; wiki DB name
35 /**
36 * Construct a new instance from configuration.
38 * @param array $config Includes:
39 * 'wiki' : wiki name to use for LoadBalancer
41 protected function __construct( array $config ) {
42 parent::__construct( $config );
44 $this->wiki = $config['wiki'];
47 /**
48 * @see FileJournal::logChangeBatch()
49 * @param array $entries
50 * @param string $batchId
51 * @return Status
53 protected function doLogChangeBatch( array $entries, $batchId ) {
54 $status = Status::newGood();
56 try {
57 $dbw = $this->getMasterDB();
58 } catch ( DBError $e ) {
59 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
61 return $status;
64 $now = wfTimestamp( TS_UNIX );
66 $data = array();
67 foreach ( $entries as $entry ) {
68 $data[] = array(
69 'fj_batch_uuid' => $batchId,
70 'fj_backend' => $this->backend,
71 'fj_op' => $entry['op'],
72 'fj_path' => $entry['path'],
73 'fj_new_sha1' => $entry['newSha1'],
74 'fj_timestamp' => $dbw->timestamp( $now )
78 try {
79 $dbw->insert( 'filejournal', $data, __METHOD__ );
80 if ( mt_rand( 0, 99 ) == 0 ) {
81 $this->purgeOldLogs(); // occasionally delete old logs
83 } catch ( DBError $e ) {
84 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
86 return $status;
89 return $status;
92 /**
93 * @see FileJournal::doGetCurrentPosition()
94 * @return bool|mixed The value from the field, or false on failure.
96 protected function doGetCurrentPosition() {
97 $dbw = $this->getMasterDB();
99 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
100 array( 'fj_backend' => $this->backend ),
101 __METHOD__
106 * @see FileJournal::doGetPositionAtTime()
107 * @param int|string $time Timestamp
108 * @return bool|mixed The value from the field, or false on failure.
110 protected function doGetPositionAtTime( $time ) {
111 $dbw = $this->getMasterDB();
113 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
115 return $dbw->selectField( 'filejournal', 'fj_id',
116 array( 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ),
117 __METHOD__,
118 array( 'ORDER BY' => 'fj_timestamp DESC' )
123 * @see FileJournal::doGetChangeEntries()
124 * @param int $start
125 * @param int $limit
126 * @return array
128 protected function doGetChangeEntries( $start, $limit ) {
129 $dbw = $this->getMasterDB();
131 $res = $dbw->select( 'filejournal', '*',
132 array(
133 'fj_backend' => $this->backend,
134 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
135 __METHOD__,
136 array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
137 $limit ? array( 'LIMIT' => $limit ) : array() )
140 $entries = array();
141 foreach ( $res as $row ) {
142 $item = array();
143 foreach ( (array)$row as $key => $value ) {
144 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
146 $entries[] = $item;
149 return $entries;
153 * @see FileJournal::purgeOldLogs()
154 * @return Status
155 * @throws DBError
157 protected function doPurgeOldLogs() {
158 $status = Status::newGood();
159 if ( $this->ttlDays <= 0 ) {
160 return $status; // nothing to do
163 $dbw = $this->getMasterDB();
164 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
166 $dbw->delete( 'filejournal',
167 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
168 __METHOD__
171 return $status;
175 * Get a master connection to the logging DB
177 * @return IDatabase
178 * @throws DBError
180 protected function getMasterDB() {
181 if ( !$this->dbw ) {
182 // Get a separate connection in autocommit mode
183 $lb = wfGetLBFactory()->newMainLB();
184 $this->dbw = $lb->getConnection( DB_MASTER, array(), $this->wiki );
185 $this->dbw->clearFlag( DBO_TRX );
188 return $this->dbw;