r86746 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86745‎ | r86746 | r86747 >
Date:02:35, 23 April 2011
Author:dantman
Status:ok (Comments)
Tags:
Comment:
Move the Interwiki class to includes/interwiki/ to make way for a more flexible Interwiki system. Do this first since svn doesn't support editing and moving a file in the same commit.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/Interwiki.php (deleted) (history)
  • /trunk/phase3/includes/interwiki (added) (history)
  • /trunk/phase3/includes/interwiki/Interwiki.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/Interwiki.php
@@ -1,272 +0,0 @@
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
12 - * schema updates etc, which aren't wiki-related)
13 - */
14 -class Interwiki {
15 -
16 - // Cache - removes oldest entry when it hits limit
17 - protected static $smCache = array();
18 - const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
19 -
20 - protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
21 -
22 - public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
23 - $this->mPrefix = $prefix;
24 - $this->mURL = $url;
25 - $this->mAPI = $api;
26 - $this->mWikiID = $wikiId;
27 - $this->mLocal = $local;
28 - $this->mTrans = $trans;
29 - }
30 -
31 - /**
32 - * Check whether an interwiki prefix exists
33 - *
34 - * @param $prefix String: interwiki prefix to use
35 - * @return Boolean: whether it exists
36 - */
37 - static public function isValidInterwiki( $prefix ) {
38 - $result = self::fetch( $prefix );
39 - return (bool)$result;
40 - }
41 -
42 - /**
43 - * Fetch an Interwiki object
44 - *
45 - * @param $prefix String: interwiki prefix to use
46 - * @return Interwiki Object, or null if not valid
47 - */
48 - static public function fetch( $prefix ) {
49 - global $wgContLang;
50 - if( $prefix == '' ) {
51 - return null;
52 - }
53 - $prefix = $wgContLang->lc( $prefix );
54 - if( isset( self::$smCache[$prefix] ) ) {
55 - return self::$smCache[$prefix];
56 - }
57 - global $wgInterwikiCache;
58 - if( $wgInterwikiCache ) {
59 - $iw = Interwiki::getInterwikiCached( $prefix );
60 - } else {
61 - $iw = Interwiki::load( $prefix );
62 - if( !$iw ) {
63 - $iw = false;
64 - }
65 - }
66 - if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
67 - reset( self::$smCache );
68 - unset( self::$smCache[key( self::$smCache )] );
69 - }
70 - self::$smCache[$prefix] = $iw;
71 - return $iw;
72 - }
73 -
74 - /**
75 - * Fetch interwiki prefix data from local cache in constant database.
76 - *
77 - * @note More logic is explained in DefaultSettings.
78 - *
79 - * @param $prefix String: interwiki prefix
80 - * @return Interwiki object
81 - */
82 - protected static function getInterwikiCached( $prefix ) {
83 - $value = self::getInterwikiCacheEntry( $prefix );
84 -
85 - $s = new Interwiki( $prefix );
86 - if ( $value != '' ) {
87 - // Split values
88 - list( $local, $url ) = explode( ' ', $value, 2 );
89 - $s->mURL = $url;
90 - $s->mLocal = (int)$local;
91 - } else {
92 - $s = false;
93 - }
94 - return $s;
95 - }
96 -
97 - /**
98 - * Get entry from interwiki cache
99 - *
100 - * @note More logic is explained in DefaultSettings.
101 - *
102 - * @param $prefix String: database key
103 - * @return String: the entry
104 - */
105 - protected static function getInterwikiCacheEntry( $prefix ) {
106 - global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
107 - static $db, $site;
108 -
109 - wfDebug( __METHOD__ . "( $prefix )\n" );
110 - if( !$db ) {
111 - $db = CdbReader::open( $wgInterwikiCache );
112 - }
113 - /* Resolve site name */
114 - if( $wgInterwikiScopes >= 3 && !$site ) {
115 - $site = $db->get( '__sites:' . wfWikiID() );
116 - if ( $site == '' ) {
117 - $site = $wgInterwikiFallbackSite;
118 - }
119 - }
120 -
121 - $value = $db->get( wfMemcKey( $prefix ) );
122 - // Site level
123 - if ( $value == '' && $wgInterwikiScopes >= 3 ) {
124 - $value = $db->get( "_{$site}:{$prefix}" );
125 - }
126 - // Global Level
127 - if ( $value == '' && $wgInterwikiScopes >= 2 ) {
128 - $value = $db->get( "__global:{$prefix}" );
129 - }
130 - if ( $value == 'undef' ) {
131 - $value = '';
132 - }
133 -
134 - return $value;
135 - }
136 -
137 - /**
138 - * Load the interwiki, trying first memcached then the DB
139 - *
140 - * @param $prefix The interwiki prefix
141 - * @return Boolean: the prefix is valid
142 - */
143 - protected static function load( $prefix ) {
144 - global $wgMemc, $wgInterwikiExpiry;
145 -
146 - $iwData = false;
147 - if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
148 - return Interwiki::loadFromArray( $iwData );
149 - }
150 -
151 - if ( !$iwData ) {
152 - $key = wfMemcKey( 'interwiki', $prefix );
153 - $iwData = $wgMemc->get( $key );
154 - }
155 -
156 - if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
157 - $iw = Interwiki::loadFromArray( $iwData );
158 - if( $iw ) {
159 - return $iw;
160 - }
161 - }
162 -
163 - $db = wfGetDB( DB_SLAVE );
164 -
165 - $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
166 - __METHOD__ ) );
167 - $iw = Interwiki::loadFromArray( $row );
168 - if ( $iw ) {
169 - $mc = array(
170 - 'iw_url' => $iw->mURL,
171 - 'iw_api' => $iw->mAPI,
172 - 'iw_local' => $iw->mLocal,
173 - 'iw_trans' => $iw->mTrans
174 - );
175 - $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
176 - return $iw;
177 - }
178 -
179 - return false;
180 - }
181 -
182 - /**
183 - * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
184 - *
185 - * @param $mc Associative array: row from the interwiki table
186 - * @return Boolean: whether everything was there
187 - */
188 - protected static function loadFromArray( $mc ) {
189 - if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
190 - $iw = new Interwiki();
191 - $iw->mURL = $mc['iw_url'];
192 - $iw->mLocal = $mc['iw_local'];
193 - $iw->mTrans = $mc['iw_trans'];
194 - $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
195 - $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
196 -
197 - return $iw;
198 - }
199 - return false;
200 - }
201 -
202 - /**
203 - * Get the URL for a particular title (or with $1 if no title given)
204 - *
205 - * @param $title String: what text to put for the article name
206 - * @return String: the URL
207 - */
208 - public function getURL( $title = null ) {
209 - $url = $this->mURL;
210 - if( $title != null ) {
211 - $url = str_replace( "$1", $title, $url );
212 - }
213 - return $url;
214 - }
215 -
216 - /**
217 - * Get the API URL for this wiki
218 - *
219 - * @return String: the URL
220 - */
221 - public function getAPI() {
222 - return $this->mAPI;
223 - }
224 -
225 - /**
226 - * Get the DB name for this wiki
227 - *
228 - * @return String: the DB name
229 - */
230 - public function getWikiID() {
231 - return $this->mWikiID;
232 - }
233 -
234 - /**
235 - * Is this a local link from a sister project, or is
236 - * it something outside, like Google
237 - *
238 - * @return Boolean
239 - */
240 - public function isLocal() {
241 - return $this->mLocal;
242 - }
243 -
244 - /**
245 - * Can pages from this wiki be transcluded?
246 - * Still requires $wgEnableScaryTransclusion
247 - *
248 - * @return Boolean
249 - */
250 - public function isTranscludable() {
251 - return $this->mTrans;
252 - }
253 -
254 - /**
255 - * Get the name for the interwiki site
256 - *
257 - * @return String
258 - */
259 - public function getName() {
260 - $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
261 - return !$msg->exists() ? '' : $msg;
262 - }
263 -
264 - /**
265 - * Get a description for this interwiki
266 - *
267 - * @return String
268 - */
269 - public function getDescription() {
270 - $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
271 - return !$msg->exists() ? '' : $msg;
272 - }
273 -}
Index: trunk/phase3/includes/interwiki/Interwiki.php
@@ -0,0 +1,272 @@
 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
 12+ * schema updates etc, which aren't wiki-related)
 13+ */
 14+class Interwiki {
 15+
 16+ // Cache - removes oldest entry when it hits limit
 17+ protected static $smCache = array();
 18+ const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
 19+
 20+ protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
 21+
 22+ public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
 23+ $this->mPrefix = $prefix;
 24+ $this->mURL = $url;
 25+ $this->mAPI = $api;
 26+ $this->mWikiID = $wikiId;
 27+ $this->mLocal = $local;
 28+ $this->mTrans = $trans;
 29+ }
 30+
 31+ /**
 32+ * Check whether an interwiki prefix exists
 33+ *
 34+ * @param $prefix String: interwiki prefix to use
 35+ * @return Boolean: whether it exists
 36+ */
 37+ static public function isValidInterwiki( $prefix ) {
 38+ $result = self::fetch( $prefix );
 39+ return (bool)$result;
 40+ }
 41+
 42+ /**
 43+ * Fetch an Interwiki object
 44+ *
 45+ * @param $prefix String: interwiki prefix to use
 46+ * @return Interwiki Object, or null if not valid
 47+ */
 48+ static public function fetch( $prefix ) {
 49+ global $wgContLang;
 50+ if( $prefix == '' ) {
 51+ return null;
 52+ }
 53+ $prefix = $wgContLang->lc( $prefix );
 54+ if( isset( self::$smCache[$prefix] ) ) {
 55+ return self::$smCache[$prefix];
 56+ }
 57+ global $wgInterwikiCache;
 58+ if( $wgInterwikiCache ) {
 59+ $iw = Interwiki::getInterwikiCached( $prefix );
 60+ } else {
 61+ $iw = Interwiki::load( $prefix );
 62+ if( !$iw ) {
 63+ $iw = false;
 64+ }
 65+ }
 66+ if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
 67+ reset( self::$smCache );
 68+ unset( self::$smCache[key( self::$smCache )] );
 69+ }
 70+ self::$smCache[$prefix] = $iw;
 71+ return $iw;
 72+ }
 73+
 74+ /**
 75+ * Fetch interwiki prefix data from local cache in constant database.
 76+ *
 77+ * @note More logic is explained in DefaultSettings.
 78+ *
 79+ * @param $prefix String: interwiki prefix
 80+ * @return Interwiki object
 81+ */
 82+ protected static function getInterwikiCached( $prefix ) {
 83+ $value = self::getInterwikiCacheEntry( $prefix );
 84+
 85+ $s = new Interwiki( $prefix );
 86+ if ( $value != '' ) {
 87+ // Split values
 88+ list( $local, $url ) = explode( ' ', $value, 2 );
 89+ $s->mURL = $url;
 90+ $s->mLocal = (int)$local;
 91+ } else {
 92+ $s = false;
 93+ }
 94+ return $s;
 95+ }
 96+
 97+ /**
 98+ * Get entry from interwiki cache
 99+ *
 100+ * @note More logic is explained in DefaultSettings.
 101+ *
 102+ * @param $prefix String: database key
 103+ * @return String: the entry
 104+ */
 105+ protected static function getInterwikiCacheEntry( $prefix ) {
 106+ global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
 107+ static $db, $site;
 108+
 109+ wfDebug( __METHOD__ . "( $prefix )\n" );
 110+ if( !$db ) {
 111+ $db = CdbReader::open( $wgInterwikiCache );
 112+ }
 113+ /* Resolve site name */
 114+ if( $wgInterwikiScopes >= 3 && !$site ) {
 115+ $site = $db->get( '__sites:' . wfWikiID() );
 116+ if ( $site == '' ) {
 117+ $site = $wgInterwikiFallbackSite;
 118+ }
 119+ }
 120+
 121+ $value = $db->get( wfMemcKey( $prefix ) );
 122+ // Site level
 123+ if ( $value == '' && $wgInterwikiScopes >= 3 ) {
 124+ $value = $db->get( "_{$site}:{$prefix}" );
 125+ }
 126+ // Global Level
 127+ if ( $value == '' && $wgInterwikiScopes >= 2 ) {
 128+ $value = $db->get( "__global:{$prefix}" );
 129+ }
 130+ if ( $value == 'undef' ) {
 131+ $value = '';
 132+ }
 133+
 134+ return $value;
 135+ }
 136+
 137+ /**
 138+ * Load the interwiki, trying first memcached then the DB
 139+ *
 140+ * @param $prefix The interwiki prefix
 141+ * @return Boolean: the prefix is valid
 142+ */
 143+ protected static function load( $prefix ) {
 144+ global $wgMemc, $wgInterwikiExpiry;
 145+
 146+ $iwData = false;
 147+ if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
 148+ return Interwiki::loadFromArray( $iwData );
 149+ }
 150+
 151+ if ( !$iwData ) {
 152+ $key = wfMemcKey( 'interwiki', $prefix );
 153+ $iwData = $wgMemc->get( $key );
 154+ }
 155+
 156+ if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
 157+ $iw = Interwiki::loadFromArray( $iwData );
 158+ if( $iw ) {
 159+ return $iw;
 160+ }
 161+ }
 162+
 163+ $db = wfGetDB( DB_SLAVE );
 164+
 165+ $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
 166+ __METHOD__ ) );
 167+ $iw = Interwiki::loadFromArray( $row );
 168+ if ( $iw ) {
 169+ $mc = array(
 170+ 'iw_url' => $iw->mURL,
 171+ 'iw_api' => $iw->mAPI,
 172+ 'iw_local' => $iw->mLocal,
 173+ 'iw_trans' => $iw->mTrans
 174+ );
 175+ $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
 176+ return $iw;
 177+ }
 178+
 179+ return false;
 180+ }
 181+
 182+ /**
 183+ * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
 184+ *
 185+ * @param $mc Associative array: row from the interwiki table
 186+ * @return Boolean: whether everything was there
 187+ */
 188+ protected static function loadFromArray( $mc ) {
 189+ if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
 190+ $iw = new Interwiki();
 191+ $iw->mURL = $mc['iw_url'];
 192+ $iw->mLocal = $mc['iw_local'];
 193+ $iw->mTrans = $mc['iw_trans'];
 194+ $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
 195+ $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
 196+
 197+ return $iw;
 198+ }
 199+ return false;
 200+ }
 201+
 202+ /**
 203+ * Get the URL for a particular title (or with $1 if no title given)
 204+ *
 205+ * @param $title String: what text to put for the article name
 206+ * @return String: the URL
 207+ */
 208+ public function getURL( $title = null ) {
 209+ $url = $this->mURL;
 210+ if( $title != null ) {
 211+ $url = str_replace( "$1", $title, $url );
 212+ }
 213+ return $url;
 214+ }
 215+
 216+ /**
 217+ * Get the API URL for this wiki
 218+ *
 219+ * @return String: the URL
 220+ */
 221+ public function getAPI() {
 222+ return $this->mAPI;
 223+ }
 224+
 225+ /**
 226+ * Get the DB name for this wiki
 227+ *
 228+ * @return String: the DB name
 229+ */
 230+ public function getWikiID() {
 231+ return $this->mWikiID;
 232+ }
 233+
 234+ /**
 235+ * Is this a local link from a sister project, or is
 236+ * it something outside, like Google
 237+ *
 238+ * @return Boolean
 239+ */
 240+ public function isLocal() {
 241+ return $this->mLocal;
 242+ }
 243+
 244+ /**
 245+ * Can pages from this wiki be transcluded?
 246+ * Still requires $wgEnableScaryTransclusion
 247+ *
 248+ * @return Boolean
 249+ */
 250+ public function isTranscludable() {
 251+ return $this->mTrans;
 252+ }
 253+
 254+ /**
 255+ * Get the name for the interwiki site
 256+ *
 257+ * @return String
 258+ */
 259+ public function getName() {
 260+ $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
 261+ return !$msg->exists() ? '' : $msg;
 262+ }
 263+
 264+ /**
 265+ * Get a description for this interwiki
 266+ *
 267+ * @return String
 268+ */
 269+ public function getDescription() {
 270+ $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
 271+ return !$msg->exists() ? '' : $msg;
 272+ }
 273+}
Property changes on: trunk/phase3/includes/interwiki/Interwiki.php
___________________________________________________________________
Added: svn:mergeinfo
1274 Merged /branches/REL1_15/phase3/includes/Interwiki.php:r51646
2275 Merged /branches/sqlite/includes/Interwiki.php:r58211-58321
3276 Merged /branches/iwtransclusion/phase3/includes/Interwiki.php:r68170,68448,69480,69541
4277 Merged /branches/new-installer/phase3/includes/Interwiki.php:r43664-66004
5278 Merged /branches/wmf-deployment/includes/Interwiki.php:r53381
Added: svn:eol-style
6279 + native
Index: trunk/phase3/includes/AutoLoader.php
@@ -133,7 +133,7 @@
134134 'ImportStringSource' => 'includes/Import.php',
135135 'IncludableSpecialPage' => 'includes/SpecialPage.php',
136136 'IndexPager' => 'includes/Pager.php',
137 - 'Interwiki' => 'includes/Interwiki.php',
 137+ 'Interwiki' => 'includes/interwiki/Interwiki.php',
138138 'IP' => 'includes/IP.php',
139139 'LCStore_CDB' => 'includes/LocalisationCache.php',
140140 'LCStore_DB' => 'includes/LocalisationCache.php',

Comments

#Comment by Reedy (talk | contribs)   10:43, 23 April 2011

SVN will let you move and edit in the same commit

Just makes reviewing it much harder

Status & tagging log