3 * Class for fetching backlink lists, approximate backlink counts and
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @author Tim Starling
23 * @copyright © 2009, Tim Starling, Domas Mituzas
24 * @copyright © 2010, Max Sem
25 * @copyright © 2011, Antoine Musso
29 * Class for fetching backlink lists, approximate backlink counts and
30 * partitions. This is a shared cache.
32 * Instances of this class should typically be fetched with the method
33 * $title->getBacklinkCache().
35 * Ideally you should only get your backlinks from here when you think
36 * there is some advantage in caching them. Otherwise it's just a waste
39 * Introduced by r47317
41 * @internal documentation reviewed on 18 Mar 2011 by hashar
44 /** @var ProcessCacheLRU */
45 protected static $cache;
48 * Multi dimensions array representing batches. Keys are:
49 * > (string) links table name
50 * > 'numRows' : Number of rows for this link table
51 * > 'batches' : array( $start, $end )
53 * @see BacklinkCache::partitionResult()
55 * Cleared with BacklinkCache::clear()
57 protected $partitionCache = array();
60 * Contains the whole links from a database result.
61 * This is raw data that will be partitioned in $partitionCache
63 * Initialized with BacklinkCache::getLinks()
64 * Cleared with BacklinkCache::clear()
66 protected $fullResultCache = array();
69 * Local copy of a database object.
71 * Accessor: BacklinkCache::getDB()
72 * Mutator : BacklinkCache::setDB()
73 * Cleared with BacklinkCache::clear()
78 * Local copy of a Title object
82 const CACHE_EXPIRY
= 3600;
85 * Create a new BacklinkCache
87 * @param Title $title : Title object to create a backlink cache for
89 public function __construct( Title
$title ) {
90 $this->title
= $title;
94 * Create a new BacklinkCache or reuse any existing one
96 * @param Title $title : Title object to get a backlink cache for
97 * @return BacklinkCache
99 public static function get( Title
$title ) {
100 if ( !self
::$cache ) { // init cache
101 self
::$cache = new ProcessCacheLRU( 2 );
103 $dbKey = $title->getPrefixedDBkey();
104 if ( !self
::$cache->has( $dbKey, 'obj' ) ) {
105 self
::$cache->set( $dbKey, 'obj', new self( $title ) );
107 return self
::$cache->get( $dbKey, 'obj' );
111 * Serialization handler, diasallows to serialize the database to prevent
112 * failures after this class is deserialized from cache with dead DB
118 return array( 'partitionCache', 'fullResultCache', 'title' );
122 * Clear locally stored data and database object.
124 public function clear() {
125 $this->partitionCache
= array();
126 $this->fullResultCache
= array();
131 * Set the Database object to use
133 * @param $db DatabaseBase
135 public function setDB( $db ) {
140 * Get the slave connection to the database
141 * When non existing, will initialize the connection.
142 * @return DatabaseBase object
144 protected function getDB() {
145 if ( !isset( $this->db
) ) {
146 $this->db
= wfGetDB( DB_SLAVE
);
153 * Get the backlinks for a given table. Cached in process memory only.
154 * @param $table String
155 * @param $startId Integer or false
156 * @param $endId Integer or false
157 * @return TitleArrayFromResult
159 public function getLinks( $table, $startId = false, $endId = false ) {
160 wfProfileIn( __METHOD__
);
162 $fromField = $this->getPrefix( $table ) . '_from';
164 if ( $startId ||
$endId ) {
165 // Partial range, not cached
166 wfDebug( __METHOD__
. ": from DB (uncacheable range)\n" );
167 $conds = $this->getConditions( $table );
169 // Use the from field in the condition rather than the joined page_id,
170 // because databases are stupid and don't necessarily propagate indexes.
172 $conds[] = "$fromField >= " . intval( $startId );
176 $conds[] = "$fromField <= " . intval( $endId );
179 $res = $this->getDB()->select(
180 array( $table, 'page' ),
181 array( 'page_namespace', 'page_title', 'page_id' ),
186 'ORDER BY' => $fromField
188 $ta = TitleArray
::newFromResult( $res );
190 wfProfileOut( __METHOD__
);
194 // @todo FIXME: Make this a function?
195 if ( !isset( $this->fullResultCache
[$table] ) ) {
196 wfDebug( __METHOD__
. ": from DB\n" );
197 $res = $this->getDB()->select(
198 array( $table, 'page' ),
199 array( 'page_namespace', 'page_title', 'page_id' ),
200 $this->getConditions( $table ),
204 'ORDER BY' => $fromField,
206 $this->fullResultCache
[$table] = $res;
209 $ta = TitleArray
::newFromResult( $this->fullResultCache
[$table] );
211 wfProfileOut( __METHOD__
);
216 * Get the field name prefix for a given table
217 * @param $table String
218 * @return null|string
220 protected function getPrefix( $table ) {
221 static $prefixes = array(
223 'imagelinks' => 'il',
224 'categorylinks' => 'cl',
225 'templatelinks' => 'tl',
229 if ( isset( $prefixes[$table] ) ) {
230 return $prefixes[$table];
233 wfRunHooks( 'BacklinkCacheGetPrefix', array( $table, &$prefix ) );
237 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
243 * Get the SQL condition array for selecting backlinks, with a join
245 * @param $table String
248 protected function getConditions( $table ) {
249 $prefix = $this->getPrefix( $table );
251 // @todo FIXME: imagelinks and categorylinks do not rely on getNamespace,
252 // they could be moved up for nicer case statements
255 case 'templatelinks':
257 "{$prefix}_namespace" => $this->title
->getNamespace(),
258 "{$prefix}_title" => $this->title
->getDBkey(),
259 "page_id={$prefix}_from"
264 "{$prefix}_namespace" => $this->title
->getNamespace(),
265 "{$prefix}_title" => $this->title
->getDBkey(),
266 $this->getDb()->makeList( array(
267 "{$prefix}_interwiki = ''",
268 "{$prefix}_interwiki is null",
270 "page_id={$prefix}_from"
275 'il_to' => $this->title
->getDBkey(),
279 case 'categorylinks':
281 'cl_to' => $this->title
->getDBkey(),
287 wfRunHooks( 'BacklinkCacheGetConditions', array( $table, $this->title
, &$conds ) );
289 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
296 * Get the approximate number of backlinks
297 * @param $table String
300 public function getNumLinks( $table ) {
301 if ( isset( $this->fullResultCache
[$table] ) ) {
302 return $this->fullResultCache
[$table]->numRows();
305 if ( isset( $this->partitionCache
[$table] ) ) {
306 $entry = reset( $this->partitionCache
[$table] );
307 return $entry['numRows'];
310 $titleArray = $this->getLinks( $table );
312 return $titleArray->count();
316 * Partition the backlinks into batches.
317 * Returns an array giving the start and end of each range. The first
318 * batch has a start of false, and the last batch has an end of false.
320 * @param $table String: the links table name
321 * @param $batchSize Integer
324 public function partition( $table, $batchSize ) {
326 // 1) try partition cache ...
328 if ( isset( $this->partitionCache
[$table][$batchSize] ) ) {
329 wfDebug( __METHOD__
. ": got from partition cache\n" );
330 return $this->partitionCache
[$table][$batchSize]['batches'];
333 $this->partitionCache
[$table][$batchSize] = false;
334 $cacheEntry =& $this->partitionCache
[$table][$batchSize];
336 // 2) ... then try full result cache ...
338 if ( isset( $this->fullResultCache
[$table] ) ) {
339 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
340 wfDebug( __METHOD__
. ": got from full result cache\n" );
342 return $cacheEntry['batches'];
345 // 3) ... fallback to memcached ...
349 $memcKey = wfMemcKey(
351 md5( $this->title
->getPrefixedDBkey() ),
356 $memcValue = $wgMemc->get( $memcKey );
358 if ( is_array( $memcValue ) ) {
359 $cacheEntry = $memcValue;
360 wfDebug( __METHOD__
. ": got from memcached $memcKey\n" );
362 return $cacheEntry['batches'];
366 // 4) ... finally fetch from the slow database :(
368 $this->getLinks( $table );
369 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
371 $wgMemc->set( $memcKey, $cacheEntry, self
::CACHE_EXPIRY
);
373 wfDebug( __METHOD__
. ": got from database\n" );
374 return $cacheEntry['batches'];
378 * Partition a DB result with backlinks in it into batches
379 * @param $res ResultWrapper database result
380 * @param $batchSize integer
383 protected function partitionResult( $res, $batchSize ) {
385 $numRows = $res->numRows();
386 $numBatches = ceil( $numRows / $batchSize );
388 for ( $i = 0; $i < $numBatches; $i++
) {
392 $rowNum = intval( $numRows * $i / $numBatches );
393 $res->seek( $rowNum );
394 $row = $res->fetchObject();
395 $start = $row->page_id
;
398 if ( $i == $numBatches - 1 ) {
401 $rowNum = intval( $numRows * ( $i +
1 ) / $numBatches );
402 $res->seek( $rowNum );
403 $row = $res->fetchObject();
404 $end = $row->page_id
- 1;
408 if ( $start && $end && $start > $end ) {
409 throw new MWException( __METHOD__
. ': Internal error: query result out of order' );
412 $batches[] = array( $start, $end );
415 return array( 'numRows' => $numRows, 'batches' => $batches );