Difference between pages "User:Mathieu/bot" and "User:Mathieu/monobook.js"

From Event-B
< User:Mathieu(Difference between pages)
Jump to navigationJump to search
imported>Mathieu
m (wikipedia version = 1.12)
 
imported>Mathieu
m
 
Line 1: Line 1:
A bot may be a useful tool to help a wiki maintenance.
+
/* <pre><nowiki> */
  
I tried to use the python framework provided by http://pywikipediabot.sourceforge.net/. Hereafter are some guidelines to make it work on this wiki.
+
// [[User:Lupin/popups.js]] - please include this line
 +
document.write('<script type="text/javascript" src="'
 +
            + 'http://wiki.event-b.org/index.php?title=User:Mathieu/popups.js'
 +
            + '&action=raw&ctype=text/javascript&dontcountme=s"></script>');
  
== Requirements ==
 
* Python >2.5
 
  
== Getting pywikipedia framewok ==
+
// This will add an [edit top] link at the top of all pages except preview pages
Get the source code from subversion:
+
// by User:Pile0nades
<pre><nowiki>
 
(prompt) svn co http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia pywikipedia
 
</nowiki></pre>
 
== Configuring the framework for this wiki ==
 
Add the following <tt>families/eventb_family.py</tt> file (be careful with the indentation):
 
<pre><nowiki>
 
# -*- coding: utf-8  -*-
 
  
import family
+
function editTopLink() {
# The event-b family
+
  // if this is preview page or generated page, stop
# user-config.py: usernames['event-b']['event-b'] = 'User name'
+
  if(document.getElementById("wikiPreview") || window.location.href.indexOf("w/index.php?title=Special:") != -1) return;
class Family(family.Family):
+
 
    def __init__(self):
+
  // get the page title
         family.Family.__init__(self)
+
  var pageTitle = document.title.split(" - ")[0].replace(" ", "_");
         self.name = 'eventb'
+
 
        self.langs = {
+
  // create div and set innerHTML to link
            'eventb': 'wiki.event-b.org',
+
  var divContainer = document.createElement("div");
 +
  divContainer.innerHTML = '<div class="editsection" style="float:right;margin-left:5px;margin-top:3px;">[<a href="/w/index.php?title='+pageTitle+'&action=edit&section=0" title="'+document.title.split(" - ")[0]+'">edit top</a>]</div>';
 +
 
 +
  // insert divContainer into the DOM before the h1
 +
  document.getElementById("content").insertBefore(divContainer, document.getElementsByTagName("h1")[0]);
 +
 
 +
}
 +
addOnloadHook(editTopLink);
 +
// addLoadEvent(editTopLink);
 +
 
 +
 
 +
var suiviManagerAllPages = new Array();
 +
                                       
 +
function SuiviManagerRegexp(regexp)
 +
{
 +
         var match = new RegExp(regexp);
 +
 
 +
         for (var i=0;i<suiviManagerAllPages.length;i++) {
 +
                var pageName = suiviManagerAllPages[i].childNodes[0].getAttribute("value");
 +
                if (match.test(pageName)) {
 +
                        suiviManagerAllPages[i].childNodes[0].checked=true;
 +
                }
 
         }
 
         }
 +
}
 +
               
 +
function SuiviManagerLiensRouges()
 +
{
  
         self.namespaces[4] = {
+
         for (var i=0;i<suiviManagerAllPages.length;i++) {
            '_default': [u'Event-B', self.namespaces[4]['_default']],
+
                var pageClass = suiviManagerAllPages[i].childNodes[1].getAttribute("class");
 +
                if (pageClass && pageClass=="new") {
 +
                        suiviManagerAllPages[i].childNodes[0].checked=true;
 +
                }
 
         }
 
         }
 +
}
  
         self.namespaces[5] = {
+
function SuiviManagerDeselect()
          '_default': [u'Event-B talk', self.namespaces[5]['_default']],
+
{
 +
         for (var i=0;i<suiviManagerAllPages.length;i++) {
 +
                suiviManagerAllPages[i].childNodes[0].checked=false;
 
         }
 
         }
 +
}
  
    def version(self, code):
 
        return '1.12'
 
  
    def path(self, code):
+
function SuiviManager() {
        return '/index.php' #The path of index.php, look at your wiki address.  
+
        if (document.URL.indexOf("http://wiki.event-b.org/Special:Watchlist/edit")!=0) return;
 +
       
 +
        var a=0;
 +
        var b=0;                       
 +
        var interfaceMsg = new Array();
 +
        var regexpList = new Array();
 +
       
 +
        //////////////////////////////////////////////////
 +
        // Expressions régulières et liens de l'interface
 +
        //
 +
        // besoin d'aide pour les regexp ?
 +
        // http://www.commentcamarche.net/javascript/jsregexp.php3
 +
        //////////////////////////////////////////////////
 +
 
 +
        interfaceMsg[a++]      = "<b>Tout cocher</b>";
 +
        regexpList[b++]        = "^.*";
 +
       
 +
        interfaceMsg[a++]      = "Utilisateurs";
 +
        regexpList[b++]        = "^Utilisateur:";
 +
       
 +
           
 +
        interfaceMsg[a++]      = "Images";
 +
        regexpList[b++]        = "^Image:";
 +
       
 +
        interfaceMsg[a++]      = "Modèles";
 +
        regexpList[b++]        = "^Modèle:";
 +
       
 +
        interfaceMsg[a++]      = "Aide";
 +
        regexpList[b++]        = "^Aide:";
 +
       
 +
        interfaceMsg[a++]      = "Catégories";
 +
        regexpList[b++]        = "^Catégorie:";     
 +
       
 +
 
 +
 
 +
        //////////////////////////////////////////////////
 +
        var topTag = document.getElementById("contentSub")
 +
       
 +
        // récupère toutes les pages
 +
        var watchlist = document.getElementsByTagName("ul");
 +
 
 +
        for (u=0;u<watchlist.length;u++) {
 +
                        var entries = watchlist[u].getElementsByTagName("li");
 +
                        for (i=0;i<entries.length;i++) {
 +
                                suiviManagerAllPages.push(entries[i]);
 +
                        }
 +
        }
 +
       
 +
        // prépare la mini-interface
 +
        var str = "<div style=\"background-color:#8ecfe4;font-size:1px;height:8px;border:1px solid #AAAAAA;-moz-border-radius-topright:0.5em;-moz-border-radius-topleft:0.5em;\"></div>"
 +
        + "<div style=\"border:1px solid #6ac1de;border-top:0px solid white;padding:5px 5px 0 5px;margin-bottom:3ex;\"><p>"
 +
        + "<div style=\"float: left; text-align: left; white-space: nowrap;\"></div>"
 +
       
 +
        for (var cpt = 0; cpt < interfaceMsg.length; cpt ++) {
 +
                str += "<a href=\"javascript:SuiviManagerRegexp('" + regexpList[cpt] + "')\">"
 +
                                                + interfaceMsg[cpt]
 +
                                                + " ·</a> "
 +
                }
 +
       
 +
                str += "<a href=\"javascript:SuiviManagerLiensRouges()\">"
 +
                        + "Retirer les liens rouges"
 +
                        + " ·</a> "
 +
                       
 +
                str += "<a href=\"javascript:SuiviManagerDeselect()\">"
 +
                        + "<b>Enlever toutes les coches</b>"
 +
                        + " ·</a> "
 +
                               
 +
        topTag.innerHTML =      topTag.innerHTML + "<br clear=all />" + str + "<p></div>"
 +
               
 +
}
  
    def scriptpath(self,code):
+
addLoadEvent(SuiviManager);
        return ''
 
   
 
#    def shared_image_repository(self, code):
 
#        return ('commons', 'commons')
 
  
</nowiki></pre>
 
  
Then add the following <tt>user-config.py</tt> file:
+
/*
<pre><nowiki>
+
* Auto Refresh
family='eventb'
+
*/
mylang='eventb'
 
usernames['eventb']['eventb'] = 'your_bot_user_name'
 
</nowiki></pre>
 
  
== Usage ==
+
function refresh()
=== Login ===
+
{
First you should log in:
+
    window.location.href = unescape(window.location);
<pre><nowiki>
+
}
(prompt) python login.py
 
</nowiki></pre>
 
=== Upload Images ===
 
<pre><nowiki>
 
(prompt) python upload.py -noverify -keep file.png "Image description"
 
</nowiki></pre>
 
  
For uploading several images from a directory, I use the following ''sh'' command:
+
function refresh1min()
<pre><nowiki>
+
{
for i in /tmp/imagedir/* ; do (yes | python upload.py -v -noverify -keep $i "Import of image $i"); done
+
    // the timeout value should be the same as in the "refresh" meta-tag
</nowiki></pre>
+
    setTimeout( "refresh()", 60000 );
 +
}
  
 +
function refresh5min()
 +
{
 +
    // the timeout value should be the same as in the "refresh" meta-tag
 +
    setTimeout( "refresh()", 300000 );
 +
}
  
=== Add a category to the last uploaded images ===
+
if (location.href.indexOf('Special:Recentchanges') != -1) {
For the last 20 images, do :
+
  addLoadEvent(refresh5min);
<pre><nowiki>
+
}
python category.py -v add -newimages:20
 
</nowiki></pre>
 
  
=== Replace text matching regular expressions ===
+
if (location.href.indexOf('Special:Watchlist') != -1) {
<pre><nowiki>
+
  addLoadEvent(refresh5min);
python replace.py -cat -regex '{\\tt ([^}]*)}' '\texttt{\1}'
+
}
</nowiki></pre>
 
  
=== Rename a category ===
+
/* </nowiki></pre> */
<pre><nowiki>
 
python category.py move -from:"Template" -to:"Templates" -batch -putthrottle:6
 
</nowiki></pre>
 

Revision as of 12:27, 12 September 2008

/* <pre><nowiki> */

// [[User:Lupin/popups.js]] - please include this line 
document.write('<script type="text/javascript" src="' 
            + 'http://wiki.event-b.org/index.php?title=User:Mathieu/popups.js' 
            + '&action=raw&ctype=text/javascript&dontcountme=s"></script>');


// This will add an [edit top] link at the top of all pages except preview pages
// by User:Pile0nades

function editTopLink() {
  // if this is preview page or generated page, stop
  if(document.getElementById("wikiPreview") || window.location.href.indexOf("w/index.php?title=Special:") != -1) return;

  // get the page title
  var pageTitle = document.title.split(" - ")[0].replace(" ", "_"); 

  // create div and set innerHTML to link
  var divContainer = document.createElement("div");
  divContainer.innerHTML = '<div class="editsection" style="float:right;margin-left:5px;margin-top:3px;">[<a href="/w/index.php?title='+pageTitle+'&action=edit&section=0" title="'+document.title.split(" - ")[0]+'">edit top</a>]</div>';

  // insert divContainer into the DOM before the h1
  document.getElementById("content").insertBefore(divContainer, document.getElementsByTagName("h1")[0]);

}
addOnloadHook(editTopLink);
// addLoadEvent(editTopLink);


var suiviManagerAllPages = new Array();
                                        
function SuiviManagerRegexp(regexp)
{
        var match = new RegExp(regexp);

        for (var i=0;i<suiviManagerAllPages.length;i++) {
                var pageName = suiviManagerAllPages[i].childNodes[0].getAttribute("value");
                if (match.test(pageName)) {
                        suiviManagerAllPages[i].childNodes[0].checked=true; 
                }
        }
}
                
function SuiviManagerLiensRouges()
{

        for (var i=0;i<suiviManagerAllPages.length;i++) {
                var pageClass = suiviManagerAllPages[i].childNodes[1].getAttribute("class");
                if (pageClass && pageClass=="new") {
                        suiviManagerAllPages[i].childNodes[0].checked=true; 
                }
        }
}

function SuiviManagerDeselect()
{
        for (var i=0;i<suiviManagerAllPages.length;i++) {
                suiviManagerAllPages[i].childNodes[0].checked=false; 
        }
}


function SuiviManager() {
        if (document.URL.indexOf("http://wiki.event-b.org/Special:Watchlist/edit")!=0) return; 
        
        var a=0;
        var b=0;                        
        var interfaceMsg = new Array(); 
        var regexpList = new Array(); 
        
        //////////////////////////////////////////////////
        // Expressions régulières et liens de l'interface
        //
        // besoin d'aide pour les regexp ? 
        // http://www.commentcamarche.net/javascript/jsregexp.php3
        //////////////////////////////////////////////////

        interfaceMsg[a++]       = "<b>Tout cocher</b>";
        regexpList[b++]         = "^.*"; 
        
        interfaceMsg[a++]       = "Utilisateurs";
        regexpList[b++]         = "^Utilisateur:";
        
            
        interfaceMsg[a++]       = "Images"; 
        regexpList[b++]         = "^Image:";
        
        interfaceMsg[a++]       = "Modèles"; 
        regexpList[b++]         = "^Modèle:"; 
        
        interfaceMsg[a++]       = "Aide"; 
        regexpList[b++]         = "^Aide:"; 
        
        interfaceMsg[a++]       = "Catégories"; 
        regexpList[b++]         = "^Catégorie:";      
        
  

        //////////////////////////////////////////////////
        var topTag = document.getElementById("contentSub")
        
        // récupère toutes les pages
        var watchlist = document.getElementsByTagName("ul");

        for (u=0;u<watchlist.length;u++) {
                        var entries = watchlist[u].getElementsByTagName("li"); 
                        for (i=0;i<entries.length;i++) {
                                suiviManagerAllPages.push(entries[i]);
                        }
        }
        
        // prépare la mini-interface
        var str = "<div style=\"background-color:#8ecfe4;font-size:1px;height:8px;border:1px solid #AAAAAA;-moz-border-radius-topright:0.5em;-moz-border-radius-topleft:0.5em;\"></div>"
        + "<div style=\"border:1px solid #6ac1de;border-top:0px solid white;padding:5px 5px 0 5px;margin-bottom:3ex;\"><p>"
        + "<div style=\"float: left; text-align: left; white-space: nowrap;\"></div>"
        
        for (var cpt = 0; cpt < interfaceMsg.length; cpt ++) {
                str += "<a href=\"javascript:SuiviManagerRegexp('" + regexpList[cpt] + "')\">"
                                                + interfaceMsg[cpt]
                                                + " ·</a> "
                }
        
                str += "<a href=\"javascript:SuiviManagerLiensRouges()\">"
                        + "Retirer les liens rouges"
                        + " ·</a> "
                        
                str += "<a href=\"javascript:SuiviManagerDeselect()\">"
                        + "<b>Enlever toutes les coches</b>"
                        + " ·</a> "
                                
        topTag.innerHTML =       topTag.innerHTML + "<br clear=all />" + str + "<p></div>"
                
}

addLoadEvent(SuiviManager);


/*
 * Auto Refresh
 */

function refresh()
{
    window.location.href = unescape(window.location);
}

function refresh1min()
{
    // the timeout value should be the same as in the "refresh" meta-tag
    setTimeout( "refresh()", 60000 );
}

function refresh5min()
{
    // the timeout value should be the same as in the "refresh" meta-tag
    setTimeout( "refresh()", 300000 );
}

if (location.href.indexOf('Special:Recentchanges') != -1) {
  addLoadEvent(refresh5min);
}

if (location.href.indexOf('Special:Watchlist') != -1) {
  addLoadEvent(refresh5min);
}

/* </nowiki></pre> */