/usr/share/mediawiki-extensions/base/Polyglot/Polyglot.php is in mediawiki-extensions-base 2.5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | <?php
/**
* Polyglot extension - automatic redirects based on user language
*
* Features:
* * Magic redirects to localized page version
* * Interlanguage links in the sidebar point to localized local pages
*
* This can be combined with LanguageSelector and MultiLang to provide more internationalization support.
*
* See the README file for more information
*
* @package MediaWiki
* @subpackage Extensions
* @author Daniel Kinzler, brightbyte.de
* @copyright © 2007 Daniel Kinzler
* @licence GNU General Public Licence 2.0 or later
*/
if( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
$wgExtensionCredits['other'][] = array(
'name' => 'Polyglot',
'author' => 'Daniel Kinzler',
'url' => 'http://mediawiki.org/wiki/Extension:Polyglot',
'description' => 'Support for content in multiple languages in a single MediaWiki',
);
/**
* Set languages with polyglot support; applies to negotiation of interface language,
* and to lookups for loclaized pages.
* Set this to a small set of languages that are likely to be used on your site to
* improve performance. Leave NULL to allow all languages known to MediaWiki via
* $wgLanguageNames.
* If the LanguageSelector extension is installed, $wgLanguageSelectorLanguages is used
* as a fallback.
*/
$wgPolyglotLanguages = NULL;
/**
* Namespaces to excempt from polyglot support, with respect to automatic redirects.
* All "magic" namespaces are excempt per default. There should be no reason to change this.
* Note: internationalizing templates is best done on-page, using the MultiLang extension.
*/
$wfPolyglotExcemptNamespaces = array(NS_CATEGORY, NS_TEMPLATE, NS_IMAGE, NS_MEDIA, NS_SPECIAL, NS_MEDIAWIKI);
/**
* Wether talk pages should be excempt from automatic polyglot support, with respect to
* automatic redirects. True per default.
*/
$wfPolyglotExcemptTalkPages = true;
/**
* Set to true if polyglot should resolve redirects that are encountered when applying an
* automatic redirect to a localized page. This requires additional database access every
* time a locaized page is accessed.
*/
$wfPolyglotFollowRedirects = false;
///// hook it up /////////////////////////////////////////////////////
$wgHooks['ArticleFromTitle'][] = 'wfPolyglotArticleFromTitle';
$wgHooks['ParserAfterTidy'][] = 'wfPolyglotParserAfterTidy';
$wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'wfPolyglotSkinTemplateOutputPageBeforeExec';
$wgExtensionFunctions[] = "wfPolyglotExtension";
function wfPolyglotExtension() {
global $wgPolyglotLanguages;
if ( $wgPolyglotLanguages === NULL ) {
$wgPolyglotLanguages = @$GLOBALS['wgLanguageSelectorLanguages'];
}
if ( $wgPolyglotLanguages === NULL ) {
$wgPolyglotLanguages = array_keys( $GLOBALS['wgLanguageNames'] );
}
}
function wfPolyglotArticleFromTitle( &$title, &$article ) {
global $wfPolyglotExcemptNamespaces, $wfPolyglotExcemptTalkPages, $wfPolyglotFollowRedirects;
global $wgLang, $wgTitle, $wgRequest;
if ($wgRequest->getVal( 'redirect' ) == 'no') {
return true;
}
$ns = $title->getNamespace();
if ( $ns < 0
|| in_array($ns, $wfPolyglotExcemptNamespaces)
|| ($wfPolyglotExcemptTalkPages && MWNamespace::isTalk($ns)) ) {
return true;
}
$n = $title->getDBkey();
$nofollow = false;
//TODO: when user-defined language links start working (see below),
// we need to look at the langlinks table here.
if (!$title->exists() && strlen($n)>1 && preg_match('!/$!', $n)) {
$t = Title::makeTitle($ns, substr($n, 0, strlen($n)-1));
$nofollow = true;
}
else {
$lang = $wgLang->getCode();
$t = Title::makeTitle($ns, $n . '/' . $lang);
}
if (!$t->exists()) {
return true;
}
if ($wfPolyglotFollowRedirects && !$nofollow) {
$a = new Article($t);
$a->loadPageData();
if ($a->mIsRedirect) {
$rt = $a->followRedirect();
if ($rt && $rt->exists()) {
//TODO: make "redirected from" show $source, not $title, if we followed a redirect internally.
// there seems to be no clean way to do that, though.
//$source = $t;
$t = $rt;
}
}
}
if (!class_exists('PolyglotRedirect')) {
class PolyglotRedirect extends Article {
var $mTarget;
function __construct( $source, $target ) {
Article::__construct($source);
$this->mTarget = $target;
$this->mIsRedirect = true;
}
function followRedirect() {
return $this->mTarget;
}
function loadPageData( $data = 'fromdb' ) {
Article::loadPageData( $data );
$this->mIsRedirect = true;
}
}
}
//print $t->getFullText();
$article = new PolyglotRedirect( $title, $t ); //trigger redirect to lovcalized page
return true;
}
function wfPolyglotGetLanguages( $title ) {
global $wgPolyglotLanguages;
if (!$wgPolyglotLanguages) return NULL;
$n = $title->getDBkey();
$ns = $title->getNamespace();
$links = array();
foreach ($wgPolyglotLanguages as $lang) {
$t = Title::makeTitle($ns, $n . '/' . $lang);
if ($t->exists()) $links[$lang] = $t->getFullText();
}
return $links;
}
function wfPolyglotParserAfterTidy( &$parser, &$text ) {
global $wgPolyglotLanguages, $wfPolyglotExcemptNamespaces, $wfPolyglotExcemptTalkPages;
global $wgContLang;
if ( !$wgPolyglotLanguages ) return true;
if ( !$parser->mOptions->getInterwikiMagic() ) return true;
$n = $parser->mTitle->getDBkey();
$ns = $parser->mTitle->getNamespace();
$contln = $wgContLang->getCode();
$userlinks = $parser->mOutput->getLanguageLinks();
$links = array();
$pagelang = NULL;
//TODO: if we followed a redirect, analyze the redirect's title too.
// at least if wgPolyglotFollowRedirects is true
if ( $ns >= 0 && !in_array($ns, $wfPolyglotExcemptNamespaces)
&& (!$wfPolyglotExcemptTalkPages || !MWNamespace::isTalk($ns)) ) {
$ll = wfPolyglotGetLanguages($parser->mTitle);
if ($ll) $links = array_merge($links, $ll);
if (preg_match('!(.+)/(\w[-\w]*\w)$!', $n, $m)) {
$pagelang = $m[2];
$t = Title::makeTitle($ns, $m[1]);
if (!isset($links[$contln]) && $t->exists()) $links[$contln] = $t->getFullText() . '/';
$ll = wfPolyglotGetLanguages($t);
if ($ll) {
unset($ll[$pagelang]);
$links = array_merge($links, $ll);
}
}
}
//TODO: would be nice to handle "normal" interwiki-links here.
// but we would have to hack into Title::getInterwikiLink, otherwise
// the links are not recognized.
/*
foreach ($userlinks as $link) {
$m = explode(':', $link, 2);
if (sizeof($m)<2) continue;
$links[$m[0]] = $m[1];
}
*/
if ($pagelang) unset($links[$pagelang]);
//print_r($links);
$fakelinks = array();
foreach ($links as $lang => $t) {
$fakelinks[] = $lang . ':' . $t;
}
$parser->mOutput->setLanguageLinks($fakelinks);
return true;
}
function wfPolyglotSkinTemplateOutputPageBeforeExec($skin, $tpl) {
global $wgOut, $wgContLang;
$language_urls = array();
foreach( $wgOut->getLanguageLinks() as $l ) {
if (preg_match('!^(\w[-\w]*\w):(.+)$!', $l, $m)) {
$lang = $m[1];
$l = $m[2];
}
else {
continue; //NOTE: shouldn't happen
}
$nt = Title::newFromText( $l );
$language_urls[] = array(
'href' => $nt->getFullURL(),
'text' => $wgContLang->getLanguageName( $lang ),
'class' => 'interwiki-' . $lang,
);
}
if(count($language_urls)) {
$tpl->setRef( 'language_urls', $language_urls);
} else {
$tpl->set('language_urls', false);
}
return true;
}
|