MediaWiki REL1_35
SpecialMyLanguage.php
Go to the documentation of this file.
1<?php
27
38 public function __construct() {
39 parent::__construct( 'MyLanguage' );
40 }
41
49 public function getRedirect( $subpage ) {
50 $title = $this->findTitle( $subpage );
51 // Go to the main page if given invalid title.
52 if ( !$title ) {
53 $title = Title::newMainPage();
54 }
55 return $title;
56 }
57
67 public function findTitle( $subpage ) {
68 $services = MediaWikiServices::getInstance();
69 // base = title without language code suffix
70 // provided = the title as it was given
71 $base = $provided = null;
72 if ( $subpage !== null ) {
73 $provided = Title::newFromText( $subpage );
74 $base = $provided;
75
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 ) && $services->getLanguageNameUtils()->isKnownLanguageTag( $code ) ) {
81 $base = Title::newFromText( $basepage );
82 }
83 }
84 }
85
86 if ( !$base ) {
87 // No subpage provided or base page does not exist
88 return null;
89 }
90
91 if ( $base->isRedirect() ) {
92 $page = new WikiPage( $base );
93 $base = $page->getRedirectTarget();
94 }
95
96 $uiLang = $this->getLanguage();
97 $contLang = $services->getContentLanguage();
98
99 if ( $uiLang->equals( $contLang ) ) {
100 // Short circuit when the current UI language is the
101 // wiki's default language to avoid unnecessary page lookups.
102 return $base;
103 }
104
105 // Check for a subpage in current UI language
106 $proposed = $base->getSubpage( $uiLang->getCode() );
107 if ( $proposed && $proposed->exists() ) {
108 return $proposed;
109 }
110
111 if ( $provided !== $base && $provided->exists() ) {
112 // Explicit language code given and the page exists
113 return $provided;
114 }
115
116 // Check for fallback languages specified by the UI language
117 $possibilities = $uiLang->getFallbackLanguages();
118 foreach ( $possibilities as $lang ) {
119 if ( $lang !== $contLang->getCode() ) {
120 $proposed = $base->getSubpage( $lang );
121 if ( $proposed && $proposed->exists() ) {
122 return $proposed;
123 }
124 }
125 }
126
127 // When all else has failed, return the base page
128 return $base;
129 }
130
139 return true;
140 }
141}
MediaWikiServices is the service locator for the application scope of MediaWiki.
Superclass for any RedirectSpecialPage which redirects the user to a particular article (as opposed t...
Unlisted special page just to redirect the user to the translated version of a page,...
findTitle( $subpage)
Assuming the user's interface language is fi.
getRedirect( $subpage)
If the special page is a redirect, then get the Title object it redirects to.
personallyIdentifiableTarget()
Target can identify a specific user's language preference.
getLanguage()
Shortcut to get user's language.
Class representing a MediaWiki article and history.
Definition WikiPage.php:51
if(!isset( $args[0])) $lang