Difference between revisions of "Caretaker.pl"

From Organic Design wiki
m (refine rule: replace _ with space)
m
 
(66 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{legacy}}
 +
<source lang="perl">
 +
#!/usr/bin/perl
 
use DBI;
 
use DBI;
 +
require "wiki.pl";
  
sub caretaker {
+
# Wiki settings
my $dbh = DBI->connect('DBI:mysql:wiki',lc $::peer,$::pwd1) or die DBI->errstr;
+
$::wiki = "http://www.organicdesign.co.nz/wiki/index.php";
my @ns = ('','Talk:','User:','User talk:','Project:','Project talk:',
+
$::wikiuser = 'bender';
'Image:','Image talk:','Mediawiki:','Mediawiki talk:','Template:',
+
$::wikipass = '******';
'Template talk:','Help:','Help talk:','Category:','Category talk:');
 
  
# Get id of first article after mediawiki built in articles
+
# DB settings
my $sth = $dbh->prepare('SELECT cur_id FROM cur WHERE cur_title = "Zhconversiontable"');
+
$::dbname = 'od';
$sth->execute();
+
$::dbpfix = '';
my @row = $sth->fetchrow_array;
+
$::dbuser = $::wikiuser;
$sth->finish;
+
$::dbpass = $::wikipass;
my $first = $row[0]+1;
 
  
# Get last article id
+
# Login to wiki
my $sth = $dbh->prepare('SELECT cur_id FROM cur ORDER BY cur_id DESC');
+
wikiLogin($::wiki, $::wikiuser, $::wikipass);
$sth->execute();
+
my %ns = wikiGetNamespaces($::wiki);
@row = $sth->fetchrow_array;
 
$sth->finish;
 
my $last = $row[0];
 
  
# Loop through all articles one per second
+
# Connect to DB
my $sth = $dbh->prepare('SELECT cur_namespace,cur_title FROM cur WHERE cur_id=?');
+
my $dbh = DBI->connect('DBI:mysql:'.$::dbname, lc $::dbuser, $::dbpass) or die DBI->errstr;
my $done = 'none';
 
for ($first..$last) {
 
$sth->execute($_);
 
@row = $sth->fetchrow_array;
 
my @comments = ();
 
my $title = '';
 
if ($title = $ns[$row[0]].$row[1]) {
 
  
# Read the article content
+
# Get id of first article after mediawiki built in articles
my $article = my $backup = wikiRawPage $::wiki, $title;
+
my $sth = $dbh->prepare('SELECT page_id FROM '.$::dbpfix.'page WHERE page_title = "Zhconversiontable"');
my $isCode = $title =~ /\.[a-z]{1,3}$/;
+
$sth->execute();
 +
my @row = $sth->fetchrow_array;
 +
$sth->finish;
 +
my $first = $row[0]+1;
  
# --------------------------------------------------------------------------------------------------------------------- #
+
# Get last article id
 +
my $sth = $dbh->prepare('SELECT page_id FROM '.$::dbpfix.'page ORDER BY page_id DESC');
 +
$sth->execute();
 +
@row = $sth->fetchrow_array;
 +
$sth->finish;
 +
my $last = $row[0];
  
# Group category links together at start of article
+
# Loop through all articles one per second
if (!$isCode) {
+
my $sth = $dbh->prepare('SELECT page_namespace,page_title,page_is_redirect FROM '.$::dbpfix.'page WHERE page_id=?');
my $tmp = $article;
+
my $done = 'none';
my @links = $article =~ /\[{2}category:([^\]]+)\]{2}/ig;
+
for ($first..$last) {
if ($#links >= 0) {
+
$sth->execute($_);
my $cats = '';
+
@row = $sth->fetchrow_array;
foreach @links {
+
my @comments = ();
tr/_/ /;
+
my $title = $ns{$row[0]} ? $ns{$row[0]}.':'.$row[1] : $row[1];
$cats .= "[[Category:$_]]";
+
if ($title && ($row[2] == 0)) {
}
+
print "$title\n";
$article =~ s/\s*\[{2}\s*category:([^\]]+)\s*\]{2}\s*//sig;
 
chomp $article;
 
$article = "$cats\n$article";
 
push @comments, 'Format cat links' if $tmp ne $article;
 
}
 
}
 
  
# Common typo's - put square brackets around first letter so it doesn't get caretaken itself!
+
# Read the article content
my $typos
+
$text = wikiRawPage($::wiki, $title);
= $article =~ s/[i]mpliment/implement/g
+
$text =~ s/^\s+//;
+ $article =~ s/[d]ependance/dependence/g
+
$text =~ s/\s+$//;
+ $article =~ s/[h]ardwire/hard-wire/g;
+
my $backup = $text;
push @comments, ($typos == 1 ? 'typo' : 'typos') if $typos;
 
  
# Apply rules to Xml:Articles
+
# ------ REPLACEMENT RULES --------------------------------------------------------------- #
if ($title =~ /^Xml:/) {
 
  
# Remove empty elements
+
# Common typo's - put square brackets around first letter so it doesn't get caretaken itself!
push @comments, 'removed empty elements' if $article =~ s/^\s*<(read|write|category|init|data|view|edit|save)>\s*<\/\w+>[\r\n]*//gms;
+
my $typos
 +
= $text =~ s/[i]mpliment/implement/g
 +
+ $text =~ s/[d]ependance/dependence/g
 +
+ $text =~ s/[f]reind/friend/g;
 +
 +
push @comments, ($typos == 1 ? 'typo' : 'typos') if $typos;
  
}
+
# ---------------------------------------------------------------------------------------- #
  
# --------------------------------------------------------------------------------------------------------------------- #
+
# If article changed, write and comment
 
+
$text =~ s/^\s+//;
# If article changed, write and comment
+
$text =~ s/\s+$//;
if ($article ne $backup) {
+
if ($text ne $backup) {
wikiPageEdit $::wiki, $title, $article, '[[talk:caretaker.pl|Caretaker]]: '.join(', ',@comments), 1;
+
wikiPageEdit($::wiki, $title, $text, join(', ',@comments), 1);
$done = $done + 1;
+
$done = $done + 1;
}
 
 
}
 
}
sleep(1);
 
 
}
 
}
 +
sleep(0.2);
 +
}
  
my $comment = "Daily [[talk:caretaker.pl|caretaking]] tasks completed: ".($last-$first)." articles examined, $done adjusted.";
+
$sth->finish;
logAdd $comment;
+
$dbh->disconnect;
wikiLogin($::wiki, $::peer, $::pwd1);
+
</source>
wikiPageAppend($::wiki, $::wikilog, "\n*".localtime()." : $comment", $comment);
+
[[Category:PERL]]
$sth->finish;
 
$dbh->disconnect;
 
}
 

Latest revision as of 03:19, 23 April 2020

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
#!/usr/bin/perl
use DBI;
require "wiki.pl";

# Wiki settings
$::wiki = "http://www.organicdesign.co.nz/wiki/index.php";
$::wikiuser = 'bender';
$::wikipass = '******';

# DB settings
$::dbname = 'od';
$::dbpfix = '';
$::dbuser = $::wikiuser;
$::dbpass = $::wikipass;

# Login to wiki
wikiLogin($::wiki, $::wikiuser, $::wikipass);
my %ns = wikiGetNamespaces($::wiki);

# Connect to DB
my $dbh = DBI->connect('DBI:mysql:'.$::dbname, lc $::dbuser, $::dbpass) or die DBI->errstr;

# Get id of first article after mediawiki built in articles
my $sth = $dbh->prepare('SELECT page_id FROM '.$::dbpfix.'page WHERE page_title = "Zhconversiontable"');
$sth->execute();
my @row = $sth->fetchrow_array;
$sth->finish;
my $first = $row[0]+1;

# Get last article id
my $sth = $dbh->prepare('SELECT page_id FROM '.$::dbpfix.'page ORDER BY page_id DESC');
$sth->execute();
@row = $sth->fetchrow_array;
$sth->finish;
my $last = $row[0];

# Loop through all articles one per second
my $sth = $dbh->prepare('SELECT page_namespace,page_title,page_is_redirect FROM '.$::dbpfix.'page WHERE page_id=?');
my $done = 'none';
for ($first..$last) {
	$sth->execute($_);
	@row = $sth->fetchrow_array;
	my @comments = ();
	my $title = $ns{$row[0]} ? $ns{$row[0]}.':'.$row[1] : $row[1];
	if ($title && ($row[2] == 0)) {
		print "$title\n";

		# Read the article content
		$text = wikiRawPage($::wiki, $title);
		$text =~ s/^\s+//;
		$text =~ s/\s+$//;
		my $backup = $text;

		# ------ REPLACEMENT RULES --------------------------------------------------------------- #

		# Common typo's - put square brackets around first letter so it doesn't get caretaken itself!
		my $typos
			= $text =~ s/[i]mpliment/implement/g
			+ $text =~ s/[d]ependance/dependence/g
			+ $text =~ s/[f]reind/friend/g;
			
		push @comments, ($typos == 1 ? 'typo' : 'typos') if $typos;

		# ---------------------------------------------------------------------------------------- #

		# If article changed, write and comment
		$text =~ s/^\s+//;
		$text =~ s/\s+$//;
		if ($text ne $backup) {
			wikiPageEdit($::wiki, $title, $text, join(', ',@comments), 1);
			$done = $done + 1;
			}
		}
	sleep(0.2);
	}

$sth->finish;
$dbh->disconnect;