Index: trunk/extensions/FlaggedRevs/FRCacheUpdate.php |
— | — | @@ -1,72 +1,129 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -class FRCacheUpdate extends HTMLCacheUpdate |
5 | | -{ |
| 4 | +class FRCacheUpdate { |
| 5 | + public $mTitle, $mTable; |
| 6 | + public $mRowsPerJob, $mRowsPerQuery; |
| 7 | + |
| 8 | + public function __construct( $titleTo ) { |
| 9 | + global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery; |
| 10 | + $this->mTitle = $titleTo; |
| 11 | + $this->mTable = 'flaggedrevs_tracking'; |
| 12 | + $this->mRowsPerJob = $wgUpdateRowsPerJob; |
| 13 | + $this->mRowsPerQuery = $wgUpdateRowsPerQuery; |
| 14 | + } |
| 15 | + |
6 | 16 | public function doUpdate() { |
7 | 17 | global $wgFlaggedRevsCacheUpdates; |
8 | 18 | if( !isset($wgFlaggedRevsCacheUpdates) ) { |
9 | | - $wgFlaggedRevsCacheUpdates = array(); |
| 19 | + $wgFlaggedRevsCacheUpdates = array(); // temp var |
10 | 20 | } |
11 | | - # No duplicates... |
12 | 21 | $key = $this->mTitle->getPrefixedDBKey(); |
13 | | - if( isset($wgFlaggedRevsCacheUpdates[$key]) ) { |
14 | | - return; |
15 | | - } |
| 22 | + if( isset($wgFlaggedRevsCacheUpdates[$key]) ) |
| 23 | + return; // No duplicates... |
16 | 24 | # Fetch the IDs |
17 | | - $cond = $this->getToCondition(); |
18 | 25 | $dbr = wfGetDB( DB_SLAVE ); |
19 | | - $res = $dbr->select( $this->mTable, $this->getFromField(), $cond, __METHOD__ ); |
20 | | - if( $dbr->numRows( $res ) != 0 ) { |
21 | | - $this->insertJobs( $res ); |
| 26 | + $res = $dbr->select( $this->mTable, $this->getFromField(), $this->getToCondition(), __METHOD__ ); |
| 27 | + if( $dbr->numRows($res) > 0 ) { |
| 28 | + # Do it right now? |
| 29 | + if( $dbr->numRows($res) <= $this->mRowsPerJob ) { |
| 30 | + $this->invalidateIDs( $res ); |
| 31 | + # Defer to job queue... |
| 32 | + } else { |
| 33 | + $this->insertJobs( $res ); |
| 34 | + } |
22 | 35 | } |
23 | | - $wgFlaggedRevsCacheUpdates[$key] = 1; |
| 36 | + $wgFlaggedRevsCacheUpdates[$key] = 1; // No duplicates... |
24 | 37 | } |
25 | 38 | |
26 | | - function insertJobs( ResultWrapper $res ) { |
| 39 | + protected function insertJobs( ResultWrapper $res ) { |
27 | 40 | $numRows = $res->numRows(); |
| 41 | + if( !$numRows ) return; // sanity check |
28 | 42 | $numBatches = ceil( $numRows / $this->mRowsPerJob ); |
29 | | - $realBatchSize = $numRows / $numBatches; |
| 43 | + $realBatchSize = ceil( $numRows / $numBatches ); |
30 | 44 | $start = false; |
31 | 45 | $jobs = array(); |
32 | 46 | do { |
33 | | - for ( $i = 0; $i <= $realBatchSize - 1; $i++ ) { |
| 47 | + $first = $last = false; // first/last page_id of this batch |
| 48 | + # Get $realBatchSize items (or less if not enough)... |
| 49 | + for( $i = 0; $i < $realBatchSize; $i++ ) { |
34 | 50 | $row = $res->fetchRow(); |
| 51 | + # Is there another row? |
35 | 52 | if( $row ) { |
36 | 53 | $id = $row[0]; |
| 54 | + $last = $id; // $id is the last page_id of this batch |
| 55 | + if( $first === false ) |
| 56 | + $first = $id; // set first page_id of this batch |
| 57 | + # Out of rows? |
37 | 58 | } else { |
38 | 59 | $id = false; |
39 | 60 | break; |
40 | 61 | } |
| 62 | + } |
| 63 | + # Insert batch into the queue if there is anything there |
| 64 | + if( $first ) { |
| 65 | + $params = array( |
| 66 | + 'table' => $this->mTable, |
| 67 | + 'start' => $first, |
| 68 | + 'end' => $last, |
| 69 | + ); |
| 70 | + $jobs[] = new FRCacheUpdateJob( $this->mTitle, $params ); |
41 | 71 | } |
42 | | - $params = array( |
43 | | - 'table' => $this->mTable, |
44 | | - 'start' => $start, |
45 | | - 'end' => ( $id !== false ? $id - 1 : false ), |
46 | | - ); |
47 | | - $jobs[] = new FRCacheUpdateJob( $this->mTitle, $params ); |
48 | | - $start = $id; |
49 | | - } while ( $start ); |
| 72 | + $start = $id; // Where the last ID left off |
| 73 | + } while( $start ); |
50 | 74 | Job::batchInsert( $jobs ); |
51 | | - } |
| 75 | + } |
52 | 76 | |
53 | | - function getPrefix() { |
54 | | - return 'ftr'; |
| 77 | + public function getFromField() { |
| 78 | + return 'ftr_from'; |
55 | 79 | } |
56 | 80 | |
57 | | - function getFromField() { |
58 | | - return $this->getPrefix() . '_from'; |
| 81 | + public function getToCondition() { |
| 82 | + return array( 'ftr_namespace' => $this->mTitle->getNamespace(), 'ftr_title' => $this->mTitle->getDBkey() ); |
59 | 83 | } |
| 84 | + |
| 85 | + /** |
| 86 | + * Invalidate a set of IDs, right now |
| 87 | + */ |
| 88 | + public function invalidateIDs( ResultWrapper $res ) { |
| 89 | + global $wgUseFileCache, $wgUseSquid; |
| 90 | + if( $res->numRows() == 0 ) return; // sanity check |
60 | 91 | |
61 | | - function getToCondition() { |
62 | | - if( $this->mTable !== 'flaggedrevs_tracking' ) { |
63 | | - throw new MWException( 'Invalid table type in ' . __CLASS__ ); |
64 | | - } |
65 | | - $prefix = $this->getPrefix(); |
66 | | - return array( |
67 | | - "{$prefix}_namespace" => $this->mTitle->getNamespace(), |
68 | | - "{$prefix}_title" => $this->mTitle->getDBkey() |
69 | | - ); |
70 | | - } |
| 92 | + $dbw = wfGetDB( DB_MASTER ); |
| 93 | + $timestamp = $dbw->timestamp(); |
| 94 | + $done = false; |
| 95 | + |
| 96 | + while( !$done ) { |
| 97 | + # Get all IDs in this query into an array |
| 98 | + $ids = array(); |
| 99 | + for( $i = 0; $i < $this->mRowsPerQuery; $i++ ) { |
| 100 | + $row = $res->fetchRow(); |
| 101 | + if( $row ) { |
| 102 | + $ids[] = $row[0]; |
| 103 | + } else { |
| 104 | + $done = true; |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + if( count($ids) == 0 ) break; |
| 109 | + # Update page_touched |
| 110 | + $dbw->update( 'page', array( 'page_touched' => $timestamp ), array( 'page_id' => $ids ), __METHOD__ ); |
| 111 | + # Update static caches |
| 112 | + if( $wgUseSquid || $wgUseFileCache ) { |
| 113 | + $titles = Title::newFromIDs( $ids ); |
| 114 | + # Update squid cache |
| 115 | + if( $wgUseSquid ) { |
| 116 | + $u = SquidUpdate::newFromTitles( $titles ); |
| 117 | + $u->doUpdate(); |
| 118 | + } |
| 119 | + # Update file cache |
| 120 | + if( $wgUseFileCache ) { |
| 121 | + foreach( $titles as $title ) { |
| 122 | + HTMLFileCache::clearFileCache( $title ); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
71 | 128 | } |
72 | 129 | |
73 | 130 | /** |
— | — | @@ -90,8 +147,8 @@ |
91 | 148 | } |
92 | 149 | |
93 | 150 | function run() { |
94 | | - $update = new FRCacheUpdate( $this->title, $this->table ); |
95 | | - |
| 151 | + $update = new FRCacheUpdate( $this->title ); |
| 152 | + # Get query conditions |
96 | 153 | $fromField = $update->getFromField(); |
97 | 154 | $conds = $update->getToCondition(); |
98 | 155 | if( $this->start ) { |
— | — | @@ -101,10 +158,11 @@ |
102 | 159 | $conds[] = "$fromField <= {$this->end}"; |
103 | 160 | } |
104 | 161 | |
| 162 | + # Run query to get page Ids |
105 | 163 | $dbr = wfGetDB( DB_SLAVE ); |
106 | 164 | $res = $dbr->select( $this->table, $fromField, $conds, __METHOD__ ); |
| 165 | + # Invalidate the pages |
107 | 166 | $update->invalidateIDs( $res ); |
108 | | - |
109 | 167 | return true; |
110 | 168 | } |
111 | 169 | } |
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php |
— | — | @@ -304,7 +304,7 @@ |
305 | 305 | * that was just changed in some way. |
306 | 306 | */ |
307 | 307 | public static function doCacheUpdate( $title ) { |
308 | | - $update = new FRCacheUpdate( $title, 'flaggedrevs_tracking' ); |
| 308 | + $update = new FRCacheUpdate( $title ); |
309 | 309 | $update->doUpdate(); |
310 | 310 | return true; |
311 | 311 | } |