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( 'page', $table ),
71 array( 'page_namespace', 'page_title', 'page_id' ),
74 $ta = TitleArray
::newFromResult( $res );
75 wfProfileOut( __METHOD__
);
79 if ( !isset( $this->fullResultCache
[$table] ) ) {
80 wfDebug( __METHOD__
.": from DB\n" );
81 $res = $this->getDB()->select(
82 array( 'page', $table ),
83 array( 'page_namespace', 'page_title', 'page_id' ),
84 $this->getConditions( $table ),
86 $this->fullResultCache
[$table] = $res;
88 $ta = TitleArray
::newFromResult( $this->fullResultCache
[$table] );
89 wfProfileOut( __METHOD__
);
94 * Get the field name prefix for a given table
96 protected function getPrefix( $table ) {
97 static $prefixes = array(
100 'categorylinks' => 'cl',
101 'templatelinks' => 'tl',
104 if ( isset( $prefixes[$table] ) ) {
105 return $prefixes[$table];
107 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
112 * Get the SQL condition array for selecting backlinks, with a join on the page table
114 protected function getConditions( $table ) {
115 $prefix = $this->getPrefix( $table );
118 case 'templatelinks':
121 "{$prefix}_namespace" => $this->title
->getNamespace(),
122 "{$prefix}_title" => $this->title
->getDBkey(),
123 "page_id={$prefix}_from"
128 'il_to' => $this->title
->getDBkey(),
132 case 'categorylinks':
134 'cl_to' => $this->title
->getDBkey(),
139 throw new MWException( "Invalid table \"$table\" in " . __CLASS__
);
145 * Get the approximate number of backlinks
147 public function getNumLinks( $table ) {
148 if ( isset( $this->fullResultCache
[$table] ) ) {
149 return $this->fullResultCache
[$table]->numRows();
151 if ( isset( $this->partitionCache
[$table] ) ) {
152 $entry = reset( $this->partitionCache
[$table] );
153 return $entry['numRows'];
155 $titleArray = $this->getLinks( $table );
156 return $titleArray->count();
160 * Partition the backlinks into batches.
161 * Returns an array giving the start and end of each range. The first batch has
162 * a start of false, and the last batch has an end of false.
164 * @param string $table The links table name
165 * @param integer $batchSize
168 public function partition( $table, $batchSize ) {
170 if ( isset( $this->partitionCache
[$table][$batchSize] ) ) {
171 wfDebug( __METHOD__
.": got from partition cache\n" );
172 return $this->partitionCache
[$table][$batchSize]['batches'];
174 $this->partitionCache
[$table][$batchSize] = false;
175 $cacheEntry =& $this->partitionCache
[$table][$batchSize];
177 // Try full result cache
178 if ( isset( $this->fullResultCache
[$table] ) ) {
179 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
180 wfDebug( __METHOD__
.": got from full result cache\n" );
181 return $cacheEntry['batches'];
185 $memcKey = wfMemcKey( 'backlinks', md5( $this->title
->getPrefixedDBkey() ),
186 $table, $batchSize );
187 $memcValue = $wgMemc->get( $memcKey );
188 if ( is_array( $memcValue ) ) {
189 $cacheEntry = $memcValue;
190 wfDebug( __METHOD__
.": got from memcached $memcKey\n" );
191 return $cacheEntry['batches'];
193 // Fetch from database
194 $this->getLinks( $table );
195 $cacheEntry = $this->partitionResult( $this->fullResultCache
[$table], $batchSize );
197 $wgMemc->set( $memcKey, $cacheEntry, self
::CACHE_EXPIRY
);
198 wfDebug( __METHOD__
.": got from database\n" );
199 return $cacheEntry['batches'];
203 * Partition a DB result with backlinks in it into batches
205 protected function partitionResult( $res, $batchSize ) {
207 $numRows = $res->numRows();
208 $numBatches = ceil( $numRows / $batchSize );
210 $batches = array( array( false, false ) );
212 for ( $i = 0; $i < $numBatches; $i++
) {
216 $rowNum = intval( $numRows * $i / $numBatches );
217 $res->seek( $rowNum );
218 $row = $res->fetchObject();
219 $start = $row->page_id
;
221 if ( $i == $numBatches - 1 ) {
224 $rowNum = intval( $numRows * ( $i +
1 ) / $numBatches );
225 $res->seek( $rowNum );
226 $row = $res->fetchObject();
227 $end = $row->page_id
- 1;
229 $batches[] = array( $start, $end );
232 return array( 'numRows' => $numRows, 'batches' => $batches );