4 * Class for fetching backlink lists, approximate backlink counts and partitions.
5 * Instances of this class should typically be fetched with $title->getBacklinkCache().
7 * Ideally you should only get your backlinks from here when you think there is some
8 * advantage in caching them. Otherwise it's just a waste of memory.
11 var $partitionCache = array();
12 var $fullResultCache = array();
16 const CACHE_EXPIRY
= 3600;
19 * Create a new BacklinkCache
21 function __construct( $title ) {
22 $this->title
= $title;
26 * Clear locally stored data
29 $this->partitionCache
= array();
30 $this->fullResultCache
= array();
35 * Set the Database object to use
37 public function setDB( $db ) {
41 protected function getDB() {
42 if ( !isset( $this->db
) ) {
43 $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';
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',
114 if ( isset( $prefixes[$table] ) ) {
115 return $prefixes[$table];
117 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
122 * Get the SQL condition array for selecting backlinks, with a join on the page table
124 protected function getConditions( $table ) {
125 $prefix = $this->getPrefix( $table );
128 case 'templatelinks':
131 "{$prefix}_namespace" => $this->title
->getNamespace(),
132 "{$prefix}_title" => $this->title
->getDBkey(),
133 "page_id={$prefix}_from"
138 'il_to' => $this->title
->getDBkey(),
142 case 'categorylinks':
144 'cl_to' => $this->title
->getDBkey(),
149 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
155 * Get the approximate number of backlinks
157 public function getNumLinks( $table ) {
158 if ( isset( $this->fullResultCache
[$table] ) ) {
159 return $this->fullResultCache
[$table]->numRows();
161 if ( isset( $this->partitionCache
[$table] ) ) {
162 $entry = reset( $this->partitionCache
[$table] );
163 return $entry['numRows'];
165 $titleArray = $this->getLinks( $table );
166 return $titleArray->count();
170 * Partition the backlinks into batches.
171 * Returns an array giving the start and end of each range. The first batch has
172 * a start of false, and the last batch has an end of false.
174 * @param $table String: the links table name
175 * @param $batchSize Integer
178 public function partition( $table, $batchSize ) {
180 if ( isset( $this->partitionCache
[$table][$batchSize] ) ) {
181 wfDebug( __METHOD__
.": got from partition cache\n" );
182 return $this->partitionCache
[$table][$batchSize]['batches'];
184 $this->partitionCache
[$table][$batchSize] = false;
185 $cacheEntry =& $this->partitionCache
[$table][$batchSize];
187 // Try full result cache
188 if ( isset( $this->fullResultCache
[$table] ) ) {
189 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
190 wfDebug( __METHOD__
.": got from full result cache\n" );
191 return $cacheEntry['batches'];
195 $memcKey = wfMemcKey( 'backlinks', md5( $this->title
->getPrefixedDBkey() ),
196 $table, $batchSize );
197 $memcValue = $wgMemc->get( $memcKey );
198 if ( is_array( $memcValue ) ) {
199 $cacheEntry = $memcValue;
200 wfDebug( __METHOD__
.": got from memcached $memcKey\n" );
201 return $cacheEntry['batches'];
203 // Fetch from database
204 $this->getLinks( $table );
205 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
207 $wgMemc->set( $memcKey, $cacheEntry, self
::CACHE_EXPIRY
);
208 wfDebug( __METHOD__
.": got from database\n" );
209 return $cacheEntry['batches'];
213 * Partition a DB result with backlinks in it into batches
215 protected function partitionResult( $res, $batchSize ) {
217 $numRows = $res->numRows();
218 $numBatches = ceil( $numRows / $batchSize );
219 for ( $i = 0; $i < $numBatches; $i++
) {
223 $rowNum = intval( $numRows * $i / $numBatches );
224 $res->seek( $rowNum );
225 $row = $res->fetchObject();
226 $start = $row->page_id
;
228 if ( $i == $numBatches - 1 ) {
231 $rowNum = intval( $numRows * ( $i +
1 ) / $numBatches );
232 $res->seek( $rowNum );
233 $row = $res->fetchObject();
234 $end = $row->page_id
- 1;
238 if ( $start && $end && $start > $end ) {
239 throw new MWException( __METHOD__
.': Internal error: query result out of order' );
242 $batches[] = array( $start, $end );
244 return array( 'numRows' => $numRows, 'batches' => $batches );