User:Chris G Bot 3/Source

From Wikipedia, the free encyclopedia

<source lang="php"> <?php /** Chris G Bot 3 - http://en.wikipedia.org/wiki/User:Chris_G_Bot_3

*  An archving bot for WP:CHU and WP:CHU/SUL
*  Copyright (C) 2008  Chris Grant - http://en.wikipedia.org/wiki/User:Chris_G
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*   
*  Developers (add your self here if you worked on the code):
*  	Chris - User:Chris_G - Wrote up the main code
*	Cobi  - User:Cobi    - Wrote some of botclasses.php
**/

/* Setup my classes */ require_once 'botclasses.php'; $wiki = new wikipedia; $wiki->url = 'http://en.wikipedia.org/w/api.php';

/* All the login stuff */ $user = 'Chris G Bot 3'; require_once 'password.php'; $wiki->login($user,$pass); unset($pass);

/* Some config vars */

/* WP:CHU */ $chu = 'Wikipedia:Changing username'; $chu_notdone = $chu.'/Unfulfilled/'.date('Y').'/'.date('F'); $chu_done = 'Wikipedia:Changing username/Archive';

/* WP:CHU/SUL */ $archive_length = 100000; $sul = 'Wikipedia:Changing username/SUL'; $sul_done = $sul.'/Completed/'; $sul_notdone = $sul.'/Rejected/';


/* Work out the length of the archives and which archive to use */ $i = 1; $temp = $sul_done . $i; while ($i != false) { $data = $wiki->getpage($sul_done.$i); sleep(1); //We're looping - we need this if (strlen($data) > $archive_length) { $i++; $temp = $sul_done . $i; } else { $i = false; $sul_done = $temp; } } $i = 1; $temp = $sul_notdone . $i; while ($i != false) { $data = $wiki->getpage($sul_notdone.$i); if (strlen($data) > $archive_length) { $i++; $temp = $sul_notdone . $i; } else { $i = false; $sul_notdone = $temp; } } $i = 60; $temp = $chu_done . $i; while ($i != false) { $data = $wiki->getpage($temp); if (strlen($data) > $archive_length) { $i++; $temp = $chu_done . $i; } else { $i = false; $chu_done = $temp; } }

/* Get all the crats names - for parsing the signatures */ $x = $wiki->query('?action=query&list=allusers&augroup=bureaucrat&aulimit=500&format=php'); foreach ($x['query']['allusers'] as $t) { $crats[] = $t['name']; }

/* Archive WP:CHU/SUL */ archive($sul,$sul_done,$sul_notdone);

/* Archive WP:CHU */ archive($chu,$chu_done,$chu_notdone);

/* Upload the source to User:$user/Source */ $page = "User:$user/Source"; $data = $wiki->getpage($page); $summary = 'Automated source upload (BOT EDIT)'; $source = "<source lang=\"php\">\n".file_get_contents(__FILE__); if ($data != $source) { $wiki->edit($page,$source,$summary); sleep(15); }

function archive($page, $done_archive, $notdone_archive) { global $crats, $wiki;

$data = $wiki->getpage($page); $new_data = $data;

/* Split the page into threads */ $temp = explode(chr(10), $data); $i = 0; foreach ($temp as $t) { if (preg_match('/^===.+===$/i', $t)) { $i++; } $requests[$i] = $requests[$i] ."\n". $t; }

$done = 0; $notdone = 0; foreach ($requests as $r) { if (!preg_match("/\{\{(not.?)?done\}\}/i",$r) and preg_match('/=== (.+) → (.+) ===/i', $r, $m)) { $x = $wiki->query('?action=query&list=logevents&letype=renameuser&format=php&letitle=User:'.urlencode($m[1])); if (isset($x['query']['logevents'][0]['user'])) { if ($x['query']['logevents'][0][0]==$m[2] or preg_match('/'.preg_quote($m[2],'/').'/i',$x['query']['logevents'][0]['comment'])) { $text_to_add = ": Done by [[User:".$x['query']['logevents'][0]['user']."|]] --MZMcBride (talk) 01:14, 21 February 2020 (UTC)\r\n"; if (!preg_match("/\n$/",$r)) { $text_to_add = "\r\n".$text_to_add; } $new_data = str_replace($r, $r.$text_to_add, $new_data); continue; } } } foreach ($crats as $c) { if (preg_match("/\{\{done\}\}.*User(( |_)talk)?:$c/i", $r, $m)) { $unix = getTimestamp($r); $time = strtotime('12 hours', $unix); if (time() > $time) { $threads['done'] = $threads['done'] . $r; $new_data = str_replace($r, , $new_data); $done++; } break; } elseif (preg_match("/\{\{not.?done\}\}.*User(( |_)talk)?:$c/i", $r, $m)) { $unix = getTimestamp($r); $time = strtotime('36 hours', $unix); if (time() > $time) { $threads['notdone'] = $threads['notdone'] . $r; $new_data = str_replace($r, , $new_data); $notdone++; } break; } } }

/* Remove the requests from the page */ /* Work out the edit summary */ $summary = "Archiving "; if ($done > 0) { $summary .= "$done Completed Request(s) "; $completed = true; } if ($notdone > 0) { if ($completed) { $summary .= "and $notdone Rejected Request(s)"; } else { $summary .= "$notdone Rejected Request(s)"; } } $summary .= " (BOT EDIT)"; $minor = true; if ($data != $new_data) { $wiki->edit($page,$new_data,$summary,$minor); sleep(15); /* From WP:BOT - bots doing non-urgent tasks may edit approximately once every ten seconds */

if ($done > 0) { /* Add the  Done requests to their archive */ $data = $wiki->getpage($done_archive); $new_data = "$data\n".$threads['done']; $summary = "Adding $done Completed Request(s) (BOT EDIT)"; $minor = true; $wiki->edit($done_archive,$new_data,$summary,$minor); sleep(15); }

if ($notdone > 0) { /* Add the  Not done requests to their archive */ $data = $wiki->getpage($notdone_archive); $new_data = "$data\n".$threads['notdone']; $summary = "Adding $notdone Rejected Request(s) (BOT EDIT)"; $minor = true; $wiki->edit($notdone_archive,$new_data,$summary,$minor); sleep(15); } } }

/* Returns the last timestamp in $text. */ function getTimestamp($text) { if (preg_match_all("/([0-9]+:[0-9]+, [0-9]+ [a-z]+ [0-9]+ \((UTC|GMT)\))/i", $text, $m)) { return strtotime($m[0][count($m[0])-1]); } else { return time(); } } ?>