3 * Class for fetching backlink lists, approximate backlink counts and partitions.
4 * Instances of this class should typically be fetched with $title->getBacklinkCache().
6 * Ideally you should only get your backlinks from here when you think there is some
7 * advantage in caching them. Otherwise it's just a waste of memory.
10 var $partitionCache = array();
11 var $fullResultCache = array();
15 const CACHE_EXPIRY
= 3600;
18 * Create a new BacklinkCache
20 function __construct( $title ) {
21 $this->title
= $title;
25 * Clear locally stored data
28 $this->partitionCache
= array();
29 $this->fullResultCache
= array();
34 * Set the Database object to use
36 public function setDB( $db ) {
40 protected function getDB() {
41 if ( !isset( $this->db
) ) {
42 $this->db
= wfGetDB( DB_SLAVE
);
48 * Get the backlinks for a given table. Cached in process memory only.
49 * @param $table String
50 * @param $startId Integer or false
51 * @param $endId Integer or false
54 public function getLinks( $table, $startId = false, $endId = false ) {
55 wfProfileIn( __METHOD__
);
57 $fromField = $this->getPrefix( $table ) . '_from';
59 if ( $startId ||
$endId ) {
60 // Partial range, not cached
61 wfDebug( __METHOD__
. ": from DB (uncacheable range)\n" );
62 $conds = $this->getConditions( $table );
63 // Use the from field in the condition rather than the joined page_id,
64 // because databases are stupid and don't necessarily propagate indexes.
66 $conds[] = "$fromField >= " . intval( $startId );
69 $conds[] = "$fromField <= " . intval( $endId );
71 $res = $this->getDB()->select(
72 array( $table, 'page' ),
73 array( 'page_namespace', 'page_title', 'page_id' ),
78 'ORDER BY' => $fromField
80 $ta = TitleArray
::newFromResult( $res );
81 wfProfileOut( __METHOD__
);
85 if ( !isset( $this->fullResultCache
[$table] ) ) {
86 wfDebug( __METHOD__
. ": from DB\n" );
87 $res = $this->getDB()->select(
88 array( $table, 'page' ),
89 array( 'page_namespace', 'page_title', 'page_id' ),
90 $this->getConditions( $table ),
94 'ORDER BY' => $fromField,
96 $this->fullResultCache
[$table] = $res;
98 $ta = TitleArray
::newFromResult( $this->fullResultCache
[$table] );
99 wfProfileOut( __METHOD__
);
104 * Get the field name prefix for a given table
106 protected function getPrefix( $table ) {
107 static $prefixes = array(
109 'imagelinks' => 'il',
110 'categorylinks' => 'cl',
111 'templatelinks' => 'tl',
115 if ( isset( $prefixes[$table] ) ) {
116 return $prefixes[$table];
118 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
123 * Get the SQL condition array for selecting backlinks, with a join on the page table
125 protected function getConditions( $table ) {
126 $prefix = $this->getPrefix( $table );
130 case 'templatelinks':
133 "{$prefix}_namespace" => $this->title
->getNamespace(),
134 "{$prefix}_title" => $this->title
->getDBkey(),
135 "page_id={$prefix}_from"
140 'il_to' => $this->title
->getDBkey(),
144 case 'categorylinks':
146 'cl_to' => $this->title
->getDBkey(),
151 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
157 * Get the approximate number of backlinks
159 public function getNumLinks( $table ) {
160 if ( isset( $this->fullResultCache
[$table] ) ) {
161 return $this->fullResultCache
[$table]->numRows();
164 if ( isset( $this->partitionCache
[$table] ) ) {
165 $entry = reset( $this->partitionCache
[$table] );
166 return $entry['numRows'];
169 $titleArray = $this->getLinks( $table );
170 return $titleArray->count();
174 * Partition the backlinks into batches.
175 * Returns an array giving the start and end of each range. The first batch has
176 * a start of false, and the last batch has an end of false.
178 * @param $table String: the links table name
179 * @param $batchSize Integer
182 public function partition( $table, $batchSize ) {
184 if ( isset( $this->partitionCache
[$table][$batchSize] ) ) {
185 wfDebug( __METHOD__
. ": got from partition cache\n" );
186 return $this->partitionCache
[$table][$batchSize]['batches'];
189 $this->partitionCache
[$table][$batchSize] = false;
190 $cacheEntry =& $this->partitionCache
[$table][$batchSize];
192 // Try full result cache
193 if ( isset( $this->fullResultCache
[$table] ) ) {
194 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
195 wfDebug( __METHOD__
. ": got from full result cache\n" );
196 return $cacheEntry['batches'];
201 $memcKey = wfMemcKey(
203 md5( $this->title
->getPrefixedDBkey() ),
207 $memcValue = $wgMemc->get( $memcKey );
209 if ( is_array( $memcValue ) ) {
210 $cacheEntry = $memcValue;
211 wfDebug( __METHOD__
. ": got from memcached $memcKey\n" );
212 return $cacheEntry['batches'];
214 // Fetch from database
215 $this->getLinks( $table );
216 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
218 $wgMemc->set( $memcKey, $cacheEntry, self
::CACHE_EXPIRY
);
219 wfDebug( __METHOD__
. ": got from database\n" );
220 return $cacheEntry['batches'];
224 * Partition a DB result with backlinks in it into batches
226 protected function partitionResult( $res, $batchSize ) {
228 $numRows = $res->numRows();
229 $numBatches = ceil( $numRows / $batchSize );
231 for ( $i = 0; $i < $numBatches; $i++
) {
235 $rowNum = intval( $numRows * $i / $numBatches );
236 $res->seek( $rowNum );
237 $row = $res->fetchObject();
238 $start = $row->page_id
;
241 if ( $i == $numBatches - 1 ) {
244 $rowNum = intval( $numRows * ( $i +
1 ) / $numBatches );
245 $res->seek( $rowNum );
246 $row = $res->fetchObject();
247 $end = $row->page_id
- 1;
251 if ( $start && $end && $start > $end ) {
252 throw new MWException( __METHOD__
. ': Internal error: query result out of order' );
255 $batches[] = array( $start, $end );
257 return array( 'numRows' => $numRows, 'batches' => $batches );