r100107 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100106‎ | r100107 | r100108 >
Date:04:05, 18 October 2011
Author:aaron
Status:ok
Tags:
Comment:
Migrated html cache purge script to use Maintenance class
Modified paths:
  • /trunk/extensions/FlaggedRevs/maintenance/purgeReviewablePages.inc (deleted) (history)
  • /trunk/extensions/FlaggedRevs/maintenance/purgeReviewablePages.php (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/maintenance/purgeReviewablePages.inc
@@ -1,77 +0,0 @@
2 -<?php
3 -
4 -function list_reviewable_pages( $db, $fileHandle ) {
5 - global $wgFlaggedRevsNamespaces, $wgUseSquid, $wgUseFileCache;
6 - echo "Building list of all reviewable pages to purge ...\n";
7 - if ( !$wgUseSquid && !$wgUseFileCache ) {
8 - echo "Squid/file cache not enabled ... nothing to purge.\n";
9 - return;
10 - } elseif ( empty( $wgFlaggedRevsNamespaces ) ) {
11 - echo "There are no reviewable namespaces ... nothing to purge.\n";
12 - return;
13 - }
14 - $BATCH_SIZE = 1000;
15 - $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
16 - $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
17 - if ( is_null( $start ) || is_null( $end ) ) {
18 - echo "... page table seems to be empty.\n";
19 - return;
20 - }
21 - # Do remaining chunk
22 - $end += $BATCH_SIZE - 1;
23 - $blockStart = $start;
24 - $blockEnd = $start + $BATCH_SIZE - 1;
25 - $count = 0;
26 - while ( $blockEnd <= $end ) {
27 - echo "... doing page_id from $blockStart to $blockEnd\n";
28 - $res = $db->select( 'page', '*',
29 - array(
30 - "page_id BETWEEN $blockStart AND $blockEnd",
31 - 'page_namespace' => $wgFlaggedRevsNamespaces ),
32 - __FUNCTION__
33 - );
34 - # Go through and append each purgeable page...
35 - foreach ( $res as $row ) {
36 - $title = Title::newFromRow( $row );
37 - $fa = FlaggableWikiPage::getTitleInstance( $title );
38 - if ( $fa->isReviewable() ) {
39 - # Need to purge this page - add to list
40 - fwrite( $fileHandle, $title->getPrefixedDBKey() . "\n" );
41 - $count++;
42 - }
43 - }
44 - $db->freeResult( $res );
45 - $blockStart += $BATCH_SIZE - 1;
46 - $blockEnd += $BATCH_SIZE - 1;
47 - wfWaitForSlaves( 5 ); // not really needed
48 - }
49 - echo "List of reviewable pages to purge complete ... {$count} pages\n";
50 -}
51 -
52 -function purge_reviewable_pages( $db, $fileHandle ) {
53 - global $wgUseSquid, $wgUseFileCache;
54 - echo "Purging squid cache for list of pages to purge ...\n";
55 - if ( !$wgUseSquid && !$wgUseFileCache ) {
56 - echo "Squid/file cache not enabled ... nothing to purge.\n";
57 - return;
58 - }
59 - $BATCH_SIZE = 500;
60 - $count = 0;
61 - while ( !feof( $fileHandle ) ) {
62 - $dbKey = trim( fgets( $fileHandle ) );
63 - if ( $dbKey == '' ) continue; // last line?
64 - $title = Title::newFromDBkey( $dbKey );
65 - if ( $title ) {
66 - $title->purgeSquid(); // send PURGE
67 - HTMLFileCache::clearFileCache( $title ); // purge poor-mans's squid
68 - $count++;
69 - echo "... $dbKey\n";
70 - if ( $count % $BATCH_SIZE ) {
71 - wfWaitForSlaves( 5 ); // not really needed
72 - }
73 - } else {
74 - echo "Invalid title - cannot purge: $dbKey\n";
75 - }
76 - }
77 - echo "Squid/file cache purge of page list complete ... {$count} pages\n";
78 -}
Index: trunk/extensions/FlaggedRevs/maintenance/purgeReviewablePages.php
@@ -1,55 +1,132 @@
22 <?php
3 -
 3+/**
 4+ * @ingroup Maintenance
 5+ */
46 if ( getenv( 'MW_INSTALL_PATH' ) ) {
5 - $IP = getenv( 'MW_INSTALL_PATH' );
 7+ $IP = getenv( 'MW_INSTALL_PATH' );
68 } else {
7 - $IP = dirname(__FILE__).'/../../..';
 9+ $IP = dirname(__FILE__).'/../../..';
810 }
9 -require "$IP/maintenance/commandLine.inc";
10 -require dirname(__FILE__) . '/purgeReviewablePages.inc';
1111
12 -$makeList = isset( $options['makelist'] );
13 -$purgeList = isset( $options['purgelist'] );
 12+require_once( "$IP/maintenance/Maintenance.php" );
1413
15 -if ( isset( $options['help'] ) || ( !$makeList && !$purgeList ) ) {
16 - echo <<<TEXT
17 -Purpose:
18 - Use to purge squid/file cache for all reviewable pages
19 -Usage:
20 - php purgeReviewablePages.php --help
21 - php purgeReviewablePages.php --makelist
22 - php purgeReviewablePages.php --purgelist
 14+class PurgeReviewablePages extends Maintenance {
2315
24 - --help : This help message
25 - --makelist : Build the list of reviewable pages to pagesToPurge.list
26 - --purgelist : Purge the list of pages in pagesToPurge.list
 16+ public function __construct() {
 17+ $this->mDescription = "Use to purge squid/file cache for all reviewable pages";
 18+ $this->addOption( 'makelist', "Build the list of reviewable pages to pagesToPurge.list", false, false );
 19+ $this->addOption( 'purgelist', "Purge the list of pages in pagesToPurge.list", false, false );
 20+ $this->setBatchSize( 1000 );
 21+ }
2722
28 -TEXT;
29 - exit( 0 );
30 -}
 23+ public function execute() {
 24+ $fileName = "pagesToPurge.list";
 25+ // Build the list file...
 26+ if ( $this->getOption( 'makelist' ) ) {
 27+ $fileHandle = fopen( $fileName, 'w+' );
 28+ if ( !$fileHandle ) {
 29+ $this->error( "Can't open file to create purge list.", 1 );
 30+ }
 31+ $this->list_reviewable_pages( $fileHandle );
 32+ fclose( $fileHandle );
 33+ // Purge pages on the list file...
 34+ } elseif ( $this->getOption( 'purgelist' ) ) {
 35+ $fileHandle = fopen( $fileName, 'r' );
 36+ if ( !$fileHandle ) {
 37+ $this->error( "Can't open file to read purge list.", 1 );
 38+ }
 39+ $this->purge_reviewable_pages( $fileHandle );
 40+ fclose( $fileHandle );
 41+ } else {
 42+ $this->error( "No purge list action specified.", 1 );
 43+ }
 44+ }
3145
32 -error_reporting( E_ALL );
 46+ protected function list_reviewable_pages( $fileHandle ) {
 47+ global $wgFlaggedRevsNamespaces, $wgUseSquid, $wgUseFileCache;
3348
34 -$fileName = "pagesToPurge.list";
 49+ $this->output( "Building list of all reviewable pages to purge ...\n" );
 50+ if ( !$wgUseSquid && !$wgUseFileCache ) {
 51+ $this->output( "Squid/file cache not enabled ... nothing to purge.\n" );
 52+ return;
 53+ } elseif ( empty( $wgFlaggedRevsNamespaces ) ) {
 54+ $this->output( "There are no reviewable namespaces ... nothing to purge.\n" );
 55+ return;
 56+ }
3557
36 -if ( $makeList ) {
37 - $db = wfGetDB( DB_MASTER );
38 - $fileHandle = fopen( $fileName, 'w+' );
39 - if ( !$fileHandle ) {
40 - echo "Can't open file to create purge list.\n";
41 - exit( -1 );
 58+ $db = wfGetDB( DB_MASTER );
 59+
 60+ $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
 61+ $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
 62+ if ( is_null( $start ) || is_null( $end ) ) {
 63+ $this->output( "... page table seems to be empty.\n" );
 64+ return;
 65+ }
 66+ # Do remaining chunk
 67+ $end += $this->mBatchSize - 1;
 68+ $blockStart = $start;
 69+ $blockEnd = $start + $this->mBatchSize - 1;
 70+
 71+ $count = 0;
 72+ while ( $blockEnd <= $end ) {
 73+ $this->output( "... doing page_id from $blockStart to $blockEnd\n" );
 74+ $res = $db->select( 'page', '*',
 75+ array(
 76+ "page_id BETWEEN $blockStart AND $blockEnd",
 77+ 'page_namespace' => $wgFlaggedRevsNamespaces ),
 78+ __FUNCTION__
 79+ );
 80+ # Go through and append each purgeable page...
 81+ foreach ( $res as $row ) {
 82+ $title = Title::newFromRow( $row );
 83+ $fa = FlaggableWikiPage::getTitleInstance( $title );
 84+ if ( $fa->isReviewable() ) {
 85+ # Need to purge this page - add to list
 86+ fwrite( $fileHandle, $title->getPrefixedDBKey() . "\n" );
 87+ $count++;
 88+ }
 89+ }
 90+ $db->freeResult( $res );
 91+ $blockStart += $this->mBatchSize - 1;
 92+ $blockEnd += $this->mBatchSize - 1;
 93+ wfWaitForSlaves( 5 ); // not really needed
 94+ }
 95+ $this->output( "List of reviewable pages to purge complete ... {$count} pages\n" );
4296 }
43 - list_reviewable_pages( $db, $fileHandle );
44 - fclose( $fileHandle );
45 -}
4697
47 -if ( $purgeList ) {
48 - $db = wfGetDB( DB_MASTER );
49 - $fileHandle = fopen( $fileName, 'r' );
50 - if ( !$fileHandle ) {
51 - echo "Can't open file to read purge list.\n";
52 - exit( -1 );
 98+ protected function purge_reviewable_pages( $fileHandle ) {
 99+ global $wgUseSquid, $wgUseFileCache;
 100+ $this->output( "Purging squid cache for list of pages to purge ...\n" );
 101+ if ( !$wgUseSquid && !$wgUseFileCache ) {
 102+ $this->output( "Squid/file cache not enabled ... nothing to purge.\n" );
 103+ return;
 104+ }
 105+
 106+ $db = wfGetDB( DB_MASTER );
 107+
 108+ $count = 0;
 109+ while ( !feof( $fileHandle ) ) {
 110+ $dbKey = trim( fgets( $fileHandle ) );
 111+ if ( $dbKey == '' ) {
 112+ continue; // last line?
 113+ }
 114+ $title = Title::newFromDBkey( $dbKey );
 115+ if ( $title ) {
 116+ $title->purgeSquid(); // send PURGE
 117+ HTMLFileCache::clearFileCache( $title ); // purge poor-mans's squid
 118+ $this->output( "... $dbKey\n" );
 119+
 120+ $count++;
 121+ if ( ( $count % $this->mBatchSize ) == 0 ) {
 122+ wfWaitForSlaves( 5 ); // not really needed
 123+ }
 124+ } else {
 125+ $this->output( "Invalid title - cannot purge: $dbKey\n" );
 126+ }
 127+ }
 128+ $this->output( "Squid/file cache purge of page list complete ... {$count} pages\n" );
53129 }
54 - purge_reviewable_pages( $db, $fileHandle );
55 - fclose( $fileHandle );
56130 }
 131+
 132+$maintClass = "PurgeReviewablePages";
 133+require_once( RUN_MAINTENANCE_IF_MAIN );

Status & tagging log