Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 352 lines (302 sloc) 9.274 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 351 352
<?php
/**
* HTML renderer for Bootstrap skin
*
* @file
* @ingroup Skins
*/

/**
* BootstrapRenderer class for Bootstrap skin
* @ingroup Skins
*/

class BootstrapRenderer {

private $skin, $data, $doc;

/*
* Constructor
*
* @param BaseTemplate, Array
*/
public function __construct( $skin, $data ) {
$this->skin = $skin;
$this->data = $data;
}

/**
* Render category links.
*
* @return DOMDocument
* @ingroup Skins
*/
public function renderCatLinks() {
$result = false;

if( $this->skin->data['catlinks'] ) {
$doc = DOMDocument::loadXML(
Xml::openElement( 'footer' ) .
$this->skin->data[ 'catlinks' ] .
Xml::closeElement( 'footer' )
);

$finder = new DOMXPath( $doc );
$container = $finder->query('//div[contains(@class,"catlinks")]')->item(0);
$container->setAttribute( 'class', $container->getAttribute('class') .
' well' );

$result = $doc->saveHTML();
echo $result;
}

return $result;
}

/*
*
* Print footer.
*
* @ingroup Skins
*
*/
public function renderFooter() {
global $sgFooterOptions;

// parse and transform
$footer = $this->parseAndTransform( $sgFooterOptions['page'], DIRNAME(__FILE__) .'/xsl/footer.xsl');

$output = new DOMDocument();
$output->loadXML( $footer );

// add footer links
foreach( $this->skin->getFooterLinks() as $category => $links ) {
$footerLinks = $output->createElement('ul');
$footerLinks->setAttribute('class', 'horizontal');
foreach( $links as $link ) {
$footerLink = $output->createDocumentFragment();
$footerLink->appendXML(
"<li>" .
$this->skin->data[$link] .
"</li>"
);
$footerLinks->appendChild( $footerLink );
}
}
$output->appendChild( $footerLinks );

// add footer icons
foreach( $this->skin->getFooterIcons("icononly")
as $blockName => $icons ) {
$footerIcons = $output->createElement('ul');
$footerIcons->setAttribute('class', 'horizontal pull-right');
foreach( $icons as $icon ) {
$footerIcon = $output->createDocumentFragment();
$footerIcon->appendXML(
"<li>" .
$this->skin->getSkin()->makeFooterIcon( $icon ) .
"</li>"
);
$footerIcons->appendChild( $footerIcon );
}
}
$output->appendChild( $footerIcons );

// output as HTML
if( $output ) echo $output->saveHTML();
}


/**
*
* Print the sidebar.
*
* @ingroup Skins
*/
public function renderSidebar() {
global $sgSidebarOptions;
$type = 'nav-' . $sgSidebarOptions['type'];

// parse and transform
$sidebar = $this->parseAndTransform( $sgSidebarOptions['page'], DIRNAME(__FILE__) .'/xsl/sidebar.xsl', array('nav-type'=>$type));

// output as HTML
$output = DOMDocument::loadXML( $sidebar );
if( $output ) echo $output->saveHTML();
}

/*
*
* Print the navbar.
*
* @ingroup Skins
*/
public function renderNavbar() {
global $sgNavbarOptions, $wgUser;
$navbar = $this->parseAndTransform( $sgNavbarOptions['page'], DIRNAME(__FILE__) .'/xsl/navbar.xsl');

$output = DOMDocument::loadXML( $navbar );

// continue rendering...
if( $output ) {
$finder = new DOMXPath( $output );

// setup brand link
$brand = $finder->query('//a[contains(@class,"brand")]')->item(0);
if( $brand ) {
$brand->setAttribute( 'href',
$this->skin->data['nav_urls']['mainpage']['href']
);
$siteName = new DOMText( $GLOBALS['wgSitename'] );
$brand->appendChild( $siteName );
}

// create the shared dropdown element
$dropdownBtn = $output->createElement('button');
$dropdownBtn->setAttribute('class', 'btn dropdown-toggle');
$dropdownBtn->setAttribute('data-toggle', 'dropdown');
$dropdownIcon = $output->createElement('span');
$dropdownIcon->setAttribute('class', 'caret');
$dropdownBtn->appendChild( $dropdownIcon );

// setup user tools
$userTool = $finder->query('//div[@id="user"]')->item(0);
if( $userTool ) {
// get user links
$userLinks = $this->skin->getPersonalTools();
$user = ( $wgUser->isLoggedIn() ) ? array_shift($userLinks) : array_pop($userLinks); //'userpage' or 'anonlogin', respectively
$userLink = $user['links'][0];

// create the user button
$userTxt = new DOMText( $userLink['text'] . ' ' );
$userBtn = $output->createElement('a');
$userBtn->setAttribute('href', $userLink['href']);
$userBtn->setAttribute('class', 'btn btn-warning');
$userBtn->appendChild( $userTxt );

$userIcon = $output->createElement('icon');
$icon = ( $wgUser->isLoggedIn() ) ? 'icon-user' : 'icon-signin';
$userIcon->setAttribute('class', $icon . ' icon-white');

$userBtn->appendChild( $userIcon );

// create user dropdown
$userCaret = $dropdownBtn->cloneNode( true );
$userCaret->setAttribute('class', $dropdownBtn->getAttribute('class') . ' btn-warning');

// create the dropdown links
$userDropdown = $this->renderDataLinks( $userLinks, 'dropdown-menu' );

// put it all together
$userTool->appendChild( $userBtn );
$userTool->appendChild( $userCaret);
$userTool->appendChild( $output->importNode($userDropdown, true) );
}

// update page tools
$pageTool = $finder->query('//div[@id="page"]')->item(0);
if( $pageTool ) {
// create the button
$pageBtn = $dropdownBtn->cloneNode( true );
$pageBtn->setAttribute('class', $dropdownBtn->getAttribute('class') . ' btn-info');
$pageIcon = $output->createElement('icon', ' ');
$pageIcon->setAttribute('class', 'icon-file icon-white');
$pageBtn->insertBefore( $pageIcon, $pageBtn->firstChild);

// create dropdown links
$pageDropdown = $this->renderDataLinks( $this->skin->data['content_actions'], 'dropdown-menu' );

// put it all together
$pageTool->appendChild( $pageBtn );
$pageTool->appendChild( $output->importNode($pageDropdown, true) );
}

// output as HTML
echo $output->saveHTML();
}
}

/**
*
* Parse the $wikipage, replace the special words, and then transform the resulting HTML fragment using XSLT $stylesheet
*
* @param String
* @param String
* @param Array
*
* @ingroup Skins
* @return String
**/
private function parseAndTransform( $wikipage, $stylesheet, $params=array() ) {
$html = new DOMDocument();

// parse wikitext
$content = BootstrapRenderer::parsePage( $wikipage );
if( $content ) {
$html->loadXML( $content->getText() );

$this->addSpecial( $html );

// load $stylesheet
$xslDoc = new DOMDocument();
$xslDoc->load( $stylesheet, LIBXML_NOENT|LIBXML_DTDLOAD);

// transform
$xslt = new XSLTProcessor();
if( isset($params) ) {
foreach( $params as $param=>$value ) {
$xslt->setParameter('', $param, $value);
}
}
$xslt->importStyleSheet( $xslDoc );
return $xslt->transformToXML( $html );
} else
return '';
}

/**
* Parse the wikitext page to HTML.
*
* @params String
* @return String
* @ingroup Skins
*/
private function parsePage( $page ) {
global $wgParser, $wgUser;
$pageTitle = Title::newFromText( $page );
$article = new Article( $pageTitle );
$raw = $article->getRawText();
if( $raw )
return $wgParser->parse( $raw, $pageTitle, ParserOptions::newFromUser($wgUser));
}

/**
* Render special Wiki words from DOMDocument.
*
* @ingroup Skins
*/
private function addSpecial( $doc ) {
// look for SPECIAL words in all list items
$finder = new DOMXPath( $doc );
$headerTextNodes = $finder->query( '//li/text()' );

// replace SPECIAL words
foreach( $headerTextNodes as $headerTextNode ) {
switch( trim( $headerTextNode->nodeValue ) ) {
case 'SEARCH':
$fragment= $this->renderSearch( $doc );
$headerTextNode->parentNode->replaceChild( $fragment, $headerTextNode );
break;
case 'TOOLBOX':
$fragment= $this->renderDataLinks( $this->skin->getToolbox() );
$headerTextNode->parentNode->appendChild( $doc->importNode($fragment,true) );
                                        $headerTextNode->nodeValue = 'Toolbox';
break;
case 'LANGUAGES':
if( $this->skin->data['language_urls'] ) {
$fragment = $this->renderDataLinks( $this->skin->data['language_urls'] );
$headerTextNode->parentNode->appendChild( $fragment );
} else
$headerTextNode->parentNode->removeChild( $headerTextNode );
break;
default:
break;
}
}
}

/**
* Render search form.
*
* @return DOMDocumentFragment
* @ingroup Skins
*/
private function renderSearch( $doc ) {
$fragment = $doc->createDocumentFragment();

$fragment->appendXml(
'<form action="' . $GLOBALS['wgScript'] .
'" class="search">' .
$this->skin->makeSearchInput( array(
'id'=>'searchInput',
'class'=>'search-query',
'placeholder'=>'Search'))
. '</form>' );

return $fragment;
}

/**
* Render Wiki link array to lists.
*
* @param Array
* @return DOMDocumentFragment
* @ingroup Skins
*/
private function renderDataLinks( $links, $class='' ) {
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();

if( is_array( $links ) ) {
$xml = '<ul class="' . $class .'">';
foreach( $links as $key => $val ) {
$xml .= $this->skin->makeListItem( $key, $val );
}
$xml .= '</ul>';
}
$fragment->appendXml( $xml );

return $fragment;
}

}
Something went wrong with that request. Please try again.