Index: trunk/extensions/GlobalUsage/refreshGlobalimagelinks.php |
— | — | @@ -0,0 +1,90 @@ |
| 2 | +<?php |
| 3 | +require_once( '../../maintenance/Maintenance.php' ); |
| 4 | + |
| 5 | +class RefreshGlobalImageLinks extends Maintenance { |
| 6 | + public function __construct() { |
| 7 | + parent::__construct(); |
| 8 | + $this->addOption( 'start-page', 'page_id of the page to start with' ); |
| 9 | + $this->addOption( 'start-image', 'il_to of the image to start with' ); |
| 10 | + $this->addOption( 'maxlag', 'Maximum replication lag', false, true ); |
| 11 | + } |
| 12 | + |
| 13 | + public function execute() { |
| 14 | + global $wgGlobalUsageDatabase; |
| 15 | + |
| 16 | + $dbr = wfGetDB( DB_SLAVE ); |
| 17 | + $gu = new GlobalUsage( wfWikiId(), |
| 18 | + wfGetDB( DB_MASTER, array(), $wgGlobalUsageDatabase ) ); |
| 19 | + |
| 20 | + $lastPageId = intval( $this->getOption( 'start-page', 0 ) ); |
| 21 | + $lastIlTo = $this->getOption( 'start-image' ); |
| 22 | + $limit = 500; |
| 23 | + $maxlag = intval( $this->getOption( 'maxlag', 0 ) ); |
| 24 | + |
| 25 | + do |
| 26 | + { |
| 27 | + $this->output( "Querying links after (page_id, il_to) = ($lastPageId, $lastIlTo)\n" ); |
| 28 | + |
| 29 | + # Query all pages and any imagelinks associated with that |
| 30 | + $quotedLastIlTo = $dbr->addQuotes( $lastIlTo ); |
| 31 | + $res = $dbr->select( |
| 32 | + array( 'page', 'imagelinks', 'image' ), |
| 33 | + array( |
| 34 | + 'page_id', 'page_namespace', 'page_title', |
| 35 | + 'il_to', 'img_name' |
| 36 | + ), |
| 37 | + "(page_id = $lastPageId AND il_to > {$quotedLastIlTo})" . |
| 38 | + " OR page_id > $lastPageId", |
| 39 | + __METHOD__, |
| 40 | + array( 'ORDER BY' => 'page_id, il_to', 'LIMIT' => $limit ), |
| 41 | + array( |
| 42 | + # LEFT JOIN imagelinks since we need to delete usage |
| 43 | + # from all images, even if they don't have images anymore |
| 44 | + 'imagelinks' => array( 'LEFT JOIN', 'page_id = il_from' ), |
| 45 | + # Check to see if images exist locally |
| 46 | + 'image' => array( 'LEFT JOIN', 'il_to = img_name' ) |
| 47 | + ) |
| 48 | + ); |
| 49 | + |
| 50 | + # Build up a tree per pages |
| 51 | + $pages = array(); |
| 52 | + $lastRow = null; |
| 53 | + foreach ( $res as $row ) { |
| 54 | + if ( !isset( $pages[$row->page_id] ) ) |
| 55 | + $pages[$row->page_id] = array(); |
| 56 | + # Add the imagelinks entry to the pages array if the image |
| 57 | + # does not exist locally |
| 58 | + if ( !is_null( $row->il_to ) && is_null( $row->img_name ) ) { |
| 59 | + $pages[$row->page_id][$row->il_to] = $row; |
| 60 | + } |
| 61 | + $lastRow = $row; |
| 62 | + } |
| 63 | + |
| 64 | + # Insert the imagelinks data to the global table |
| 65 | + foreach ( $pages as $pageId => $rows ) { |
| 66 | + # Delete all original links if this page is not a continuation |
| 67 | + # of last iteration. |
| 68 | + if ( $pageId != $lastPageId ) |
| 69 | + $gu->deleteFrom( $pageId ); |
| 70 | + if ( $rows ) { |
| 71 | + $title = Title::newFromRow( reset( $rows ) ); |
| 72 | + $images = array_keys( $rows ); |
| 73 | + # Since we have a pretty accurate page_id, don't specify |
| 74 | + # GAID_FOR_UPDATE |
| 75 | + $gu->setUsage( $title, $images, /* $flags */ 0 ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if ( $lastRow ) { |
| 80 | + # We've processed some rows in this iteration, so save |
| 81 | + # continuation variables |
| 82 | + $lastPageId = $lastRow->page_id; |
| 83 | + $lastIlTo = $lastRow->il_to; |
| 84 | + wfWaitForSlaves( $maxlag ); |
| 85 | + } |
| 86 | + } while ( !is_null( $lastRow ) ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +$maintClass = 'RefreshGlobalImageLinks'; |
| 91 | +require_once( DO_MAINTENANCE ); |
Property changes on: trunk/extensions/GlobalUsage/refreshGlobalimagelinks.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 92 | + native |