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