merging latest master
[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 /**
26 * Version of FileJournal that logs to a DB table
27 * @since 1.20
28 */
29 class DBFileJournal extends FileJournal {
30 protected $wiki = false; // string; wiki DB name
31
32 /**
33 * Construct a new instance from configuration.
34 * $config includes:
35 * 'wiki' : wiki name to use for LoadBalancer
36 *
37 * @param $config Array
38 */
39 protected function __construct( array $config ) {
40 parent::__construct( $config );
41
42 $this->wiki = $config['wiki'];
43 }
44
45 /**
46 * @see FileJournal::logChangeBatch()
47 * @return Status
48 */
49 protected function doLogChangeBatch( array $entries, $batchId ) {
50 $status = Status::newGood();
51
52 try {
53 $dbw = $this->getMasterDB();
54 } catch ( DBError $e ) {
55 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
56 return $status;
57 }
58
59 $now = wfTimestamp( TS_UNIX );
60
61 $data = array();
62 foreach ( $entries as $entry ) {
63 $data[] = array(
64 'fj_batch_uuid' => $batchId,
65 'fj_backend' => $this->backend,
66 'fj_op' => $entry['op'],
67 'fj_path' => $entry['path'],
68 'fj_new_sha1' => $entry['newSha1'],
69 'fj_timestamp' => $dbw->timestamp( $now )
70 );
71 }
72
73 try {
74 $dbw->begin();
75 $dbw->insert( 'filejournal', $data, __METHOD__ );
76 $dbw->commit();
77 } catch ( DBError $e ) {
78 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
79 return $status;
80 }
81
82 return $status;
83 }
84
85 /**
86 * @see FileJournal::doGetChangeEntries()
87 * @return Array
88 * @throws DBError
89 */
90 protected function doGetChangeEntries( $start, $limit ) {
91 $dbw = $this->getMasterDB();
92
93 $res = $dbw->select( 'filejournal', '*',
94 array(
95 'fj_backend' => $this->backend,
96 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
97 __METHOD__,
98 array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
99 $limit ? array( 'LIMIT' => $limit ) : array() )
100 );
101
102 $entries = array();
103 foreach ( $res as $row ) {
104 $item = array();
105 foreach ( (array)$row as $key => $value ) {
106 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
107 }
108 $entries[] = $item;
109 }
110
111 return $entries;
112 }
113
114 /**
115 * @see FileJournal::purgeOldLogs()
116 * @return Status
117 * @throws DBError
118 */
119 protected function doPurgeOldLogs() {
120 $status = Status::newGood();
121 if ( $this->ttlDays <= 0 ) {
122 return $status; // nothing to do
123 }
124
125 $dbw = $this->getMasterDB();
126 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
127
128 $dbw->begin();
129 $dbw->delete( 'filejournal',
130 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
131 __METHOD__
132 );
133 $dbw->commit();
134
135 return $status;
136 }
137
138 /**
139 * Get a master connection to the logging DB
140 *
141 * @return DatabaseBase
142 * @throws DBError
143 */
144 protected function getMasterDB() {
145 $lb = wfGetLBFactory()->newMainLB();
146 return $lb->getConnection( DB_MASTER, array(), $this->wiki );
147 }
148 }