Remove ?>'s from files. They're pointless, and just asking for people to mess with...
[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 class HTMLCacheUpdate
25 {
26 public $mTitle, $mTable, $mPrefix;
27 public $mRowsPerJob, $mRowsPerQuery;
28
29 function __construct( $titleTo, $table ) {
30 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
31
32 $this->mTitle = $titleTo;
33 $this->mTable = $table;
34 $this->mRowsPerJob = $wgUpdateRowsPerJob;
35 $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
36 }
37
38 function doUpdate() {
39 # Fetch the IDs
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 );
47 } else {
48 $this->invalidateIDs( $resWrap );
49 }
50 }
51 $dbr->freeResult( $res );
52 }
53
54 function insertJobs( ResultWrapper $res ) {
55 $numRows = $res->numRows();
56 $numBatches = ceil( $numRows / $this->mRowsPerJob );
57 $realBatchSize = $numRows / $numBatches;
58 $start = false;
59 $jobs = array();
60 do {
61 for ( $i = 0; $i < $realBatchSize - 1; $i++ ) {
62 $row = $res->fetchRow();
63 if ( $row ) {
64 $id = $row[0];
65 } else {
66 $id = false;
67 break;
68 }
69 }
70
71 $params = array(
72 'table' => $this->mTable,
73 'start' => $start,
74 'end' => ( $id !== false ? $id - 1 : false ),
75 );
76 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
77
78 $start = $id;
79 } while ( $start );
80
81 Job::batchInsert( $jobs );
82 }
83
84 function getPrefix() {
85 static $prefixes = array(
86 'pagelinks' => 'pl',
87 'imagelinks' => 'il',
88 'categorylinks' => 'cl',
89 'templatelinks' => 'tl',
90
91 # Not needed
92 # 'externallinks' => 'el',
93 # 'langlinks' => 'll'
94 );
95
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__ );
100 }
101 }
102 return $this->mPrefix;
103 }
104
105 function getFromField() {
106 return $this->getPrefix() . '_from';
107 }
108
109 function getToCondition() {
110 switch ( $this->mTable ) {
111 case 'pagelinks':
112 return array(
113 'pl_namespace' => $this->mTitle->getNamespace(),
114 'pl_title' => $this->mTitle->getDBkey()
115 );
116 case 'templatelinks':
117 return array(
118 'tl_namespace' => $this->mTitle->getNamespace(),
119 'tl_title' => $this->mTitle->getDBkey()
120 );
121 case 'imagelinks':
122 return array( 'il_to' => $this->mTitle->getDBkey() );
123 case 'categorylinks':
124 return array( 'cl_to' => $this->mTitle->getDBkey() );
125 }
126 throw new MWException( 'Invalid table type in ' . __CLASS__ );
127 }
128
129 /**
130 * Invalidate a set of IDs, right now
131 */
132 function invalidateIDs( ResultWrapper $res ) {
133 global $wgUseFileCache, $wgUseSquid;
134
135 if ( $res->numRows() == 0 ) {
136 return;
137 }
138
139 $dbw = wfGetDB( DB_MASTER );
140 $timestamp = $dbw->timestamp();
141 $done = false;
142
143 while ( !$done ) {
144 # Get all IDs in this query into an array
145 $ids = array();
146 for ( $i = 0; $i < $this->mRowsPerQuery; $i++ ) {
147 $row = $res->fetchRow();
148 if ( $row ) {
149 $ids[] = $row[0];
150 } else {
151 $done = true;
152 break;
153 }
154 }
155
156 if ( !count( $ids ) ) {
157 break;
158 }
159
160 # Update page_touched
161 $dbw->update( 'page',
162 array( 'page_touched' => $timestamp ),
163 array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
164 __METHOD__
165 );
166
167 # Update squid
168 if ( $wgUseSquid || $wgUseFileCache ) {
169 $titles = Title::newFromIDs( $ids );
170 if ( $wgUseSquid ) {
171 $u = SquidUpdate::newFromTitles( $titles );
172 $u->doUpdate();
173 }
174
175 # Update file cache
176 if ( $wgUseFileCache ) {
177 foreach ( $titles as $title ) {
178 $cm = new HTMLFileCache($title);
179 @unlink($cm->fileCacheName());
180 }
181 }
182 }
183 }
184 }
185 }
186
187 /**
188 * @todo document (e.g. one-sentence top-level class description).
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 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( new ResultWrapper( $dbr, $res ) );
221 $dbr->freeResult( $res );
222
223 return true;
224 }
225 }
226