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
26 public $mTitle, $mTable, $mPrefix;
27 public $mRowsPerJob, $mRowsPerQuery;
30 function __construct( $titleTo, $table ) {
31 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
33 $this->mTitle
= $titleTo;
34 $this->mTable
= $table;
35 $this->mRowsPerJob
= $wgUpdateRowsPerJob;
36 $this->mRowsPerQuery
= $wgUpdateRowsPerQuery;
41 $cond = $this->getToCondition();
42 $dbr = wfGetDB( DB_SLAVE
);
43 $res = $dbr->select( $this->mTable
, $this->getFromField(), $cond, __METHOD__
);
44 $this->mResult
= $res;
45 if ( $dbr->numRows( $res ) != 0 ) {
46 if ( $dbr->numRows( $res ) > $this->mRowsPerJob
) {
47 $this->insertJobs( $res );
49 $this->invalidateIDs( $res );
54 function insertJobs( ResultWrapper
$res ) {
55 $numRows = $res->numRows();
56 $numBatches = ceil( $numRows / $this->mRowsPerJob
);
57 $realBatchSize = $numRows / $numBatches;
61 for ( $i = 0; $i < $realBatchSize - 1; $i++
) {
62 $row = $res->fetchRow();
72 'table' => $this->mTable
,
74 'end' => ( $id !== false ?
$id - 1 : false ),
76 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
81 Job
::batchInsert( $jobs );
84 function getPrefix() {
85 static $prefixes = array(
88 'categorylinks' => 'cl',
89 'templatelinks' => 'tl',
93 # 'externallinks' => 'el',
97 if ( is_null( $this->mPrefix
) ) {
98 $this->mPrefix
= $prefixes[$this->mTable
];
99 if ( is_null( $this->mPrefix
) ) {
100 throw new MWException( "Invalid table type \"{$this->mTable}\" in " . __CLASS__
);
103 return $this->mPrefix
;
106 function getFromField() {
107 return $this->getPrefix() . '_from';
110 function getToCondition() {
111 $prefix = $this->getPrefix();
112 switch ( $this->mTable
) {
114 case 'templatelinks':
117 "{$prefix}_namespace" => $this->mTitle
->getNamespace(),
118 "{$prefix}_title" => $this->mTitle
->getDBkey()
121 return array( 'il_to' => $this->mTitle
->getDBkey() );
122 case 'categorylinks':
123 return array( 'cl_to' => $this->mTitle
->getDBkey() );
125 throw new MWException( 'Invalid table type in ' . __CLASS__
);
129 * Invalidate a set of IDs, right now
131 function invalidateIDs( ResultWrapper
$res ) {
132 global $wgUseFileCache, $wgUseSquid;
134 if ( $res->numRows() == 0 ) {
138 $dbw = wfGetDB( DB_MASTER
);
139 $timestamp = $dbw->timestamp();
143 # Get all IDs in this query into an array
145 for ( $i = 0; $i < $this->mRowsPerQuery
; $i++
) {
146 $row = $res->fetchRow();
155 if ( !count( $ids ) ) {
159 # Update page_touched
160 $dbw->update( 'page',
161 array( 'page_touched' => $timestamp ),
162 array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
167 if ( $wgUseSquid ||
$wgUseFileCache ) {
168 $titles = Title
::newFromIDs( $ids );
170 $u = SquidUpdate
::newFromTitles( $titles );
175 if ( $wgUseFileCache ) {
176 foreach ( $titles as $title ) {
177 $cm = new HTMLFileCache($title);
178 @unlink
($cm->fileCacheName());
187 * @todo document (e.g. one-sentence top-level class description).
189 class HTMLCacheUpdateJob
extends Job
{
190 var $table, $start, $end;
194 * @param Title $title The title linked to
195 * @param array $params Job parameters (table, start and end page_ids)
196 * @param integer $id job_id
198 function __construct( $title, $params, $id = 0 ) {
199 parent
::__construct( 'htmlCacheUpdate', $title, $params, $id );
200 $this->table
= $params['table'];
201 $this->start
= $params['start'];
202 $this->end
= $params['end'];
206 $update = new HTMLCacheUpdate( $this->title
, $this->table
);
208 $fromField = $update->getFromField();
209 $conds = $update->getToCondition();
210 if ( $this->start
) {
211 $conds[] = "$fromField >= {$this->start}";
214 $conds[] = "$fromField <= {$this->end}";
217 $dbr = wfGetDB( DB_SLAVE
);
218 $res = $dbr->select( $this->table
, $fromField, $conds, __METHOD__
);
219 $update->invalidateIDs( new ResultWrapper( $dbr, $res ) );