(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / BacklinkCache.php
bloba7bcd8583a2e778b9b951bb82c5e7881e82a6728
1 <?php
3 /**
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.
9 */
10 class BacklinkCache {
11 var $partitionCache = array();
12 var $fullResultCache = array();
13 var $title;
14 var $db;
16 const CACHE_EXPIRY = 3600;
18 /**
19 * Create a new BacklinkCache
21 function __construct( $title ) {
22 $this->title = $title;
25 /**
26 * Clear locally stored data
28 function clear() {
29 $this->partitionCache = array();
30 $this->fullResultCache = array();
31 unset( $this->db );
34 /**
35 * Set the Database object to use
37 public function setDB( $db ) {
38 $this->db = $db;
41 protected function getDB() {
42 if ( !isset( $this->db ) ) {
43 $this->db = wfGetDB( DB_SLAVE );
45 return $this->db;
48 /**
49 * Get the backlinks for a given table. Cached in process memory only.
50 * @param string $table
51 * @return TitleArray
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';
63 if ( $startId ) {
64 $conds[] = "$fromField >= " . intval( $startId );
66 if ( $endId ) {
67 $conds[] = "$fromField <= " . intval( $endId );
69 $res = $this->getDB()->select(
70 array( $table, 'page' ),
71 array( 'page_namespace', 'page_title', 'page_id'),
72 $conds,
73 __METHOD__,
74 array('STRAIGHT_JOIN') );
75 $ta = TitleArray::newFromResult( $res );
76 wfProfileOut( __METHOD__ );
77 return $ta;
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 ),
86 __METHOD__,
87 array('STRAIGHT_JOIN') );
88 $this->fullResultCache[$table] = $res;
90 $ta = TitleArray::newFromResult( $this->fullResultCache[$table] );
91 wfProfileOut( __METHOD__ );
92 return $ta;
95 /**
96 * Get the field name prefix for a given table
98 protected function getPrefix( $table ) {
99 static $prefixes = array(
100 'pagelinks' => 'pl',
101 'imagelinks' => 'il',
102 'categorylinks' => 'cl',
103 'templatelinks' => 'tl',
104 'redirect' => 'rd',
106 if ( isset( $prefixes[$table] ) ) {
107 return $prefixes[$table];
108 } else {
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 );
118 switch ( $table ) {
119 case 'pagelinks':
120 case 'templatelinks':
121 case 'redirect':
122 $conds = array(
123 "{$prefix}_namespace" => $this->title->getNamespace(),
124 "{$prefix}_title" => $this->title->getDBkey(),
125 "page_id={$prefix}_from"
127 break;
128 case 'imagelinks':
129 $conds = array(
130 'il_to' => $this->title->getDBkey(),
131 'page_id=il_from'
133 break;
134 case 'categorylinks':
135 $conds = array(
136 'cl_to' => $this->title->getDBkey(),
137 'page_id=cl_from',
139 break;
140 default:
141 throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
143 return $conds;
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
168 * @return array
170 public function partition( $table, $batchSize ) {
171 // Try cache
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'];
185 // Try memcached
186 global $wgMemc;
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 );
198 // Save to memcached
199 $wgMemc->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
200 wfDebug( __METHOD__.": got from database\n" );
201 return $cacheEntry['batches'];
204 /**
205 * Partition a DB result with backlinks in it into batches
207 protected function partitionResult( $res, $batchSize ) {
208 $batches = array();
209 $numRows = $res->numRows();
210 $numBatches = ceil( $numRows / $batchSize );
211 for ( $i = 0; $i < $numBatches; $i++ ) {
212 if ( $i == 0 ) {
213 $start = false;
214 } else {
215 $rowNum = intval( $numRows * $i / $numBatches );
216 $res->seek( $rowNum );
217 $row = $res->fetchObject();
218 $start = $row->page_id;
220 if ( $i == $numBatches - 1 ) {
221 $end = false;
222 } else {
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 );