Fixed some doxygen warnings
[lhc/web/wiklou.git] / includes / BacklinkCache.php
1 <?php
2
3 /**
4 * Class for fetching backlink lists, approximate backlink counts and partitions.
5 * Instances of this class should typically be fetched with $title->getBacklinkCache().
6 *
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;
15
16 const CACHE_EXPIRY = 3600;
17
18 /**
19 * Create a new BacklinkCache
20 */
21 function __construct( $title ) {
22 $this->title = $title;
23 }
24
25 /**
26 * Clear locally stored data
27 */
28 function clear() {
29 $this->partitionCache = array();
30 $this->fullResultCache = array();
31 unset( $this->db );
32 }
33
34 /**
35 * Set the Database object to use
36 */
37 public function setDB( $db ) {
38 $this->db = $db;
39 }
40
41 protected function getDB() {
42 if ( !isset( $this->db ) ) {
43 $this->db = wfGetDB( DB_SLAVE );
44 }
45 return $this->db;
46 }
47
48 /**
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
53 * @return TitleArray
54 */
55 public function getLinks( $table, $startId = false, $endId = false ) {
56 wfProfileIn( __METHOD__ );
57
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.
65 if ( $startId ) {
66 $conds[] = "$fromField >= " . intval( $startId );
67 }
68 if ( $endId ) {
69 $conds[] = "$fromField <= " . intval( $endId );
70 }
71 $res = $this->getDB()->select(
72 array( $table, 'page' ),
73 array( 'page_namespace', 'page_title', 'page_id'),
74 $conds,
75 __METHOD__,
76 array(
77 'STRAIGHT_JOIN',
78 'ORDER BY' => $fromField
79 ) );
80 $ta = TitleArray::newFromResult( $res );
81 wfProfileOut( __METHOD__ );
82 return $ta;
83 }
84
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 ),
91 __METHOD__,
92 array(
93 'STRAIGHT_JOIN',
94 'ORDER BY' => $fromField,
95 ) );
96 $this->fullResultCache[$table] = $res;
97 }
98 $ta = TitleArray::newFromResult( $this->fullResultCache[$table] );
99 wfProfileOut( __METHOD__ );
100 return $ta;
101 }
102
103 /**
104 * Get the field name prefix for a given table
105 */
106 protected function getPrefix( $table ) {
107 static $prefixes = array(
108 'pagelinks' => 'pl',
109 'imagelinks' => 'il',
110 'categorylinks' => 'cl',
111 'templatelinks' => 'tl',
112 'redirect' => 'rd',
113 );
114 if ( isset( $prefixes[$table] ) ) {
115 return $prefixes[$table];
116 } else {
117 throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
118 }
119 }
120
121 /**
122 * Get the SQL condition array for selecting backlinks, with a join on the page table
123 */
124 protected function getConditions( $table ) {
125 $prefix = $this->getPrefix( $table );
126 switch ( $table ) {
127 case 'pagelinks':
128 case 'templatelinks':
129 case 'redirect':
130 $conds = array(
131 "{$prefix}_namespace" => $this->title->getNamespace(),
132 "{$prefix}_title" => $this->title->getDBkey(),
133 "page_id={$prefix}_from"
134 );
135 break;
136 case 'imagelinks':
137 $conds = array(
138 'il_to' => $this->title->getDBkey(),
139 'page_id=il_from'
140 );
141 break;
142 case 'categorylinks':
143 $conds = array(
144 'cl_to' => $this->title->getDBkey(),
145 'page_id=cl_from',
146 );
147 break;
148 default:
149 throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
150 }
151 return $conds;
152 }
153
154 /**
155 * Get the approximate number of backlinks
156 */
157 public function getNumLinks( $table ) {
158 if ( isset( $this->fullResultCache[$table] ) ) {
159 return $this->fullResultCache[$table]->numRows();
160 }
161 if ( isset( $this->partitionCache[$table] ) ) {
162 $entry = reset( $this->partitionCache[$table] );
163 return $entry['numRows'];
164 }
165 $titleArray = $this->getLinks( $table );
166 return $titleArray->count();
167 }
168
169 /**
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.
173 *
174 * @param $table String: the links table name
175 * @param $batchSize Integer
176 * @return Array
177 */
178 public function partition( $table, $batchSize ) {
179 // Try cache
180 if ( isset( $this->partitionCache[$table][$batchSize] ) ) {
181 wfDebug( __METHOD__.": got from partition cache\n" );
182 return $this->partitionCache[$table][$batchSize]['batches'];
183 }
184 $this->partitionCache[$table][$batchSize] = false;
185 $cacheEntry =& $this->partitionCache[$table][$batchSize];
186
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'];
192 }
193 // Try memcached
194 global $wgMemc;
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'];
202 }
203 // Fetch from database
204 $this->getLinks( $table );
205 $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );
206 // Save to memcached
207 $wgMemc->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
208 wfDebug( __METHOD__.": got from database\n" );
209 return $cacheEntry['batches'];
210 }
211
212 /**
213 * Partition a DB result with backlinks in it into batches
214 */
215 protected function partitionResult( $res, $batchSize ) {
216 $batches = array();
217 $numRows = $res->numRows();
218 $numBatches = ceil( $numRows / $batchSize );
219 for ( $i = 0; $i < $numBatches; $i++ ) {
220 if ( $i == 0 ) {
221 $start = false;
222 } else {
223 $rowNum = intval( $numRows * $i / $numBatches );
224 $res->seek( $rowNum );
225 $row = $res->fetchObject();
226 $start = $row->page_id;
227 }
228 if ( $i == $numBatches - 1 ) {
229 $end = false;
230 } else {
231 $rowNum = intval( $numRows * ( $i + 1 ) / $numBatches );
232 $res->seek( $rowNum );
233 $row = $res->fetchObject();
234 $end = $row->page_id - 1;
235 }
236
237 # Sanity check order
238 if ( $start && $end && $start > $end ) {
239 throw new MWException( __METHOD__.': Internal error: query result out of order' );
240 }
241
242 $batches[] = array( $start, $end );
243 }
244 return array( 'numRows' => $numRows, 'batches' => $batches );
245 }
246 }