Difference between revisions of "Caretaker.pl"

From Organic Design wiki
m (Don't do caretaking on mediawiki articles)
m
 
(88 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 last article id
+
# DB settings
my $sth = $dbh->prepare('SELECT cur_id FROM cur WHERE cur_namespace != 8 ORDER BY cur_id DESC');
+
$::dbname = 'od';
$sth->execute();
+
$::dbpfix = '';
my @row = $sth->fetchrow_array;
+
$::dbuser = $::wikiuser;
$sth->finish;
+
$::dbpass = $::wikipass;
my $last = $row[0];
 
  
# Loop through all articles one per second
+
# Login to wiki
my $sth = $dbh->prepare('SELECT cur_namespace,cur_title FROM cur WHERE cur_id=?');
+
wikiLogin($::wiki, $::wikiuser, $::wikipass);
my $done = 0;
+
my %ns = wikiGetNamespaces($::wiki);
for ($first..$last) {
 
$sth->execute($_);
 
@row = $sth->fetchrow_array;
 
my @comments = ();
 
my $title = '';
 
if ($title = $ns[$row[0]].$row[1]) {
 
  
# Read the article content
+
# Connect to DB
my $article = my $backup = wikiRawPage $::wiki, $title;
+
my $dbh = DBI->connect('DBI:mysql:'.$::dbname, lc $::dbuser, $::dbpass) or die DBI->errstr;
  
# Apply rules to Xml:Articles
+
# Get id of first article after mediawiki built in articles
if ($title =~ /^Xml:/) {
+
my $sth = $dbh->prepare('SELECT page_id FROM '.$::dbpfix.'page WHERE page_title = "Zhconversiontable"');
push @comments, 'removed empty elements' if $article =~ s/^\s*<(read|write|category|init|data|view|edit|save)>\s*<\/\w+>[\r\n]*//gms;
+
$sth->execute();
}
+
my @row = $sth->fetchrow_array;
 +
$sth->finish;
 +
my $first = $row[0]+1;
  
# If article changed, write and comment
+
# Get last article id
wikiPageEdit $::wiki, $title, $article, join(', ',@comments) if $article ne $backup;
+
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(1);
 
 
}
 
}
 +
sleep(0.2);
 +
}
  
my $comment = "Daily caretaking tasks completed: $last 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;