4 * Class to invalidate the HTML cache of all the pages linking to a given title.
5 * Small numbers of links will be done immediately, large numbers are pushed onto
8 * This class is designed to work efficiently with small numbers of links, and
9 * to work reasonably well with up to ~10^5 links. Above ~10^6 links, the memory
10 * and time requirements of loading all backlinked IDs in doUpdate() might become
11 * prohibitive. The requirements measured at Wikimedia are approximately:
13 * memory: 48 bytes per row
14 * time: 16us per row for the query plus processing
16 * The reason this query is done is to support partitioning of the job
17 * by backlinked ID. The memory issue could be allieviated by doing this query in
18 * batches, but of course LIMIT with an offset is inefficient on the DB side.
20 * The class is nevertheless a vast improvement on the previous method of using
21 * Image::getLinksTo() and Title::touchArray(), which uses about 2KB of memory per
28 public $mTitle, $mTable, $mPrefix;
29 public $mRowsPerJob, $mRowsPerQuery;
32 function __construct( $titleTo, $table ) {
33 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
35 $this->mTitle
= $titleTo;
36 $this->mTable
= $table;
37 $this->mRowsPerJob
= $wgUpdateRowsPerJob;
38 $this->mRowsPerQuery
= $wgUpdateRowsPerQuery;
43 $cond = $this->getToCondition();
44 $dbr = wfGetDB( DB_SLAVE
);
45 $res = $dbr->select( $this->mTable
, $this->getFromField(), $cond, __METHOD__
);
46 $this->mResult
= $res;
47 if ( $dbr->numRows( $res ) != 0 ) {
48 if ( $dbr->numRows( $res ) > $this->mRowsPerJob
) {
49 $this->insertJobs( $res );
51 $this->invalidateIDs( $res );
56 function insertJobs( ResultWrapper
$res ) {
57 $numRows = $res->numRows();
58 $numBatches = ceil( $numRows / $this->mRowsPerJob
);
59 $realBatchSize = $numRows / $numBatches;
63 for ( $i = 0; $i < $realBatchSize - 1; $i++
) {
64 $row = $res->fetchRow();
74 'table' => $this->mTable
,
76 'end' => ( $id !== false ?
$id - 1 : false ),
78 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
83 Job
::batchInsert( $jobs );
86 function getPrefix() {
87 static $prefixes = array(
90 'categorylinks' => 'cl',
91 'templatelinks' => 'tl',
95 # 'externallinks' => 'el',
99 if ( is_null( $this->mPrefix
) ) {
100 $this->mPrefix
= $prefixes[$this->mTable
];
101 if ( is_null( $this->mPrefix
) ) {
102 throw new MWException( "Invalid table type \"{$this->mTable}\" in " . __CLASS__
);
105 return $this->mPrefix
;
108 function getFromField() {
109 return $this->getPrefix() . '_from';
112 function getToCondition() {
113 $prefix = $this->getPrefix();
114 switch ( $this->mTable
) {
116 case 'templatelinks':
119 "{$prefix}_namespace" => $this->mTitle
->getNamespace(),
120 "{$prefix}_title" => $this->mTitle
->getDBkey()
123 return array( 'il_to' => $this->mTitle
->getDBkey() );
124 case 'categorylinks':
125 return array( 'cl_to' => $this->mTitle
->getDBkey() );
127 throw new MWException( 'Invalid table type in ' . __CLASS__
);
131 * Invalidate a set of IDs, right now
133 function invalidateIDs( ResultWrapper
$res ) {
134 global $wgUseFileCache, $wgUseSquid;
136 if ( $res->numRows() == 0 ) {
140 $dbw = wfGetDB( DB_MASTER
);
141 $timestamp = $dbw->timestamp();
145 # Get all IDs in this query into an array
147 for ( $i = 0; $i < $this->mRowsPerQuery
; $i++
) {
148 $row = $res->fetchRow();
157 if ( !count( $ids ) ) {
161 # Update page_touched
162 $dbw->update( 'page',
163 array( 'page_touched' => $timestamp ),
164 array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
169 if ( $wgUseSquid ||
$wgUseFileCache ) {
170 $titles = Title
::newFromIDs( $ids );
172 $u = SquidUpdate
::newFromTitles( $titles );
177 if ( $wgUseFileCache ) {
178 foreach ( $titles as $title ) {
179 $cm = new HTMLFileCache($title);
180 @unlink
($cm->fileCacheName());
189 * @todo document (e.g. one-sentence top-level class description).
192 class HTMLCacheUpdateJob
extends Job
{
193 var $table, $start, $end;
197 * @param Title $title The title linked to
198 * @param array $params Job parameters (table, start and end page_ids)
199 * @param integer $id job_id
201 function __construct( $title, $params, $id = 0 ) {
202 parent
::__construct( 'htmlCacheUpdate', $title, $params, $id );
203 $this->table
= $params['table'];
204 $this->start
= $params['start'];
205 $this->end
= $params['end'];
209 $update = new HTMLCacheUpdate( $this->title
, $this->table
);
211 $fromField = $update->getFromField();
212 $conds = $update->getToCondition();
213 if ( $this->start
) {
214 $conds[] = "$fromField >= {$this->start}";
217 $conds[] = "$fromField <= {$this->end}";
220 $dbr = wfGetDB( DB_SLAVE
);
221 $res = $dbr->select( $this->table
, $fromField, $conds, __METHOD__
);
222 $update->invalidateIDs( new ResultWrapper( $dbr, $res ) );