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