Merge "Add new-inline-tags to tidy.conf"
[lhc/web/wiklou.git] / includes / filerepo / backend / 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 $dbw = $this->getMasterDB();
53 if ( !$dbw ) {
54 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
55 return $status;
56 }
57 $now = wfTimestamp( TS_UNIX );
58
59 $data = array();
60 foreach ( $entries as $entry ) {
61 $data[] = array(
62 'fj_batch_uuid' => $batchId,
63 'fj_backend' => $this->backend,
64 'fj_op' => $entry['op'],
65 'fj_path' => $entry['path'],
66 'fj_path_sha1' => wfBaseConvert( sha1( $entry['path'] ), 16, 36, 31 ),
67 'fj_new_sha1' => $entry['newSha1'],
68 'fj_timestamp' => $dbw->timestamp( $now )
69 );
70 }
71
72 try {
73 $dbw->begin();
74 $dbw->insert( 'filejournal', $data, __METHOD__ );
75 $dbw->commit();
76 } catch ( DBError $e ) {
77 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
78 return $status;
79 }
80
81 return $status;
82 }
83
84 /**
85 * @see FileJournal::purgeOldLogs()
86 * @return Status
87 */
88 protected function doPurgeOldLogs() {
89 $status = Status::newGood();
90 if ( $this->ttlDays <= 0 ) {
91 return $status; // nothing to do
92 }
93
94 $dbw = $this->getMasterDB();
95 if ( !$dbw ) {
96 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
97 return $status;
98 }
99 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
100
101 try {
102 $dbw->begin();
103 $dbw->delete( 'filejournal',
104 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
105 __METHOD__
106 );
107 $dbw->commit();
108 } catch ( DBError $e ) {
109 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
110 return $status;
111 }
112
113 return $status;
114 }
115
116 /**
117 * Get a master connection to the logging DB
118 *
119 * @return DatabaseBase|null
120 */
121 protected function getMasterDB() {
122 try {
123 $lb = wfGetLBFactory()->newMainLB();
124 return $lb->getConnection( DB_MASTER, array(), $this->wiki );
125 } catch ( DBConnectionError $e ) {
126 return null;
127 }
128 }
129 }