/usr/share/mediawiki-extensions/base/Polyglot/Polyglot.php is in mediawiki-extensions-base 3.6.
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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | <?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
*
* @file
* @ingroup 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(
'path' => __FILE__,
'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
* languages/Names.php.
* 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['InitializeArticleMaybeRedirect'][] = 'wfPolyglotInitializeArticleMaybeRedirect';
$wgHooks['LinkBegin'][] = 'wfPolyglotLinkBegin';
$wgHooks['ParserAfterTidy'][] = 'wfPolyglotParserAfterTidy';
$wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'wfPolyglotSkinTemplateOutputPageBeforeExec';
$wgExtensionFunctions[] = "wfPolyglotExtension";
function wfPolyglotExtension() {
global $wgPolyglotLanguages;
if ( $wgPolyglotLanguages === null ) {
$wgPolyglotLanguages = @$GLOBALS['wgLanguageSelectorLanguages'];
}
if ( $wgPolyglotLanguages === null ) {
$wgPolyglotLanguages = array_keys( Language::getLanguageNames() );
}
}
function wfPolyglotInitializeArticleMaybeRedirect( &$title, &$request, &$ignoreRedirect, &$target, &$article ) {
global $wfPolyglotExcemptNamespaces, $wfPolyglotExcemptTalkPages, $wfPolyglotFollowRedirects;
global $wgLang, $wgContLang;
$ns = $title->getNamespace();
if ( $ns < 0 || in_array( $ns, $wfPolyglotExcemptNamespaces )
|| ( $wfPolyglotExcemptTalkPages && MWNamespace::isTalk( $ns ) ) ) {
return true;
}
$dbkey = $title->getDBkey();
$force = false;
//TODO: when user-defined language links start working (see below),
// we need to look at the langlinks table here.
if ( !$title->exists() && strlen( $dbkey ) > 1 ) {
$escContLang = preg_quote( $wgContLang->getCode(), '!' );
if ( preg_match( '!/$!', $dbkey ) ) {
$force = true;
$remove = 1;
} elseif ( preg_match( "!/{$escContLang}$!", $dbkey ) ) {
$force = true;
$remove = strlen( $wgContLang->getCode() ) + 1;
}
}
if ( $force ) {
$t = Title::makeTitle( $ns, substr( $dbkey, 0, strlen( $dbkey ) - $remove ) );
} else {
$lang = $wgLang->getCode();
$t = Title::makeTitle( $ns, $dbkey . '/' . $lang );
}
if ( !$t->exists() ) {
return true;
}
if ( $wfPolyglotFollowRedirects && !$force ) {
$page = WikiPage::factory( $t );
if ( $page->isRedirect() ) {
$rt = $page->getRedirectTarget();
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;
}
}
}
$target = $t;
return true;
}
function wfPolyglotLinkBegin( $linker, $target, &$text, &$customAttribs, &$query, &$options, &$ret ) {
global $wfPolyglotExcemptNamespaces, $wfPolyglotExcemptTalkPages, $wgContLang;
$ns = $target->getNamespace();
if ( $ns < 0
|| in_array( $ns, $wfPolyglotExcemptNamespaces )
|| ( $wfPolyglotExcemptTalkPages && MWNamespace::isTalk( $ns ) ) ) {
return true;
}
$dbKey = $target->getDBkey();
if ( !$target->exists() && strlen( $dbKey ) > 1 ) {
$escContLang = preg_quote( $wgContLang->getCode(), '!' );
if ( preg_match( '!/$!', $dbKey ) ) {
$remove = 1;
} elseif ( preg_match( "!/{$escContLang}$!", $dbKey ) ) {
$remove = strlen( $wgContLang->getCode() ) + 1;
} else {
return true;
}
} else {
return true;
}
$t = Title::makeTitle( $ns, substr( $dbKey, 0, strlen( $dbKey ) - $remove ) );
if ( $t->exists() ) {
foreach( $options as $key => $val ) {
if ( $val === 'broken' ) {
unset( $options[$key] );
}
}
$options[] = 'known';
}
return true;
}
function wfPolyglotGetLanguages( $title ) {
global $wgPolyglotLanguages;
if (!$wgPolyglotLanguages) return null;
$n = $title->getDBkey();
$ns = $title->getNamespace();
$titles = array();
$batch = new LinkBatch();
foreach ( $wgPolyglotLanguages as $lang ) {
$obj = Title::makeTitle( $ns, $n . '/' . $lang );
$batch->addObj( $obj );
$titles[] = array( $obj, $lang );
}
$batch->execute();
$links = array();
foreach( $titles as $parts ) {
list( $t, $lang ) = $parts;
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;
}
|