Home > coding, php > mediawiki user language recognition

mediawiki user language recognition

so i had the problem, that there was a multilingual mediawiki installation with lot of translations. So far so good, the user could select the appropriate language if he visited the Base English page. So i was wondering if there was a tool that tries to detect the users browser language and act upon that. To my surprise there was none that could be used easily in the way i needed it to be :-\

So i added some parts to the Polyglot extension so it redirects the user to the correct page. What i changes is really trivial:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        $accept = @$_SERVER["HTTP_ACCEPT_LANGUAGE"];
        $redir=false;
        if(!empty($accept))
        {
                $accept = explode( ',', $accept );
                foreach($accept as $ulang)
                {
                        $ulanga = explode( '-', $ulang );
                        $tu = Title::makeTitle( $ns, $n . '/' . $ulanga[0] );
                        if($tu->exists())
                        {
                                $t = $tu;
                                $redir=true;
                                break;
                        }

                }
        }

not really difficult ;)

example pages (works if your first browser language is not English …):
http://wiki.rigsofrods.com/pages/Portal
or:
http://wiki.rigsofrods.com/pages/Truck_Description_File

the whole file for your usage:

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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?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(
        '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
* $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 = true;
///// hook it up /////////////////////////////////////////////////////
$wgHooks['ArticleFromTitle'][] = 'wfPolyglotArticleFromTitle';
$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( $GLOBALS['wgLanguageNames'] );
        }
}

function wfPolyglotArticleFromTitle( &$title, &$article ) {
        global $wfPolyglotExcemptNamespaces, $wfPolyglotExcemptTalkPages, $wfPolyglotFollowRedirects;
        global $wgLang, $wgContLang, $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;
        $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( $n ) > 1 ) {
                $escContLang = preg_quote( $wgContLang->getCode(),  '!' );
                if ( preg_match( '!/$!', $n ) ) {
                        $force = true;
                        $remove = 1;
                } elseif ( preg_match( "!/{$escContLang}$!", $n ) ) {
                        $force = true;
                        $remove = strlen( $wgContLang->getCode() ) + 1;
                }
        }

        $accept = @$_SERVER["HTTP_ACCEPT_LANGUAGE"];
        $redir=false;
        if(!empty($accept))
        {
                $accept = explode( ',', $accept );
                foreach($accept as $ulang)
                {
                        $ulanga = explode( '-', $ulang );
                        $tu = Title::makeTitle( $ns, $n . '/' . $ulanga[0] );
                        if($tu->exists())
                        {
                                $t = $tu;
                                $redir=true;
                                break;
                        }

                }
        }

        if(!$redir)
        {
                if ( $force ) {
                        $t = Title::makeTitle( $ns, substr( $n, 0, strlen( $n ) - $remove ) );
                        $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 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() ) {
                $options = array_diff( $options, array( 'broken' ) );
                $options []= 'known';
        }

        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();
                //$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;
}
?>

Related posts:

  1. decent identicons with php
  2. ‘strict’ typing of function arguments in python
  3. IP WHOIS query via python
  4. CPU flags explained (linux)

Categories: coding, php Tags:
  1. No comments yet.
  1. No trackbacks yet.