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