r113256 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113255‎ | r113256 | r113257 >
Date:17:32, 7 March 2012
Author:netbrain
Status:deferred
Tags:
Comment:
added some constants instead of referencing same strings over and over again.
Modified paths:
  • /trunk/extensions/SideBarMenu/SideBarMenu.hooks.php (modified) (history)
  • /trunk/extensions/SideBarMenu/SideBarMenu.php (modified) (history)
  • /trunk/extensions/SideBarMenu/includes/MenuItem.php (modified) (history)
  • /trunk/extensions/SideBarMenu/includes/MenuParser.php (modified) (history)
  • /trunk/extensions/SideBarMenu/js/ext.sidebarmenu.js (modified) (history)
  • /trunk/extensions/SideBarMenu/test/MenuParserTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SideBarMenu/test/MenuParserTest.php
@@ -3,86 +3,95 @@
44 class MenuParserTest extends MediaWikiTestCase
55 {
66
 7+ private $menuParser;
 8+
 9+ protected function setUp()
 10+ {
 11+ $this->menuParser = new MenuParser(true);
 12+ }
 13+
714 public function testValidInputWhenNull(){
8 - $this->assertFalse(MenuParser::isValidInput(null));
 15+ $this->assertFalse($this->menuParser->isValidInput(null));
916 }
1017
1118 public function testValidInputWhenEmpty(){
12 - $this->assertFalse(MenuParser::isValidInput(""));
 19+ $this->assertFalse($this->menuParser->isValidInput(""));
1320 }
1421
1522 public function testValidInput(){
16 - $this->assertTrue(MenuParser::isValidInput("+MenuItem"));
 23+ $this->assertTrue($this->menuParser->isValidInput("+MenuItem"));
1724 }
1825
1926 public function testGetLevelWhenNull(){
20 - $this->assertEquals(0,MenuParser::getLevel(null));
 27+ $this->assertEquals(0,$this->menuParser->getLevel(null));
2128 }
2229
2330 public function testGetLevelWhenEmpty(){
24 - $this->assertEquals(0,MenuParser::getLevel(""));
 31+ $this->assertEquals(0,$this->menuParser->getLevel(""));
2532 }
2633
2734 public function testGetLevelWhenValidButNoStars(){
28 - $this->assertEquals(0,MenuParser::getLevel(""));
 35+ $this->assertEquals(0,$this->menuParser->getLevel(""));
2936 }
3037
3138 public function testGetLevelWithValid(){
32 - $this->assertEquals(3,MenuParser::getLevel("***MenuItem"));
 39+ $this->assertEquals(3,$this->menuParser->getLevel("***MenuItem"));
3340 }
3441
3542 public function testGetExpandedParameterWhenNoneSupplied(){
36 - //default is false
37 - $this->assertFalse(MenuParser::getExpandedParameter("MenuItem"));
 43+ $this->menuParser = new MenuParser(true);
 44+ $this->assertTrue($this->menuParser->getExpandedParameter("MenuItem"));
 45+ $this->menuParser = new MenuParser(false);
 46+ $this->assertFalse($this->menuParser->getExpandedParameter("MenuItem"));
3847 }
3948
4049 public function testGetExpandedParameterWhenNotExpanded(){
41 - $this->assertFalse(MenuParser::getExpandedParameter("-MenuItem"));
 50+ $this->assertFalse($this->menuParser->getExpandedParameter("-MenuItem"));
4251 }
4352
4453 public function testGetExpandedParameterWhenExpanded(){
45 - $this->assertTrue(MenuParser::getExpandedParameter("+MenuItem"));
 54+ $this->assertTrue($this->menuParser->getExpandedParameter("+MenuItem"));
4655 }
4756
4857 public function testGetTextParameter(){
49 - $this->assertEquals("MenuItem",MenuParser::getTextParameter("+***MenuItem"));
50 - $this->assertEquals("+MenuItem",MenuParser::getTextParameter("+***+MenuItem"));
51 - $this->assertEquals("MenuItem",MenuParser::getTextParameter("-MenuItem"));
52 - $this->assertEquals("MenuItem",MenuParser::getTextParameter("-*MenuItem"));
53 - $this->assertEquals("MenuItem",MenuParser::getTextParameter("MenuItem"));
54 - $this->assertEquals("+*MenuItem",MenuParser::getTextParameter("+***+*MenuItem"));
 58+ $this->assertEquals("MenuItem",$this->menuParser->getTextParameter("+***MenuItem"));
 59+ $this->assertEquals("+MenuItem",$this->menuParser->getTextParameter("+***+MenuItem"));
 60+ $this->assertEquals("MenuItem",$this->menuParser->getTextParameter("-MenuItem"));
 61+ $this->assertEquals("MenuItem",$this->menuParser->getTextParameter("-*MenuItem"));
 62+ $this->assertEquals("MenuItem",$this->menuParser->getTextParameter("MenuItem"));
 63+ $this->assertEquals("+*MenuItem",$this->menuParser->getTextParameter("+***+*MenuItem"));
5564 }
5665
5766 public function testGetMenuItemWhenInputIsNull(){
5867 $this->setExpectedException('InvalidArgumentException');
59 - $this->assertNull(MenuParser::getMenuItem(null));
 68+ $this->assertNull($this->menuParser->getMenuItem(null));
6069 }
6170
6271 public function testGetMenuItemWhenInputIsEmpty(){
6372 $this->setExpectedException('InvalidArgumentException');
64 - $this->assertNull(MenuParser::getMenuItem(""));
 73+ $this->assertNull($this->menuParser->getMenuItem(""));
6574 }
6675
6776 public function testGetMenuItemWhenInputIsValid(){
6877 $data = "MenuItem";
69 - $menuItem = MenuParser::getMenuItem($data);
 78+ $menuItem = $this->menuParser->getMenuItem($data);
7079 $this->assertNotNull($menuItem);
7180 $this->assertEquals($data,$menuItem->getText());
72 - $this->assertFalse($menuItem->isExpanded()); //false is default
 81+ $this->assertTrue($menuItem->isExpanded());
7382 }
7483
7584 public function testGetMenuItemWhenInputIsValidAndExpandIsSet(){
7685 $text = "MenuItem";
7786 $data = "+".$text;
78 - $menuItem = MenuParser::getMenuItem($data);
 87+ $menuItem = $this->menuParser->getMenuItem($data);
7988 $this->assertNotNull($menuItem);
8089 $this->assertEquals($text,$menuItem->getText());
81 - $this->assertTrue($menuItem->isExpanded()); //false is default
 90+ $this->assertTrue($menuItem->isExpanded());
8291 }
8392
8493 public function testParseDataIntoHierarchicalArray(){
8594 $data = "MenuItem";
86 - $array = MenuParser::parseDataIntoHierarchicalArray($data);
 95+ $array = $this->menuParser->parseDataIntoHierarchicalArray($data);
8796 $this->assertNotNull($array);
8897 $this->assertEquals($data,$array[0]);
8998 }
@@ -90,7 +99,7 @@
91100 public function testParseDataIntoHierarchicalArrayWithSubLevel(){
92101 $lines = array("MenuItem","*SubMenuItem");
93102 $data = join("\n",$lines);
94 - $array = MenuParser::parseDataIntoHierarchicalArray($data);
 103+ $array = $this->menuParser->parseDataIntoHierarchicalArray($data);
95104 $this->assertNotNull($array);
96105 $this->assertArrayHasKey($lines[0],$array);
97106 $this->assertEquals(
@@ -105,7 +114,7 @@
106115 public function testParseDataIntoHierarchicalArrayWithSeveralSubLevels(){
107116 $lines = array("MenuItem","*SubMenuItem","*SubMenuItem2","**SubMenuItemOf2");
108117 $data = join("\n",$lines);
109 - $array = MenuParser::parseDataIntoHierarchicalArray($data);
 118+ $array = $this->menuParser->parseDataIntoHierarchicalArray($data);
110119 $this->assertNotNull($array);
111120 $this->assertEquals(
112121 array(
@@ -122,7 +131,7 @@
123132 public function testParseDataIntoHierarchicalArrayWithSubLevelAndBack(){
124133 $lines = array("MenuItem","*SubMenuItem","MenuItem2");
125134 $data = join("\n",$lines);
126 - $array = MenuParser::parseDataIntoHierarchicalArray($data);
 135+ $array = $this->menuParser->parseDataIntoHierarchicalArray($data);
127136 $this->assertNotNull($array);
128137 $this->assertEquals(
129138 array(
@@ -137,7 +146,7 @@
138147 public function testParseDataIntoHierarchicalArrayWithSubLevelAndBackSeveralLevels(){
139148 $lines = array("MenuItem","*SubMenuItem1","**SubMenuItem2","***SubMenuItem3","MenuItem2");
140149 $data = join("\n",$lines);
141 - $array = MenuParser::parseDataIntoHierarchicalArray($data);
 150+ $array = $this->menuParser->parseDataIntoHierarchicalArray($data);
142151 $this->assertNotNull($array);
143152 $this->assertEquals(
144153 array(
@@ -155,12 +164,12 @@
156165
157166
158167 public function testGetMenuWithInvalidInput(){
159 - $this->assertNull(MenuParser::getMenuTree(null));
160 - $this->assertNull(MenuParser::getMenuTree(""));
 168+ $this->assertNull($this->menuParser->getMenuTree(null));
 169+ $this->assertNull($this->menuParser->getMenuTree(""));
161170 }
162171
163172 public function testGetMenuWithValidInput(){
164 - $menu = MenuParser::getMenuTree("MenuItem");
 173+ $menu = $this->menuParser->getMenuTree("MenuItem");
165174 $this->assertNotNull($menu);
166175 $this->assertTrue($menu->isRoot());
167176 $this->assertEquals(1,sizeof($menu->getChildren()));
@@ -180,7 +189,7 @@
181190 'MenuItem2',
182191 '*SubMenuItem1OfMenuItem2'
183192 );
184 - $menu = MenuParser::getMenuTree(join("\n",$data));
 193+ $menu = $this->menuParser->getMenuTree(join("\n",$data));
185194 $this->assertNotNull($menu);
186195 $this->assertEquals(2,sizeof($menu->getChildren()));
187196
@@ -193,7 +202,7 @@
194203 '',
195204 ''
196205 );
197 - $menu = MenuParser::getMenuTree(join("\n",$data));
 206+ $menu = $this->menuParser->getMenuTree(join("\n",$data));
198207 $this->assertNotNull($menu);
199208 $this->assertEquals(1,sizeof($menu->getChildren()));
200209
Index: trunk/extensions/SideBarMenu/SideBarMenu.php
@@ -4,6 +4,19 @@
55 die('Not an entry point.');
66 }
77
 8+//SideBarMenu constants
 9+const SBM_EXPANDED = 'parser.menuitem.expanded';
 10+const SBM_CONTROLS_SHOW = 'controls.show';
 11+const SBM_CONTROLS_HIDE = 'controls.hide';
 12+const SBM_JS_ANIMATE = 'js.animate';
 13+
 14+
 15+//default settings
 16+$wgSideBarMenuConfig[SBM_CONTROLS_SHOW] = null;
 17+$wgSideBarMenuConfig[SBM_CONTROLS_HIDE]= null;
 18+$wgSideBarMenuConfig[SBM_JS_ANIMATE] = true;
 19+$wgSideBarMenuConfig[SBM_EXPANDED] = true;
 20+
821 $wgExtensionCredits['parserhook'][] = array(
922 'path' => __FILE__,
1023 'name' => 'SideBarMenu',
@@ -54,9 +67,4 @@
5568 'group' => 'ext.sidebarmenu',
5669 'localBasePath' => dirname( __FILE__ ),
5770 'remoteExtPath' => 'SideBarMenu'
58 -);
59 -
60 -//default settings
61 -$wgSideBarMenuConfig['controls.show'] = null;
62 -$wgSideBarMenuConfig['controls.hide']= null;
63 -$wgSideBarMenuConfig['js.animate'] = true;
\ No newline at end of file
 71+);
\ No newline at end of file
Index: trunk/extensions/SideBarMenu/SideBarMenu.hooks.php
@@ -2,6 +2,8 @@
33
44 class SideBarMenuHooks
55 {
 6+
 7+
68 public static function init(Parser &$parser){
79 $parser->setHook('sidebarmenu','SideBarMenuHooks::renderFromTag');
810 return true;
@@ -10,16 +12,20 @@
1113 public static function renderFromTag( $input, array $args, Parser $parser, PPFrame $frame ){
1214 $parser->getOutput()->addModules('ext.sidebarmenu.core');
1315
 16+ //default settings
 17+ $config = self::getTagConfig($args);
 18+
1419 $output = '<div class="sidebar-menu-container">';
1520 try{
16 - $output .= MenuParser::getMenuTree($input)->toHTML();
 21+ $menuParser = new MenuParser($config[SBM_EXPANDED]);
 22+ $output .= $menuParser->getMenuTree($input)->toHTML();
1723 }catch(Exception $x){
1824 wfDebug("An error occured during parsing of: '$input' caught exception: $x");
1925 return wfMsg('parser.input-error',$x->getMessage());
2026 }
2127 $output .= '</div>';
2228
23 - $jsOutput = self::getJSConfig($args);
 29+ $jsOutput = self::getJSConfig($config);
2430
2531 return array( $jsOutput.$parser->recursiveTagParse($output,$frame), 'noparse' => true, 'isHTML' => true );
2632 }
@@ -41,40 +47,45 @@
4248 return true;
4349 }
4450
45 - private static function minifyJavascript(&$js)
 51+ private static function minifyJavascript($js)
4652 {
4753 $js = preg_replace("/[\n\r]/", "", $js); //remove newlines
4854 $js = preg_replace("/[\s]{2,}/", " ", $js); //remove spaces
 55+
 56+ return $js;
4957 }
5058
5159 private static function getAsJSEncodedString($s)
5260 {
53 - return "'" . $s . "'";
 61+ return "'$s'";
5462 }
5563
56 - private static function getJSConfig(&$args){
57 - global $wgSideBarMenuConfig;
58 - //default settings
59 - $defaults['controls.show'] = isset($wgSideBarMenuConfig['controls.show']) ? $wgSideBarMenuConfig['controls.show'] : wfMsg('controls.show');
60 - $defaults['controls.hide'] = isset($wgSideBarMenuConfig['controls.hide']) ? $wgSideBarMenuConfig['controls.hide'] : wfMsg('controls.hide');
61 - $defaults['js.animate'] = $wgSideBarMenuConfig['js.animate'];
62 -
 64+ private static function getJSConfig($config){
6365 //javascript config output
6466 $jsOutput = Html::inlineScript("
6567 var sidebarmenu = {
6668 config: {
6769 controls: {
68 - show: " . (array_key_exists('controls.show', $args) ? self::getAsJSEncodedString($args['controls.show']) : self::getAsJSEncodedString($defaults['controls.show'])) . ",
69 - hide: " . (array_key_exists('controls.hide', $args) ? self::getAsJSEncodedString($args['controls.hide']) : self::getAsJSEncodedString($defaults['controls.hide'])) . "
 70+ show: " . self::getAsJSEncodedString($config[SBM_CONTROLS_SHOW]) . ",
 71+ hide: " . self::getAsJSEncodedString($config[SBM_CONTROLS_HIDE]) . "
7072 },
7173 js: {
72 - animate: " . (array_key_exists('js.animate', $args) ? is_bool($args['js.animate']) ? 'true' : 'false' : $defaults['js.animate']) . "
 74+ animate: " . (filter_var($config[SBM_JS_ANIMATE], FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false') . "
7375 }
7476 }
7577 };
7678 ");
7779 //minify js to prevent <p> tags to be rendered
78 - self::minifyJavascript($jsOutput);
79 - return $jsOutput;
 80+ return self::minifyJavascript($jsOutput);
8081 }
 82+
 83+ private static function getTagConfig($args)
 84+ {
 85+ global $wgSideBarMenuConfig;
 86+ $config[SBM_EXPANDED] = array_key_exists(SBM_EXPANDED, $args) ? filter_var($args[SBM_EXPANDED], FILTER_VALIDATE_BOOLEAN) : $wgSideBarMenuConfig[SBM_EXPANDED];
 87+ $config[SBM_CONTROLS_SHOW] = array_key_exists(SBM_CONTROLS_SHOW, $args) ? $args[SBM_CONTROLS_SHOW] : (isset($wgSideBarMenuConfig[SBM_CONTROLS_SHOW]) ? $wgSideBarMenuConfig[SBM_CONTROLS_SHOW] : wfMsg(SBM_CONTROLS_SHOW));
 88+ $config[SBM_CONTROLS_HIDE] = array_key_exists(SBM_CONTROLS_HIDE, $args) ? $args[SBM_CONTROLS_HIDE] : (isset($wgSideBarMenuConfig[SBM_CONTROLS_HIDE]) ? $wgSideBarMenuConfig[SBM_CONTROLS_HIDE] : wfMsg(SBM_CONTROLS_HIDE));
 89+ $config[SBM_JS_ANIMATE] = array_key_exists(SBM_JS_ANIMATE, $args) ? $args[SBM_JS_ANIMATE] : $wgSideBarMenuConfig[SBM_JS_ANIMATE];
 90+ return $config;
 91+ }
8192 }
Index: trunk/extensions/SideBarMenu/includes/MenuParser.php
@@ -1,65 +1,74 @@
22 <?php
33 class MenuParser {
44
5 - public static function isValidInput($data)
 5+ private $expandedByDefault;
 6+
 7+ function __construct($expandedByDefault)
68 {
 9+ $this->expandedByDefault = $expandedByDefault;
 10+ }
 11+
 12+
 13+ public function isValidInput($data)
 14+ {
715 return !(is_null($data) || strlen($data) == 0);
816 }
917
1018
11 - public static function getLevel($line)
 19+ public function getLevel($line)
1220 {
1321 return substr_count($line, '*');
1422 }
1523
16 - public static function getExpandedParameter($line)
 24+ public function getExpandedParameter($line)
1725 {
18 - if (self::startsWith($line, '+')) {
 26+ if ($this->startsWith($line, '+')) {
1927 return true;
20 - } else {
 28+ } else if($this->startsWith($line, '-')) {
2129 return false;
2230 }
 31+ return $this->expandedByDefault;
2332 }
2433
25 - public static function getTextParameter($line)
 34+ public function getTextParameter($line)
2635 {
2736 return preg_filter("/\+|\-?\**(.*)/", "$1", $line);
2837 }
2938
3039
31 - public static function getMenuTree($data){
32 - if(self::isValidInput($data)){
33 - $data = self::cleanupData($data);
 40+ public function getMenuTree($data){
 41+ if($this->isValidInput($data)){
 42+ $data = $this->cleanupData($data);
3443 $root = new MenuItem();
3544 $root->setExpanded(true);
36 - $arrayData = self::parseDataIntoHierarchicalArray($data);
37 - self::addChildrenToMenuItemFromArrayData($root,$arrayData);
 45+ $arrayData = $this->parseDataIntoHierarchicalArray($data);
 46+ $this->addChildrenToMenuItemFromArrayData($root,$arrayData);
3847 return $root;
3948 }
4049 }
4150
4251
43 - public static function getMenuItem($line)
 52+ public function getMenuItem($line)
4453 {
4554 $line = trim($line);
46 - if(self::isValidInput($line)){
 55+ if($this->isValidInput($line)){
4756 $menuItem = new MenuItem();
48 - $menuItem->setExpanded(self::getExpandedParameter($line));
49 - $menuItem->setText(self::getTextParameter($line));
 57+ $menuItem->setExpanded($this->getExpandedParameter($line));
 58+ $menuItem->setText($this->getTextParameter($line));
5059 return $menuItem;
5160 }else{
5261 throw new InvalidArgumentException();
5362 }
5463 }
5564
56 - public static function parseDataIntoHierarchicalArray($data)
 65+ public function parseDataIntoHierarchicalArray($data)
5766 {
5867 $rootArray = array();
5968 $prevArray = &$rootArray;
6069 $prevLevel = 0;
6170 $levelArray[0] = &$rootArray;
6271 foreach(preg_split("/\n/",$data) as $line){
63 - $level = self::getLevel($line);
 72+ $level = $this->getLevel($line);
6473 if($level == $prevLevel){
6574 $levelArray[$level][] = $line;
6675 }else if($level-1 == $prevLevel){
@@ -79,15 +88,15 @@
8089 return $rootArray;
8190 }
8291
83 - public static function addChildrenToMenuItemFromArrayData(&$rootMenuItem,$arrayData)
 92+ public function addChildrenToMenuItemFromArrayData(&$rootMenuItem,$arrayData)
8493 {
8594 foreach ($arrayData as $key => $val) {
8695 if (is_string($key)) {
87 - $menuItem = self::getMenuItem($key);
 96+ $menuItem = $this->getMenuItem($key);
8897 $rootMenuItem->addChild($menuItem);
89 - self::addChildrenToMenuItemFromArrayData($menuItem,$val);
 98+ $this->addChildrenToMenuItemFromArrayData($menuItem,$val);
9099 } else {
91 - $menuItem = self::getMenuItem($val);
 100+ $menuItem = $this->getMenuItem($val);
92101 $rootMenuItem->addChild($menuItem);
93102 }
94103 }
Index: trunk/extensions/SideBarMenu/includes/MenuItem.php
@@ -2,7 +2,7 @@
33
44 class MenuItem
55 {
6 - private $expanded;
 6+ private $expanded = false;
77 private $children = array();
88 private $parent = null;
99 private $text;
@@ -10,7 +10,11 @@
1111
1212 public function setExpanded($expanded)
1313 {
14 - $this->expanded = $expanded;
 14+ if(is_null($expanded)){
 15+ throw new InvalidArgumentException();
 16+ }else{
 17+ $this->expanded = $expanded;
 18+ }
1519 }
1620
1721 public function isExpanded()
Index: trunk/extensions/SideBarMenu/js/ext.sidebarmenu.js
@@ -1,5 +1,5 @@
22 $(document).ready(function(){
3 - if(sidebarmenu != undefined){
 3+ if(typeof(sidebarmenu) !== 'undefined'){
44 var showText = sidebarmenu.config.controls.show;
55 var hideText = sidebarmenu.config.controls.hide;
66 var useAnimations = sidebarmenu.config.js.animate;

Status & tagging log