comment
[lhc/web/wiklou.git] / includes / SkinPHPTal.php
1 <?php
2 # Generic PHPTal (http://phptal.sourceforge.net/) skin
3 # Based on Brion's smarty skin
4 # Copyright (C) Gabriel Wicke -- http://www.aulinx.de/
5 #
6 # Todo: Needs some serious refactoring into functions that correspond
7 # to the computations individual esi snippets need. Most importantly no body
8 # parsing for most of those of course.
9 #
10 # Set this in LocalSettings to enable phptal:
11 # set_include_path(get_include_path() . ":" . $IP.'/PHPTAL-NP-0.7.0/libs');
12 # $wgUsePHPTal = true;
13 #
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along
25 # with this program; if not, write to the Free Software Foundation, Inc.,
26 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 # http://www.gnu.org/copyleft/gpl.html
28
29 require_once "PHPTAL.php";
30
31 class MediaWiki_I18N extends PHPTAL_I18N
32 {
33 var $_context = array();
34
35 function set($varName, $value)
36 {
37 $this->_context[$varName] = $value;
38 }
39
40 function translate($value)
41 {
42 $value = wfMsg( $value );
43
44 // interpolate variables
45 while (preg_match('/\$([0-9]*?)/sm', $value, $m)) {
46 list($src, $var) = $m;
47 $varValue = $this->_context[$var];
48 $value = str_replace($src, $varValue, $value);
49 }
50 return $value;
51 }
52 }
53
54 class SkinPHPTal extends Skin {
55 var $template;
56
57 function initPage() {
58 $this->skinname = "davinci";
59 $this->template = "xhtml_slim";
60 }
61
62 function outputPage( &$out ) {
63 global $wgTitle, $wgArticle, $wgUser, $wgLang, $wgOut;
64 global $wgScriptPath, $wgStyleSheetPath, $wgLanguageCode, $wgUseNewInterlanguage;
65 global $wgOutputEncoding, $wgUseDatabaseMessages, $wgRequest;
66
67 $action = $wgRequest->getText( 'action' );
68 $oldid = $wgRequest->getVal( 'oldid' );
69 $diff = $wgRequest->getVal( 'diff' );
70
71 $this->initPage();
72 $tpl = new PHPTAL($this->template . '.pt', 'templates');
73 #if ( $wgUseDatabaseMessages ) { // uncomment this to fall back to GetText
74
75 $tpl->setTranslator(new MediaWiki_I18N());
76 #}
77
78 $title = $wgTitle->getPrefixedText();
79 $tpl->setRef( "title", &$title ); // ?
80 $thispage = $wgTitle->getPrefixedDbKey();
81 $tpl->setRef( "thispage", &$thispage );
82 $tpl->set( "subtitle", $out->getSubtitle() );
83 $tpl->setRef( 'charset', $wgOutputEncoding);
84 $tpl->setRef( 'skinname', $this->skinname );
85
86 $loggedin = $wgUser->getID() != 0;
87 $tpl->setRef( "loggedin", &$loggedin );
88 $tpl->set( "editable", ($wgTitle->getNamespace != Namespace::getSpecial() ) );
89 $tpl->set( "exists", $wgTitle->getArticleID() != 0 );
90 $tpl->set( "watch", $wgTitle->userIsWatching() ? "unwatch" : "watch" );
91 $tpl->set( "protect", count($wgTitle->getRestrictions()) ? "unprotect" : "protect" );
92 $tpl->set( "helppage", wfMsg('helppage'));
93 $tpl->setRef( "searchaction", &$wgScriptPath );
94 $tpl->setRef( "stylepath", &$wgStyleSheetPath );
95 $tpl->setRef( "lang", &$wgLanguageCode );
96 $tpl->set( "langname", $wgLang->getLanguageName( $wgLanguageCode ) );
97
98 $username = $wgUser->getName();
99 $tpl->setRef( "username", &$username );
100 $userpage = $wgLang->getNsText( Namespace::getUser() ) . ":" . $wgUser->getName();
101 $tpl->setRef( "userpage", &$userpage);
102 $tpl->set( "sysop", $wgUser->isSysop() );
103 if( $wgUser->getNewtalk() ) {
104 $ntl = wfMsg( "newmessages",
105 $this->makeKnownLink(
106 $wgLang->getNsText( Namespace::getTalk( Namespace::getUser() ) )
107 . ":" . $wgUser->getName(),
108 wfMsg("newmessageslink") )
109 );
110 } else {
111 $ntl = "";
112 }
113 $tpl->setRef( "newtalk", &$ntl );
114 $tpl->setRef( "skin", &$this);
115 $tpl->set( "logo", $this->logoText() );
116 $tpl->set( "pagestats", $this->pageStats() );
117 $tpl->set( "disclaimer", $this->disclaimerLink() );
118 $tpl->set( "about", $this->aboutLink() );
119
120 $tpl->setRef( "debug", &$out->mDebugtext );
121 $tpl->set( "reporttime", $out->reportTime() );
122
123 $tpl->setRef( "bodytext", &$out->mBodytext );
124
125 $language_urls = array();
126 foreach( $wgOut->getLanguageLinks() as $l ) {
127 $nt = Title::newFromText( $l );
128 $language_urls[] = array('href' => $nt->getFullURL(),
129 'text' => ($wgLang->getLanguageName( $nt->getInterwiki()) != ''?$wgLang->getLanguageName( $nt->getInterwiki()) : $l),
130 'class' => $wgLang->isRTL() ? 'rtl' : 'ltr');
131 }
132 if(count($language_urls) != 0 ) {
133 $tpl->setRef( 'language_urls', &$language_urls);
134 } else {
135 $tpl->set('language_urls', false);
136 }
137
138 /* set up the default links for the personal toolbar */
139 $personal_urls = array();
140 if ($loggedin) {
141 $personal_urls['userpage'] = array('text' => $username,
142 'href' => $this->makeUrl($userpage),
143 'ttip' => wfMsg('tooltip-userpage'),
144 'akey' => wfMsg('accesskey-userpage'));
145 $personal_urls['mytalk'] = array('text' => wfMsg('mytalk'),
146 'href' => $this->makeTalkUrl($userpage),
147 'ttip' => wfMsg('tooltip-mytalk'),
148 'akey' => wfMsg('accesskey-mytalk'));
149 $personal_urls['preferences'] = array('text' => wfMsg('preferences'),
150 'href' => $this->makeSpecialUrl('Preferences'),
151 'ttip' => wfMsg('tooltip-preferences'),
152 'akey' => wfMsg('accesskey-preferences'));
153 $personal_urls['watchlist'] = array('text' => wfMsg('watchlist'),
154 'href' => $this->makeSpecialUrl('Watchlist'),
155 'ttip' => wfMsg('tooltip-watchlist'),
156 'akey' => wfMsg('accesskey-watchlist'));
157 $personal_urls['mycontris'] = array('text' => wfMsg('mycontris'),
158 'href' => $this->makeSpecialUrl('Contributions','target=' . $username),
159 'ttip' => wfMsg('tooltip-mycontris'),
160 'akey' => wfMsg('accesskey-mycontris'));
161 $personal_urls['logout'] = array('text' => wfMsg('userlogout'),
162 'href' => $this->makeSpecialUrl('Userlogout','returnpage=' . thispage),
163 'ttip' => wfMsg('tooltip-logout'),
164 'akey' => wfMsg('accesskey-logout'));
165 } else {
166 $personal_urls['login'] = array('text' => wfMsg('userlogin'),
167 'href' => $this->makeSpecialUrl('Userlogin'),
168 'ttip' => wfMsg('tooltip-login'),
169 'akey' => wfMsg('accesskey-login'));
170 }
171 $tpl->setRef('personal_urls', &$personal_urls);
172
173 /* set up the content actions */
174 $iscontent = ($wgTitle->getNamespace() != Namespace::getSpecial() );
175
176 $content_actions = array();
177 /*$content_actions['view'] = array('class' => ($action == 'view' and !Namespace::isTalk( $wgTitle->getNamespace())) ? 'selected' : '',*/
178
179
180 /* the edit tab */
181 if( $iscontent) {
182
183 $content_actions['article'] = array('class' => (!Namespace::isTalk( $wgTitle->getNamespace())) ? 'selected' : '',
184 'text' => wfMsg('article'),
185 'href' => $this->makeArticleUrl($thispage),
186 'ttip' => wfMsg('tooltip-article'),
187 'akey' => wfMsg('accesskey-article'));
188
189 $content_actions['talk'] = array('class' => (Namespace::isTalk( $wgTitle->getNamespace()) ? 'selected' : ''),
190 'text' => wfMsg('talk'),
191 'href' => $this->makeTalkUrl($title),
192 'ttip' => wfMsg('tooltip-talk'),
193 'akey' => wfMsg('accesskey-talk'));
194
195 if ( $wgTitle->userCanEdit() ) {
196 if ( $oldid && ! isset( $diff ) ) {
197 $oid = "&oldid={$oldid}";
198 }
199 $content_actions['edit'] = array('class' => ($action == 'edit' or $action == 'submit') ? 'selected' : '',
200 'text' => wfMsg('edit'),
201 'href' => $this->makeUrl($thispage, 'action=edit'.$oid),
202 'ttip' => wfMsg('tooltip-edit'),
203 'akey' => wfMsg('accesskey-edit'));
204 } else {
205 if ( $oldid && ! isset( $diff ) ) {
206 $oid = "&oldid={$oldid}";
207 }
208 $content_actions['edit'] = array('class' => ($action == 'edit') ? 'selected' : '',
209 'text' => wfMsg('viewsource'),
210 'href' => $this->makeUrl($thispage, 'action=edit'.$oid),
211 'ttip' => wfMsg('tooltip-edit'),
212 'akey' => wfMsg('accesskey-edit'));
213 }
214
215 if ( $wgTitle->getArticleId() ) {
216
217 $content_actions['history'] = array('class' => ($action == 'history') ? 'selected' : '',
218 'text' => wfMsg('history_short'),
219 'href' => $this->makeUrl($thispage, 'action=history'),
220 'ttip' => wfMsg('tooltip-history'),
221 'akey' => wfMsg('accesskey-history'));
222
223 /*
224 $content_actions['revert'] = array('class' => ($action == 'revert') ? 'selected' : '',
225 'i18n_key' => 'revert',
226 'href' => $this->makeUrl($wgTitle->getPrefixedDbKey(), 'action=revert'),
227 'akey' => wfMsg('accesskeyrevert'));
228 */
229 if( $wgUser->getNewtalk() ) {
230 $content_actions['rollback'] = array('class' => ($action == 'rollback') ? 'selected' : '',
231 'text' => wfMsg('rollback_short'),
232 'href' => $this->makeUrl($thispage, 'action=rollback'),
233 'ttip' => wfMsg('tooltip-rollback'),
234 'akey' => wfMsg('accesskey-rollback'));
235 }
236 if($wgUser->isSysop()){
237 if(!$wgTitle->isProtected()){
238 $content_actions['protect'] = array('class' => ($action == 'protect') ? 'selected' : '',
239 'text' => wfMsg('protect'),
240 'href' => $this->makeUrl($thispage, 'action=protect'),
241 'ttip' => wfMsg('tooltip-protect'),
242 'akey' => wfMsg('accesskey-protect'));
243
244 } else {
245 $content_actions['unprotect'] = array('class' => ($action == 'unprotect') ? 'selected' : '',
246 'text' => wfMsg('unprotect'),
247 'href' => $this->makeUrl($thispage, 'action=unprotect'),
248 'ttip' => wfMsg('tooltip-protect'),
249 'akey' => wfMsg('accesskey-protect'));
250 }
251 $content_actions['delete'] = array('class' => ($action == 'delete') ? 'selected' : '',
252 'text' => wfMsg('delete'),
253 'href' => $this->makeUrl($thispage, 'action=delete'),
254 'ttip' => wfMsg('tooltip-delete'),
255 'akey' => wfMsg('accesskey-delete'));
256 }
257 if ( $wgUser->getID() != 0 ) {
258 if ( $wgTitle->userCanEdit()) {
259 $content_actions['move'] = array('class' => ($wgTitle->getDbKey() == 'Movepage' and $wgTitle->getNamespace == Namespace::getSpecial()) ? 'selected' : '',
260 'text' => wfMsg('move'),
261 'href' => $this->makeSpecialUrl('Movepage', 'target='.$thispage),
262 'ttip' => wfMsg('tooltip-move'),
263 'akey' => wfMsg('accesskey-move'));
264 } else {
265 $content_actions['move'] = array('class' => 'inactive',
266 'text' => wfMsg('move'),
267 'href' => false,
268 'akey' => false);
269
270 }
271 }
272 }
273
274 if ( $wgUser->getID() != 0 and $action != 'edit' and $action != 'submit' ) {
275 if( !$wgTitle->userIsWatching()) {
276 $content_actions['watch'] = array('class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : '',
277 'text' => wfMsg('watch'),
278 'href' => $this->makeUrl($thispage, 'action=watch'),
279 'ttip' => wfMsg('tooltip-watch'),
280 'akey' => wfMsg('accesskey-watch'));
281 } else {
282 $content_actions['watch'] = array('class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : '',
283 'text' => wfMsg('unwatch'),
284 'href' => $this->makeUrl($thispage, 'action=unwatch'),
285 'ttip' => wfMsg('tooltip-unwatch'),
286 'akey' => wfMsg('accesskey-unwatch'));
287
288 }
289 }
290 } else {
291 /* show special page tab */
292
293 $content_actions['article'] = array('class' => 'selected',
294 'text' => wfMsg('specialpage'),
295 'href' => false,
296 'ttip' => wfMsg('tooltip-specialpage'),
297 'akey' => false);
298 }
299 $tpl->setRef('content_actions', &$content_actions);
300
301
302 /* prepare an array of common navigation links */
303
304 $nav_urls = array();
305 $nav_urls['mainpage'] = array('href' => $this->makeI18nUrl('mainpage'));
306 $nav_urls['randompage'] = array('href' => $this->makeSpecialUrl('Randompage'));
307 $nav_urls['recentchanges'] = array('href' => $this->makeSpecialUrl('Recentchanges'));
308 $nav_urls['whatlinkshere'] = array('href' => $this->makeSpecialUrl('Whatlinkshere', 'target='.$thispage));
309 $nav_urls['currentevents'] = array('href' => $this->makeI18nUrl('currentevents'));
310 $nav_urls['recentchangeslinked'] = array('href' => $this->makeSpecialUrl('Recentchangeslinked', 'target='.$thispage));
311 $nav_urls['bugreports'] = array('href' => $this->makeI18nUrl('bugreportspage'));
312 global $wgSiteSupportPage;
313 // $nav_urls['sitesupport'] = array('href' => $this->makeI18nUrl('sitesupportpage'));
314 $nav_urls['sitesupport'] = array('href' => $this->makeUrl($wgSiteSupportPage));
315 $nav_urls['help'] = array('href' => $this->makeI18nUrl('helppage'));
316 $nav_urls['upload'] = array('href' => $this->makeSpecialUrl('Upload'));
317 $nav_urls['specialpages'] = array('href' => $this->makeSpecialUrl('Specialpages'));
318 $tpl->setRef( "nav_urls", &$nav_urls );
319
320
321 // execute template
322 $res = $tpl->execute();
323 // result may be an error
324 if (PEAR::isError($res)) {
325 echo $res->toString(), "\n";
326 } else {
327 echo $res;
328 }
329
330 }
331
332
333
334 /*static*/ function makeSpecialUrl( $name, $urlaction='' ) {
335 $title = Title::makeTitle( NS_SPECIAL, $name );
336 return $title->getLocalURL( $urlaction );
337 }
338 /*static*/ function makeTalkUrl ( $name, $urlaction='' ) {
339 $title = Title::newFromText( $name );
340 $title = $title->getTalkPage();
341 return $title->getLocalURL( $urlaction );
342 }
343 /*static*/ function makeArticleUrl ( $name, $urlaction='' ) {
344 $title = Title::newFromText( $name );
345 #$title->setNamespace(0);
346 #$title = Title::makeTitle( Namespace::getSubject( $wgTitle->getNamespace() ), $wgTitle->getDbKey() );
347 $title= $title->getSubjectPage();
348 return $title->getLocalURL( $urlaction );
349 }
350 /*static*/ function makeI18nUrl ( $name, $urlaction='' ) {
351 $title = Title::newFromText( wfMsg($name) );
352 if(!is_object($title)) {
353 $title = Title::newFromText( $name );
354 }
355 return $title->getLocalURL( $urlaction );
356 }
357 /*static*/ function makeUrl ( $name, $urlaction='' ) {
358 $title = Title::newFromText( $name );
359 return $title->getLocalURL( $urlaction );
360 }
361
362 }
363
364 class SkinDaVinci extends SkinPHPTal {
365 function initPage() {
366 SkinPHPTal::initPage();
367 $this->skinname = "davinci";
368 $this->template = "xhtml_slim";
369 }
370 }
371
372 class SkinMono extends SkinPHPTal {
373 function initPage() {
374 SkinPHPTal::initPage();
375 $this->skinname = "mono";
376 $this->template = "xhtml_slim";
377 }
378 }
379
380 class SkinMonoBook extends SkinPHPTal {
381 function initPage() {
382 SkinPHPTal::initPage();
383 $this->skinname = "monobook";
384 $this->template = "xhtml_slim";
385 }
386 }
387
388 ?>