Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoggedUpdateMaintenance
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
 setForce
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 updateSkippedMessage
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
n/a
0 / 0
n/a
0 / 0
0
 getUpdateKey
n/a
0 / 0
n/a
0 / 0
0
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21/**
22 * Class for scripts that perform database maintenance and want to log the
23 * update in `updatelog` so we can later skip it
24 * @ingroup Maintenance
25 */
26abstract class LoggedUpdateMaintenance extends Maintenance {
27    public function __construct() {
28        parent::__construct();
29        $this->addOption( 'force', 'Run the update even if it was completed already' );
30        $this->setBatchSize( 200 );
31    }
32
33    public function execute() {
34        $db = $this->getPrimaryDB();
35        $key = $this->getUpdateKey();
36        $queryBuilder = $db->newSelectQueryBuilder()
37            ->select( '1' )
38            ->from( 'updatelog' )
39            ->where( [ 'ul_key' => $key ] );
40
41        if ( !$this->hasOption( 'force' )
42            && $queryBuilder->caller( __METHOD__ )->fetchRow()
43        ) {
44            $this->output( "..." . $this->updateSkippedMessage() . "\n" );
45
46            return true;
47        }
48
49        if ( !$this->doDBUpdates() ) {
50            return false;
51        }
52
53        $db->newInsertQueryBuilder()
54            ->insertInto( 'updatelog' )
55            ->ignore()
56            ->row( [ 'ul_key' => $key ] )
57            ->caller( __METHOD__ )->execute();
58
59        return true;
60    }
61
62    /**
63     * Sets whether a run of this maintenance script has the force parameter set
64     * @param bool $forced
65     */
66    public function setForce( $forced = true ) {
67        $this->mOptions['force'] = $forced;
68    }
69
70    /**
71     * Message to show that the update was done already and was just skipped
72     * @return string
73     */
74    protected function updateSkippedMessage() {
75        $key = $this->getUpdateKey();
76
77        return "Update '{$key}' already logged as completed. Use --force to run it again.";
78    }
79
80    /**
81     * Do the actual work. All child classes will need to implement this.
82     * Return true to log the update as done or false (usually on failure).
83     * @return bool
84     */
85    abstract protected function doDBUpdates();
86
87    /**
88     * Get the update key name to go in the update log table
89     * @return string
90     */
91    abstract protected function getUpdateKey();
92}