-
Notifications
You must be signed in to change notification settings - Fork 3
/
MasonryMainPage.class.php
146 lines (121 loc) · 4.5 KB
/
MasonryMainPage.class.php
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
<?php
/**
* The MasonryMainPage extension enables the use of Masonry blocks
* within MediaWiki.
*
* Documentation: https://github.com/enterprisemediawiki/MasonryMainPage
* Support: https://github.com/enterprisemediawiki/MasonryMainPage
* Source code: https://github.com/enterprisemediawiki/MasonryMainPage
*
* @file MasonryMainPage.php
* @addtogroup Extensions
* @author Daren Welsh
* @copyright © 2014 by Daren Welsh
* @licence GNU GPL v3+
*/
class MasonryMainPage
{
static function setup ( &$parser ) {
$parser->setFunctionHook(
// name of parser function
// same as $magicWords value set in MasonryMainPage.i18n.php
'masonry-block',
array(
'MasonryMainPage', // class to call function from
'renderMasonryMainPage' // function to call within that class
),
SFH_OBJECT_ARGS // defines format of how data is passed to function
);
return true;
}
static function renderMasonryMainPage ( &$parser, $frame, $args ) {
//The $args array looks like this (not necessarily in order):
// [0] => 'Title = Block Title Example' (Such as MW page name) //Optional header
// [1] => 'Body = Body of block'
// [2] => 'Color = Blue' //Optional, default is green (choices in Masonry.css)
// //Specify 'none' for no color formatting
// [3] => 'Width = 2' //Optional width of block, default is 1 (item or item w2)
// more options to come later like priority, expiration date, etc
//Run extractOptions on $args
$options = self::extractOptions( $frame, $args );
/**
* Adds the following meta tag to the HTML header of all pages
* <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
*
* This forces IE to load the page NOT in compatibility mode. Loading
* in compatibility mode breaks the Masonry extension.
**/
global $wgOut, $wgServer, $wgExtensionAssetsPath;
$wgOut->addMeta( 'http:X-UA-Compatible', 'IE=9; IE=8; IE=7; IE=EDGE' );
// There are issues loading Masonry via ResourceLoader in MW 1.27, but not in MW 1.25.
// Functionality in other MW versions is unknown at this writing.
$scriptURL = "$wgServer/$wgExtensionAssetsPath/MasonryMainPage/masonry.pkgd.js";
$wgOut->addScript( "<script type='text/javascript' src='$scriptURL'></script>" );
$wgOut->addModules( 'ext.masonrymainpage.base' );
$itemClass = 'item';
if ( $options['width']=="2" ) {
$itemClass .= " w2";
}
if ( $options['color']=="none") {
$tableClass = '';
} else {
$tableClass = 'class="main-page-box main-page-box-' . $options['color'] . '"';
}
//Define the main output
$text =
"<div class='$itemClass'>
<div class='item-content'>
<table $tableClass>";
//This contains the heading of the masonry block (a wiki link to whatever is passed)
//Check if 'title' has a value
if ( !isset($options['title']) || $options['title']=="" ) {
//If no 'title' then omit table header tags
} else {
$text .= "<tr><th>" . $options['title'] . "</th></tr>";
}
$body = trim( $options['body'] );
// This contains the body of the masonry block
// Wiki code like links can be included; templates and wiki tables cannot
// \n before $body so user input starts on new line without
// any whitespace before it.
$text .= "<tr><td>\n$body</td></tr></table></div></div>";
return $text;
}
/**
* Converts an array of values in form [0] => "name=value" into a real
* associative array in form [name] => value
*
* @param array string $args
* @return array $options
*/
static function extractOptions( $frame, array $args ) {
$options = array();
foreach ( $args as $arg ) {
$pair = explode( '=', $frame->expand($arg) , 2 );
if ( count( $pair ) == 2 ) {
$name = strtolower(trim( $pair[0] )); //Convert to lower case so it is case-insensitive
$value = trim( $pair[1] );
$options[$name] = $value;
}
}
//Now you've got an array that looks like this:
// [title] => Block Title Example
// [body] => Body of block
// [color] => Blue
// [width] => 2
//Check for empties, set defaults
//Default 'title'
if ( !isset($options['title']) || $options['title']=="" ) {
$options['title']= ""; //no default, but left here for future options
}
//Default 'color'
if ( !isset($options['color']) || $options['color']=="" ) {
$options['color']= "green"; //green is default color
}
//Default 'width'
if ( !isset($options['width']) || $options['width']=="" ) {
$options['width']= "1"; //default of 1
}
return $options;
}
}