PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ContentRightSidebar.php

https://gitlab.com/GethN7/monaco-port
PHP | 210 lines | 157 code | 21 blank | 32 comment | 21 complexity | 93ca47cdae8b7fd5c7ac8eb7fb8f90bb MD5 | raw file
  1. <?php
  2. /**
  3. * Monaco Skin Right Sidebar Extension
  4. *
  5. * @package MediaWiki
  6. * @subpackage Skins
  7. *
  8. * @author Daniel Friesen
  9. * @author James Haley
  10. */
  11. if(!defined('MEDIAWIKI'))
  12. die( "This is an extension to the MediaWiki package and cannot be run standalone." );
  13. $wgExtensionCredits['skin'][] = array (
  14. 'path' => __FILE__,
  15. 'name' => 'ContentRightSidebar',
  16. 'author' => array('[http://mediawiki.org/wiki/User:Dantman Daniel Friesen]', '[http://doomwiki.org/wiki/User:Quasar James Haley]'),
  17. 'descriptionmsg' => 'contentrightsidebar-desc',
  18. 'url' => "https://github.com/haleyjd/monaco-port",
  19. );
  20. $wgExtensionMessagesFiles['ContentRightSidebar'] = dirname(__FILE__).'/ContentRightSidebar.i18n.php';
  21. /**
  22. * Resource module for ContentRightSidebar
  23. * haleyjd 20140423
  24. */
  25. $wgResourceModules['skins.monaco.ContentRightSidebar'] = array (
  26. 'scripts' => 'skins/monaco/style/js/ContentRightSidebar.js',
  27. );
  28. define('RIGHT_SIDEBAR_START_TOKEN', "<!-- RIGHT SIDEBAR START -->");
  29. define('RIGHT_SIDEBAR_END_TOKEN', "<!-- RIGHT SIDEBAR END -->");
  30. define('RIGHT_SIDEBAR_WITHBOX_TOKEN', "<!-- RIGHT SIDEBAR WITHBOX -->");
  31. define('RIGHT_SIDEBAR_TITLE_START_TOKEN', "<!-- RIGHT SIDEBAR TITLE START>");
  32. define('RIGHT_SIDEBAR_TITLE_END_TOKEN', "<RIGHT SIDEBAR TITLE END -->");
  33. define('RIGHT_SIDEBAR_CLASS_START_TOKEN', "<!-- RIGHT SIDEBAR CLASS START>");
  34. define('RIGHT_SIDEBAR_CLASS_END_TOKEN', "<RIGHT SIDEBAR CLASS END -->");
  35. define('RIGHT_SIDEBAR_CONTENT_START_TOKEN', "<!-- RIGHT SIDEBAR CONTENT START -->");
  36. define('RIGHT_SIDEBAR_CONTENT_END_TOKEN', "<!-- RIGHT SIDEBAR CONTENT END -->");
  37. class MonacoContentRightSidebar
  38. {
  39. /**
  40. * BeforePageDisplay hook handler
  41. * @author James Haley
  42. */
  43. public static function BeforePageDisplay(&$out, &$skin)
  44. {
  45. if($skin->getSkinName() == 'monaco')
  46. $out->addModules(array('skins.monaco.ContentRightSidebar'));
  47. }
  48. /**
  49. * Register parser extensions
  50. */
  51. public static function ContentRightSidebarRegisterParser(&$parser)
  52. {
  53. $parser->setHook('right-sidebar', 'MonacoContentRightSidebar::ContentRightSidebarTag');
  54. return true;
  55. }
  56. /**
  57. * Parser extension callback
  58. */
  59. public static function ContentRightSidebarTag($input, $arg, $parser, $frame)
  60. {
  61. $isContentTagged = false;
  62. $m = array();
  63. if(preg_match( '#^(.*)<content>(.*?)</content>(.*)$#is', $input, $m))
  64. {
  65. $isContentTagged = true;
  66. $startUniq = $parser->uniqPrefix() . "-right-sidebar-content-start-" . Parser::MARKER_SUFFIX;
  67. $endUniq = $parser->uniqPrefix() . "-right-sidebar-content-end-" . Parser::MARKER_SUFFIX;
  68. $input = "{$m[1]}{$startUniq}{$m[2]}{$endUniq}{$m[3]}";
  69. $input = $parser->recursiveTagParse( $input, $frame );
  70. $input = str_replace($startUniq, RIGHT_SIDEBAR_CONTENT_START_TOKEN, $input);
  71. $input = str_replace($endUniq, RIGHT_SIDEBAR_CONTENT_END_TOKEN, $input);
  72. }
  73. else
  74. {
  75. $input = $parser->recursiveTagParse( $input, $frame );
  76. }
  77. $with_box = (isset($arg["with-box"]) ? $arg["with-box"] : (isset($arg["withbox"]) ? $arg["withbox"] : null));
  78. $out = RIGHT_SIDEBAR_START_TOKEN;
  79. if($with_box && !in_array(strtolower($with_box), array("false", "off", "no", "none")))
  80. {
  81. $out .= RIGHT_SIDEBAR_WITHBOX_TOKEN;
  82. }
  83. if(isset($arg["title"]))
  84. {
  85. $out .= RIGHT_SIDEBAR_TITLE_START_TOKEN . urlencode($arg["title"]) . RIGHT_SIDEBAR_TITLE_END_TOKEN;
  86. }
  87. if(isset($arg["class"]))
  88. {
  89. $out .= RIGHT_SIDEBAR_CLASS_START_TOKEN . urlencode($arg["class"]) . RIGHT_SIDEBAR_CLASS_END_TOKEN;
  90. }
  91. if($isContentTagged)
  92. {
  93. $out .= $input;
  94. }
  95. else
  96. {
  97. $out .= '<div style="float: right; clear: right; position: relative;">';
  98. $out .= RIGHT_SIDEBAR_CONTENT_START_TOKEN . $input . RIGHT_SIDEBAR_CONTENT_END_TOKEN;
  99. $out .= '</div>';
  100. }
  101. $out .= RIGHT_SIDEBAR_END_TOKEN;
  102. return $out;
  103. }
  104. /**
  105. * Private function for extraction of the right sidebar content from an article
  106. */
  107. private static function ExtractRightSidebarBoxes(&$html)
  108. {
  109. $boxes = array();
  110. while(true)
  111. {
  112. $withBox = false;
  113. $title = '';
  114. $class = null;
  115. $start = strpos($html, RIGHT_SIDEBAR_START_TOKEN);
  116. if($start === false)
  117. break;
  118. $end = strpos($html, RIGHT_SIDEBAR_END_TOKEN, $start);
  119. if($end === false)
  120. break;
  121. $content = substr($html, $start, $end-$start);
  122. if(strpos($content, RIGHT_SIDEBAR_WITHBOX_TOKEN) !== false)
  123. {
  124. $withBox = true;
  125. }
  126. $startTitle = strpos($content, RIGHT_SIDEBAR_TITLE_START_TOKEN);
  127. if($startTitle !== false)
  128. {
  129. $endTitle = strpos($content, RIGHT_SIDEBAR_TITLE_END_TOKEN, $startTitle);
  130. if($endTitle !== false)
  131. {
  132. $title = urldecode(substr($content, $startTitle+strlen(RIGHT_SIDEBAR_TITLE_START_TOKEN), $endTitle-$startTitle-strlen(RIGHT_SIDEBAR_TITLE_START_TOKEN)));
  133. }
  134. }
  135. $startClass = strpos($content, RIGHT_SIDEBAR_CLASS_START_TOKEN);
  136. if($startClass !== false)
  137. {
  138. $endClass = strpos($content, RIGHT_SIDEBAR_CLASS_END_TOKEN, $startClass);
  139. if($endClass !== false)
  140. {
  141. $class = urldecode(substr($content, $startClass+strlen(RIGHT_SIDEBAR_CLASS_START_TOKEN), $endClass-$startClass-strlen(RIGHT_SIDEBAR_CLASS_START_TOKEN)));
  142. }
  143. }
  144. $contentStart = strpos($content, RIGHT_SIDEBAR_CONTENT_START_TOKEN);
  145. if($contentStart !== false)
  146. {
  147. $content = substr($content, $contentStart+strlen(RIGHT_SIDEBAR_CONTENT_START_TOKEN));
  148. }
  149. $contentEnd = strpos($content, RIGHT_SIDEBAR_CONTENT_END_TOKEN);
  150. if($contentStart !== false)
  151. {
  152. $content = substr($content, 0, $contentEnd);
  153. }
  154. $boxes[] = array( "with-box" => $withBox, "title" => $title, "class" => $class, "content" => $content );
  155. $html = substr($html, 0, $start) . substr($html, $end+strlen(RIGHT_SIDEBAR_END_TOKEN));
  156. }
  157. return $boxes;
  158. }
  159. /**
  160. * MonacoRightSidebar custom hook handler. This is invoked by the Monaco skin
  161. * to add the right sidebar. If this hook is not invoked, then right sidebar
  162. * content renders as a right-floating box inside the article.
  163. */
  164. public static function ContentRightSidebarMonacoRightSidebar( $sk )
  165. {
  166. $boxes = self::ExtractRightSidebarBoxes($sk->data['bodytext']);
  167. foreach($boxes as $box)
  168. {
  169. if($box["with-box"])
  170. {
  171. $attrs = array();
  172. if(isset($box["class"]))
  173. {
  174. $attrs["class"] = $box["class"];
  175. }
  176. $sk->sidebarBox($box["title"], $box["content"], $attrs);
  177. }
  178. else
  179. {
  180. echo $box["content"];
  181. }
  182. }
  183. return true;
  184. }
  185. }
  186. // Register hooks
  187. $wgHooks['BeforePageDisplay' ][] = 'MonacoContentRightSidebar::BeforePageDisplay';
  188. $wgHooks['ParserFirstCallInit'][] = 'MonacoContentRightSidebar::ContentRightSidebarRegisterParser';
  189. $wgHooks['MonacoRightSidebar' ][] = 'MonacoContentRightSidebar::ContentRightSidebarMonacoRightSidebar';