Jump to content

Wikipedia:Scripts

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by CXI (talk | contribs) at 08:20, 18 January 2005 ([[mIRC]]: wikilinks 1.2.1 -> 1.3: code reduction, piped links). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Wikipedia:Tools

This page serves as a central repository for scripts and scripting requests on Wikipedia. Please feel free to improve any of these scripts but make sure to test your changes first. Like the rest of Wikipedia, all material here is under the GFDL. See licensing for further details.

Guidelines

KISS

Remember to Keep it Simple, Stupid, do one thing and do it well, this is a place for simple scripts and not whole programs.

Style

This style guide is by no means a rule, however keep in mind that your script must be easy to paste in here as well as easy to copy by others.

<pre>
 #!/usr/bin/evn perl
 use strict;

 print "Hello World\n";
<nowiki>

</nowiki>


Which would render as:

#!/usr/bin/evn perl
use strict;

print "Hello World\n";

Licence

All material on Wikipedia is by default under the GNU Free Documentation License, this licence is designed for documentation and written works but not for software. If you want your work to be of the greatest use to the public please consider dual-licensing it and releasing it under a Free software licence such as the GNU General Public License in addition to the GFDL. Like the GFDL, The GPL is released by the Free Software Foundation. You can do so by explicitly stating that your script is dual-licensed under the GPL in a comment at the start of your script.

IRC channel scripts

This mIRC script allows you to double-click on [[wiki-links]] and {{templates}} within mIRC, opening up a browser window at that article. It supports links to all existant languages and WikiMedia projects, such as Meta: and commons:, as well as allowing users to change which language Wikipedia will be opened by default when an mIRC wiki link is clicked.

To install, copy the complete text between the horizontal lines below, open up mIRC, click the "scripts editor" icon (or go to 'Tools' -> 'Remote', or press Alt+R), and paste it all there in the "Remote" section.

This script supersedes the older "Wikipedia-style Linking" script, which can be found below if needed. However, it has fewer features and is less efficient. If you have another Wikipedia link script already installed, you will need to delete it (selecting the lines in the script editor and pressing delete is sufficient) before installing this one.

; Wikilinks 1.3
; By CXI, based on a script by Masterhomer
; Just hit Alt-R and paste the following lines of code into a new script.
; * Supports {{links}} and [[links]], and [[piped|links]]
; * Supports multi-word links and multiple links per line
; * Supports any Wikipedia language, but defaults to English. This can be changed with:
;   /set %wiki-lang CODE (for example, /set %wiki-lang fr will default to French Wikipedia)
; Language/project links such as [[fr:Accueil]] will work no matter what the default language is.

; This script is dual-licensed under the GPL,
; version 2 or (at your option) any later version.
; See http://www.gnu.org/licenses/gpl.txt for more details.

on ^$*:HOTLINK:/[\{\}\[\]]{2}/:#,?,=:{ return }
on *:HOTLINK:*:#,?,=:{ 
  tokenize 32 $remove($1,[[,]],{{,}})
  if ($regex($hotline,(.*\[\[(.*? $+ $1 $+ .*?)(\|.*)?\]\].*|.*(\{\{.*? $+ $1 $+ .*?)\}\}.*))) {
    url $+(http://,$iif(%wiki-lang,$ifmatch,en),.wikipedia.org/wiki/,$replace($gettok($regml(2),1,124),{{,Template:,$chr(32),_))
  }
}


; Wikipedia-style Linking, Version 2.2.1
; Originally created by MadEwokHerd, modified by nobody and Mark_Ryan,
;   cleaned up by toad, majorly improved by Timwi, added extra functions by Netoholic
; fixed by Masterhomer
;  (now supports links with m:, meta:, commons:, WP:, {{template}}, and language codes)
on ^*:hotlink:*:*:{
  if (*[[* iswm $1) || (*]]* iswm $1) || ($+(*,[,[,*,$1,*,],],*) iswm $hotline) { return }
  elseif (*{{* iswm $1) || (*}}* iswm $1) || ($+(*,\{,\{,*,$1,*,\},\},*) iswm $hotline) { return }
  else { halt }
}

on *:hotlink:*:*:{
  var %page,%template,%dummy,%dummy2,%dummyc,%openb,%closeb
  if (*[[* iswm $1) || (*]]* iswm $1) || ($+(*,[,[,*,$1,*,],],*) iswm $hotline) {
    %openb = $pos($1,[[)
    %closeb = $pos($1,]])
    if (%openb == $null) {
      if (%closeb == $null) {
        %dummy = $regex($hotline,\[\[([^\]]* $+ $1 $+ [^\]]*)\]\])
      }
      else {
        %dummyc = %closeb - 1
        %dummy = $regex($hotline,\[\[([^\]]* $+ $left($1,%dummyc) $+ )\]\])
      }
    }
    elseif (%closeb == $null) {
      %dummyc = $len($1) - %openb
      %dummyc = %dummyc - 1
      %dummy = $regex($hotline,\[\[( $+ $right($1,%dummyc) $+ [^\]]*)\]\])
    }
    else {
      %dummy = $regex($1,\[\[([^\]]*)\]\])
    }
  }
  else {
    %template = yes
    %openb = $pos($1,{{)
    %closeb = $pos($1,}})
    if (%openb == $null) {
      if (%closeb == $null) {
        %dummy = $regex($hotline,\{\{([^\}]* $+ $1 $+ [^\}]*)\}\})
      }
      else {
        %dummyc = %closeb - 1
        %dummy = $regex($hotline,\{\{([^\}]* $+ $left($1,%dummyc) $+ )\}\})
      }
    }
    elseif (%closeb == $null) {
      %dummyc = $len($1) - %openb
      %dummyc = %dummyc - 1
      %dummy = $regex($hotline,\{\{( $+ $right($1,%dummyc) $+ [^\}]*)\}\})
    }
    else {
      %dummy = $regex($1,\{\{([^\}]*)\}\})
    }
  }

  %page = $regml(1)
  if (%template == yes) { %page = Template: $+ %page }

  if ($left(%page,2) == m:) {
    %dummy2 = $len(%page) - 2
    url -an http://meta.wikimedia.org/wiki/ $+ $replace($right(%page,%dummy2),$chr(32),_)
  }
  elseif ($left(%page,5) == meta:) {
    %dummy2 = $len(%page) - 5
    url -an http://meta.wikimedia.org/wiki/ $+ $replace($right(%page,%dummy2),$chr(32),_)
  }
  elseif ($left(%page,8) == commons:) {
    %dummy2 = $len(%page) - 8
    url -an http://commons.wikimedia.org/ $+ $replace($right(%page,%dummy2),$chr(32),_)
  }
  elseif ($left(%page,3) == WP:) {
    %dummy2 = $len(%page) - 3
    url -an http://en.wikipedia.org/wiki/ $+ $replace(%page,$chr(32),_)
  }
  elseif ($right($left(%page,3),1) == :) {
    %dummy2 = $len(%page) - 3
    url -an http:// $+ $left(%page,2) $+ .wikipedia.org/wiki/ $+ $replace($right(%page,%dummy2),$chr(32),_)
  }
  elseif ($right($left(%page,4),1) == :) {
    %dummy2 = $len(%page) - 4
    url -an http:// $+ $left(%page,3) $+ .wikipedia.org/wiki/ $+ $replace($right(%page,%dummy2),$chr(32),_)
  }
  else {
    url -an http://en.wikipedia.org/wiki/ $+ $replace(%page,$chr(32),_)
  }
}


; Wikipedia WikiLink Hotlinker, Version 1.0.1
; By Masterhomer
; Just hit Alt-R and paste the following three lines of code into mIRC. 
; Not as advanced as the above one, but it works and it's fast.
; Works for all WikiMedia projects and all Wikipedia.
; Thanks to CXI for pointing this out.
; Licensed under the MIT Licence. 

on ^*:HOTLINK:*[[*:#: { return }
on ^*:HOTLINK:*]]*:#: { return }
on *:HOTLINK:*:#: if ([[ isin $hotline) { url $+(http://en.wikipedia.org/wiki/,$replace($gettok($gettok($hotline,2,91),1,93),$chr(32),_)) }


; Tea time., Version 1.1
; Makes you talk like this. On IRC. 
; Ex: i'm using the internet changes to I'm using the Internet.
; Spell check soon..
; Inspired by IRC User Austin
; Licensed under the MIT Licence.

on *:INPUT:#: { 
  if (($asc($right($1-,1)) > 96) && ($asc($right($1-,1)) < 123)) || (($asc($right($1-,1)) > 64) && ($asc($right($1-,1)) < 91))  || ($right($1-,1) isnum) {
    msg $active $+($upper($left($1-,1)),$right($1-,-1),.) 
    halt
  }
}

X-Chat

This Python script for X-Chat replaces links like this: [[de:Linux]] with the corresponding URL, when either you or someone else types them. It requires X-Chat 2.0.6 or later.

To install it, copy the text below and save it into a file called wmlinksubber.py in your xchat directory and then start/restart X-Chat, or load it via the menu Window -> Plugins and Scripts....
X-Chat directories:

  1. Unix-based systems
    1. most likely ~/.xchat or ~/.xchat2
  2. Windows
    1. C:\Documents and Settings\username\Application Data\X-Chat 2\

If you don't want it to replace text which you type, then place a # at the beginning of the line EVENTS.append(("Your Message", 1)), and reload the plugin with the Window -> Plugins and Scripts... menu, if it is already loaded.

Script is available on authors subpages:

Unicode numeric converter scripts

Perl

A HTML character entity converter written in Perl that uses the ord() function to convert a character to its corresponding number in the character set. It operatates on standard input. --Ævar Arnfjörð Bjarmason 18:05, 2004 May 16 (UTC)


 #!/usr/bin/env perl
 # Written by Ævar Arnfjörð Bjarmason at 2004-05-16
 # Code is in the public domain.
 use strict;

 my @input  = split (//, <>);
 for ( my $i = 0; $i<$#input; $i++ ) {
        if ( ord($input[$i]) >= 255 ) {
                print '&#' . ord($input[$i]) . ';';
        } else {
                print $input[$i];
        }
 }

Author: Olof

Notes: I got fed up with looking up Unicode characters, so I wrote an AppleScript for my favorite styled text editor (Style) to write them for me. Now, I can just type Japanese into a text edit window like this

小 泉 純 一 郎

select it, choose my script from the scripts menu, and it turns into

小 泉 純 一 郎 &#23567; &#27849; &#32020; &#19968; &#37070;

Which is what you can paste into the Wikipedia edit window. I'm using a Mac, so this is a mac only solution, but I thought I'd share it for those of you who can use it. Here's the script:

compile this in ScriptEditor, and save it in the "Style Scripts folder in the same folder where the Style application lives. I have mine saved as "Append Unicoded HTML". Thereafter it will appear in Style's Scripts menu.

Style is available at merzwaren ($20 shareware). I have this running on Mac OS X 10.2.1 with Style version 1.9.2

Enjoy !

set theHTML to "" 
tell application "Style" 
    set selText to selection of document 1 as Unicode text
    set selStart to get offset of selection of document 1
    set selEnd to get offset of end of selection of document 1
    set dataLen to (selEnd - selStart)
end tell
set tempName to "unicode temp"

tell application "Finder" 
    if alias tempName exists then
        move alias tempName to the trash
    end if 
end tell 

set fileRef to open for access tempName with write permission
write selText to fileRef
set myRawData to read fileRef from 0 for dataLen
close access fileRef

tell application "Finder" 
    move alias tempName to the trash 
end tell 

set numChars to dataLen div 2 
repeat with n from 1 to numChars 
    set theHTML to theHTML & "&#" 
    set a to get character (2 * n - 1) of myRawData 
    set b to get character (2 * n) of myRawData
    set lVal to ((ASCII number b) + (256 * (ASCII number a))) 
    set theHTML to (theHTML & lVal as string) & "; "	 
end repeat 

tell application "Style" 
    set selection of document 1 to ((selection of document 1) & " (  " & theHTML & " ) ") 
end tell

Authors:

Known to work on:


Notes:

You may not need a script for converting CJK characters if you have a Mac running Mac OS X 10.2 and have Mozilla as your browser. Just do the editing from within Mozilla. Mozilla automatically does the conversion. For example, in adding this edit, I type in the Japanese characters for "edit," which are 編集. Mozilla automatically converted these characters to the proper romanized Unicode format. Just look at the above lines in the editing box to see for yourself. -User: IppikiOokami 5 September 2003

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Unicode conversion utility</title>
  </head>
  <body>
   <form name="charform">
     <p>Type here, and all characters with a code greater than 127 will be converted to &#1234; codes.</p>
     <p>Input:</p>
     <textarea name="input" cols="80" rows="25" onKeyUp="revtxt()">
       Sorry, this page is useless with Javascript disabled.
     </textarea>
     <p>Output:</p>
     <textarea name="output" cols="80" rows="25">
       Sorry, this page is useless with JavaScript disabled.
     </textarea>
    </form>
    <script language="Javascript">
      document.charform.input.value="";
      document.charform.output.value="Don't type here.";
      function revtxt() {
        var s=document.charform.input.value;
        o="";
        for( m=0;s.charAt(m);++m )
          if ( (c=s.charCodeAt(m))<128&&c!=38) o+=s.charAt(m);
          else if (c==38) o+="&";
          else o+="&#"+c+";";
          document.charform.output.value=o;
      }
    </script>
</html>

Scripting requests

Database scripting

There are a number of pages designed to help with Wikipedia maintenance and editing which are generated by copying the entire Wikipedia database to a machine and running a script or program on it.

Here you can request that someone with a recent copy of the database dump and the appropriate skills update the Specialpages and other pages requiring scripting.

See also: Wikipedia:SQL query requests.

I'm working up scripts to generate the equivalent of many of these from an offline copy of the database. Some are a bit amateurish right now and should be considered at best beta versions, but please cast your eyes over User:Topbanana/Reports if you're eager to get going. These will find their way into the wikipedia namespace eventually, in the meantime I'll be delighted to hear feedback.