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;
31 function __construct( $titleTo, $table ) {
32 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
34 $this->mTitle
= $titleTo;
35 $this->mTable
= $table;
36 $this->mRowsPerJob
= $wgUpdateRowsPerJob;
37 $this->mRowsPerQuery
= $wgUpdateRowsPerQuery;
42 $cond = $this->getToCondition();
43 $dbr = wfGetDB( DB_SLAVE
);
44 $res = $dbr->select( $this->mTable
, $this->getFromField(), $cond, __METHOD__
);
46 if ( $dbr->numRows( $res ) != 0 ) {
47 if ( $dbr->numRows( $res ) > $this->mRowsPerJob
) {
48 $this->insertJobs( $res );
50 $this->invalidateIDs( $res );
55 function insertJobs( ResultWrapper
$res ) {
56 $numRows = $res->numRows();
57 $numBatches = ceil( $numRows / $this->mRowsPerJob
);
58 $realBatchSize = $numRows / $numBatches;
62 for ( $i = 0; $i < $realBatchSize - 1; $i++
) {
63 $row = $res->fetchRow();
73 'table' => $this->mTable
,
75 'end' => ( $id !== false ?
$id - 1 : false ),
77 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
82 Job
::batchInsert( $jobs );
85 function getPrefix() {
86 static $prefixes = array(
89 'categorylinks' => 'cl',
90 'templatelinks' => 'tl',
94 # 'externallinks' => 'el',
98 if ( is_null( $this->mPrefix
) ) {
99 $this->mPrefix
= $prefixes[$this->mTable
];
100 if ( is_null( $this->mPrefix
) ) {
101 throw new MWException( "Invalid table type \"{$this->mTable}\" in " . __CLASS__
);
104 return $this->mPrefix
;
107 function getFromField() {
108 return $this->getPrefix() . '_from';
111 function getToCondition() {
112 $prefix = $this->getPrefix();
113 switch ( $this->mTable
) {
115 case 'templatelinks':
118 "{$prefix}_namespace" => $this->mTitle
->getNamespace(),
119 "{$prefix}_title" => $this->mTitle
->getDBkey()
122 return array( 'il_to' => $this->mTitle
->getDBkey() );
123 case 'categorylinks':
124 return array( 'cl_to' => $this->mTitle
->getDBkey() );
126 throw new MWException( 'Invalid table type in ' . __CLASS__
);
130 * Invalidate a set of IDs, right now
132 function invalidateIDs( ResultWrapper
$res ) {
133 global $wgUseFileCache, $wgUseSquid;
135 if ( $res->numRows() == 0 ) {
139 $dbw = wfGetDB( DB_MASTER
);
140 $timestamp = $dbw->timestamp();
144 # Get all IDs in this query into an array
146 for ( $i = 0; $i < $this->mRowsPerQuery
; $i++
) {
147 $row = $res->fetchRow();
156 if ( !count( $ids ) ) {
160 # Update page_touched
161 $dbw->update( 'page',
162 array( 'page_touched' => $timestamp ),
163 array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
168 if ( $wgUseSquid ||
$wgUseFileCache ) {
169 $titles = Title
::newFromIDs( $ids );
171 $u = SquidUpdate
::newFromTitles( $titles );
176 if ( $wgUseFileCache ) {
177 foreach ( $titles as $title ) {
178 $cm = new HTMLFileCache($title);
179 @unlink
($cm->fileCacheName());
188 * @todo document (e.g. one-sentence top-level class description).
191 class HTMLCacheUpdateJob
extends Job
{
192 var $table, $start, $end;
196 * @param Title $title The title linked to
197 * @param array $params Job parameters (table, start and end page_ids)
198 * @param integer $id job_id
200 function __construct( $title, $params, $id = 0 ) {
201 parent
::__construct( 'htmlCacheUpdate', $title, $params, $id );
202 $this->table
= $params['table'];
203 $this->start
= $params['start'];
204 $this->end
= $params['end'];
208 $update = new HTMLCacheUpdate( $this->title
, $this->table
);
210 $fromField = $update->getFromField();
211 $conds = $update->getToCondition();
212 if ( $this->start
) {
213 $conds[] = "$fromField >= {$this->start}";
216 $conds[] = "$fromField <= {$this->end}";
219 $dbr = wfGetDB( DB_SLAVE
);
220 $res = $dbr->select( $this->table
, $fromField, $conds, __METHOD__
);
221 $update->invalidateIDs( $res );