MediaWiki master
UndoLog.php
Go to the documentation of this file.
1<?php
2
4
6
11class UndoLog {
13 private $file;
15 private $dbw;
16
21 public function __construct( $fileName, IDatabase $dbw ) {
22 if ( $fileName !== null ) {
23 $this->file = fopen( $fileName, 'a' );
24 if ( !$this->file ) {
25 throw new \RuntimeException( 'Unable to open undo log' );
26 }
27 }
28 $this->dbw = $dbw;
29 }
30
38 public function update( $table, array $newValues, array $oldValues, $fname ) {
39 $this->dbw->newUpdateQueryBuilder()
40 ->update( $table )
41 ->set( $newValues )
42 ->where( $oldValues )
43 ->caller( $fname )->execute();
44
45 $updated = (bool)$this->dbw->affectedRows();
46 if ( $this->file && $updated ) {
47 $table = $this->dbw->tableName( $table );
48 fwrite(
49 $this->file,
50 "UPDATE $table" .
51 ' SET ' . $this->dbw->makeList( $oldValues, IDatabase::LIST_SET ) .
52 ' WHERE ' . $this->dbw->makeList( $newValues, IDatabase::LIST_AND ) . ";\n"
53 );
54 }
55 return $updated;
56 }
57}
Update a database while optionally writing SQL that reverses the update to a file.
Definition UndoLog.php:11
__construct( $fileName, IDatabase $dbw)
Definition UndoLog.php:21
update( $table, array $newValues, array $oldValues, $fname)
Definition UndoLog.php:38
Interface to a relational database.
Definition IDatabase.php:45