Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
UndoLog | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
update | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Maintenance; |
4 | |
5 | use Wikimedia\Rdbms\IDatabase; |
6 | |
7 | /** |
8 | * Update a database while optionally writing SQL that reverses the update to |
9 | * a file. |
10 | */ |
11 | class UndoLog { |
12 | /** @var resource */ |
13 | private $file; |
14 | /** @var IDatabase */ |
15 | private $dbw; |
16 | |
17 | /** |
18 | * @param string|null $fileName |
19 | * @param IDatabase $dbw |
20 | */ |
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 | |
31 | /** |
32 | * @param string $table |
33 | * @param array $newValues |
34 | * @param array $oldValues |
35 | * @param string $fname |
36 | * @return bool |
37 | */ |
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 | } |