Skip to content

Commit

Permalink
add cron job to update user metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Benny Situ committed Apr 14, 2012
1 parent fad711c commit 92a675a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 15 deletions.
90 changes: 90 additions & 0 deletions cron/updateUserMetadata.php
@@ -0,0 +1,90 @@
<?php

$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = dirname( __FILE__ ) . '/../../..';
}
require_once( "$IP/maintenance/Maintenance.php" );

/**
* A maintenance script that updates expired user metadata
*/
class updateUserMetadata extends Maintenance {

/**
* Max number of article to process at a time
* @var int
*/
protected $batchSize = 500;

/**
* Database Object
*/
protected $dbr;
protected $dbw;

public function __construct() {
parent::__construct();
$this->mDescription = "Update the user metadata in pagetriage_page_tags table";
}

protected function init() {
$this->dbr = wfGetDB( DB_SLAVE );
}

public function execute() {
$this->init();
$this->output( "Started processing... \n" );

// Make the start time really old
$startTime = wfTimestamp( TS_UNIX ) - 60 * 60 * 24 * 365 * 10;
$count = $this->batchSize;
$startId = 0;

while ( $count === $this->batchSize ) {
$count = 0;
$startTime = $this->dbr->addQuotes( $this->dbr->timestamp( $startTime ) );

// Data should expire in a day, keep this inside loop so
// it's update to second
$expiration = wfTimestamp( TS_UNIX ) - 60 * 60 * 24;
$res = $this->dbr->select(
array( 'pagetriage_page' ),
array( 'ptrp_page_id', 'ptrp_created' ),
array(
'(ptrp_created > ' . $startTime . ') OR
(ptrp_created = ' . $startTime . ' AND ptrp_page_id > ' . $startId . ')',
'ptrp_tags_updated < ' . $this->dbr->addQuotes( $this->dbr->timestamp( $expiration ) )
),
__METHOD__,
array( 'LIMIT' => $this->batchSize, 'ORDER BY' => 'ptrp_created, ptrp_page_id' )
);

$pageId = array();
foreach ( $res as $row ) {
$pageId[] = $row->ptrp_page_id;
$count++;
}

if ( $pageId ) {
// update the startTime with the last row
$startTime = wfTimestamp( TS_UNIX, $row->ptrp_created );
$startId = $row->ptrp_page_id;

$acp = ArticleCompileProcessor::newFromPageId( $pageId );
if ( $acp ) {
$acp->registerComponent( 'UserData' );
$acp->compileMetadata();
}

$this->output( "processed $count \n" );
wfWaitForSlaves();
}
}

$this->output( "Completed \n" );
}
}

$maintClass = "updateUserMetadata";
require_once( DO_MAINTENANCE );
6 changes: 4 additions & 2 deletions includes/ArticleMetadata.php
Expand Up @@ -407,10 +407,12 @@ protected function save() {
}
$dbw->replace( 'pagetriage_page_tags', array( 'ptrpt_page_id', 'ptrpt_tag_id' ), $row, __METHOD__ );
}
$pt = new PageTriage( $pageId );
$row = array( 'ptrp_tags_updated' => $dbw->timestamp( wfTimestampNow() ) );
if ( isset( $data['deleted'] ) ) {
$pt = new PageTriage( $pageId );
$pt->setDeleted( $data['deleted'] ? '1' : '0' );
$row['ptrp_deleted'] = $data['deleted'] ? '1' : '0';
}
$pt->update( $row );
$dbw->commit();
}
}
Expand Down
46 changes: 33 additions & 13 deletions includes/PageTriage.php
Expand Up @@ -5,8 +5,9 @@ class PageTriage {
// database property
protected $mPageId;
protected $mReviewed;
protected $mTimestamp;
protected $mCreated;
protected $mDeleted;
protected $mTagsUpdated;

// additional property
protected $mLoaded;
Expand Down Expand Up @@ -110,22 +111,23 @@ public function setTriageStatus( $reviewed, User $user = null, $fromRc = false )
}

/**
* Set the deleted status
*/
public function setDeleted( $deleted ) {
if ( $deleted === '1' ) {
$this->mDeleted = '1';
} else {
$this->mDeleted = '0';
* Update the database record
* @param $row array key => value pair to be updated
* Todo: ptrpt_reviewed should not updated from this function, add exception to catch this
* or find a better solution
*/
public function update( $row ) {
if ( !$row ) {
return;
}

$dbw = wfGetDB( DB_MASTER );
$dbw->update(
'pagetriage_page',
array( 'ptrp_deleted' => $this->mDeleted ),
$row,
array( 'ptrp_page_id' => $this->mPageId ),
__METHOD__
);
);
}

/**
Expand All @@ -141,7 +143,7 @@ public function retrieve() {

$res = $dbr->selectRow(
array( 'pagetriage_page' ),
array( 'ptrp_reviewed', 'ptrp_created', 'ptrp_deleted' ),
array( 'ptrp_reviewed', 'ptrp_created', 'ptrp_deleted', 'ptrp_tags_updated' ),
array( 'ptrp_page_id' => $this->mPageId ),
__METHOD__
);
Expand All @@ -151,8 +153,9 @@ public function retrieve() {
}

$this->mReviewed = $res->ptrp_reviewed;
$this->mTimestamp = $res->ptrp_created;
$this->mCreated = $res->ptrp_created;
$this->mDeleted = $res->ptrp_deleted;
$this->mTagsUpdated = wfTimestamp( TS_UNIX, $res->ptrp_tags_updated );
$this->mLoaded = true;
return true;
}
Expand Down Expand Up @@ -208,6 +211,23 @@ public function deleteFromPageTriage() {
$dbw->commit();
}

/**
* Set the tags updated timestamp
*/
public static function bulkSetTagsUpdated( $pageIds ) {
$dbw = wfGetDB( DB_MASTER );

$now = wfTimestampNow();
$dbw->update(
'pagetriage_page',
array( 'ptrp_tags_updated' => $dbw->timestamp( $now ) ),
array( 'ptrp_page_id' => $pageIds ),
__METHOD__
);

return $now;
}

}

class PageTriageMissingRevisionException extends MWException {}
3 changes: 3 additions & 0 deletions includes/PageTriageUtil.php
Expand Up @@ -315,11 +315,14 @@ public static function updateMetadataOnBlockChange( $block, $status = 1 ) {
}

$dbw = wfGetDB( DB_MASTER );
$dbw->start();
$dbw->update(
'pagetriage_page_tags',
array( 'ptrpt_value' => $status ),
array( 'ptrpt_page_id' => $pageIds, 'ptrpt_tag_id' => $tags['user_block_status'] )
);
PageTriage::bulkSetTagsUpdated( $pageIds );
$dbw->commit();

$metadata = new ArticleMetadata( $pageIds );
$metadata->updateMetadataInCache( array( 'user_block_status' => $status ) );
Expand Down

0 comments on commit 92a675a

Please sign in to comment.