Difference between revisions of "Caretaker.pl"

From Organic Design wiki
(don't apply to code)
m
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{legacy}}
 +
<source lang="perl">
 +
#!/usr/bin/perl
 
use DBI;
 
use DBI;
 +
require "wiki.pl";
  
sub formatLink {
+
# Wiki settings
my $link = shift;
+
$::wiki = "http://www.organicdesign.co.nz/wiki/index.php";
$link =~ tr/_/ /;
+
$::wikiuser = 'bender';
$link =~ s/\s*([\[\]\|])\s*/$1/g;
+
$::wikipass = '******';
return $link;
 
}
 
  
sub upgradeSecurity {
+
# DB settings
my $x = shift;
+
$::dbname = 'od';
if ($x eq 'read') { $x = '*' }
+
$::dbpfix = '';
if ($x eq 'write') { $x = 'edit' }
+
$::dbuser = $::wikiuser;
my $y = shift;
+
$::dbpass = $::wikipass;
if ($y eq 'anyone') { $y = '*' }
 
if ($x ne '*' or $y ne '*') {
 
my $security = "{{#security:$x|$y}}";
 
$::article = "$security\n$::article";
 
$::comments .= $security;
 
}
 
}
 
  
sub caretaker {
+
# Login to wiki
my $dbh = DBI->connect('DBI:mysql:od',lc $::peer,$::pwd1) or die DBI->errstr;
+
wikiLogin($::wiki, $::wikiuser, $::wikipass);
my @ns = ('','Talk:','User:','User talk:','Project:','Project talk:',
+
my %ns = wikiGetNamespaces($::wiki);
'Image:','Image talk:','Mediawiki:','Mediawiki talk:','Template:',
 
'Template talk:','Help:','Help talk:','Category:','Category talk:');
 
  
# Get id of first article after mediawiki built in articles
+
# Connect to DB
my $sth = $dbh->prepare('SELECT cur_id FROM cur WHERE cur_title = "Zhconversiontable"');
+
my $dbh = DBI->connect('DBI:mysql:'.$::dbname, lc $::dbuser, $::dbpass) or die DBI->errstr;
$sth->execute();
 
my @row = $sth->fetchrow_array;
 
$sth->finish;
 
my $first = $row[0]+1;
 
  
# Get last article id
+
# Get id of first article after mediawiki built in articles
my $sth = $dbh->prepare('SELECT cur_id FROM cur ORDER BY cur_id DESC');
+
my $sth = $dbh->prepare('SELECT page_id FROM '.$::dbpfix.'page WHERE page_title = "Zhconversiontable"');
$sth->execute();
+
$sth->execute();
@row = $sth->fetchrow_array;
+
my @row = $sth->fetchrow_array;
$sth->finish;
+
$sth->finish;
my $last = $row[0];
+
my $first = $row[0]+1;
  
# Loop through all articles one per second
+
# Get last article id
my $sth = $dbh->prepare('SELECT cur_namespace,cur_title,cur_is_redirect FROM cur WHERE cur_id=?');
+
my $sth = $dbh->prepare('SELECT page_id FROM '.$::dbpfix.'page ORDER BY page_id DESC');
my $done = 'none';
+
$sth->execute();
for ($first..$last) {
+
@row = $sth->fetchrow_array;
$sth->execute($_);
+
$sth->finish;
@row = $sth->fetchrow_array;
+
my $last = $row[0];
my @comments = ();
 
my $title = $ns[$row[0]].$row[1];
 
if ($title && ($row[2] == 0)) {
 
  
# Read the article content
+
# Loop through all articles one per second
$::article = wikiRawPage $::wiki, $title;
+
my $sth = $dbh->prepare('SELECT page_namespace,page_title,page_is_redirect FROM '.$::dbpfix.'page WHERE page_id=?');
$::article =~ s/^\s+//;
+
my $done = 'none';
$::article =~ s/\s+$//;
+
for ($first..$last) {
my $backup = $::article;
+
$sth->execute($_);
my $isCode = $title =~ /\.(as|c|cpp|c\+\+|h|sh|css|html?|pl|php|R|tex|xml|xslt)$/i;
+
@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;
  
if (!$isCode) {
+
# ------ REPLACEMENT RULES --------------------------------------------------------------- #
  
# Format catlinks and normal links
+
# Common typo's - put square brackets around first letter so it doesn't get caretaken itself!
my $tmp = $::article;
+
my $typos
my @links = $::article =~ /\[{2}category:([^\]]+)\]{2}/ig;
+
= $text =~ s/[i]mpliment/implement/g
my %links = (); $links{$_} = 1 for @links;
+
+ $text =~ s/[d]ependance/dependence/g
my $cats = '';
+
+ $text =~ s/[f]reind/friend/g;
if ($title =~ /^[a-z0-9-]+\.(com?|edu|govt?|net|org|mil|info|school|iwi|ac|geek|aero|biz|coop|museum|name|pro)(\.[a-z]{2})?$/i) {
+
push @comments, 'cat in [[Category:Domain names|Domain names]]'
+
push @comments, ($typos == 1 ? 'typo' : 'typos') if $typos;
unless $links{'Domain names'}++;
 
}
 
if ($::article =~ /<\/math>/) {
 
push @comments, 'cat in [[Category:Articles containing maths|Maths]]'
 
unless $links{'Articles containing maths'}++;
 
}
 
@links = sort keys %links;
 
if ($#links >= 0) {
 
for (@links) { tr/_/ /; $cats .= "[[Category:$_]]"; }
 
$::article =~ s/\s*\[{2}\s*category:[^\]]+\s*\]{2} *//sig;
 
$::article =~ s/^\s+//;
 
$::article =~ s/\s+$//;
 
$::article = "$cats\n$::article";
 
push @comments, 'categories' if $::article ne $tmp;
 
}
 
  
# Format normal links
+
# ---------------------------------------------------------------------------------------- #
$tmp = $::article;
 
$::article =~ s/(\[{2}[^\]\r\n]+\]{2})/formatLink($1)/eg;
 
push @comments, 'links' if $::article ne $tmp;
 
  
# Format headings
+
# If article changed, write and comment
$tmp = $::article;
+
$text =~ s/^\s+//;
my $min = 10;
+
$text =~ s/\s+$//;
for ($::article =~ /^(=+) *.+? *=+ *$/mg) { $min = length if $min > length }
+
if ($text ne $backup) {
$::article =~ s/[\r\n]+ *={$min}(=*) *([^=\r\n]+?) *=+ *[\r\n]+/\n\n$1 $2 $1\n/g if --$min;
+
wikiPageEdit($::wiki, $title, $text, join(', ',@comments), 1);
push @comments, 'headings' if $::article ne $tmp;
+
$done = $done + 1;
}
 
 
 
# Common typo's - put square brackets around first letter so it doesn't get caretaken itself!
 
my $typos
 
= $::article =~ s/[i]mpliment/implement/g
 
+ $::article =~ s/[d]ependance/dependence/g
 
+ $::article =~ s/[h]ardwire/hard-wire/g;
 
push @comments, ($typos == 1 ? 'typo' : 'typos') if $typos;
 
 
 
# Apply rules to Xml:Articles
 
if ($title =~ /^Xml:/) {
 
 
 
# Remove empty elements
 
push @comments, 'removed empty elements' if $::article =~ s/^\s*<(read|write|category|init|data|view|edit|save)>\s*<\/\w+>[\r\n]*//gms;
 
 
 
}
 
# Convert XML permissions to Simple-Security format
 
elsif (!$isCode) {
 
my $xml = wikiRawPage $::wiki, "Xml:$title";
 
$::comments = '';
 
$xml =~ s/<(read|write)>(.+?)<\//upgradeSecurity($1,$2)/eg;
 
push @comments, $::comments if $::comments;
 
}
 
 
 
# --------------------------------------------------------------------------------------------------------------------- #
 
 
 
# If article changed, write and comment
 
$::article =~ s/^\s+//;
 
$::article =~ s/\s+$//;
 
if ($::article ne $backup) {
 
wikiPageEdit $::wiki, $title, $::article, '[[talk:caretaker.pl|Caretaker]]: '.join(', ',@comments), 1;
 
$done = $done + 1;
 
}
 
 
}
 
}
sleep(1);
 
 
}
 
}
 +
sleep(0.2);
 +
}
  
my $comment = "Daily [[talk:caretaker.pl|caretaking]] tasks completed: ".($last-$first)." articles examined, ".($done-1)." adjusted.";
+
$sth->finish;
logAdd $comment;
+
$dbh->disconnect;
wikiPageAppend($::wiki, $::wikilog, "\n*".localtime()." : $comment", $comment);
+
</source>
$sth->finish;
+
[[Category:PERL]]
$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;