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;
40 public function doUpdate() {
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 );
53 wfRunHooks( 'HTMLCacheUpdate::doUpdate', array($this->mTitle
) );
56 protected 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 protected function getPrefix() {
87 static $prefixes = array(
90 'categorylinks' => 'cl',
91 'templatelinks' => 'tl',
95 if ( is_null( $this->mPrefix
) ) {
96 $this->mPrefix
= $prefixes[$this->mTable
];
97 if ( is_null( $this->mPrefix
) ) {
98 throw new MWException( "Invalid table type \"{$this->mTable}\" in " . __CLASS__
);
101 return $this->mPrefix
;
104 public function getFromField() {
105 return $this->getPrefix() . '_from';
108 public function getToCondition() {
109 $prefix = $this->getPrefix();
110 switch ( $this->mTable
) {
112 case 'templatelinks':
115 "{$prefix}_namespace" => $this->mTitle
->getNamespace(),
116 "{$prefix}_title" => $this->mTitle
->getDBkey()
119 return array( 'il_to' => $this->mTitle
->getDBkey() );
120 case 'categorylinks':
121 return array( 'cl_to' => $this->mTitle
->getDBkey() );
123 throw new MWException( 'Invalid table type in ' . __CLASS__
);
127 * Invalidate a set of IDs, right now
129 public function invalidateIDs( ResultWrapper
$res ) {
130 global $wgUseFileCache, $wgUseSquid;
132 if ( $res->numRows() == 0 ) {
136 $dbw = wfGetDB( DB_MASTER
);
137 $timestamp = $dbw->timestamp();
141 # Get all IDs in this query into an array
143 for ( $i = 0; $i < $this->mRowsPerQuery
; $i++
) {
144 $row = $res->fetchRow();
153 if ( !count( $ids ) ) {
157 # Update page_touched
158 $dbw->update( 'page',
159 array( 'page_touched' => $timestamp ),
160 array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
165 if ( $wgUseSquid ||
$wgUseFileCache ) {
166 $titles = Title
::newFromIDs( $ids );
168 $u = SquidUpdate
::newFromTitles( $titles );
173 if ( $wgUseFileCache ) {
174 foreach ( $titles as $title ) {
175 $cm = new HTMLFileCache($title);
176 @unlink
($cm->fileCacheName());
185 * @todo document (e.g. one-sentence top-level class description).
188 class HTMLCacheUpdateJob
extends Job
{
189 var $table, $start, $end;
193 * @param Title $title The title linked to
194 * @param array $params Job parameters (table, start and end page_ids)
195 * @param integer $id job_id
197 function __construct( $title, $params, $id = 0 ) {
198 parent
::__construct( 'htmlCacheUpdate', $title, $params, $id );
199 $this->table
= $params['table'];
200 $this->start
= $params['start'];
201 $this->end
= $params['end'];
204 public function run() {
205 $update = new HTMLCacheUpdate( $this->title
, $this->table
);
207 $fromField = $update->getFromField();
208 $conds = $update->getToCondition();
209 if ( $this->start
) {
210 $conds[] = "$fromField >= {$this->start}";
213 $conds[] = "$fromField <= {$this->end}";
216 $dbr = wfGetDB( DB_SLAVE
);
217 $res = $dbr->select( $this->table
, $fromField, $conds, __METHOD__
);
218 $update->invalidateIDs( $res );