Bsitu has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/153372

Change subject: [WIP]Delete invalid records from echo_target_page
......................................................................

[WIP]Delete invalid records from echo_target_page

Purpose:

* If a page is no longer valid, it should be removed

* If a notification somehow has been read, it should be removed

Change-Id: I282568691d6649c6e3263aea598c3bba29119a29
---
A maintenance/removeInvalidTargetPage.php
1 file changed, 88 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/72/153372/1

diff --git a/maintenance/removeInvalidTargetPage.php 
b/maintenance/removeInvalidTargetPage.php
new file mode 100644
index 0000000..b84bc32
--- /dev/null
+++ b/maintenance/removeInvalidTargetPage.php
@@ -0,0 +1,88 @@
+<?php
+/**
+ * Remove invalid records from echo_target_page
+ *
+ * @ingroup Maintenance
+ */
+require_once ( getenv( 'MW_INSTALL_PATH' ) !== false
+       ? getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'
+       : dirname( __FILE__ ) . '/../../../maintenance/Maintenance.php' );
+
+/**
+ * Maintenance script that removes invalid target page
+ *
+ * @ingroup Maintenance
+ */
+class removeInvalidTargetPage extends Maintenance {
+
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = "Delete invalid records from 
echo_target_page";
+               $this->setBatchSize( 500 );
+       }
+
+       public function execute() {
+               global $wgEchoCluster;
+
+               $dbFactory = MWEchoDbFactory::newFromDefault();
+               $dbw = $dbFactory->getEchoDb( DB_MASTER );
+               $dbr = $dbFactory->getEchoDb( DB_SLAVE );
+
+               $count = $this->mBatchSize;
+               $userId = $eventId = 0;
+
+               while ( $count == $this->mBatchSize ) {
+                       $res = $dbr->select(
+                               array( 'echo_target_page', 'echo_notification' 
),
+                               array( 'etp_page', 'etp_user', 'etp_event', 
'notification_read_timestamp' ),
+                               array(
+                                       "etp_user > $userId OR ( etp_user = 
$userId AND etp_event > $eventId )"
+                               ),
+                               __METHOD__,
+                               array( 'ORDER BY' => 'etp_user, etp_event', 
'LIMIT' => $this->mBatchSize ),
+                               array(
+                                       'echo_notification' => array( 'LEFT 
JOIN', 'notification_event=etp_event AND notification_user = etp_user' ),
+                               )
+                       );
+                       if ( !$res ) {
+                               throw new MWException( 'Could not select record 
from echo_target_page' );
+                       }
+
+                       $count = $invalidCount = 0;
+                       foreach( $res as $row ) {
+                               $delete = false;
+                               // Delete if notification is read
+                               if ( !$row['notification_read_timestamp'] ) {
+                                       $delete = true;
+                               }
+                               if ( !$delete ) {
+                                       // Delete if title is no longer valid
+                                       if ( !Title::newFromId( 
$row['etp_page'] ) ) {
+                                               $delete = true;
+                                       }
+                               }
+                               
+                               if ( $delete ) {
+                                       $dbw->delete(
+                                               'echo_target_page',
+                                               array(
+                                                       'etp_user' => 
$row['etp_user'],
+                                                       'etp_event' => 
$row['etp_event']
+                                               ),
+                                               __METHOD__
+                                       );
+                                       $invalidCount++;
+                               }
+                               $userId = $row['etp_user'];
+                               $eventId = $row['etp_event'];
+                               $count++;
+                       };
+
+                       $this->output( "Deleted $invalidCount records from 
$count records\n" );
+                       wfWaitForSlaves( false, false, $wgEchoCluster );
+               }
+       }
+}
+
+$maintClass = 'removeInvalidTargetPage'; // Tells it to run the class
+require_once( RUN_MAINTENANCE_IF_MAIN );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I282568691d6649c6e3263aea598c3bba29119a29
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to