User:Dmilewski/Wikibot

From D&D Wiki
Jump to navigation Jump to search

I set this bot up so that I only need to edit one section to do my work. Everything else stays the same.

The bot is not displaying correctly. Edit the page to get the raw text.

use CMS::MediaWiki;


#--------------------------
# GENERAL SETUP
#---------------------------

# Establish Control Variables
$wikiusername='USERNAME';
$wikipassword='PASSWORD';
$Purpose=;
$WikiPageTitle=;
$WikiPageSummary='Auto-creating links';

#
# Create a session with the dndwiki
#

 my $mw = CMS::MediaWiki->new(
       # protocol => 'https',  # Optional, default is http
       host  => 'www.dandwiki.com',   # Default: localhost
       # path  => 'wiki' ,       # Can be empty on 3rd-level domain Wikis
       debug => 0             # Optional. 0=no debug msgs, 1=some msgs, 2=more msgs
 );
 #
 # Log into the wiki
 #

 if ($mw->login(user => $wikiusername, pass => $wikipassword)) {
       print STDERR "Could not login\n";
       exit;
 }
 else {
       print "Logged In\n";
 }



#Open the list of pages that this script will process

open (INFILE, $ARGV[0]) || die "No such file.";
  1. ----------------------------------------
  2. Process Data
  3. ----------------------------------------
  1. for each page title in the designated file, go editing.


  1. Load pages into a hash. This is a trick to reduce redundant pages, and so speed up editing.
  2. The keys are the important data. The values in the hash are all zero.

foreach (<INFILE>) {

   s/[\n\t\r]//g;
   $PageHash{$_}=0;    

}


  1. Process each page.
  2. Added sorting to help me track a job's process.

foreach (sort(keys %PageHash)) {

   print "$_...";
   ProcessPage($mw, $_);

}


  1. END END END END END
  1. ---------------------------------
  2. ---------------------------------- SUBROUTINES AFTER HERE
  1. -----------------------------------

sub ProcessPage {

   my $WikiReference= shift (@_);
   $WikiPageTitle = shift (@_);
   
   # Fetch the page from the wiki
   # Returns a pointer
   
   my $lines_ref = $WikiReference->getPage(title => $WikiPageTitle);
   #print "$_\n" foreach @$lines_ref;
   # sends the text through the actual editing routines
   my $EditedText = "";
   $EditedText = PreprocessWikiText($lines_ref);
   my $CompareText=$EditedText;
   
   $EditedText = EditWikiText($EditedText);
   #print $EditedText;
   # places the page back onto the  
   if ($CompareText eq $EditedText) {
       print "SAME...not writing\n";
   } else {
       if ($Purpose =~ /WRITE/) {
           print "writing...";
           
           $rc = $mw->editPage(
               title => $WikiPageTitle,
               section => ,
               text => $EditedText,
               summary => $WikiPageSummary
           );
           print " done\n";
       } elsif ($Purpose =~ /PRINT/) {
           print "*********************************************\n$EditedText";
       } elsif ($Purpose =~ /SANDBOX/) {
           print "writing to sandbox...";
           
           $rc = $mw->editPage(
               title => 'Sandbox',
               section => ,
               text => $EditedText,
               summary => 'Testing script in sandbox'
           );
           print "done\n";
       } else {
           print "not writing\n";
       }
   }

}

sub PreprocessWikiText {

   # Turns the referenced text into a string, then applies any universal editing.
   # Strings are separated by newline characters.
   
   # Values received:
   # $WikiTextRef -> this is a pointer to an object containing the wiki text.
   # $WikiText -> this is a string variable that receives the data from the dereferenced $WikiTextRef
   
   #Verify that variable is clear
   $WikiText = "";
   
   my $WikiTextRef = shift (@_);
   # Concatenate the array into a string
   foreach (@$WikiTextRef) {
       $WikiText="$WikiText$_\n";
   }
   # Some special characters get changed into web characters. This messes up formatting marks
   # Change them back so that formatting commands act correctly.
   $WikiText =~ s/</</g;
   $WikiText =~ s/>/>/g;
   $WikiText =~ s/"/"/g;
   $WikiText =~ s/&/&/g;
   # Return the processed text to the calling function    
   return $WikiText;   

}

sub EditWikiText {

   my $DirtyText = shift (@_);
   my $CompareText = $DirtyText;
       
   #-------------------------------------------------------------------------------------------
   # Everything below can be edited to customize the task.
   #
   # Yes, I know lots can be improved. Right now, I accept that this works
   # as it is. There's a story about what damage a little tweak can do. 
   # Ask blue dragon about cleaning up that mess.
   #---------------------------------------------------------------------------------------------


   #	$DirtyText =~ s/\(Endhaven\)/(Endhaven Supplement)/g;    
   
   $DirtyText =~ s/(\[\[Category:.*)\]\]/$1|Dmilewski]]/g;
   


  #$WikiPageSummary is the updating text listed in recent changes. I often forget to change this.
  $WikiPageSummary='Changes references from (Endhaven) to (Endhaven Supplmenet)';
   # Purpose is TEST (or blank), PRINT, SANDBOX, or WRITE
   # TEST runs through the process, but does not write back to the wiki
   # PRINT shows you the changes that will be made, but does not write back
   # SANDBOX writes changes to the Sandbox
   # WRITE writes back to the edited article
   #
   # I really ought to handle this by command line argument, but I've been lazy
   $Purpose ="WRITE";
       
   return $DirtyText;

}