Merge "resourceloader: Add basic tests for getScript() and buildContent()"
[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 use Wikimedia\Rdbms\IDatabase;
27 use Wikimedia\Rdbms\DBError;
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
37 protected $wiki = false; // string; wiki DB name
38
39 /**
40 * Construct a new instance from configuration.
41 *
42 * @param array $config Includes:
43 * 'wiki' : wiki name to use for LoadBalancer
44 */
45 protected function __construct( array $config ) {
46 parent::__construct( $config );
47
48 $this->wiki = $config['wiki'];
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 = wfTimestamp( TS_UNIX );
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 if ( mt_rand( 0, 99 ) == 0 ) {
85 $this->purgeOldLogs(); // occasionally delete old logs
86 }
87 } catch ( DBError $e ) {
88 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
89
90 return $status;
91 }
92
93 return $status;
94 }
95
96 /**
97 * @see FileJournal::doGetCurrentPosition()
98 * @return bool|mixed The value from the field, or false on failure.
99 */
100 protected function doGetCurrentPosition() {
101 $dbw = $this->getMasterDB();
102
103 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
104 [ 'fj_backend' => $this->backend ],
105 __METHOD__
106 );
107 }
108
109 /**
110 * @see FileJournal::doGetPositionAtTime()
111 * @param int|string $time Timestamp
112 * @return bool|mixed The value from the field, or false on failure.
113 */
114 protected function doGetPositionAtTime( $time ) {
115 $dbw = $this->getMasterDB();
116
117 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
118
119 return $dbw->selectField( 'filejournal', 'fj_id',
120 [ 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ],
121 __METHOD__,
122 [ 'ORDER BY' => 'fj_timestamp DESC' ]
123 );
124 }
125
126 /**
127 * @see FileJournal::doGetChangeEntries()
128 * @param int $start
129 * @param int $limit
130 * @return array
131 */
132 protected function doGetChangeEntries( $start, $limit ) {
133 $dbw = $this->getMasterDB();
134
135 $res = $dbw->select( 'filejournal', '*',
136 [
137 'fj_backend' => $this->backend,
138 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
139 __METHOD__,
140 array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
141 $limit ? [ 'LIMIT' => $limit ] : [] )
142 );
143
144 $entries = [];
145 foreach ( $res as $row ) {
146 $item = [];
147 foreach ( (array)$row as $key => $value ) {
148 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
149 }
150 $entries[] = $item;
151 }
152
153 return $entries;
154 }
155
156 /**
157 * @see FileJournal::purgeOldLogs()
158 * @return StatusValue
159 * @throws DBError
160 */
161 protected function doPurgeOldLogs() {
162 $status = StatusValue::newGood();
163 if ( $this->ttlDays <= 0 ) {
164 return $status; // nothing to do
165 }
166
167 $dbw = $this->getMasterDB();
168 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
169
170 $dbw->delete( 'filejournal',
171 [ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
172 __METHOD__
173 );
174
175 return $status;
176 }
177
178 /**
179 * Get a master connection to the logging DB
180 *
181 * @return IDatabase
182 * @throws DBError
183 */
184 protected function getMasterDB() {
185 if ( !$this->dbw ) {
186 // Get a separate connection in autocommit mode
187 $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->newMainLB();
188 $this->dbw = $lb->getConnection( DB_MASTER, [], $this->wiki );
189 $this->dbw->clearFlag( DBO_TRX );
190 }
191
192 return $this->dbw;
193 }
194 }