Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / filebackend / filejournal / DBFileJournal.php
blob9250aa5e0118a2c238cad4b344aebda1eafb8584
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 DatabaseBase */
31 protected $dbw;
33 protected $wiki = false; // string; wiki DB name
35 /**
36 * Construct a new instance from configuration.
37 * $config includes:
38 * 'wiki' : wiki name to use for LoadBalancer
40 * @param $config Array
42 protected function __construct( array $config ) {
43 parent::__construct( $config );
45 $this->wiki = $config['wiki'];
48 /**
49 * @see FileJournal::logChangeBatch()
50 * @return Status
52 protected function doLogChangeBatch( array $entries, $batchId ) {
53 $status = Status::newGood();
55 try {
56 $dbw = $this->getMasterDB();
57 } catch ( DBError $e ) {
58 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
59 return $status;
62 $now = wfTimestamp( TS_UNIX );
64 $data = array();
65 foreach ( $entries as $entry ) {
66 $data[] = array(
67 'fj_batch_uuid' => $batchId,
68 'fj_backend' => $this->backend,
69 'fj_op' => $entry['op'],
70 'fj_path' => $entry['path'],
71 'fj_new_sha1' => $entry['newSha1'],
72 'fj_timestamp' => $dbw->timestamp( $now )
76 try {
77 $dbw->insert( 'filejournal', $data, __METHOD__ );
78 if ( mt_rand( 0, 99 ) == 0 ) {
79 $this->purgeOldLogs(); // occasionally delete old logs
81 } catch ( DBError $e ) {
82 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
83 return $status;
86 return $status;
89 /**
90 * @see FileJournal::doGetCurrentPosition()
91 * @return integer|false
93 protected function doGetCurrentPosition() {
94 $dbw = $this->getMasterDB();
96 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
97 array( 'fj_backend' => $this->backend ),
98 __METHOD__
103 * @see FileJournal::doGetPositionAtTime()
104 * @param $time integer|string timestamp
105 * @return integer|false
107 protected function doGetPositionAtTime( $time ) {
108 $dbw = $this->getMasterDB();
110 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
111 return $dbw->selectField( 'filejournal', 'fj_id',
112 array( 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ),
113 __METHOD__,
114 array( 'ORDER BY' => 'fj_timestamp DESC' )
119 * @see FileJournal::doGetChangeEntries()
120 * @return Array
121 * @throws DBError
123 protected function doGetChangeEntries( $start, $limit ) {
124 $dbw = $this->getMasterDB();
126 $res = $dbw->select( 'filejournal', '*',
127 array(
128 'fj_backend' => $this->backend,
129 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
130 __METHOD__,
131 array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
132 $limit ? array( 'LIMIT' => $limit ) : array() )
135 $entries = array();
136 foreach ( $res as $row ) {
137 $item = array();
138 foreach ( (array)$row as $key => $value ) {
139 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
141 $entries[] = $item;
144 return $entries;
148 * @see FileJournal::purgeOldLogs()
149 * @return Status
150 * @throws DBError
152 protected function doPurgeOldLogs() {
153 $status = Status::newGood();
154 if ( $this->ttlDays <= 0 ) {
155 return $status; // nothing to do
158 $dbw = $this->getMasterDB();
159 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
161 $dbw->delete( 'filejournal',
162 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
163 __METHOD__
166 return $status;
170 * Get a master connection to the logging DB
172 * @return DatabaseBase
173 * @throws DBError
175 protected function getMasterDB() {
176 if ( !$this->dbw ) {
177 // Get a separate connection in autocommit mode
178 $lb = wfGetLBFactory()->newMainLB();
179 $this->dbw = $lb->getConnection( DB_MASTER, array(), $this->wiki );
180 $this->dbw->clearFlag( DBO_TRX );
182 return $this->dbw;