Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / filebackend / filejournal / DBFileJournal.php
1 <?php
2 /**
3 * Version of FileJournal that logs to a DB table.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileJournal
22 * @author Aaron Schulz
23 */
24
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * Version of FileJournal that logs to a DB table
29 * @since 1.20
30 */
31 class DBFileJournal extends FileJournal {
32 /** @var IDatabase */
33 protected $dbw;
34
35 protected $wiki = false; // string; wiki DB name
36
37 /**
38 * Construct a new instance from configuration.
39 *
40 * @param array $config Includes:
41 * 'wiki' : wiki name to use for LoadBalancer
42 */
43 protected function __construct( array $config ) {
44 parent::__construct( $config );
45
46 $this->wiki = $config['wiki'];
47 }
48
49 /**
50 * @see FileJournal::logChangeBatch()
51 * @param array $entries
52 * @param string $batchId
53 * @return StatusValue
54 */
55 protected function doLogChangeBatch( array $entries, $batchId ) {
56 $status = StatusValue::newGood();
57
58 try {
59 $dbw = $this->getMasterDB();
60 } catch ( DBError $e ) {
61 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
62
63 return $status;
64 }
65
66 $now = wfTimestamp( TS_UNIX );
67
68 $data = [];
69 foreach ( $entries as $entry ) {
70 $data[] = [
71 'fj_batch_uuid' => $batchId,
72 'fj_backend' => $this->backend,
73 'fj_op' => $entry['op'],
74 'fj_path' => $entry['path'],
75 'fj_new_sha1' => $entry['newSha1'],
76 'fj_timestamp' => $dbw->timestamp( $now )
77 ];
78 }
79
80 try {
81 $dbw->insert( 'filejournal', $data, __METHOD__ );
82 if ( mt_rand( 0, 99 ) == 0 ) {
83 $this->purgeOldLogs(); // occasionally delete old logs
84 }
85 } catch ( DBError $e ) {
86 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
87
88 return $status;
89 }
90
91 return $status;
92 }
93
94 /**
95 * @see FileJournal::doGetCurrentPosition()
96 * @return bool|mixed The value from the field, or false on failure.
97 */
98 protected function doGetCurrentPosition() {
99 $dbw = $this->getMasterDB();
100
101 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
102 [ 'fj_backend' => $this->backend ],
103 __METHOD__
104 );
105 }
106
107 /**
108 * @see FileJournal::doGetPositionAtTime()
109 * @param int|string $time Timestamp
110 * @return bool|mixed The value from the field, or false on failure.
111 */
112 protected function doGetPositionAtTime( $time ) {
113 $dbw = $this->getMasterDB();
114
115 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
116
117 return $dbw->selectField( 'filejournal', 'fj_id',
118 [ 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ],
119 __METHOD__,
120 [ 'ORDER BY' => 'fj_timestamp DESC' ]
121 );
122 }
123
124 /**
125 * @see FileJournal::doGetChangeEntries()
126 * @param int $start
127 * @param int $limit
128 * @return array
129 */
130 protected function doGetChangeEntries( $start, $limit ) {
131 $dbw = $this->getMasterDB();
132
133 $res = $dbw->select( 'filejournal', '*',
134 [
135 'fj_backend' => $this->backend,
136 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
137 __METHOD__,
138 array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
139 $limit ? [ 'LIMIT' => $limit ] : [] )
140 );
141
142 $entries = [];
143 foreach ( $res as $row ) {
144 $item = [];
145 foreach ( (array)$row as $key => $value ) {
146 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
147 }
148 $entries[] = $item;
149 }
150
151 return $entries;
152 }
153
154 /**
155 * @see FileJournal::purgeOldLogs()
156 * @return StatusValue
157 * @throws DBError
158 */
159 protected function doPurgeOldLogs() {
160 $status = StatusValue::newGood();
161 if ( $this->ttlDays <= 0 ) {
162 return $status; // nothing to do
163 }
164
165 $dbw = $this->getMasterDB();
166 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
167
168 $dbw->delete( 'filejournal',
169 [ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
170 __METHOD__
171 );
172
173 return $status;
174 }
175
176 /**
177 * Get a master connection to the logging DB
178 *
179 * @return IDatabase
180 * @throws DBError
181 */
182 protected function getMasterDB() {
183 if ( !$this->dbw ) {
184 // Get a separate connection in autocommit mode
185 $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->newMainLB();
186 $this->dbw = $lb->getConnection( DB_MASTER, [], $this->wiki );
187 $this->dbw->clearFlag( DBO_TRX );
188 }
189
190 return $this->dbw;
191 }
192 }