forked from Universal-Omega/Monaco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkinMonaco.php
More file actions
350 lines (288 loc) · 9.21 KB
/
SkinMonaco.php
File metadata and controls
350 lines (288 loc) · 9.21 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Title\Title;
use MediaWiki\User\UserOptionsLookup;
class SkinMonaco extends SkinTemplate {
/**
* Overwrite few SkinTemplate methods which we don't need in Monaco
*/
public function buildSidebar() {
return [];
}
function getCopyrightIcon() {}
function getPoweredBy() {}
function disclaimerLink() {}
function privacyLink() {}
function aboutLink() {}
function getHostedBy() {}
function diggsLink() {}
function deliciousLink() {}
/**
* @var Config
*/
private $config;
private $mMastheadUser;
private $mMastheadTitleVisible;
private UserOptionsLookup $mUserOptionsLookup;
public function __construct( array $options = [] ) {
$this->config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'monaco' );
$this->mUserOptionsLookup = $mUserOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
parent::__construct( $options );
}
/**
* @return string
*/
public static function getSkinMonacoDefaultTheme() {
return "sapphire";
}
/**
* @return string[]
*/
public static function getSkinMonacoThemeList() {
return [ "beach", "brick", "carbon", "forest", "gaming", "jade", "moonlight", "obsession", "ruby", "sapphire", "sky", "slate", "smoke", "spring" ];
}
/**
* @param OutputPage $out
*/
public function initPage( OutputPage $out ) {
parent::initPage( $out );
// ResourceLoader doesn't do ie specific styles that well iirc, so we have
// to do those manually.
$out->addStyle( 'Monaco/style/css/monaco_ie8.css', 'screen', 'IE 8' );
$out->addStyle( 'Monaco/style/css/monaco_gteie8.css', 'screen', 'gte IE 8');
// Likewise the masthead is a conditional feature so it's hard to include
// inside of the ResourceLoader.
if ( $this->showMasthead() ) {
$out->addStyle( 'Monaco/style/css/masthead.css', 'screen' );
}
$request = $this->getRequest();
$user = RequestContext::getMain()->getUser();
// Check the following things in this order:
// 1) value of $wgDefaultTheme (set in site configuration)
// 2) user's personal preference/override
// 3) per-page usetheme URL parameter
$theme = $this->config->get( 'MonacoTheme' );
$theme = $this->mUserOptionsLookup->getOption( $user, 'theme_monaco', $theme );
$theme = $request->getText( 'usetheme', $theme );
$themes = SkinMonaco::getSkinMonacoThemeList();
$theme_fallback = SkinMonaco::getSkinMonacoDefaultTheme();
if ( !in_array( $theme, $themes ) ) {
$theme = $theme_fallback;
}
if ( $this->config->get( 'MonacoAllowUseTheme' ) ) {
// Theme is another conditional feature, we can't really resource load this
if ( isset( $theme ) && is_string( $theme ) && ( $theme != $theme_fallback ) ) {
$out->addStyle( "Monaco/style/{$theme}/css/main.css", 'screen' );
}
}
// TODO: explicit RTL style sheets are supposed to be obsolete w/ResourceLoader
// I have no way to test this currently, however. -haleyjd
// rtl... hmm, how do we resource load this?
$out->addStyle( 'Monaco/style/rtl.css', 'screen', '', 'rtl' );
$out->addScript(
'<!--[if IE]><script type="text/javascript' .
'">\'abbr article aside audio canvas details figcaption figure ' .
'footer header hgroup mark menu meter nav output progress section ' .
'summary time video\'' .
'.replace(/\w+/g,function(n){document.createElement(n)})</script><![endif]-->'
);
}
/**
* @return array
*/
public function getDefaultModules() {
$modules = parent::getDefaultModules();
return $modules;
}
/**
* @return bool
*/
public function showMasthead() {
if ( !$this->config->get( 'MonacoUseMasthead' ) ) {
return false;
}
return (bool)$this->getMastheadUser();
}
/**
* @return User
*/
public function getMastheadUser() {
$title = $this->getTitle();
if ( !isset( $this->mMastheadUser ) ) {
$ns = $title->getNamespace();
if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
$this->mMastheadUser = User::newFromName( strtok( $title->getText(), '/' ), false );
$this->mMastheadTitleVisible = false;
} else {
$this->mMastheadUser = false;
$this->mMastheadTitleVisible = true; // title is visible anyways if we're not on a masthead using page
}
}
return $this->mMastheadUser;
}
/**
* @return bool
*/
public function isMastheadTitleVisible() {
if ( !$this->showMasthead() ) {
return true;
}
$this->getMastheadUser();
return $this->mMastheadTitleVisible;
}
/**
* @param array $lines
* @return array
*/
public function parseToolboxLinks( $lines ) {
$nodes = [];
if ( is_array( $lines ) ) {
foreach ( $lines as $line ) {
$trimmed = trim( $line, ' *' );
if ( strlen( $trimmed ) == 0 ) { # ignore empty lines
continue;
}
$item = MonacoSidebar::parseItem( $trimmed );
$nodes[] = $item;
}
}
return $nodes;
}
/**
* @param string $message_key
* @return array
*/
public function getLines( $message_key ) {
$revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
$revision = $revisionStore->getRevisionByTitle( Title::newFromText( $message_key, NS_MEDIAWIKI ) );
if ( is_object( $revision ) ) {
$content = $revision->getContent( SlotRecord::MAIN );
$text = ContentHandler::getContentText( $content );
if ( trim( $text ) != '' ) {
$temp = MonacoSidebar::getMessageAsArray( $message_key );
if ( count( $temp ) > 0 ) {
$lines = $temp;
}
}
}
if ( empty( $lines ) ) {
$lines = MonacoSidebar::getMessageAsArray( $message_key );
}
return $lines;
}
/**
* @return array
*/
public function getToolboxLinks() {
return $this->parseToolboxLinks( $this->getLines( 'Monaco-toolbox' ) );
}
var $lastExtraIndex = 1000;
/**
* @param array &$node
* @param array &$nodes
*/
public function addExtraItemsToSidebarMenu( &$node, &$nodes ) {
$extraWords = [
'#voted#' => [ 'highest_ratings', 'GetTopVotedArticles' ],
'#popular#' => [ 'most_popular', 'GetMostPopularArticles' ],
'#visited#' => [ 'most_visited', 'GetMostVisitedArticles' ],
'#newlychanged#' => [ 'newly_changed', 'GetNewlyChangedArticles' ],
'#topusers#' => [ 'community', 'GetTopFiveUsers' ]
];
if ( isset( $extraWords[ strtolower( $node['org'] ) ] ) ) {
if ( substr( $node['org'], 0, 1 ) == '#' ) {
if ( strtolower( $node['org'] ) == strtolower( $node['text'] ) ) {
$node['text'] = wfMessage( trim( strtolower( $node['org'] ), ' *' ) )->text();
}
$node['magic'] = true;
}
$results = DataProvider::$extraWords[strtolower($node['org'])][1]();
$results[] = [ 'url' => SpecialPage::getTitleFor( 'Top/'.$extraWords[ strtolower( $node['org'] ) ][0] )->getLocalURL(), 'text' => strtolower( wfMessage( 'moredotdotdot' )->text() ), 'class' => 'Monaco-sidebar_more' ];
if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
if ( strtolower( $node['org'] ) == '#popular#' ) {
$results[] = [ 'url' => Title::makeTitle( NS_MEDIAWIKI, 'Most popular articles' )->getLocalUrl(), 'text' => wfMessage( 'monaco-edit-this-menu' )->text(), 'class' => 'Monaco-sidebar_edit' ];
}
}
foreach ( $results as $key => $val ) {
$node['children'][] = $this->lastExtraIndex;
$nodes[$this->lastExtraIndex]['text'] = $val['text'];
$nodes[$this->lastExtraIndex]['href'] = $val['url'];
if ( !empty( $val['class'] ) ) {
$nodes[$this->lastExtraIndex]['class'] = $val['class'];
}
$this->lastExtraIndex++;
}
}
}
/**
* @param array $lines
* @return array
*/
public function parseSidebarMenu( $lines ) {
$nodes = [];
$nodes[] = [];
$lastDepth = 0;
$i = 0;
if ( is_array( $lines ) ) {
foreach ( $lines as $line ) {
if ( strlen( $line ) == 0 ) { # ignore empty lines
continue;
}
$node = MonacoSidebar::parseItem( $line );
$node['depth'] = strrpos( $line, '*' ) + 1;
if ( $node['depth'] == $lastDepth ) {
$node['parentIndex'] = $nodes[$i]['parentIndex'];
} elseif ( $node['depth'] == $lastDepth + 1 ) {
$node['parentIndex'] = $i;
} else {
for ( $x = $i; $x >= 0; $x-- ) {
if ( $x == 0 ) {
$node['parentIndex'] = 0;
break;
}
if ( $nodes[$x]['depth'] == $node['depth'] - 1 ) {
$node['parentIndex'] = $x;
break;
}
}
}
if ( substr( $node['org'],0,1 ) == '#' ) {
$this->addExtraItemsToSidebarMenu( $node, $nodes );
}
$nodes[$i+1] = $node;
$nodes[ $node['parentIndex'] ]['children'][] = $i+1;
$lastDepth = $node['depth'];
$i++;
}
}
return $nodes;
}
/**
* @return array
*/
public function getSidebarLinks() {
return $this->parseSidebarMenu( $this->getLines( 'Monaco-sidebar' ) );
}
/**
* @param string $name
* @param bool $asArray|false
* @return array|string|null
*/
public function getTransformedArticle( $name, $asArray = false ) {
$revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
$revision = $revisionStore->getRevisionByTitle( Title::newFromText( $name ) );
$parser = MediaWikiServices::getInstance()->getParser();
if ( is_object( $revision ) ) {
$text = $revision->getText();
if ( !empty( $text ) ) {
$ret = $parser->transformMsg( $text, $parser->getOptions() );
if ( $asArray ) {
$ret = explode( "\n", $ret );
}
return $ret;
}
}
return null;
}
}