MediaWiki:Common2.js
[http://www.wikizamki.org/links.htm]
(Различия между версиями)
Elden (обсуждение | вклад) |
Elden (обсуждение | вклад) |
||
| Строка 289: | Строка 289: | ||
addOnloadHook(CustomizeModificationsOfSidebar); | addOnloadHook(CustomizeModificationsOfSidebar); | ||
| + | |||
| + | |||
| + | addOnloadHook(CustomizeModificationsOfSidebar); | ||
| + | |||
| + | to | ||
| + | |||
| + | if (isArray(wgUserGroups)) { | ||
| + | if (wgUserGroups.Contains('bureaucrat')) { | ||
| + | addOnloadHook(CustomizeModificationsOfSidebar); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | and add | ||
| + | |||
| + | function isObject(obj) { | ||
| + | if (typeof obj == "object" && obj != null) | ||
| + | return true; | ||
| + | else | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | function isArray(obj) { | ||
| + | if (isObject(obj)) { | ||
| + | if (obj.constructor.toString().indexOf("Array") == -1) | ||
| + | return false; | ||
| + | else | ||
| + | return true; | ||
| + | } | ||
| + | else | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | Array.prototype.Contains = function(element,strict) { | ||
| + | for(i in this) { | ||
| + | if(this[i] == element && !strict) return true; | ||
| + | else if(this[i] === element) return true; | ||
| + | } | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | For restricting the modifications to IPs instead of a specific usergroup use | ||
| + | |||
| + | if (!isArray(wgUserGroups)) { | ||
| + | addOnloadHook(CustomizeModificationsOfSidebar); | ||
| + | } | ||
| + | |||
| + | [править] Sidebar width (CSS) | ||
| + | |||
| + | * Bug: only works with the default MonoBook skin. | ||
| + | |||
| + | You can change the width of your sidebar by adding the following CSS rules to your MediaWiki:Common.css article (MediaWiki:Monobook.css if pre 1.9), note that this is an article not a file. This changes the width to 15em, the actions position and portlet width should be an em or so less, so I've set them to 14em in this example. | ||
| + | |||
| + | #column-content { margin-left: -15em } | ||
| + | #content { margin-left: 15em } | ||
| + | #p-logo a, #p-logo a:hover { width: 15em } | ||
| + | .portlet { width: 14em } | ||
| + | #p-cactions { left: 14em } | ||
| + | |||
| + | [править] Change sidebar content when logged in (PHP) | ||
| + | |||
| + | To get anonymous users to have their own sidebars change includes/Skin.php. There is a function called buildSidebar. Edit this function to check for $wgUser->isLoggedIn() and call a different system message than sidebar. | ||
| + | |||
| + | if ($wgUser->isLoggedIn()) { | ||
| + | $lines = explode( "\n", wfMsgForContent( 'sidebar' ) ); | ||
| + | } else { | ||
| + | $lines = explode( "\n", wfMsgForContent( 'anon_sidebar' ) ); | ||
| + | } | ||
| + | |||
| + | Then in your Wiki, go to MediaWiki:anon_sidebar and create your new sidebar. You may need to add $wgUser to the list of global variables in the buildSidebar(). | ||
| + | |||
| + | You'll also have to add $wgUser in the globals declaration of the function. | ||
| + | |||
| + | A better solution is to modify the sidebar per hook. You don't have to change the Skin.php for this. Just create a simple extension like this one: | ||
| + | |||
| + | <?php | ||
| + | $wgHooks['SkinBuildSidebar'][] = 'efHideSidebar'; | ||
| + | |||
| + | function efHideSidebar($skin, &$bar) { | ||
| + | global $wgUser; | ||
| + | // Hide sidebar for anonymous users | ||
| + | if (!$wgUser->isLoggedIn()) { | ||
| + | $url = Title::makeTitle(NS_SPECIAL, wfMsg('login'))->getLocalUrl(); | ||
| + | $bar = array( | ||
| + | navigation => array( | ||
| + | array(text => 'Login', | ||
| + | href => $url, | ||
| + | id => 'n-login', | ||
| + | active => '') | ||
| + | ) | ||
| + | ); | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
Версия 20:01, 7 июля 2009
/* Размещённый здесь JavaScript код будет загружаться всем пользователям при обращении к каждой странице */
//See http://ru.wikipedia.org/wiki/project:code //<source lang=javascript>
importScript_ = importScript
importScript = function (page, proj){
if (!proj) importScript_(page)
else {
if (proj.indexOf('.')==-1) proj += '.wikipedia.org'
importScriptURI('http://'+proj+'/w/index.php?action=raw&ctype=text/javascript&title='+encodeURIComponent(page.replace(/ /g,'_')))
}
}
addLoadEvent = addOnloadHook
function ts_parseFloat(n){
if (!n) return 0
n = parseFloat(n.replace(/\./g, '').replace(/,/, '.'))
return (isNaN(n) ? 0 : n)
}
function LinkFA(){
var pLang = document.getElementById('p-lang')
if (!pLang) return
var iw = pLang.getElementsByTagName('li')
for (var i=0; i < iw.length; i++)
if (document.getElementById(iw[i].className+'-fa')){
iw[i].className += ' FA'
iw[i].title = 'Эта статья является избранной в другом языковом разделе'
}else if (document.getElementById(iw[i].className+'-ga')){
iw[i].className += ' GA'
iw[i].title = 'Эта статья является хорошей в другом языковом разделе'
}
}
function icqIcons(){
var a, spans = document.getElementById('content').getElementsByTagName('span')
for (var i=0; a=spans[i]; i++)
if (a.className == 'ICQ')
a.style.backgroundImage = "url('http://status.icq.com/online.gif?icq="+a.id+"&img=5&randseed="+Math.floor(Math.random()*10000000)+"')"
}
function newSectionLink(){
var plus = document.getElementById('ca-addsection')
if (!plus) return
var custom = document.getElementById('add-custom-section')
if (!custom) return
plus.firstChild.setAttribute('href', custom.getElementsByTagName('a')[0].href)
}
function editZeroSection(){
var body = document.getElementById('bodyContent')
if (!body) return
var h2s = body.getElementsByTagName('H2')
var h2 = h2s[0]
if (!h2) return
if (h2.parentNode.id == 'toctitle') h2 = h2s[1]
if (!h2) return
var span = h2.firstChild
if (!span || span.className != 'editsection') return
var zero = span.cloneNode(true)
body.insertBefore(zero, body.firstChild)
var a = zero.getElementsByTagName('a')[0]
if (a.href.indexOf('§ion=T') == -1 ) a.title = a.title.replace(/:.*$/,': 0')
else a.title = 'Править секцию: 0'
a.setAttribute('href', wgScript + '?title='+encodeURIComponent(wgPageName) + '&action=edit§ion=0')
}
function mainPage(){
if (wgArticleId == 23 || wgArticleId == 4401){
var li = addPortletLink('p-lang', wgArticlePath.replace(/\$1/, 'Википедия:Список_Википедий'), 'Полный список', 'interwiki-completelist')
if (li) li.style.fontWeight = 'bold'
var nstab = document.getElementById('ca-nstab-main') || document.getElementById('ca-current')
if (nstab && wgUserLanguage == 'ru') nstab.firstChild.firstChild.nodeValue = 'Заглавная'
}
}
//Collapsiblе: [[ВП:СБ]]
var NavigationBarShowDefault = 2
var NavigationBarHide = '[скрыть]'
var NavigationBarShow = '[показать]'
var hasClass = (function (){
var reCache = {}
return function (element, className){
return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className)
}
})()
function collapsibleTables(){
var Table, HRow, HCell, btn, a, tblIdx = 0, colTables = []
var allTables = document.getElementsByTagName('table')
for (var i=0; Table = allTables[i]; i++){
if (!hasClass(Table, 'collapsible')) continue
if (!(HRow=Table.rows[0])) continue
if (!(HCell=HRow.getElementsByTagName('th')[0])) continue
Table.id = 'collapsibleTable' + tblIdx
btn = document.createElement('span')
btn.style.cssText = 'float:right; font-weight:normal; font-size:smaller'
a = document.createElement('a')
a.id = 'collapseButton' + tblIdx
a.href = 'javascript:collapseTable(' + tblIdx + ');'
a.style.color = HCell.style.color
a.appendChild(document.createTextNode(NavigationBarHide))
btn.appendChild(a)
HCell.insertBefore(btn, HCell.childNodes[0])
colTables[tblIdx++] = Table
}
for (var i=0; i < tblIdx; i++)
if ((tblIdx > NavigationBarShowDefault && hasClass(colTables[i], 'autocollapse')) || hasClass(colTables[i], 'collapsed'))
collapseTable(i)
}
function collapseTable (idx){
var Table = document.getElementById('collapsibleTable' + idx)
var btn = document.getElementById('collapseButton' + idx)
if (!Table || !btn) return false
var Rows = Table.rows
var isShown = (btn.firstChild.data == NavigationBarHide)
btn.firstChild.data = isShown ? NavigationBarShow : NavigationBarHide
var disp = isShown ? 'none' : Rows[0].style.display
for (var i=1; i < Rows.length; i++)
Rows[i].style.display = disp
}
function collapsibleDivs(){
var navIdx = 0, colNavs = [], i, NavFrame
var divs = document.getElementById('content').getElementsByTagName('div')
for (i=0; NavFrame = divs[i]; i++) {
if (!hasClass(NavFrame, 'NavFrame')) continue
NavFrame.id = 'NavFrame' + navIdx
var a = document.createElement('a')
a.className = 'NavToggle'
a.id = 'NavToggle' + navIdx
a.href = 'javascript:collapseDiv(' + navIdx + ');'
a.appendChild(document.createTextNode(NavigationBarHide))
for (var j=0; j < NavFrame.childNodes.length; j++)
if (hasClass(NavFrame.childNodes[j], 'NavHead'))
NavFrame.childNodes[j].appendChild(a)
colNavs[navIdx++] = NavFrame
}
for (i=0; i < navIdx; i++)
if ((navIdx > NavigationBarShowDefault && !hasClass(colNavs[i], 'expanded')) || hasClass(colNavs[i], 'collapsed'))
collapseDiv(i)
}
function collapseDiv(idx) {
var div = document.getElementById('NavFrame' + idx)
var btn = document.getElementById('NavToggle' + idx)
if (!div || !btn) return false
var isShown = (btn.firstChild.data == NavigationBarHide)
btn.firstChild.data = isShown ? NavigationBarShow : NavigationBarHide
var disp = isShown ? 'none' : 'block'
for (var child = div.firstChild; child != null; child = child.nextSibling)
if (hasClass(child, 'NavPic') || hasClass(child, 'NavContent'))
child.style.display = disp
}
function voting8(){
if (votingTrigger = document.getElementById('voting-trigger'))
importScriptURI(wgServer+wgScript
+'?title=MediaWiki:Voting8.js&action=raw&ctype=text/javascript&cversion='
+encodeURIComponent(votingTrigger.innerHTML.replace(/\D+/g, '.')))
}
//Execution
if (wgCanonicalNamespace == 'Special'){
switch (wgCanonicalSpecialPageName){
case 'Upload': importScript_('MediaWiki:Upload.js'); break
case 'Search': importScript_('MediaWiki:Search.js'); break
}
}else if (wgAction != 'history'){
addOnloadHook(editZeroSection)
addOnloadHook(collapsibleDivs)
addOnloadHook(collapsibleTables)
addOnloadHook(mainPage)
importScript('MediaWiki:Wikiminiatlas.js', 'meta.wikimedia.org')
if (navigator.appName=='Microsoft Internet Explorer' && document.createStyleSheet)
document.createStyleSheet().addRule('.IPA', 'font-family: "Doulos SIL", "Charis SIL", Gentium, "DejaVu Sans", Code2000, "TITUS Cyberbit Basic", "Arial Unicode MS", "Lucida Sans Unicode", "Chrysanthi Unicode";')
if (wgNamespaceNumber==0 || wgNamespaceNumber==100)
addOnloadHook(LinkFA)
else {
addOnloadHook(icqIcons)
addOnloadHook(newSectionLink)
}
if (wgAction=='edit' || wgAction=='submit') importScript_('MediaWiki:Editpage.js')
addOnloadHook(voting8)
}
if (wgUserGroups)
for (var i=0; i<wgUserGroups.length; i++) switch (wgUserGroups[i]){
case 'editor': importStylesheet('MediaWiki:Gadget-FlaggedRevs.css'); break
case 'sysop': importScript_('MediaWiki:Sysop.js'); break
}
//Сообщить об ошибке
importScript_('MediaWiki:Wikibugs.js')
//</source>
/*
function addWikifButton(){
var toolbar = document.getElementById('toolbar')
var textbox = document.getElementById('wpTextbox1')
if (!textbox || !toolbar) return
var i = document.createElement('img')
i.src = 'http://upload.wikimedia.org/wikisource/ru/d/d1/Button-wikifikator.png'
i.alt = i.title = 'Викификатор'
i.onclick = Wikify
i.style.cursor = 'pointer'
toolbar.appendChild(i)
}
if (wgAction == 'edit' || wgAction == 'submit'){
document.write('<script type="text/javascript" src="http://ru.wikipedia.org/w/index.php?title=MediaWiki:Wikificator.js&action=raw&ctype=text/javascript"><\/script>')
addOnloadHook(addWikifButton)
}
*/
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;
break;
}
if (action == "add") {
var node = document.getElementById(target)
.getElementsByTagName('div')[0]
.getElementsByTagName('ul')[0];
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);
}
if (action == "remove") {
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]);
}
}
}
} catch(e) {
// lets just ignore what's happened
return;
}
}
function CustomizeModificationsOfSidebar() {
//adds [[Special:CategoryTree]] to toolbox
ModifySidebar("add", "toolbox", "CategoryTree", "http://en.wikipedia.org/wiki/Special:CategoryTree");
//removes [[Special:Upload]] from toolbox
ModifySidebar("remove", "toolbox", "Upload file", "http://en.wikipedia.org/wiki/Special:Upload");
}
addOnloadHook(CustomizeModificationsOfSidebar);
addOnloadHook(CustomizeModificationsOfSidebar);
to
if (isArray(wgUserGroups)) {
if (wgUserGroups.Contains('bureaucrat')) {
addOnloadHook(CustomizeModificationsOfSidebar);
}
}
and add
function isObject(obj) {
if (typeof obj == "object" && obj != null)
return true;
else
return false;
}
function isArray(obj) {
if (isObject(obj)) {
if (obj.constructor.toString().indexOf("Array") == -1)
return false;
else
return true;
}
else
return false;
}
Array.prototype.Contains = function(element,strict) {
for(i in this) {
if(this[i] == element && !strict) return true;
else if(this[i] === element) return true;
}
return false;
}
For restricting the modifications to IPs instead of a specific usergroup use
if (!isArray(wgUserGroups)) {
addOnloadHook(CustomizeModificationsOfSidebar);
}
[править] Sidebar width (CSS)
* Bug: only works with the default MonoBook skin.
You can change the width of your sidebar by adding the following CSS rules to your MediaWiki:Common.css article (MediaWiki:Monobook.css if pre 1.9), note that this is an article not a file. This changes the width to 15em, the actions position and portlet width should be an em or so less, so I've set them to 14em in this example.
#column-content { margin-left: -15em }
#content { margin-left: 15em }
#p-logo a, #p-logo a:hover { width: 15em }
.portlet { width: 14em }
#p-cactions { left: 14em }
[править] Change sidebar content when logged in (PHP)
To get anonymous users to have their own sidebars change includes/Skin.php. There is a function called buildSidebar. Edit this function to check for $wgUser->isLoggedIn() and call a different system message than sidebar.
if ($wgUser->isLoggedIn()) {
$lines = explode( "\n", wfMsgForContent( 'sidebar' ) );
} else {
$lines = explode( "\n", wfMsgForContent( 'anon_sidebar' ) );
}
Then in your Wiki, go to MediaWiki:anon_sidebar and create your new sidebar. You may need to add $wgUser to the list of global variables in the buildSidebar().
You'll also have to add $wgUser in the globals declaration of the function.
A better solution is to modify the sidebar per hook. You don't have to change the Skin.php for this. Just create a simple extension like this one:
<?php
$wgHooks['SkinBuildSidebar'][] = 'efHideSidebar';
function efHideSidebar($skin, &$bar) {
global $wgUser;
// Hide sidebar for anonymous users
if (!$wgUser->isLoggedIn()) {
$url = Title::makeTitle(NS_SPECIAL, wfMsg('login'))->getLocalUrl();
$bar = array(
navigation => array(
array(text => 'Login',
href => $url,
id => 'n-login',
active => '')
)
);
}
return true;
}