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 string $table
53 public function getLinks( $table, $startId = false, $endId = false ) {
54 wfProfileIn( __METHOD__
);
56 if ( $startId ||
$endId ) {
57 // Partial range, not cached
58 wfDebug( __METHOD__
.": from DB (uncacheable range)\n" );
59 $conds = $this->getConditions( $table );
60 // Use the from field in the condition rather than the joined page_id,
61 // because databases are stupid and don't necessarily propagate indexes.
62 $fromField = $this->getPrefix( $table ) . '_from';
64 $conds[] = "$fromField >= " . intval( $startId );
67 $conds[] = "$fromField <= " . intval( $endId );
69 $res = $this->getDB()->select(
70 array( $table, 'page' ),
71 array( 'page_namespace', 'page_title', 'page_id'),
74 array('STRAIGHT_JOIN') );
75 $ta = TitleArray
::newFromResult( $res );
76 wfProfileOut( __METHOD__
);
80 if ( !isset( $this->fullResultCache
[$table] ) ) {
81 wfDebug( __METHOD__
.": from DB\n" );
82 $res = $this->getDB()->select(
83 array( $table, 'page' ),
84 array( 'page_namespace', 'page_title', 'page_id' ),
85 $this->getConditions( $table ),
87 array('STRAIGHT_JOIN') );
88 $this->fullResultCache
[$table] = $res;
90 $ta = TitleArray
::newFromResult( $this->fullResultCache
[$table] );
91 wfProfileOut( __METHOD__
);
96 * Get the field name prefix for a given table
98 protected function getPrefix( $table ) {
99 static $prefixes = array(
101 'imagelinks' => 'il',
102 'categorylinks' => 'cl',
103 'templatelinks' => 'tl',
106 if ( isset( $prefixes[$table] ) ) {
107 return $prefixes[$table];
109 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
114 * Get the SQL condition array for selecting backlinks, with a join on the page table
116 protected function getConditions( $table ) {
117 $prefix = $this->getPrefix( $table );
120 case 'templatelinks':
123 "{$prefix}_namespace" => $this->title
->getNamespace(),
124 "{$prefix}_title" => $this->title
->getDBkey(),
125 "page_id={$prefix}_from"
130 'il_to' => $this->title
->getDBkey(),
134 case 'categorylinks':
136 'cl_to' => $this->title
->getDBkey(),
141 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
147 * Get the approximate number of backlinks
149 public function getNumLinks( $table ) {
150 if ( isset( $this->fullResultCache
[$table] ) ) {
151 return $this->fullResultCache
[$table]->numRows();
153 if ( isset( $this->partitionCache
[$table] ) ) {
154 $entry = reset( $this->partitionCache
[$table] );
155 return $entry['numRows'];
157 $titleArray = $this->getLinks( $table );
158 return $titleArray->count();
162 * Partition the backlinks into batches.
163 * Returns an array giving the start and end of each range. The first batch has
164 * a start of false, and the last batch has an end of false.
166 * @param string $table The links table name
167 * @param integer $batchSize
170 public function partition( $table, $batchSize ) {
172 if ( isset( $this->partitionCache
[$table][$batchSize] ) ) {
173 wfDebug( __METHOD__
.": got from partition cache\n" );
174 return $this->partitionCache
[$table][$batchSize]['batches'];
176 $this->partitionCache
[$table][$batchSize] = false;
177 $cacheEntry =& $this->partitionCache
[$table][$batchSize];
179 // Try full result cache
180 if ( isset( $this->fullResultCache
[$table] ) ) {
181 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
182 wfDebug( __METHOD__
.": got from full result cache\n" );
183 return $cacheEntry['batches'];
187 $memcKey = wfMemcKey( 'backlinks', md5( $this->title
->getPrefixedDBkey() ),
188 $table, $batchSize );
189 $memcValue = $wgMemc->get( $memcKey );
190 if ( is_array( $memcValue ) ) {
191 $cacheEntry = $memcValue;
192 wfDebug( __METHOD__
.": got from memcached $memcKey\n" );
193 return $cacheEntry['batches'];
195 // Fetch from database
196 $this->getLinks( $table );
197 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
199 $wgMemc->set( $memcKey, $cacheEntry, self
::CACHE_EXPIRY
);
200 wfDebug( __METHOD__
.": got from database\n" );
201 return $cacheEntry['batches'];
205 * Partition a DB result with backlinks in it into batches
207 protected function partitionResult( $res, $batchSize ) {
209 $numRows = $res->numRows();
210 $numBatches = ceil( $numRows / $batchSize );
211 for ( $i = 0; $i < $numBatches; $i++
) {
215 $rowNum = intval( $numRows * $i / $numBatches );
216 $res->seek( $rowNum );
217 $row = $res->fetchObject();
218 $start = $row->page_id
;
220 if ( $i == $numBatches - 1 ) {
223 $rowNum = intval( $numRows * ( $i +
1 ) / $numBatches );
224 $res->seek( $rowNum );
225 $row = $res->fetchObject();
226 $end = $row->page_id
- 1;
228 $batches[] = array( $start, $end );
230 return array( 'numRows' => $numRows, 'batches' => $batches );