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
);
49 * Get the backlinks for a given table. Cached in process memory only.
50 * @param $table String
51 * @param $startId Integer or false
52 * @param $endId Integer or false
55 public function getLinks( $table, $startId = false, $endId = false ) {
56 wfProfileIn( __METHOD__
);
58 $fromField = $this->getPrefix( $table ) . '_from';
60 if ( $startId ||
$endId ) {
61 // Partial range, not cached
62 wfDebug( __METHOD__
. ": from DB (uncacheable range)\n" );
63 $conds = $this->getConditions( $table );
65 // Use the from field in the condition rather than the joined page_id,
66 // because databases are stupid and don't necessarily propagate indexes.
68 $conds[] = "$fromField >= " . intval( $startId );
72 $conds[] = "$fromField <= " . intval( $endId );
75 $res = $this->getDB()->select(
76 array( $table, 'page' ),
77 array( 'page_namespace', 'page_title', 'page_id' ),
82 'ORDER BY' => $fromField
84 $ta = TitleArray
::newFromResult( $res );
86 wfProfileOut( __METHOD__
);
90 if ( !isset( $this->fullResultCache
[$table] ) ) {
91 wfDebug( __METHOD__
. ": from DB\n" );
92 $res = $this->getDB()->select(
93 array( $table, 'page' ),
94 array( 'page_namespace', 'page_title', 'page_id' ),
95 $this->getConditions( $table ),
99 'ORDER BY' => $fromField,
101 $this->fullResultCache
[$table] = $res;
104 $ta = TitleArray
::newFromResult( $this->fullResultCache
[$table] );
106 wfProfileOut( __METHOD__
);
111 * Get the field name prefix for a given table
113 protected function getPrefix( $table ) {
114 static $prefixes = array(
116 'imagelinks' => 'il',
117 'categorylinks' => 'cl',
118 'templatelinks' => 'tl',
122 if ( isset( $prefixes[$table] ) ) {
123 return $prefixes[$table];
125 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
130 * Get the SQL condition array for selecting backlinks, with a join on the page table
132 protected function getConditions( $table ) {
133 $prefix = $this->getPrefix( $table );
137 case 'templatelinks':
140 "{$prefix}_namespace" => $this->title
->getNamespace(),
141 "{$prefix}_title" => $this->title
->getDBkey(),
142 "page_id={$prefix}_from"
147 'il_to' => $this->title
->getDBkey(),
151 case 'categorylinks':
153 'cl_to' => $this->title
->getDBkey(),
158 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
165 * Get the approximate number of backlinks
167 public function getNumLinks( $table ) {
168 if ( isset( $this->fullResultCache
[$table] ) ) {
169 return $this->fullResultCache
[$table]->numRows();
172 if ( isset( $this->partitionCache
[$table] ) ) {
173 $entry = reset( $this->partitionCache
[$table] );
174 return $entry['numRows'];
177 $titleArray = $this->getLinks( $table );
179 return $titleArray->count();
183 * Partition the backlinks into batches.
184 * Returns an array giving the start and end of each range. The first batch has
185 * a start of false, and the last batch has an end of false.
187 * @param $table String: the links table name
188 * @param $batchSize Integer
191 public function partition( $table, $batchSize ) {
193 if ( isset( $this->partitionCache
[$table][$batchSize] ) ) {
194 wfDebug( __METHOD__
. ": got from partition cache\n" );
195 return $this->partitionCache
[$table][$batchSize]['batches'];
198 $this->partitionCache
[$table][$batchSize] = false;
199 $cacheEntry =& $this->partitionCache
[$table][$batchSize];
201 // Try full result cache
202 if ( isset( $this->fullResultCache
[$table] ) ) {
203 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
204 wfDebug( __METHOD__
. ": got from full result cache\n" );
206 return $cacheEntry['batches'];
212 $memcKey = wfMemcKey(
214 md5( $this->title
->getPrefixedDBkey() ),
219 $memcValue = $wgMemc->get( $memcKey );
221 if ( is_array( $memcValue ) ) {
222 $cacheEntry = $memcValue;
223 wfDebug( __METHOD__
. ": got from memcached $memcKey\n" );
225 return $cacheEntry['batches'];
228 // Fetch from database
229 $this->getLinks( $table );
230 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
232 $wgMemc->set( $memcKey, $cacheEntry, self
::CACHE_EXPIRY
);
234 wfDebug( __METHOD__
. ": got from database\n" );
235 return $cacheEntry['batches'];
239 * Partition a DB result with backlinks in it into batches
241 protected function partitionResult( $res, $batchSize ) {
243 $numRows = $res->numRows();
244 $numBatches = ceil( $numRows / $batchSize );
246 for ( $i = 0; $i < $numBatches; $i++
) {
250 $rowNum = intval( $numRows * $i / $numBatches );
251 $res->seek( $rowNum );
252 $row = $res->fetchObject();
253 $start = $row->page_id
;
256 if ( $i == $numBatches - 1 ) {
259 $rowNum = intval( $numRows * ( $i +
1 ) / $numBatches );
260 $res->seek( $rowNum );
261 $row = $res->fetchObject();
262 $end = $row->page_id
- 1;
266 if ( $start && $end && $start > $end ) {
267 throw new MWException( __METHOD__
. ': Internal error: query result out of order' );
270 $batches[] = array( $start, $end );
273 return array( 'numRows' => $numRows, 'batches' => $batches );