MediaWiki  1.23.15
Interwiki.php
Go to the documentation of this file.
1 <?php
29 class Interwiki {
30  // Cache - removes oldest entry when it hits limit
31  protected static $smCache = array();
32  const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
33 
35  protected $mPrefix;
36 
38  protected $mURL;
39 
41  protected $mAPI;
42 
46  protected $mWikiID;
47 
49  protected $mLocal;
50 
52  protected $mTrans;
53 
54  public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0,
55  $trans = 0
56  ) {
57  $this->mPrefix = $prefix;
58  $this->mURL = $url;
59  $this->mAPI = $api;
60  $this->mWikiID = $wikiId;
61  $this->mLocal = $local;
62  $this->mTrans = $trans;
63  }
64 
71  public static function isValidInterwiki( $prefix ) {
72  $result = self::fetch( $prefix );
73 
74  return (bool)$result;
75  }
76 
83  public static function fetch( $prefix ) {
85 
86  if ( $prefix == '' ) {
87  return null;
88  }
89 
90  $prefix = $wgContLang->lc( $prefix );
91  if ( isset( self::$smCache[$prefix] ) ) {
92  return self::$smCache[$prefix];
93  }
94 
95  global $wgInterwikiCache;
96  if ( $wgInterwikiCache ) {
97  $iw = Interwiki::getInterwikiCached( $prefix );
98  } else {
99  $iw = Interwiki::load( $prefix );
100  if ( !$iw ) {
101  $iw = false;
102  }
103  }
104 
105  if ( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
106  reset( self::$smCache );
107  unset( self::$smCache[key( self::$smCache )] );
108  }
109 
110  self::$smCache[$prefix] = $iw;
111 
112  return $iw;
113  }
114 
123  protected static function getInterwikiCached( $prefix ) {
125 
126  $s = new Interwiki( $prefix );
127  if ( $value != '' ) {
128  // Split values
129  list( $local, $url ) = explode( ' ', $value, 2 );
130  $s->mURL = $url;
131  $s->mLocal = (int)$local;
132  } else {
133  $s = false;
134  }
135 
136  return $s;
137  }
138 
147  protected static function getInterwikiCacheEntry( $prefix ) {
148  global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
149  static $db, $site;
150 
151  wfDebug( __METHOD__ . "( $prefix )\n" );
152  $value = false;
153  try {
154  if ( !$db ) {
155  $db = CdbReader::open( $wgInterwikiCache );
156  }
157  /* Resolve site name */
158  if ( $wgInterwikiScopes >= 3 && !$site ) {
159  $site = $db->get( '__sites:' . wfWikiID() );
160  if ( $site == '' ) {
161  $site = $wgInterwikiFallbackSite;
162  }
163  }
164 
165  $value = $db->get( wfMemcKey( $prefix ) );
166  // Site level
167  if ( $value == '' && $wgInterwikiScopes >= 3 ) {
168  $value = $db->get( "_{$site}:{$prefix}" );
169  }
170  // Global Level
171  if ( $value == '' && $wgInterwikiScopes >= 2 ) {
172  $value = $db->get( "__global:{$prefix}" );
173  }
174  if ( $value == 'undef' ) {
175  $value = '';
176  }
177  } catch ( CdbException $e ) {
178  wfDebug( __METHOD__ . ": CdbException caught, error message was "
179  . $e->getMessage() );
180  }
181 
182  return $value;
183  }
184 
191  protected static function load( $prefix ) {
192  global $wgMemc, $wgInterwikiExpiry;
193 
194  $iwData = array();
195  if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
196  return Interwiki::loadFromArray( $iwData );
197  }
198 
199  if ( !$iwData ) {
200  $key = wfMemcKey( 'interwiki', $prefix );
201  $iwData = $wgMemc->get( $key );
202  if ( $iwData === '!NONEXISTENT' ) {
203  // negative cache hit
204  return false;
205  }
206  }
207 
208  // is_array is hack for old keys
209  if ( $iwData && is_array( $iwData ) ) {
210  $iw = Interwiki::loadFromArray( $iwData );
211  if ( $iw ) {
212  return $iw;
213  }
214  }
215 
216  $db = wfGetDB( DB_SLAVE );
217 
218  $row = $db->fetchRow( $db->select(
219  'interwiki',
220  self::selectFields(),
221  array( 'iw_prefix' => $prefix ),
222  __METHOD__
223  ) );
224 
225  $iw = Interwiki::loadFromArray( $row );
226  if ( $iw ) {
227  $mc = array(
228  'iw_url' => $iw->mURL,
229  'iw_api' => $iw->mAPI,
230  'iw_local' => $iw->mLocal,
231  'iw_trans' => $iw->mTrans
232  );
233  $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
234 
235  return $iw;
236  }
237 
238  // negative cache hit
239  $wgMemc->add( $key, '!NONEXISTENT', $wgInterwikiExpiry );
240 
241  return false;
242  }
243 
250  protected static function loadFromArray( $mc ) {
251  if ( isset( $mc['iw_url'] ) ) {
252  $iw = new Interwiki();
253  $iw->mURL = $mc['iw_url'];
254  $iw->mLocal = isset( $mc['iw_local'] ) ? $mc['iw_local'] : 0;
255  $iw->mTrans = isset( $mc['iw_trans'] ) ? $mc['iw_trans'] : 0;
256  $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
257  $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
258 
259  return $iw;
260  }
261 
262  return false;
263  }
264 
272  protected static function getAllPrefixesCached( $local ) {
273  global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
274  static $db, $site;
275 
276  wfDebug( __METHOD__ . "()\n" );
277  $data = array();
278  try {
279  if ( !$db ) {
280  $db = CdbReader::open( $wgInterwikiCache );
281  }
282  /* Resolve site name */
283  if ( $wgInterwikiScopes >= 3 && !$site ) {
284  $site = $db->get( '__sites:' . wfWikiID() );
285  if ( $site == '' ) {
286  $site = $wgInterwikiFallbackSite;
287  }
288  }
289 
290  // List of interwiki sources
291  $sources = array();
292  // Global Level
293  if ( $wgInterwikiScopes >= 2 ) {
294  $sources[] = '__global';
295  }
296  // Site level
297  if ( $wgInterwikiScopes >= 3 ) {
298  $sources[] = '_' . $site;
299  }
300  $sources[] = wfWikiID();
301 
302  foreach ( $sources as $source ) {
303  $list = $db->get( "__list:{$source}" );
304  foreach ( explode( ' ', $list ) as $iw_prefix ) {
305  $row = $db->get( "{$source}:{$iw_prefix}" );
306  if ( !$row ) {
307  continue;
308  }
309 
310  list( $iw_local, $iw_url ) = explode( ' ', $row );
311 
312  if ( $local !== null && $local != $iw_local ) {
313  continue;
314  }
315 
316  $data[$iw_prefix] = array(
317  'iw_prefix' => $iw_prefix,
318  'iw_url' => $iw_url,
319  'iw_local' => $iw_local,
320  );
321  }
322  }
323  } catch ( CdbException $e ) {
324  wfDebug( __METHOD__ . ": CdbException caught, error message was "
325  . $e->getMessage() );
326  }
327 
328  ksort( $data );
329 
330  return array_values( $data );
331  }
332 
340  protected static function getAllPrefixesDB( $local ) {
341  $db = wfGetDB( DB_SLAVE );
342 
343  $where = array();
344 
345  if ( $local !== null ) {
346  if ( $local == 1 ) {
347  $where['iw_local'] = 1;
348  } elseif ( $local == 0 ) {
349  $where['iw_local'] = 0;
350  }
351  }
352 
353  $res = $db->select( 'interwiki',
354  self::selectFields(),
355  $where, __METHOD__, array( 'ORDER BY' => 'iw_prefix' )
356  );
357 
358  $retval = array();
359  foreach ( $res as $row ) {
360  $retval[] = (array)$row;
361  }
362 
363  return $retval;
364  }
365 
373  public static function getAllPrefixes( $local = null ) {
374  global $wgInterwikiCache;
375 
376  if ( $wgInterwikiCache ) {
377  return self::getAllPrefixesCached( $local );
378  }
379 
380  return self::getAllPrefixesDB( $local );
381  }
382 
392  public function getURL( $title = null ) {
393  $url = $this->mURL;
394  if ( $title !== null ) {
395  $url = str_replace( "$1", wfUrlencode( $title ), $url );
396  }
397 
398  return $url;
399  }
400 
406  public function getAPI() {
407  return $this->mAPI;
408  }
409 
415  public function getWikiID() {
416  return $this->mWikiID;
417  }
418 
425  public function isLocal() {
426  return $this->mLocal;
427  }
428 
435  public function isTranscludable() {
436  return $this->mTrans;
437  }
438 
444  public function getName() {
445  $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
446 
447  return !$msg->exists() ? '' : $msg;
448  }
449 
455  public function getDescription() {
456  $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
457 
458  return !$msg->exists() ? '' : $msg;
459  }
460 
466  public static function selectFields() {
467  return array(
468  'iw_prefix',
469  'iw_url',
470  'iw_api',
471  'iw_wikiid',
472  'iw_local',
473  'iw_trans'
474  );
475  }
476 }
Interwiki\load
static load( $prefix)
Load the interwiki, trying first memcached then the DB.
Definition: Interwiki.php:185
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
Interwiki\getAllPrefixesCached
static getAllPrefixesCached( $local)
Fetch all interwiki prefixes from interwiki cache.
Definition: Interwiki.php:266
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Interwiki\CACHE_LIMIT
const CACHE_LIMIT
Definition: Interwiki.php:32
Interwiki\$mAPI
string $mAPI
The URL of the file api.php *.
Definition: Interwiki.php:38
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3714
Interwiki\$mURL
string $mURL
The URL of the wiki, with "$1" as a placeholder for an article name.
Definition: Interwiki.php:36
Interwiki\selectFields
static selectFields()
Return the list of interwiki fields that should be selected to create a new Interwiki object.
Definition: Interwiki.php:460
wfUrlencode
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Definition: GlobalFunctions.php:377
Interwiki\getAllPrefixes
static getAllPrefixes( $local=null)
Returns all interwiki prefixes.
Definition: Interwiki.php:367
Interwiki\fetch
static fetch( $prefix)
Fetch an Interwiki object.
Definition: Interwiki.php:77
$s
$s
Definition: mergeMessageFileList.php:156
Interwiki\$smCache
static $smCache
Definition: Interwiki.php:31
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
Interwiki\getWikiID
getWikiID()
Get the DB name for this wiki.
Definition: Interwiki.php:409
Interwiki\getAllPrefixesDB
static getAllPrefixesDB( $local)
Fetch all interwiki prefixes from DB.
Definition: Interwiki.php:334
Interwiki\$mPrefix
string $mPrefix
The interwiki prefix, (e.g.
Definition: Interwiki.php:34
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3635
Interwiki\__construct
__construct( $prefix=null, $url='', $api='', $wikiId='', $local=0, $trans=0)
Definition: Interwiki.php:48
wfMessage
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4066
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
Interwiki\getName
getName()
Get the name for the interwiki site.
Definition: Interwiki.php:438
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:980
Interwiki\$mWikiID
string $mWikiID
The name of the database (for a connection to be established with wfGetLB( 'wikiid' ))
Definition: Interwiki.php:42
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3668
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$value
$value
Definition: styleTest.css.php:45
Interwiki\$mTrans
bool $mTrans
Whether interwiki transclusions are allowed *.
Definition: Interwiki.php:46
Interwiki\getInterwikiCached
static getInterwikiCached( $prefix)
Fetch interwiki prefix data from local cache in constant database.
Definition: Interwiki.php:117
Interwiki\isLocal
isLocal()
Is this a local link from a sister project, or is it something outside, like Google.
Definition: Interwiki.php:419
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
CdbReader\open
static open( $fileName)
Open a file and return a subclass instance.
Definition: Cdb.php:41
Interwiki\getInterwikiCacheEntry
static getInterwikiCacheEntry( $prefix)
Get entry from interwiki cache.
Definition: Interwiki.php:141
CdbException
Exception for Cdb errors.
Definition: Cdb.php:159
Interwiki
The interwiki class All information is loaded on creation when called by Interwiki::fetch( $prefix ).
Definition: Interwiki.php:29
Interwiki\getAPI
getAPI()
Get the API URL for this wiki.
Definition: Interwiki.php:400
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$source
if(PHP_SAPI !='cli') $source
Definition: mwdoc-filter.php:18
Interwiki\isTranscludable
isTranscludable()
Can pages from this wiki be transcluded? Still requires $wgEnableScaryTransclusion.
Definition: Interwiki.php:429
$res
$res
Definition: database.txt:21
Interwiki\getDescription
getDescription()
Get a description for this interwiki.
Definition: Interwiki.php:449
$retval
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account incomplete not yet checked for validity & $retval
Definition: hooks.txt:237
Interwiki\loadFromArray
static loadFromArray( $mc)
Fill in member variables from an array (e.g.
Definition: Interwiki.php:244
Interwiki\getURL
getURL( $title=null)
Get the URL for a particular title (or with $1 if no title given)
Definition: Interwiki.php:386
Interwiki\isValidInterwiki
static isValidInterwiki( $prefix)
Check whether an interwiki prefix exists.
Definition: Interwiki.php:65
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:1632
Interwiki\$mLocal
bool $mLocal
whether the wiki is in this project *
Definition: Interwiki.php:44