STOWiki:Vector skin/STOWikiVector

STOWikiVector.php
 * @copyright Copyright � 2012 Shawn Bruckner * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */

// If this is run directly from the web die as this is not a valid entry point. if( !defined( 'MEDIAWIKI' ) ) die( 'Invalid entry point.' );

$wgExtensionCredits['other'][] = array( 'name'           => 'STOWikiVector',  'url'            => 'http://www.stowiki.org/',   'author'         => 'Shawn Bruckner',   'description'    => 'This simple extension allows for random backgrounds selected from ' .                       'a list in MediaWiki:Sto-vector-backgrounds and provides an embedded font for an LCARS look.',  'descriptionmsg' => 'stowikivector-desc',  'version'        => 1.0, ); $wgExtensionMessagesFiles['STOWikiVector'] = dirname ( __FILE__ ). '/STOWikiVector.i18n.php';

$wgAutoloadClasses['STOWikiVector'] = dirname ( __FILE__ ). '/STOWikiVector.body.php';

$wgHooks['BeforePageDisplay'][] = 'STOWikiVector::execute'; ?>

STOWikiVector.i18n.php
 * @copyright Copyright � 2012 Shawn Bruckner * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */ // If this is run directly from the web die as this is not a valid entry point. if( !defined( 'MEDIAWIKI' ) ) die( 'Invalid entry point.' );

$messages = array; $messages['en'] = array( /* General messages */  'stowikivector'                 => 'STOWiki Vector',  'stowikivector-desc'            => 'This simple extension allows for random backgrounds selected from ' .                                      'a list in MediaWiki:Sto-vector-backgrounds and provides an embedded font ' .                                     'for an LCARS look.', ); ?>

STOWikiVector.body.php
 * @copyright Copyright � 2012 Shawn Bruckner * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */

// If this is run directly from the web die as this is not a valid entry point. if( !defined( 'MEDIAWIKI' ) ) die( 'Invalid entry point.' );

class STOWikiVector { /**  * Adds the embedded font and chooses the random background if this is the vector skin. * @param OutputPage $out The output page object. * @param Skin $skin The current skin. * @return bool Returns true to tell MediaWiki processing was successful. */ static public function execute( &$out, &$skin ) { if ( $skin->getSkinName === 'vector' ) { self::addEmbeddedFont( $out ); self::chooseBackgroundImage( $out ); }   return true; } /**   * Adds a @font-face declaration inline. * @param OutputPage $out The output page object. */ static public function addEmbeddedFont( &$out ) { global $wgScriptPath; $css = "@font-face { ". "font-family: 'EnigmaticRegular'; ". "src: url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma__2-webfont.eot'); ". "src: url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma__2-webfont.eot?#iefix') ". "format('embedded-opentype'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma__2-webfont.woff') format('woff'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma__2-webfont.ttf') format('truetype'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma__2-webfont.svg#EngimaticRegular') ". "format('svg'); ". "font-weight: normal; ". "font-style: normal; }". "@font-face { ". "font-family: 'EnigmaticItalic'; ". "src: url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma_2i-webfont.eot'); ". "src: url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma_2i-webfont.eot?#iefix') ". "format('embedded-opentype'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma_2i-webfont.woff') format('woff'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma_2i-webfont.ttf') format('truetype'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/Enigma_2i-webfont.svg#EngimaticItalic') ". "format('svg'); ". "font-weight: normal; ". "font-style: normal; }". "@font-face { ". "font-family: 'EnigmaticBold'; ". "src: url('" . $wgScriptPath. "/extensions/STOWikiVector/EnigmaB_2-webfont.eot'); ". "src: url('" . $wgScriptPath. "/extensions/STOWikiVector/EnigmaB_2-webfont.eot?#iefix') ". "format('embedded-opentype'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/EnigmaB_2-webfont.woff') format('woff'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/EnigmaB_2-webfont.ttf') format('truetype'), ". "url('" . $wgScriptPath. "/extensions/STOWikiVector/EnigmaB_2-webfont.svg#EngimaticBold') ". "format('svg'); ". "font-weight: normal; ". "font-style: normal; }"; $out->addInlineStyle( $css ); } /**   * Attempts to choose a valid random background and adds it to the page. * @param OutputPage $out The output page object. */ static public function chooseBackgroundImage( &$out ) { $image = self::getRandomBackgroundImage; if ( $image !== '' ) { $out->addInlineStyle( "body { background-image: url('". $image. "'); }" );   }  }  /**   * Processes the sto-vector-backgrounds message and selects a background image from it. * @return string The url to the image, or an empty string if this fails or image is not protected. */ static public function getRandomBackgroundImage { $imagesMsg = wfMessage( 'sto-vector-backgrounds' )->inContentLanguage->text; $imagesMsg = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $imagesMsg ); /* remove empty lines */ $imagesMsg = str_replace( "\r", "\n", str_replace( "\r\n", "\n", $imagesMsg ) ); /* all newlines should be '\n' */ $images = explode( "\n", $imagesMsg );

if ( count ( $images ) > 0 ) { $image = $images[ array_rand( $images )]; if ( strtolower( substr( $image, 0, 5 ) ) === 'file:' ) { return self::getImageURL( substr( $image, 5 ) ); } elseif ( strtolower( substr( $image, 0, 6 ) ) === 'image:' ) { return self::getImageURL( substr( $image, 6 ) ); } elseif ( strtolower( substr( $image, 0, 6 ) ) === ':file:' ) { return self::getImageURL( substr( $image, 6 ) ); } elseif ( strtolower( substr( $image, 0, 7 ) ) === ':image:' ) { return self::getImageURL( substr( $image, 7 ) ); } else { return $image; }   } else { return ""; } }  /**   * If given a wiki page title to an image page, returns the url if the image is protected. * @param string $image The wiki page title. * @return string The url to the image, or empty string if it fails or image is not protected. */ static public function getImageURL( $image ) { $title = Title::makeTitle( NS_IMAGE, $image ); if ( !( $title->isProtected ) ) { return ''; } else { $file = wfFindFile( $title ); if ( $file ) { return $file->getURL; } else { return ""; }   }  } } ?>