MediaWiki master
LoggedUpdateMaintenance.php
Go to the documentation of this file.
1<?php
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
66 public function setForce( $forced = true ) {
67 $this->mOptions['force'] = $forced;
68 }
69
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
85 abstract protected function doDBUpdates();
86
91 abstract protected function getUpdateKey();
92}
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
__construct()
Default constructor.
doDBUpdates()
Do the actual work.
setForce( $forced=true)
Sets whether a run of this maintenance script has the force parameter set.
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
getUpdateKey()
Get the update key name to go in the update log table.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
setBatchSize( $s=0)