3 * File for BacklinkCache class
8 * Class for fetching backlink lists, approximate backlink counts and
9 * partitions. This is a shared cache.
11 * Instances of this class should typically be fetched with the method
12 * $title->getBacklinkCache().
14 * Ideally you should only get your backlinks from here when you think
15 * there is some advantage in caching them. Otherwise it's just a waste
18 * Introduced by r47317
20 * @internal documentation reviewed on 18 Mar 2011 by hashar
22 * @author Tim Starling
23 * @copyright © 2009, Tim Starling, Domas Mituzas
24 * @copyright © 2010, Max Sem
25 * @copyright © 2011, Ashar Voultoiz
30 * Multi dimensions array representing batches. Keys are:
31 * > (string) links table name
32 * > 'numRows' : Number of rows for this link table
33 * > 'batches' : array( $start, $end )
35 * @see BacklinkCache::partitionResult()
37 * Cleared with BacklinkCache::clear()
39 protected $partitionCache = array();
42 * Contains the whole links from a database result.
43 * This is raw data that will be partitioned in $partitionCache
45 * Initialized with BacklinkCache::getLinks()
46 * Cleared with BacklinkCache::clear()
48 protected $fullResultCache = array();
51 * Local copy of a database object.
53 * Accessor: BacklinkCache::getDB()
54 * Mutator : BacklinkCache::setDB()
55 * Cleared with BacklinkCache::clear()
60 * Local copy of a Title object
64 const CACHE_EXPIRY
= 3600;
67 * Create a new BacklinkCache
68 * @param Title $title : Title object to create a backlink cache for.
70 function __construct( $title ) {
71 $this->title
= $title;
75 * Serialization handler, diasallows to serialize the database to prevent
76 * failures after this class is deserialized from cache with dead DB
80 return array( 'partitionCache', 'fullResultCache', 'title' );
84 * Clear locally stored data and database object.
86 public function clear() {
87 $this->partitionCache
= array();
88 $this->fullResultCache
= array();
93 * Set the Database object to use
95 * @param $db DatabaseBase
97 public function setDB( $db ) {
102 * Get the slave connection to the database
103 * When non existing, will initialize the connection.
104 * @return Database object
106 protected function getDB() {
107 if ( !isset( $this->db
) ) {
108 $this->db
= wfGetDB( DB_SLAVE
);
115 * Get the backlinks for a given table. Cached in process memory only.
116 * @param $table String
117 * @param $startId Integer or false
118 * @param $endId Integer or false
119 * @return TitleArrayFromResult
121 public function getLinks( $table, $startId = false, $endId = false ) {
122 wfProfileIn( __METHOD__
);
124 $fromField = $this->getPrefix( $table ) . '_from';
126 if ( $startId ||
$endId ) {
127 // Partial range, not cached
128 wfDebug( __METHOD__
. ": from DB (uncacheable range)\n" );
129 $conds = $this->getConditions( $table );
131 // Use the from field in the condition rather than the joined page_id,
132 // because databases are stupid and don't necessarily propagate indexes.
134 $conds[] = "$fromField >= " . intval( $startId );
138 $conds[] = "$fromField <= " . intval( $endId );
141 $res = $this->getDB()->select(
142 array( $table, 'page' ),
143 array( 'page_namespace', 'page_title', 'page_id' ),
148 'ORDER BY' => $fromField
150 $ta = TitleArray
::newFromResult( $res );
152 wfProfileOut( __METHOD__
);
156 // @todo FIXME: Make this a function?
157 if ( !isset( $this->fullResultCache
[$table] ) ) {
158 wfDebug( __METHOD__
. ": from DB\n" );
159 $res = $this->getDB()->select(
160 array( $table, 'page' ),
161 array( 'page_namespace', 'page_title', 'page_id' ),
162 $this->getConditions( $table ),
166 'ORDER BY' => $fromField,
168 $this->fullResultCache
[$table] = $res;
171 $ta = TitleArray
::newFromResult( $this->fullResultCache
[$table] );
173 wfProfileOut( __METHOD__
);
178 * Get the field name prefix for a given table
179 * @param $table String
181 protected function getPrefix( $table ) {
182 static $prefixes = array(
184 'imagelinks' => 'il',
185 'categorylinks' => 'cl',
186 'templatelinks' => 'tl',
190 if ( isset( $prefixes[$table] ) ) {
191 return $prefixes[$table];
193 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
198 * Get the SQL condition array for selecting backlinks, with a join
200 * @param $table String
202 protected function getConditions( $table ) {
203 $prefix = $this->getPrefix( $table );
205 // @todo FIXME: imagelinks and categorylinks do not rely on getNamespace,
206 // they could be moved up for nicer case statements
209 case 'templatelinks':
212 "{$prefix}_namespace" => $this->title
->getNamespace(),
213 "{$prefix}_title" => $this->title
->getDBkey(),
214 "page_id={$prefix}_from"
219 'il_to' => $this->title
->getDBkey(),
223 case 'categorylinks':
225 'cl_to' => $this->title
->getDBkey(),
230 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
237 * Get the approximate number of backlinks
238 * @param $table String
241 public function getNumLinks( $table ) {
242 if ( isset( $this->fullResultCache
[$table] ) ) {
243 return $this->fullResultCache
[$table]->numRows();
246 if ( isset( $this->partitionCache
[$table] ) ) {
247 $entry = reset( $this->partitionCache
[$table] );
248 return $entry['numRows'];
251 $titleArray = $this->getLinks( $table );
253 return $titleArray->count();
257 * Partition the backlinks into batches.
258 * Returns an array giving the start and end of each range. The firsti
259 * batch has a start of false, and the last batch has an end of false.
261 * @param $table String: the links table name
262 * @param $batchSize Integer
265 public function partition( $table, $batchSize ) {
267 // 1) try this per process cache first
269 if ( isset( $this->partitionCache
[$table][$batchSize] ) ) {
270 wfDebug( __METHOD__
. ": got from partition cache\n" );
271 return $this->partitionCache
[$table][$batchSize]['batches'];
274 $this->partitionCache
[$table][$batchSize] = false;
275 $cacheEntry =& $this->partitionCache
[$table][$batchSize];
277 // 2) try full result cache
279 if ( isset( $this->fullResultCache
[$table] ) ) {
280 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
281 wfDebug( __METHOD__
. ": got from full result cache\n" );
283 return $cacheEntry['batches'];
286 // 3) ... fallback to memcached ...
290 $memcKey = wfMemcKey(
292 md5( $this->title
->getPrefixedDBkey() ),
297 $memcValue = $wgMemc->get( $memcKey );
299 if ( is_array( $memcValue ) ) {
300 $cacheEntry = $memcValue;
301 wfDebug( __METHOD__
. ": got from memcached $memcKey\n" );
303 return $cacheEntry['batches'];
307 // 4) ... finally fetch from the slow database :(
309 $this->getLinks( $table );
310 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
312 $wgMemc->set( $memcKey, $cacheEntry, self
::CACHE_EXPIRY
);
314 wfDebug( __METHOD__
. ": got from database\n" );
315 return $cacheEntry['batches'];
319 * Partition a DB result with backlinks in it into batches
320 * @param $res ResultWrapper database result
321 * @param $batchSize integer
324 protected function partitionResult( $res, $batchSize ) {
326 $numRows = $res->numRows();
327 $numBatches = ceil( $numRows / $batchSize );
329 for ( $i = 0; $i < $numBatches; $i++
) {
333 $rowNum = intval( $numRows * $i / $numBatches );
334 $res->seek( $rowNum );
335 $row = $res->fetchObject();
336 $start = $row->page_id
;
339 if ( $i == $numBatches - 1 ) {
342 $rowNum = intval( $numRows * ( $i +
1 ) / $numBatches );
343 $res->seek( $rowNum );
344 $row = $res->fetchObject();
345 $end = $row->page_id
- 1;
349 if ( $start && $end && $start > $end ) {
350 throw new MWException( __METHOD__
. ': Internal error: query result out of order' );
353 $batches[] = array( $start, $end );
356 return array( 'numRows' => $numRows, 'batches' => $batches );