MediaWiki:Common.js

From Wikimedia Commons, the free media repository
Revision as of 20:29, 4 September 2007 by Dschwen (talk | contribs) (trust me, I have a bad feeling about this. while I do not have a working exploit yet, there is too much possibility to inject JS here!)
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
//<source lang="javascript">

/** JSconfig (old version, to be replaced soon) ************
 * Global configuration options to enable and disable specific
 * script features from [[MediaWiki:Common.js]]
 * Override these default settings in your [[Special:Mypage/monobook.js]]
 * for Example: JSconfig.loadAutoInformationTemplate = false;
 *
 *  Maintainer: [[User:Dschwen]]
 */

var JSconfig_old =
{
 loadAutoInformationTemplate : true,
 specialSearchEnhanced : true,
 subPagesLink : true,
 attributeSelf : true,
 userUploadsLink : true
}


/** JSconfig ************
 * Global configuration options to enable/disable and configure
 * specific script features from [[MediaWiki:Common.js]] and
 * [[MediaWiki:Monobook.js]]
 * This framework adds config options (saved as cookies) to [[Special:Preferences]]
 * For a more permanent change you can override the default settings in your 
 * [[Special:Mypage/monobook.js]]
 * for Example: JSconfig.keys[loadAutoInformationTemplate] = false;
 *
 *  Maintainer: [[User:Dschwen]]
 */

var JSconfig =
{
 prefix : 'jsconfig_',
 keys : {},
 meta : {},

 //
 // Register a new configuration item
 //  * name          : String, internal name
 //  * default_value : String or Boolean (type determines configuration widget)
 //  * description   : String, text appearing next to the widget in the preferences
 //  * prefpage      : Integer (optional), section in the preferences to insert the widget:
 //                     0 : User profile
 //                     1 : Skin
 //                     2 : Math
 //                     3 : Files
 //                     4 : Date and time
 //                     5 : Editing
 //                     6 : Recent changes
 //                     7 : Watchlist
 //                     8 : Search
 //                     9 : Misc
 //
 // Access keys through JSconfig.keys[name]
 //
 registerKey : function( name, default_value, description, prefpage )
 {
  if( typeof(JSconfig.keys[name]) == 'undefined' ) 
   JSconfig.keys[name] = default_value;
  else {

   // all cookies are read as strings, 
   // convert to the type of the default value
   switch( typeof(default_value) )
   {
    case 'boolean' : JSconfig.keys[name] = ( JSconfig.keys[name] == 'true' ); break;
    case 'number'  : JSconfig.keys[name] = JSconfig.keys[name]/1; break;
   }

  }

  JSconfig.meta[name] = { 'description' : description, 'page' : prefpage || 0, 'default_value' : default_value };
 },

 readCookies : function()
 {
  var cookies = document.cookie.split("; ");
  var p =JSconfig.prefix.length;
  var i;

  for( var key in cookies )
  {
   if( cookies[key].substring(0,p) == JSconfig.prefix )
   {
    i = cookies[key].indexOf('=');
    //alert( cookies[key] + ',' + key + ',' + cookies[key].substring(p,i) );
    JSconfig.keys[cookies[key].substring(p,i)] = cookies[key].substring(i+1);
   }
  }
 },

 writeCookies : function()
 {
  for( var key in JSconfig.keys )
   document.cookie = JSconfig.prefix + key + '=' + JSconfig.keys[key] + '; path=/; expires=Thu, 2 Aug 2009 10:10:10 UTC';
 },

 evaluateForm : function()
 {
  var w_ctrl,wt;
  //alert('about to save JSconfig');
  for( var key in JSconfig.meta ) {
   w_ctrl = document.getElementById( JSconfig.prefix + key )
   if( w_ctrl ) 
   {
    wt = typeof( JSconfig.meta[key].default_value );
    switch( wt ) {
     case 'boolean' : JSconfig.keys[key] = w_ctrl.checked; break;
     case 'string' : JSconfig.keys[key] = w_ctrl.value; break;
    }
   }
  }

  JSconfig.writeCookies();
  return true;
 },

 setUpForm : function()
 { 
  var prefChild = document.getElementById('preferences');
  if( !prefChild ) return;
  prefChild = prefChild.childNodes;

  //
  // make a list of all preferences sections
  //
  var tabs = new Array;
  var len = prefChild.length;
  for( var key = 0; key < len; key++ ) {
   if( prefChild[key].tagName &&
       prefChild[key].tagName.toLowerCase() == 'fieldset' ) 
    tabs.push(prefChild[key]);
  }

  //
  // Create Widgets for all registered config keys
  //
  var w_div, w_label, w_ctrl, wt;
  for( var key in JSconfig.meta ) {
   w_div = document.createElement( 'DIV' );

   w_label = document.createElement( 'LABEL' );
   w_label.appendChild( document.createTextNode( JSconfig.meta[key].description ) )
   w_label.htmlFor = JSconfig.prefix + key;

   wt = typeof( JSconfig.meta[key].default_value );

   w_ctrl = document.createElement( 'INPUT' );
   w_ctrl.id = JSconfig.prefix + key;

   // before insertion into the DOM tree
   switch( wt ) {
    case 'boolean' : w_ctrl.type = 'checkbox'; break;
    case 'string'  : w_ctrl.type = 'text'; break;
   }

   w_div.appendChild( w_label );
   w_div.appendChild( w_ctrl );
   tabs[JSconfig.meta[key].page].appendChild( w_div );
  
   // after insertion into the DOM tree
   switch( wt ) {
    case 'boolean' : w_ctrl.defaultChecked = w_ctrl.checked = JSconfig.keys[key]; break;
    case 'string' : w_ctrl.defaultValue = w_ctrl.value = JSconfig.keys[key]; break;
   }

  }
  addEvent(document.getElementById('preferences').parentNode, 'submit', JSconfig.evaluateForm );
 }
}

JSconfig.readCookies();
addOnloadHook(JSconfig.setUpForm);



/** extract a URL parameter from the current URL **********
 * From [[en:User:Lupin/autoedit.js]]
 *
 * paramName  : the name of the parameter to extract
 *
 * Local Maintainer: [[User:Dschwen]]
 */

function getParamValue( paramName ) 
{
 var cmdRe=RegExp( '[&?]' + paramName + '=([^&]*)' );
 var h = document.location.href;
 var m=cmdRe.exec(h);
 if (m) {
  try {
   return decodeURIComponent(m[1]);
  } catch (someError) {}
 }
 return null;
}


/** includePage ************
 * force the loading of another JavaScript file
 *
 * Local Maintainer: [[User:Dschwen]]
 */

function includePage( name )
{
 document.write('<script type="text/javascript" src="' + wgScript + '?title='
  + name 
  + '&action=raw&ctype=text/javascript"><\/script>' 
 );
}


/** &withJS= URL parameter *******
 * Allow to try custom scripts on the MediaWiki namespace without
 * editing [[Special:Mypage/monobook.js]]
 *
 * Maintainer: [[User:Platonides]]
 */

{
 var extraJS = getParamValue("withJS");
 if (extraJS)
  if (extraJS.match("^MediaWiki:[^&<>=%]*\.js$"))
   includePage(extraJS);
  else
   alert(extraJS + " javascript not allowed to be loaded.");
}


/** Attach (or remove) an Event to a specific object **********
 * Cross-browser event attachment (John Resig)
 * http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
 *
 * obj  : DOM tree object to attach the event to
 * type : String, event type ("click", "mouseover", "submit", etc.)
 * fn   : Function to be called when the event is triggered (the ''this''
 *        keyword points to ''obj'' inside ''fn'' when the event is triggered)
 *
 * Local Maintainer: [[User:Dschwen]]
 */

function addEvent( obj, type, fn )
{
 if (obj.addEventListener)
  obj.addEventListener( type, fn, false );
 else if (obj.attachEvent)
 {
  obj["e"+type+fn] = fn;
  obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
  obj.attachEvent( "on"+type, obj[type+fn] );
 }
}

function removeEvent( obj, type, fn )
{
 if (obj.removeEventListener)
  obj.removeEventListener( type, fn, false );
 else if (obj.detachEvent)
 {
  obj.detachEvent( "on"+type, obj[type+fn] );
  obj[type+fn] = null;
  obj["e"+type+fn] = null;
 }
}



/** Extra toolbar options ***********
 * Append custom buttons to the edit mode toolbar. 
 * This is a modified copy of a script by User:MarkS for extra features added by User:Voice of All.
 * This is based on the original code on Wikipedia:Tools/Editing tools
 * To disable this script, add <code>mwCustomEditButtons = [];<code> to [[Special:Mypage/monobook.js]]
 *  
 *  Maintainers: [[User:MarkS]]?, [[User:Voice of All]], [[User:R. Koot]]
 */

if (mwCustomEditButtons) {
  mwCustomEditButtons[mwCustomEditButtons.length] = {
    "imageFile": "http://upload.wikimedia.org/wikipedia/en/c/c8/Button_redirect.png",
    "speedTip": "Redirect",
    "tagOpen": "#REDIRECT [[",
    "tagClose": "]]",
    "sampleText": "Insert text"};
}


/***** SpecialSearchEnhanced ********
 * Improvement of the search page v4
 * Written by Marc Mongenet & Suisui (GFDL & GPL)
 *
 * Maintainers: none, ([[User:Dschwen]]?)
 ****/

function SpecialSearchEnhanced() 
{
 function SearchForm(engine_name, engine_url, logo_url, search_action_url, 
                     search_field_name, add_search_field, field_array)
 {
  var span= document.createElement("span");
  span.style.marginRight = "1em";

  var form = document.createElement("form");
  form.method = "get";
  form.action = search_action_url;
  form.style.display = "inline";
  span.appendChild(form);

  var input = document.createElement("input");
  input.type = "hidden";
  input.name = search_field_name;
  form.appendChild(input);

  for( var i in field_array){
   var fld = document.createElement("input");
   fld.type = "hidden";
   fld.name = i;
   fld.value = field_array[i];
   form.appendChild(fld);
  }

  var submit = document.createElement("input");
  submit.type = "submit";
  submit.value = "Find media with Mayflower";
  form.appendChild(submit);

  form.onsubmit = function() {
   if(add_search_field == ""){
    input.value = document.getElementById("lsearchbox").value;
   }else{
    input.value = document.getElementById("lsearchbox").value+add_search_field;
   }
  }

  var a = document.createElement("a");
  a.href = engine_url;
  span.appendChild(a);

  var img = document.createElement("img");
  img.src = logo_url;
  img.alt = engine_name;
  img.style.borderWidth = "0";
  img.style.padding = "5px";
  img.style.width = "220px";
  img.style.height = "53px";
  a.appendChild(img);

  return span;
 }

 //honor user configuration
 if( !JSconfig_old.specialSearchEnhanced ) return;
 
 if (wgCanonicalNamespace != "Special" || wgCanonicalSpecialPageName != "Search") return;

 if(skin == "monobook" || skin == "cologneblue" || skin == "simple"){var mainNode = document.getElementsByTagName("form");}
 if (!mainNode) return;
 mainNode = mainNode[0];
 mainNode.appendChild(document.createElement("center"));
 mainNode = mainNode.lastChild;

 var searchValue = document.getElementById("lsearchbox").value;

 var div= document.createElement("div");
 div.style.width = "100%";
//  ul.style.list-style-type = "none";
 mainNode.appendChild(div);

 var engine;
 var mayflowero = new Object();
 mayflowero["t"] = "n";
 engine = SearchForm("MayFlower", "http://tools.wikimedia.de/~tangotango/mayflower/index.php", "http://tools.wikimedia.de/~tangotango/mayflower/images/mayflower-logo.png",  
                     "http://tools.wikimedia.de/~tangotango/mayflower/search.php",
                     "q", "", mayflowero);
 div.appendChild(engine);
}
addOnloadHook(SpecialSearchEnhanced);

// Scripts for Special:Upload have been moved to [[MediaWiki:Upload.js]]
if (wgPageName == 'Special:Upload') 
{
 includePage( 'MediaWiki:Upload.js' );
}


/***** subPagesLink ********
 * Adds a link to subpages of current page
 *
 *  Maintainers: [[:he:משתמש:ערן]], [[User:Dschwen]]
 *
 *  JSconfig items: bool JSconfig.subPagesLink
 *                       (true=enabled (default), false=disabled)
 ****/
var subPagesLink =
{ 
 //
 // Translations of the menu item
 //
 i18n :
 {
  'de': 'Unterseiten',
  'en': 'Subpages',    // default
  'es': 'Subpáginas',
  'fr': 'Sous-pages',
  'he': 'דפי משנה',
  'it': 'Sottopagine',
  'is': 'Undirsíður',
  'nl': "Subpagina's",
  'ru': 'Подстраницы'
 },

 install: function()
 {
  // honor user configuration
  if( !JSconfig.keys['subPagesLink'] ) return;

  if ( document.getElementById("t-whatlinkshere") 
       &&  wgNamespaceNumber != -2   // Media: (upcoming)
       &&  wgNamespaceNumber != -1   // Special:
       && wgNamespaceNumber != 6     // Image:
       &&  wgNamespaceNumber != 14   // Category:
     )
  {
   var subpagesText = subPagesLink.i18n[wgUserLanguage] || subPagesLink.i18n['en'];
   var subpagesLink ='/wiki/Special:Prefixindex/' + wgPageName +'/';

   addPortletLink( 'p-tb', subpagesLink, subpagesText, 't-subpages' );
  }
 }
}
JSconfig.registerKey('subPagesLink', true, 'Show a Subpages link in the toolbox:', 9);
addOnloadHook(subPagesLink.install);


//</source>