Fatal error: Call to undefined function getInterwikiCacheEntry() in ./includes/Interw...
[lhc/web/wiklou.git] / includes / Interwiki.php
1 <?php
2 /**
3 * @file
4 * Interwiki table entry
5 */
6
7 /**
8 * The interwiki class
9 * All information is loaded on creation when called by Interwiki::fetch( $prefix ).
10 * All work is done on slave, because this should *never* change (except during schema updates etc, which arent wiki-related)
11 */
12 class Interwiki {
13
14 // Cache - removes oldest entry when it hits limit
15 protected static $smCache = array();
16 const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
17
18 protected $mPrefix, $mURL, $mLocal, $mTrans;
19
20 function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 )
21 {
22 $this->mPrefix = $prefix;
23 $this->mURL = $url;
24 $this->mLocal = $local;
25 $this->mTrans = $trans;
26 }
27
28 /**
29 * Check whether an interwiki prefix exists
30 *
31 * @return bool Whether it exists
32 * @param $prefix string Interwiki prefix to use
33 */
34 static public function isValidInterwiki( $prefix ){
35 $result = self::fetch( $prefix );
36 return (bool)$result;
37 }
38
39 /**
40 * Fetch an Interwiki object
41 *
42 * @return Interwiki Object, or null if not valid
43 * @param $prefix string Interwiki prefix to use
44 */
45 static public function fetch( $prefix ) {
46 global $wgContLang;
47 if( $prefix == '' ) {
48 return null;
49 }
50 $prefix = $wgContLang->lc( $prefix );
51 if( isset( self::$smCache[$prefix] ) ){
52 return self::$smCache[$prefix];
53 }
54 global $wgInterwikiCache;
55 if ($wgInterwikiCache) {
56 return Interwiki::getInterwikiCached( $key );
57 }
58 $iw = Interwiki::load( $prefix );
59 if( !$iw ){
60 $iw = false;
61 }
62 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){
63 reset( self::$smCache );
64 unset( self::$smCache[ key( self::$smCache ) ] );
65 }
66 self::$smCache[$prefix] = $iw;
67 return $iw;
68 }
69
70 /**
71 * Fetch interwiki prefix data from local cache in constant database.
72 *
73 * @note More logic is explained in DefaultSettings.
74 *
75 * @param $key \type{\string} Database key
76 * @return \type{\Interwiki} An interwiki object
77 */
78 protected static function getInterwikiCached( $key ) {
79 $value = self::getInterwikiCacheEntry( $key );
80
81 $s = new Interwiki( $key );
82 if ( $value != '' ) {
83 // Split values
84 list( $local, $url ) = explode( ' ', $value, 2 );
85 $s->mURL = $url;
86 $s->mLocal = (int)$local;
87 }else{
88 $s = false;
89 }
90 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){
91 reset( self::$smCache );
92 unset( self::$smCache[ key( self::$smCache ) ] );
93 }
94 self::$smCache[$prefix] = $s;
95 return $s;
96 }
97
98 /**
99 * Get entry from interwiki cache
100 *
101 * @note More logic is explained in DefaultSettings.
102 *
103 * @param $key \type{\string} Database key
104 * @return \type{\string) The entry
105 */
106 protected static function getInterwikiCacheEntry( $key ){
107 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
108 static $db, $site;
109
110 if( !$db ){
111 $db = dba_open( $wgInterwikiCache, 'r', 'cdb' );
112 }
113 /* Resolve site name */
114 if( $wgInterwikiScopes>=3 && !$site ) {
115 $site = dba_fetch( '__sites:' . wfWikiID(), $db );
116 if ( $site == "" ){
117 $site = $wgInterwikiFallbackSite;
118 }
119 }
120
121 $value = dba_fetch( wfMemcKey( $key ), $db );
122 // Site level
123 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
124 $value = dba_fetch( "_{$site}:{$key}", $db );
125 }
126 // Global Level
127 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
128 $value = dba_fetch( "__global:{$key}", $db );
129 }
130 if ( $value == 'undef' )
131 $value = '';
132
133 return $value;
134 }
135
136 /**
137 * Load the interwiki, trying first memcached then the DB
138 *
139 * @param $prefix The interwiki prefix
140 * @return bool The prefix is valid
141 * @static
142 *
143 */
144 protected static function load( $prefix ) {
145 global $wgMemc;
146 $key = wfMemcKey( 'interwiki', $prefix );
147 $mc = $wgMemc->get( $key );
148 $iw = false;
149 if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
150 $iw = Interwiki::loadFromArray( $mc );
151 if( $iw ){
152 return $iw;
153 }
154 }
155
156 $db = wfGetDB( DB_SLAVE );
157
158 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
159 __METHOD__ ) );
160 $iw = Interwiki::loadFromArray( $row );
161 if ( $iw ) {
162 $mc = array( 'iw_url' => $iw->mURL, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
163 $wgMemc->add( $key, $mc );
164 return $iw;
165 }
166
167 return false;
168 }
169
170 /**
171 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
172 *
173 * @return bool Whether everything was there
174 * @param $res ResultWrapper Row from the interwiki table
175 * @static
176 */
177 protected static function loadFromArray( $mc ) {
178 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ){
179 $iw = new Interwiki();
180 $iw->mURL = $mc['iw_url'];
181 $iw->mLocal = $mc['iw_local'];
182 $iw->mTrans = $mc['iw_trans'];
183 return $iw;
184 }
185 return false;
186 }
187
188 /**
189 * Get the URL for a particular title (or with $1 if no title given)
190 *
191 * @param $title string What text to put for the article name
192 * @return string The URL
193 */
194 function getURL( $title = null ){
195 $url = $this->mURL;
196 if( $title != null ){
197 $url = str_replace( "$1", $title, $url );
198 }
199 return $url;
200 }
201
202 function isLocal(){
203 return $this->mLocal;
204 }
205
206 function isTranscludable(){
207 return $this->mTrans;
208 }
209
210 }