Merge branch 'master' into Wikidata
[lhc/web/wiklou.git] / includes / filerepo / backend / filejournal / DBFileJournal.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileJournal
5 * @author Aaron Schulz
6 */
7
8 /**
9 * Version of FileJournal that logs to a DB table
10 * @since 1.20
11 */
12 class DBFileJournal extends FileJournal {
13 protected $wiki = false; // string; wiki DB name
14
15 /**
16 * Construct a new instance from configuration.
17 * $config includes:
18 * 'wiki' : wiki name to use for LoadBalancer
19 *
20 * @param $config Array
21 */
22 protected function __construct( array $config ) {
23 parent::__construct( $config );
24
25 $this->wiki = $config['wiki'];
26 }
27
28 /**
29 * @see FileJournal::logChangeBatch()
30 * @return Status
31 */
32 protected function doLogChangeBatch( array $entries, $batchId ) {
33 $status = Status::newGood();
34
35 $dbw = $this->getMasterDB();
36 if ( !$dbw ) {
37 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
38 return $status;
39 }
40 $now = wfTimestamp( TS_UNIX );
41
42 $data = array();
43 foreach ( $entries as $entry ) {
44 $data[] = array(
45 'fj_batch_uuid' => $batchId,
46 'fj_backend' => $this->backend,
47 'fj_op' => $entry['op'],
48 'fj_path' => $entry['path'],
49 'fj_path_sha1' => wfBaseConvert( sha1( $entry['path'] ), 16, 36, 31 ),
50 'fj_new_sha1' => $entry['newSha1'],
51 'fj_timestamp' => $dbw->timestamp( $now )
52 );
53 }
54
55 try {
56 $dbw->begin();
57 $dbw->insert( 'filejournal', $data, __METHOD__ );
58 $dbw->commit();
59 } catch ( DBError $e ) {
60 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
61 return $status;
62 }
63
64 return $status;
65 }
66
67 /**
68 * @see FileJournal::purgeOldLogs()
69 * @return Status
70 */
71 protected function doPurgeOldLogs() {
72 $status = Status::newGood();
73 if ( $this->ttlDays <= 0 ) {
74 return $status; // nothing to do
75 }
76
77 $dbw = $this->getMasterDB();
78 if ( !$dbw ) {
79 $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
80 return $status;
81 }
82 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
83
84 try {
85 $dbw->begin();
86 $dbw->delete( 'filejournal',
87 array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
88 __METHOD__
89 );
90 $dbw->commit();
91 } catch ( DBError $e ) {
92 $status->fatal( 'filejournal-fail-dbquery', $this->backend );
93 return $status;
94 }
95
96 return $status;
97 }
98
99 /**
100 * Get a master connection to the logging DB
101 *
102 * @return DatabaseBase|null
103 */
104 protected function getMasterDB() {
105 try {
106 $lb = wfGetLBFactory()->newMainLB();
107 return $lb->getConnection( DB_MASTER, array(), $this->wiki );
108 } catch ( DBConnectionError $e ) {
109 return null;
110 }
111 }
112 }