Index: trunk/extensions/Polyglot/Polyglot.php |
— | — | @@ -63,6 +63,7 @@ |
64 | 64 | |
65 | 65 | ///// hook it up ///////////////////////////////////////////////////// |
66 | 66 | $wgHooks['ArticleFromTitle'][] = 'wfPolyglotArticleFromTitle'; |
| 67 | +$wgHooks['LinkBegin'][] = 'wfPolyglotLinkBegin'; |
67 | 68 | $wgHooks['ParserAfterTidy'][] = 'wfPolyglotParserAfterTidy'; |
68 | 69 | $wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'wfPolyglotSkinTemplateOutputPageBeforeExec'; |
69 | 70 | |
— | — | @@ -82,7 +83,7 @@ |
83 | 84 | |
84 | 85 | function wfPolyglotArticleFromTitle( &$title, &$article ) { |
85 | 86 | global $wfPolyglotExcemptNamespaces, $wfPolyglotExcemptTalkPages, $wfPolyglotFollowRedirects; |
86 | | - global $wgLang, $wgRequest; |
| 87 | + global $wgLang, $wgContLang, $wgRequest; |
87 | 88 | |
88 | 89 | if ($wgRequest->getVal( 'redirect' ) == 'no') { |
89 | 90 | return true; |
— | — | @@ -98,17 +99,27 @@ |
99 | 100 | |
100 | 101 | $n = $title->getDBkey(); |
101 | 102 | $nofollow = false; |
| 103 | + $force = false; |
102 | 104 | |
103 | 105 | //TODO: when user-defined language links start working (see below), |
104 | 106 | // we need to look at the langlinks table here. |
| 107 | + if ( !$title->exists() && strlen( $n ) > 1 ) { |
| 108 | + $escContLang = preg_quote( $wgContLang->getCode(), '!' ); |
| 109 | + if ( preg_match( '!/$!', $n ) ) { |
| 110 | + $force = true; |
| 111 | + $remove = 1; |
| 112 | + } elseif ( preg_match( "!/{$escContLang}$!", $n ) ) { |
| 113 | + $force = true; |
| 114 | + $remove = strlen( $wgContLang->getCode() ) + 1; |
| 115 | + } |
| 116 | + } |
105 | 117 | |
106 | | - if (!$title->exists() && strlen($n)>1 && preg_match('!/$!', $n)) { |
107 | | - $t = Title::makeTitle($ns, substr($n, 0, strlen($n)-1)); |
| 118 | + if ( $force ) { |
| 119 | + $t = Title::makeTitle( $ns, substr( $n, 0, strlen( $n ) - $remove ) ); |
108 | 120 | $nofollow = true; |
109 | | - } |
110 | | - else { |
| 121 | + } else { |
111 | 122 | $lang = $wgLang->getCode(); |
112 | | - $t = Title::makeTitle($ns, $n . '/' . $lang); |
| 123 | + $t = Title::makeTitle( $ns, $n . '/' . $lang ); |
113 | 124 | } |
114 | 125 | |
115 | 126 | if (!$t->exists()) { |
— | — | @@ -158,6 +169,42 @@ |
159 | 170 | return true; |
160 | 171 | } |
161 | 172 | |
| 173 | +function wfPolyglotLinkBegin( $linker, $target, &$text, &$customAttribs, &$query, &$options, &$ret ) { |
| 174 | + global $wfPolyglotExcemptNamespaces, $wfPolyglotExcemptTalkPages, $wgContLang; |
| 175 | + |
| 176 | + $ns = $target->getNamespace(); |
| 177 | + |
| 178 | + if ( $ns < 0 |
| 179 | + || in_array( $ns, $wfPolyglotExcemptNamespaces ) |
| 180 | + || ( $wfPolyglotExcemptTalkPages && MWNamespace::isTalk( $ns ) ) ) { |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
| 184 | + $dbKey = $target->getDBkey(); |
| 185 | + |
| 186 | + if ( !$target->exists() && strlen( $dbKey ) > 1 ) { |
| 187 | + $escContLang = preg_quote( $wgContLang->getCode(), '!' ); |
| 188 | + if ( preg_match( '!/$!', $dbKey ) ) { |
| 189 | + $remove = 1; |
| 190 | + } elseif ( preg_match( "!/{$escContLang}$!", $dbKey ) ) { |
| 191 | + $remove = strlen( $wgContLang->getCode() ) + 1; |
| 192 | + } else { |
| 193 | + return true; |
| 194 | + } |
| 195 | + } else { |
| 196 | + return true; |
| 197 | + } |
| 198 | + |
| 199 | + $t = Title::makeTitle( $ns, substr( $dbKey, 0, strlen( $dbKey ) - $remove ) ); |
| 200 | + |
| 201 | + if ( $t->exists() ) { |
| 202 | + $options = array_diff( $options, array( 'broken' ) ); |
| 203 | + $options []= 'known'; |
| 204 | + } |
| 205 | + |
| 206 | + return true; |
| 207 | +} |
| 208 | + |
162 | 209 | function wfPolyglotGetLanguages( $title ) { |
163 | 210 | global $wgPolyglotLanguages; |
164 | 211 | if (!$wgPolyglotLanguages) return NULL; |