FANDOM


Warning: This page is incomplete, use this article with caution. Please help finish it



Modifying MediaWiki pages in Perl.

There are a number of ways to edit MediaWiki pages in Perl.

CMS::MediaWikiEdit

CMS::MediaWiki is the main Perl module for reading reading editing MediaWiki pages

Logging InEdit

sub loginWiki {
	my $host=$_[0];
	my $username=$_[1];
	my $password=$_[2];

	$mw = CMS::MediaWiki->new( #keep global
		host  => $host,
		path  => '.' ,     #  Can be empty on 3rd-level domain Wikis
		debug => 0            #  0=no debug msgs, 1=some msgs, 2=more msgs
	);

	if ($mw->login(user => "$username", pass => "$password")) {
		print STDERR "Could not login, $username\n";
		exit;
	} else {
		print "Logged in. $username ...\n";
	}
}
  • Call sub
    • &loginWiki($host,$username,$password);

Write a pageEdit

sub updatePage {
	my $pageTitle=$_[0];
	my $pageText=$_[1]; 
	my $sleepVar=0;
	my $sleepInterval=2; #in seconds
	#Post the page
	if($pageTitle eq ''){
		print "!!!!!!!!!!!!!!!!!!!!!!!!!!";
		print "!! name error: name empty";
		print "!!!!!!!!!!!!!!!!!!!!!!!!!!";
	}else{
		$rc = $mw->editPage(   
			title   => "$pageTitle" ,
			section => '' , 
			text    => "$pageText" ,
		  summary => "Updated via  Bot." , #add summary as you wish
		);
		# randome sleep from 0-n in sec
		# to put less load on server
	}
	$sleepVar = rand($sleepInterval);
	system(" sleep $sleepVar");
	print "Page writen...\n";
}
  • Call sub
    • &writePage($title, $text);

Reading pageEdit

Extracting pages from Archived databaseEdit

Downloading database dump

$dbfileLocation="http://wikistats.wikia.com/dbdumps/howto/pages_current.xml.gz";

sub downloadDB {
	my $dbfile=$dbfileLocation;
	my $dbfileOut='';
	my $dbfileType='';

	print "\n# Get the database\n\n";
	$dbfile =~ s/.*\///;
	$dbfileOut = $dbfile;
	$dbfileOut =~ s/\.gz$//;
	$dbfileOut =~ s/\.bz2$//;
	system("rm -f $dbfile"); #removing old file
	system("rm -f $dbfileOut");
	system("wget  $dbfileLocation"); #getting db
	$dbfileType = $dbfile;
	$dbfileType =~ s/.*\.//; #aquiring db file type
	if ( $dbfileType eq 'gz' ) { #extraxting db
		system("gzip -d $dbfile");
	} elsif ( $dbfileType eq 'bz2' ) {
		system("bzip2 -d $dbfile");
	}
	return $dbfileOut;
}
  • Calling subrutine
    • $dbfile = &downloadDB;
      $dbfile is the variable using for the database file name

link title

WWW::Mediawiki::ClientEdit

To be added