]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/BacklinkCache.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.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 string $table
51          * @return TitleArray
52          */
53         public function getLinks( $table, $startId = false, $endId = false ) {
54                 wfProfileIn( __METHOD__ );
55
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 );
65                         }
66                         if ( $endId ) {
67                                 $conds[] = "$fromField <= " . intval( $endId );
68                         }
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;
78                 }
79
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;
89                 }
90                 $ta = TitleArray::newFromResult( $this->fullResultCache[$table] );
91                 wfProfileOut( __METHOD__ );
92                 return $ta;
93         }
94
95         /**
96          * Get the field name prefix for a given table
97          */
98         protected function getPrefix( $table ) {
99                 static $prefixes = array(
100                         'pagelinks' => 'pl',
101                         'imagelinks' => 'il',
102                         'categorylinks' => 'cl',
103                         'templatelinks' => 'tl',
104                         'redirect' => 'rd',
105                 );
106                 if ( isset( $prefixes[$table] ) ) {
107                         return $prefixes[$table];
108                 } else {
109                         throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
110                 }
111         }
112
113         /**
114          * Get the SQL condition array for selecting backlinks, with a join on the page table
115          */
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"
126                                 );
127                                 break;
128                         case 'imagelinks':
129                                 $conds = array( 
130                                         'il_to' => $this->title->getDBkey(),
131                                         'page_id=il_from'
132                                 );
133                                 break;
134                         case 'categorylinks':
135                                 $conds = array( 
136                                         'cl_to' => $this->title->getDBkey(),
137                                         'page_id=cl_from',
138                                 );
139                                 break;
140                         default:
141                                 throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
142                 }
143                 return $conds;
144         }
145
146         /**
147          * Get the approximate number of backlinks
148          */
149         public function getNumLinks( $table ) {
150                 if ( isset( $this->fullResultCache[$table] ) ) {
151                         return $this->fullResultCache[$table]->numRows();
152                 }
153                 if ( isset( $this->partitionCache[$table] ) ) {
154                         $entry = reset( $this->partitionCache[$table] );
155                         return $entry['numRows'];
156                 }
157                 $titleArray = $this->getLinks( $table );
158                 return $titleArray->count();
159         }
160
161         /**
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.
165          *
166          * @param string $table The links table name
167          * @param integer $batchSize
168          * @return array
169          */
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'];
175                 }
176                 $this->partitionCache[$table][$batchSize] = false;
177                 $cacheEntry =& $this->partitionCache[$table][$batchSize];
178
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'];
184                 }
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'];
194                 }
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'];
202         }
203
204         /** 
205          * Partition a DB result with backlinks in it into batches
206          */
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;
219                         }
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;
227                         }
228                         $batches[] = array( $start, $end );
229                 }
230                 return array( 'numRows' => $numRows, 'batches' => $batches );
231         }
232 }