How To Wiki
Advertisement
Warning: This page it's very incomplete, use this article, particular has very caution! Please help finish it this page!



Modifying MediaWiki pages in Perl.

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

CMS::MediaWiki[]

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

Logging In[]

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 page[]

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 page[]

Extracting pages from Archived database[]

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::Client[]

To be added

Advertisement