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;
29 function __construct( $titleTo, $table ) {
30 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
32 $this->mTitle
= $titleTo;
33 $this->mTable
= $table;
34 $this->mRowsPerJob
= $wgUpdateRowsPerJob;
35 $this->mRowsPerQuery
= $wgUpdateRowsPerQuery;
40 $cond = $this->getToCondition();
41 $dbr = wfGetDB( DB_SLAVE
);
42 $res = $dbr->select( $this->mTable
, $this->getFromField(), $cond, __METHOD__
);
43 $resWrap = new ResultWrapper( $dbr, $res );
44 if ( $dbr->numRows( $res ) != 0 ) {
45 if ( $dbr->numRows( $res ) > $this->mRowsPerJob
) {
46 $this->insertJobs( $resWrap );
48 $this->invalidateIDs( $resWrap );
51 $dbr->freeResult( $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();
70 if ( $id !== false ) {
71 // One less on the end to avoid duplicating the boundary
72 $job = new HTMLCacheUpdateJob( $this->mTitle
, $this->mTable
, $start, $id - 1 );
74 $job = new HTMLCacheUpdateJob( $this->mTitle
, $this->mTable
, $start, false );
81 Job
::batchInsert( $jobs );
84 function getPrefix() {
85 static $prefixes = array(
88 'categorylinks' => 'cl',
89 'templatelinks' => 'tl',
92 # 'externallinks' => 'el',
96 if ( is_null( $this->mPrefix
) ) {
97 $this->mPrefix
= $prefixes[$this->mTable
];
98 if ( is_null( $this->mPrefix
) ) {
99 throw new MWException( "Invalid table type \"{$this->mTable}\" in " . __CLASS__
);
102 return $this->mPrefix
;
105 function getFromField() {
106 return $this->getPrefix() . '_from';
109 function getToCondition() {
110 switch ( $this->mTable
) {
113 'pl_namespace' => $this->mTitle
->getNamespace(),
114 'pl_title' => $this->mTitle
->getDBkey()
116 case 'templatelinks':
118 'tl_namespace' => $this->mTitle
->getNamespace(),
119 'tl_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).
190 class HTMLCacheUpdateJob
extends Job
{
191 var $table, $start, $end;
195 * @param Title $title The title linked to
196 * @param string $table The name of the link table.
197 * @param integer $start Beginning page_id or false for open interval
198 * @param integer $end End page_id or false for open interval
199 * @param integer $id job_id
201 function __construct( $title, $table, $start, $end, $id = 0 ) {
206 parent
::__construct( 'htmlCacheUpdate', $title, $params, $id );
207 $this->table
= $table;
208 $this->start
= intval( $start );
209 $this->end
= intval( $end );
213 $update = new HTMLCacheUpdate( $this->title
, $this->table
);
215 $fromField = $update->getFromField();
216 $conds = $update->getToCondition();
217 if ( $this->start
) {
218 $conds[] = "$fromField >= {$this->start}";
221 $conds[] = "$fromField <= {$this->end}";
224 $dbr = wfGetDB( DB_SLAVE
);
225 $res = $dbr->select( $this->table
, $fromField, $conds, __METHOD__
);
226 $update->invalidateIDs( new ResultWrapper( $dbr, $res ) );
227 $dbr->freeResult( $res );