Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UndoLog
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 update
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Maintenance;
4
5use Wikimedia\Rdbms\IDatabase;
6
7/**
8 * Update a database while optionally writing SQL that reverses the update to
9 * a file.
10 */
11class UndoLog {
12    private $file;
13    private $dbw;
14
15    /**
16     * @param string|null $fileName
17     * @param IDatabase $dbw
18     */
19    public function __construct( $fileName, IDatabase $dbw ) {
20        if ( $fileName !== null ) {
21            $this->file = fopen( $fileName, 'a' );
22            if ( !$this->file ) {
23                throw new \RuntimeException( 'Unable to open undo log' );
24            }
25        }
26        $this->dbw = $dbw;
27    }
28
29    /**
30     * @param string $table
31     * @param array $newValues
32     * @param array $oldValues
33     * @param string $fname
34     * @return bool
35     */
36    public function update( $table, array $newValues, array $oldValues, $fname ) {
37        $this->dbw->newUpdateQueryBuilder()
38            ->update( $table )
39            ->set( $newValues )
40            ->where( $oldValues )
41            ->caller( $fname )->execute();
42
43        $updated = (bool)$this->dbw->affectedRows();
44        if ( $this->file && $updated ) {
45            $table = $this->dbw->tableName( $table );
46            fwrite(
47                $this->file,
48                "UPDATE $table" .
49                    ' SET ' . $this->dbw->makeList( $oldValues, IDatabase::LIST_SET ) .
50                    ' WHERE ' . $this->dbw->makeList( $newValues, IDatabase::LIST_AND ) . ";\n"
51            );
52        }
53        return $updated;
54    }
55}