A few misc. docs here and there.
[lhc/web/wiklou.git] / includes / HTMLCacheUpdate.php
1 <?php
2
3 /**
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
6 * the job queue.
7 *
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:
12 *
13 * memory: 48 bytes per row
14 * time: 16us per row for the query plus processing
15 *
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.
19 *
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
22 * link.
23 *
24 * @ingroup Cache
25 */
26 class HTMLCacheUpdate
27 {
28 public $mTitle, $mTable, $mPrefix;
29 public $mRowsPerJob, $mRowsPerQuery;
30
31 function __construct( $titleTo, $table ) {
32 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
33
34 $this->mTitle = $titleTo;
35 $this->mTable = $table;
36 $this->mRowsPerJob = $wgUpdateRowsPerJob;
37 $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
38 }
39
40 public function doUpdate() {
41 # Fetch the IDs
42 $cond = $this->getToCondition();
43 $dbr = wfGetDB( DB_SLAVE );
44 $res = $dbr->select( $this->mTable, $this->getFromField(), $cond, __METHOD__ );
45
46 if ( $dbr->numRows( $res ) != 0 ) {
47 if ( $dbr->numRows( $res ) > $this->mRowsPerJob ) {
48 $this->insertJobs( $res );
49 } else {
50 $this->invalidateIDs( $res );
51 }
52 }
53 wfRunHooks( 'HTMLCacheUpdate::doUpdate', array($this->mTitle) );
54 }
55
56 protected function insertJobs( ResultWrapper $res ) {
57 $numRows = $res->numRows();
58 $numBatches = ceil( $numRows / $this->mRowsPerJob );
59 $realBatchSize = $numRows / $numBatches;
60 $start = false;
61 $jobs = array();
62 do {
63 for ( $i = 0; $i <= $realBatchSize - 1; $i++ ) {
64 $row = $res->fetchRow();
65 if ( $row ) {
66 $id = $row[0];
67 } else {
68 $id = false;
69 break;
70 }
71 }
72
73 $params = array(
74 'table' => $this->mTable,
75 'start' => $start,
76 'end' => ( $id !== false ? $id - 1 : false ),
77 );
78 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
79
80 $start = $id;
81 } while ( $start );
82
83 Job::batchInsert( $jobs );
84 }
85
86 protected function getPrefix() {
87 static $prefixes = array(
88 'pagelinks' => 'pl',
89 'imagelinks' => 'il',
90 'categorylinks' => 'cl',
91 'templatelinks' => 'tl',
92 'redirect' => 'rd',
93 );
94
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__ );
99 }
100 }
101 return $this->mPrefix;
102 }
103
104 public function getFromField() {
105 return $this->getPrefix() . '_from';
106 }
107
108 public function getToCondition() {
109 $prefix = $this->getPrefix();
110 switch ( $this->mTable ) {
111 case 'pagelinks':
112 case 'templatelinks':
113 case 'redirect':
114 return array(
115 "{$prefix}_namespace" => $this->mTitle->getNamespace(),
116 "{$prefix}_title" => $this->mTitle->getDBkey()
117 );
118 case 'imagelinks':
119 return array( 'il_to' => $this->mTitle->getDBkey() );
120 case 'categorylinks':
121 return array( 'cl_to' => $this->mTitle->getDBkey() );
122 }
123 throw new MWException( 'Invalid table type in ' . __CLASS__ );
124 }
125
126 /**
127 * Invalidate a set of IDs, right now
128 */
129 public function invalidateIDs( ResultWrapper $res ) {
130 global $wgUseFileCache, $wgUseSquid;
131
132 if ( $res->numRows() == 0 ) {
133 return;
134 }
135
136 $dbw = wfGetDB( DB_MASTER );
137 $timestamp = $dbw->timestamp();
138 $done = false;
139
140 while ( !$done ) {
141 # Get all IDs in this query into an array
142 $ids = array();
143 for ( $i = 0; $i < $this->mRowsPerQuery; $i++ ) {
144 $row = $res->fetchRow();
145 if ( $row ) {
146 $ids[] = $row[0];
147 } else {
148 $done = true;
149 break;
150 }
151 }
152
153 if ( !count( $ids ) ) {
154 break;
155 }
156
157 # Update page_touched
158 $dbw->update( 'page',
159 array( 'page_touched' => $timestamp ),
160 array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
161 __METHOD__
162 );
163
164 # Update squid
165 if ( $wgUseSquid || $wgUseFileCache ) {
166 $titles = Title::newFromIDs( $ids );
167 if ( $wgUseSquid ) {
168 $u = SquidUpdate::newFromTitles( $titles );
169 $u->doUpdate();
170 }
171
172 # Update file cache
173 if ( $wgUseFileCache ) {
174 foreach ( $titles as $title ) {
175 $cm = new HTMLFileCache($title);
176 @unlink($cm->fileCacheName());
177 }
178 }
179 }
180 }
181 }
182 }
183
184 /**
185 * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
186 * job gets called from the queue.
187 *
188 * @ingroup JobQueue
189 */
190 class HTMLCacheUpdateJob extends Job {
191 var $table, $start, $end;
192
193 /**
194 * Construct a job
195 * @param Title $title The title linked to
196 * @param array $params Job parameters (table, start and end page_ids)
197 * @param integer $id job_id
198 */
199 function __construct( $title, $params, $id = 0 ) {
200 parent::__construct( 'htmlCacheUpdate', $title, $params, $id );
201 $this->table = $params['table'];
202 $this->start = $params['start'];
203 $this->end = $params['end'];
204 }
205
206 public function run() {
207 $update = new HTMLCacheUpdate( $this->title, $this->table );
208
209 $fromField = $update->getFromField();
210 $conds = $update->getToCondition();
211 if ( $this->start ) {
212 $conds[] = "$fromField >= {$this->start}";
213 }
214 if ( $this->end ) {
215 $conds[] = "$fromField <= {$this->end}";
216 }
217
218 $dbr = wfGetDB( DB_SLAVE );
219 $res = $dbr->select( $this->table, $fromField, $conds, __METHOD__ );
220 $update->invalidateIDs( $res );
221
222 return true;
223 }
224 }