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