
btongminh at svn
Nov 7, 2009, 5:52 AM
Post #1 of 1
(14 views)
Permalink
|
|
SVN: [58706] trunk/extensions/GlobalUsage/refreshGlobalimagelinks.php
|
|
http://www.mediawiki.org/wiki/Special:Code/MediaWiki/58706 Revision: 58706 Author: btongminh Date: 2009-11-07 13:52:41 +0000 (Sat, 07 Nov 2009) Log Message: ----------- Add script to refresh the globalimagelinks table Added Paths: ----------- trunk/extensions/GlobalUsage/refreshGlobalimagelinks.php Added: trunk/extensions/GlobalUsage/refreshGlobalimagelinks.php =================================================================== --- trunk/extensions/GlobalUsage/refreshGlobalimagelinks.php (rev 0) +++ trunk/extensions/GlobalUsage/refreshGlobalimagelinks.php 2009-11-07 13:52:41 UTC (rev 58706) @@ -0,0 +1,90 @@ +<?php +require_once( '../../maintenance/Maintenance.php' ); + +class RefreshGlobalImageLinks extends Maintenance { + public function __construct() { + parent::__construct(); + $this->addOption( 'start-page', 'page_id of the page to start with' ); + $this->addOption( 'start-image', 'il_to of the image to start with' ); + $this->addOption( 'maxlag', 'Maximum replication lag', false, true ); + } + + public function execute() { + global $wgGlobalUsageDatabase; + + $dbr = wfGetDB( DB_SLAVE ); + $gu = new GlobalUsage( wfWikiId(), + wfGetDB( DB_MASTER, array(), $wgGlobalUsageDatabase ) ); + + $lastPageId = intval( $this->getOption( 'start-page', 0 ) ); + $lastIlTo = $this->getOption( 'start-image' ); + $limit = 500; + $maxlag = intval( $this->getOption( 'maxlag', 0 ) ); + + do + { + $this->output( "Querying links after (page_id, il_to) = ($lastPageId, $lastIlTo)\n" ); + + # Query all pages and any imagelinks associated with that + $quotedLastIlTo = $dbr->addQuotes( $lastIlTo ); + $res = $dbr->select( + array( 'page', 'imagelinks', 'image' ), + array( + 'page_id', 'page_namespace', 'page_title', + 'il_to', 'img_name' + ), + "(page_id = $lastPageId AND il_to > {$quotedLastIlTo})" . + " OR page_id > $lastPageId", + __METHOD__, + array( 'ORDER BY' => 'page_id, il_to', 'LIMIT' => $limit ), + array( + # LEFT JOIN imagelinks since we need to delete usage + # from all images, even if they don't have images anymore + 'imagelinks' => array( 'LEFT JOIN', 'page_id = il_from' ), + # Check to see if images exist locally + 'image' => array( 'LEFT JOIN', 'il_to = img_name' ) + ) + ); + + # Build up a tree per pages + $pages = array(); + $lastRow = null; + foreach ( $res as $row ) { + if ( !isset( $pages[$row->page_id] ) ) + $pages[$row->page_id] = array(); + # Add the imagelinks entry to the pages array if the image + # does not exist locally + if ( !is_null( $row->il_to ) && is_null( $row->img_name ) ) { + $pages[$row->page_id][$row->il_to] = $row; + } + $lastRow = $row; + } + + # Insert the imagelinks data to the global table + foreach ( $pages as $pageId => $rows ) { + # Delete all original links if this page is not a continuation + # of last iteration. + if ( $pageId != $lastPageId ) + $gu->deleteFrom( $pageId ); + if ( $rows ) { + $title = Title::newFromRow( reset( $rows ) ); + $images = array_keys( $rows ); + # Since we have a pretty accurate page_id, don't specify + # GAID_FOR_UPDATE + $gu->setUsage( $title, $images, /* $flags */ 0 ); + } + } + + if ( $lastRow ) { + # We've processed some rows in this iteration, so save + # continuation variables + $lastPageId = $lastRow->page_id; + $lastIlTo = $lastRow->il_to; + wfWaitForSlaves( $maxlag ); + } + } while ( !is_null( $lastRow ) ); + } +} + +$maintClass = 'RefreshGlobalImageLinks'; +require_once( DO_MAINTENANCE ); Property changes on: trunk/extensions/GlobalUsage/refreshGlobalimagelinks.php ___________________________________________________________________ Added: svn:eol-style + native _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS[at]lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs
|