User:Goldbishop/Sidebar
|
| MediaWiki version: | 1.15 |
[edit] Reason
In response to one of my sites that I host, i needed to lock-down certain sections from the normal user/anon viewers. One way i did this was, to create two forms of the sidebar, Anon_sidebar and Sidebar. Obviously, the Sidebar form is hard-coded into the wiki site and I used this for those registerd users of the site. The second form, Anon_sidebar, is for those that are viewing the site and do not feel the need to register.
[edit] Sidebar
This form is very similar to most, I setup various sections of the sidebar. As my site has three navigation sections outside of the normal (information, toolbox, etc), I setup the main Parents in here, as such:
[edit] MediaWiki:Sidebar
* SEARCH * navigation ** mainpage|mainpage-description ** Production|Production ** Core|Core * source * project * Information ** portal-url|portal ** currentevents-url|currentevents ** recentchanges-url|recentchanges ** randompage-url|randompage ** helppage|help * TOOLBOX * LANGUAGES
[edit] MediaWiki:Anon_sidebar
(viewable at Silverlight 3D wiki)
* SEARCH * navigation ** Home|mainpage-description ** Administrativo|Production * information ** portal-url|portal ** currentevents-url|currentevents ** helppage|help
[edit] Skin.php
After developing these two pages, went into the Skin.php page and edited the following section (Manual:Interface/Sidebar#Change_sidebar_content_when_logged_in_.28PHP.29)
... function buildSidebar() { ... if( $wgUser->isLoggedIn() ){ $lines = explode( "\n", wfMsgForContent( 'sidebar' ) ); }else{ $lines = explode( "\n", wfMsgForContent( 'anon_sidebar' ) ); } ... } ...
[edit] Common.js
In reference to Manual:Interface/Sidebar#Add_or_remove_sections_.28JavaScript.29, I changed up the code tee-tiny bit to allow me to visibly manage what is available for the anon/users to the site.
Only part i really changed is the way the addOnloadHook() function is used, as you will see below. I will not repeat what has already been stated but will simply show you the changes that i made to accomidate what i was using it for.
This file can be accessed through your wiki via http://<your wiki address>/MediaWiki:Common.js
function ModifySidebar(action, section, name, link) { try { switch (section) { case "languages": var target = "p-lang"; break; case "toolbox": var target = "p-tb"; break; case "navigation": var target = "p-navigation"; break; default: var target = "p-" + section; var custom = true; break; } if (action == "addC") { var node = document.getElementById(target) .getElementsByTagName('div')[0] .getElementsByTagName('ul')[0]; var nodeelements = node.getElementsByTagName('li'); for (var i = 0; i < nodeelements.length; i++) { if (nodeelements[i].getElementsByTagName('a')[0].innerHTML == name || nodeelements[i].getElementsByTagName('a')[0].href == link) { node.removeChild(nodeelements[i]); } } var aNode = document.createElement('a'); var liNode = document.createElement('li'); aNode.appendChild(document.createTextNode(name)); aNode.setAttribute('href', link); liNode.appendChild(aNode); liNode.className='plainlinks'; node.appendChild(liNode); node.style.display = "visible"; } if (action == "removeC") { var list = document.getElementById(target) .getElementsByTagName('div')[0] .getElementsByTagName('ul')[0]; var listelements = list.getElementsByTagName('li'); for (var i = 0; i < listelements.length; i++) { if (listelements[i].getElementsByTagName('a')[0].innerHTML == name || listelements[i].getElementsByTagName('a')[0].href == link) { list.removeChild(listelements[i]); } } } if (action == "removeP"){ var node = document.getElementById(target); node.style.visibility = "hidden"; } } catch(e) { // lets just ignore what's happened return; } } function AdminSidebar() { ModifySidebar("addC", "source", "Design", "http://sl3d.woodassoc.us/wiki/Source:Design"); ModifySidebar("addC", "source", "Conceptual", "http://sl3d.woodassoc.us/wiki/Source:Conceptual"); ModifySidebar("addC", "source", "Layout", "http://sl3d.woodassoc.us/wiki/Source:Layout"); ModifySidebar("addC", "project", "Design", "http://sl3d.woodassoc.us/wiki/Silverlight_3D:Design"); ModifySidebar("addC", "project", "Conceptual", "http://sl3d.woodassoc.us/wiki/Silverlight_3D:Conceptual"); ModifySidebar("addC", "project", "Layout", "http://sl3d.woodassoc.us/wiki/Silverlight_3D:Layout"); ModifySidebar("addC", "toolbox", "Upload file", "http://sl3d.woodassoc.us/wiki/Special:Upload"); } function UserSidebar(){ ModifySidebar("removeP", "toolbox", "toolbox", null); } if (isArray(wgUserGroups)){ if (wgUserGroups.Contains('bureaucrat') || wgUserGroups.Contains('sysop')){ //If the user logged in is a Bureaucrat or Sysop addOnloadHook(AdminSidebar); }else{ //If the user logged in is not a Bureaucrat or Sysop addOnloadHook(UserSidebar); } }else{ //If an anonymous user is viewing the site addOnloadHook(UserSidebar); }
I changed the "add" and "remove" actions to "addC", "addP", "removeC" and "removeP" only to clarify if the items being added/removed was a Child (C) or Parent (P).
Parent nodes are those that contain mutliple children, such as "navigation", "toolbox", "information", etc. Child nodes are those that are contained within a Parent node.
WORD OF ADVISE
If you are looking to have several parent-child-grandchild relationships, it is best to get an extension that utilizes ajax or java to overlay a menu structure. When using the Sidebar, it should be noted that it should contain only parent-child relationships, going only 1 deep. Any further than that and the reader may not be able to effectively browse your content. Think of the Sidebar as more of a Category-Topic relationship. As long as you keep to a simple 1 tier menu structure, this hack should work just fine.
[edit] Suggested Extension(s)
For further lockdown by action (create, edit, delete, etc), it has been suggested to me to use an extension that post-processes the wiki information before being shown to the user. I personally used th(is/ese) on my wiki sites and have had a relatively good outcome. Most of the time the not-so-good outcomes were due to my lack of knowledge of the action groups and as such learned everything i could about that and then the results came out just fine and as i expected.