John Erling Blad has submitted this change and it was merged.

Change subject: (bug 46565) Don't prune undispatched changes.
......................................................................


(bug 46565) Don't prune undispatched changes.

This makes the pruneChanges script consider which changes have been dispatched 
yet.

Change-Id: Ic7663a6153a75548c3984a9d61664ff85c934f3d
---
M repo/maintenance/pruneChanges.php
1 file changed, 92 insertions(+), 14 deletions(-)

Approvals:
  John Erling Blad: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/repo/maintenance/pruneChanges.php 
b/repo/maintenance/pruneChanges.php
index 9ee56b6..0cbe081 100644
--- a/repo/maintenance/pruneChanges.php
+++ b/repo/maintenance/pruneChanges.php
@@ -33,14 +33,37 @@
  * @ingroup Maintenance
  */
 class PruneChanges extends Maintenance {
+
+       /**
+        * @var int the minimum number of seconds to keep changes for.
+        */
+       protected $keepSeconds = 0;
+
+       /**
+        * @var int the minimum number of seconds after dispatching to keep 
changes for.
+        */
+       protected $graceSeconds = 0;
+
+       /**
+        * @var bool whether the dispatch time should be ignored
+        */
+       protected $ignoreDispatch = false;
+
        public function __construct() {
                parent::__construct();
                $this->mDescription = "Prune the Wikibase changes table to a 
maximum number of entries";
 
-               $this->addOption( 'number-of-days', 'Number of days to keep 
entries in the table after the '
-                                               . 'maintenance script has been 
run (default: 7)', false, true, 'n' );
+               $this->addOption( 'number-of-days', 'Keep changes at least N 
days (deprecated).', false, true, 'n' );
+               $this->addOption( 'keep-days',  'Keep changes at least N 
days.', false, true, 'd' );
+               $this->addOption( 'keep-hours', 'Keep changes at least N 
hours.', false, true, 'h' );
+               $this->addOption( 'keep-minutes', 'Keep changes at least N 
minutes.', false, true, 'm' );
+               $this->addOption( 'grace-minutes', 'Keep changes at least N 
more minutes after they have been dispatched.', false, true, 'g' );
+
                $this->addOption( 'force', 'Run regardless of whether the PID 
file says it is running already.',
                                                 false, false, 'f' );
+
+               $this->addOption( 'ignore-dispatch', 'Ignore whether changes 
have been dispatched or not.',
+                                               false, false, 'D' );
        }
 
        public function execute() {
@@ -49,7 +72,6 @@
                        exit;
                }
 
-               $numDays = intval( $this->getOption( 'number-of-days', 7 ) );
                $force = $this->getOption( 'force', false );
                $pidfile = Utils::makePidFilename( 'WBpruneChanges', wfWikiID() 
);
 
@@ -58,28 +80,84 @@
                        exit( 5 );
                }
 
-               $this->pruneChanges( $numDays );
+               $this->ignoreDispatch = $this->getOption( 'ignore-dispatch', 
false );
 
-               $this->output( date( 'H:i:s' ) . " done, exiting\n" );
+               $this->keepSeconds = 0;
+               $this->keepSeconds += intval( $this->getOption( 
'number-of-days', 0 ) ) * 24 * 60 * 60;
+               $this->keepSeconds += intval( $this->getOption( 'keep-days', 0 
) ) * 24 * 60 * 60;
+               $this->keepSeconds += intval( $this->getOption( 'keep-hours', 0 
) ) * 60 * 60;
+               $this->keepSeconds += intval( $this->getOption( 'keep-minutes', 
0 ) ) * 60;
+
+               if ( $this->keepSeconds === 0 ) {
+                       // one day
+                       $this->keepSeconds = 1 * 24 * 60 * 60;
+               }
+
+               $this->graceSeconds = 0;
+               $this->graceSeconds += intval( $this->getOption( 
'grace-minutes', 0 ) ) * 60;
+
+               if ( $this->graceSeconds === 0 ) {
+                       // one hour
+                       $this->graceSeconds = 1 * 60 * 60;
+               }
+
+               $until = $this->getCutoffTimestamp();
+               $this->output( date( 'H:i:s' ) . " pruning entries older than "
+                       . wfTimestamp( TS_ISO_8601, $until ) . "\n" );
+
+               $deleted = $this->pruneChanges( $until );
+               $this->output( date( 'H:i:s' ) . " $deleted rows pruned.\n" );
+
                unlink( $pidfile ); // delete lockfile on normal exit
        }
 
-       public function pruneChanges( $numDays ) {
+       /**
+        * Calculates the timestamp up to which changes can be pruned.
+        *
+        * @return int timstamp up to which changes can be pruned (as unix 
period)
+        */
+       protected function getCutoffTimestamp() {
+               $until = time() - $this->keepSeconds;
+
+               if ( !$this->ignoreDispatch ) {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $row = $dbw->selectRow(
+                               array ( 'wb_changes_dispatch', 'wb_changes' ),
+                               'min(change_time) as timestamp',
+                               array(
+                                       'chd_disabled' => 0,
+                                       'chd_seen = change_id'
+                               ),
+                               __METHOD__
+                       );
+
+                       if ( isset( $row->timestamp ) ) {
+                               $dispatched = wfTimestamp( TS_UNIX, 
$row->timestamp ) - $this->graceSeconds;
+
+                               $until = min( $until, $dispatched );
+                       }
+               }
+
+               return $until;
+       }
+
+       /**
+        * Prunes all changes older than $until from the changes table.
+        *
+        * @param $until
+        *
+        * @return int the number of changes deleted.
+        */
+       public function pruneChanges( $until ) {
                $dbw = wfGetDB( DB_MASTER );
-
-               $since = wfTimestamp( TS_MW, time() - $numDays * 24 * 60 * 60 );
-
-               $this->output( date( 'H:i:s' ) . " pruning entries older than "
-                                               . wfTimestamp( TS_ISO_8601, 
$since ) . "\n" );
 
                $dbw->delete(
                        'wb_changes',
-                       array( "change_time < " . $dbw->addQuotes( $since ) ), 
// added after request
+                       array( "change_time < " . $dbw->addQuotes( wfTimestamp( 
TS_MW, $until ) ) ),
                        __METHOD__
                );
 
-               $rows = $dbw->affectedRows();
-               $this->output( date( 'H:i:s' ) . " $rows rows pruned\n" );
+               return $dbw->affectedRows();
        }
 
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/55896
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic7663a6153a75548c3984a9d61664ff85c934f3d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: John Erling Blad <john.b...@wikimedia.de>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to