3 * Implements Special:MyLanguage
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Niklas Laxström
22 * @author Siebrand Mazeland
23 * @copyright Copyright © 2010-2013 Niklas Laxström, Siebrand Mazeland
26 use MediaWiki\MediaWikiServices
;
29 * Unlisted special page just to redirect the user to the translated version of
30 * a page, if it exists.
32 * Usage: [[Special:MyLanguage/Page name|link text]]
35 * @ingroup SpecialPage
37 class SpecialMyLanguage
extends RedirectSpecialArticle
{
38 public function __construct() {
39 parent
::__construct( 'MyLanguage' );
43 * If the special page is a redirect, then get the Title object it redirects to.
46 * @param string|null $subpage
49 public function getRedirect( $subpage ) {
50 $title = $this->findTitle( $subpage );
51 // Go to the main page if given invalid title.
53 $title = Title
::newMainPage();
59 * Assuming the user's interface language is fi. Given input Page, it
60 * returns Page/fi if it exists, otherwise Page. Given input Page/de,
61 * it returns Page/fi if it exists, otherwise Page/de if it exists,
64 * @param string|null $subpage
67 public function findTitle( $subpage ) {
68 // base = title without language code suffix
69 // provided = the title as it was given
70 $base = $provided = null;
71 if ( $subpage !== null ) {
72 $provided = Title
::newFromText( $subpage );
76 if ( $provided && strpos( $subpage, '/' ) !== false ) {
77 $pos = strrpos( $subpage, '/' );
78 $basepage = substr( $subpage, 0, $pos );
79 $code = substr( $subpage, $pos +
1 );
80 if ( strlen( $code ) && Language
::isKnownLanguageTag( $code ) ) {
81 $base = Title
::newFromText( $basepage );
86 // No subpage provided or base page does not exist
90 if ( $base->isRedirect() ) {
91 $page = new WikiPage( $base );
92 $base = $page->getRedirectTarget();
95 $uiLang = $this->getLanguage();
96 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
98 if ( $uiLang->equals( $contLang ) ) {
99 // Short circuit when the current UI language is the
100 // wiki's default language to avoid unnecessary page lookups.
104 // Check for a subpage in current UI language
105 $proposed = $base->getSubpage( $uiLang->getCode() );
106 if ( $proposed && $proposed->exists() ) {
110 if ( $provided !== $base && $provided->exists() ) {
111 // Explicit language code given and the page exists
115 // Check for fallback languages specified by the UI language
116 $possibilities = $uiLang->getFallbackLanguages();
117 foreach ( $possibilities as $lang ) {
118 if ( $lang !== $contLang->getCode() ) {
119 $proposed = $base->getSubpage( $lang );
120 if ( $proposed && $proposed->exists() ) {
126 // When all else has failed, return the base page
131 * Target can identify a specific user's language preference.
137 public function personallyIdentifiableTarget() {