Skip to content

Instantly share code, notes, and snippets.

@johnduhart
Created May 29, 2010 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnduhart/418088 to your computer and use it in GitHub Desktop.
Save johnduhart/418088 to your computer and use it in GitHub Desktop.
<?php
/**
* Signpost Book Bot
*
* Creates books for the Wikipedia Signpost
*
* Vesrion one
*/
// REMOVE THIS LATER
$config = array(
'user' => 'Signpost Book Bot',
'pass' => ''
);
require_once('../final_shared.php');
require_once('../wikibot.php');
msg('Creating bot instance');
$bot = new wikipedia;
// Login
msg('Logging in');
$bot->login($config['user'], $config['pass']);
// Get where the template is used
msg('Getting all the prefixed pages (this will take awhile)...');
$pages = $bot->getallpages_prefix('Wikipedia Signpost', '&apnamespace=4');
msg('Done!');
msg('Getting books that are already existing...');
$created = $bot->categorymembers('Category:Wikipedia books on the Wikipedia Signpost');
msg('Done!');
msg('Beginning to process pages');
foreach(array_values($pages) as $page)
{
//Check to see if it matches our article regex
if(!preg_match('/^Wikipedia:Wikipedia Signpost\/(20\d\d-\d\d-\d\d)$/', $page, $match))
{
msg($page.' is not a valid page, skipping');
continue;
}
// Check to see if we did this one already
if(in_array($page, array_values($created)))
{
msg($page.' was created already');
continue;
}
msg('Processing '.$page);
$timestamp = strtotime($match[1]);
$human_date = date('j F Y', $timestamp);
$book_page = <<<EOT
{{saved book
|title=Wikipedia Signpost
|subtitle=$human_date
|cover-image=WikipediaSignpostIcon.svg
|cover-color=White
}}
==The Wikipedia Signpost==
===$human_date===
EOT;
$post_page = $bot->getpage($page);
// Check for the new format of the articles page
if(strpos($post_page, '{{s-s') === FALSE AND strpos($post_page, '{{Wikipedia:Signpost/Template:Cover-item') === FALSE)
{
$articles_list = str_replace('*', ':', $post_page);
}
else
{
preg_match_all('/\{\{(Wikipedia:Signpost\/Template:Cover-item|s-s)\|\{\{\{1\}\}\}\|\d{1,3}\|20\d\d-\d\d-\d\d\|([^\|]*?)\|([^\}]*?)\}\}/', $post_page, $matches);
$articles_list = '';
//print_r($matches); die;
foreach(array_keys($matches[0]) as $id)
{
$articles_list .= ':[[Wikipedia:Wikipedia_Signpost/'.$match[1].'/'.$matches[2][$id].'|'.$matches[3][$id]."]]\n";
}
}
$book_page .= $articles_list."\n\n[[Category:Wikipedia books on the Wikipedia Signpost|{$match[1]}]]";
msg("\tSaving book...");
if($bot->nobots('Book:Wikipedia Signpost/'.$match[1]))
{
//echo "BOOK CONTENT:\n\n".$book_page."\n\n";
$bot->edit('Book:Wikipedia Signpost/'.$match[1], $book_page, 'Creating book page for the [[Wikipedia:Wikipedia Signpost|Wikipedia Signpost]] ([[WP:BOT|BOT]])');
}
else
{
msg("\t\tError: hit nobots");
}
msg("\tSaving talkpage");
if($bot->nobots ('Book:Wikipedia Signpost/'.$match[1]))
{
$bot->edit('Book_talk:Wikipedia Signpost/'.$match[1], "{{WBOOKS|class=book}}\n{{Wikipedia Signpost Book}}", 'Creating book talk page for the [[Wikipedia:Wikipedia Signpost|Wikipedia Signpost]] ([[WP:BOT|BOT]])');
}
else
{
msg("\t\tError: hit nobots");
}
msg('Done.');
}
<?php
/**
* Signpost Book Bot
*
* Creates books for the Wikipedia Signpost
*
* Vesrion one
*/
// REMOVE THIS LATER
$config = array(
'user' => 'Signpost Book Bot',
'pass' => ''
);
require_once('../final_shared.php');
require_once('../wikibot.php');
msg('Creating bot instance');
$bot = new wikipedia;
// Login
msg('Logging in');
$bot->login($config['user'], $config['pass']);
msg('Getting books that are already existing...');
$pages = $bot->categorymembers('Category:Wikipedia books on the Wikipedia Signpost');
msg('Done!');
$timed_pages = array();
msg('Beginning to process pages to timestamps');
foreach(array_values($pages) as $page)
{
//Check to see if it matches our article regex
if(!preg_match('/^Book:Wikipedia Signpost\/(20\d\d-\d\d-\d\d)$/', $page, $match))
{
msg("\t".$page.' is not a valid page, skipping');
continue;
}
$timed_pages[strtotime($match[1])] = $page;
}
msg('Done');
$book_page = "<!-- Do not edit this list, it is gernated by a robot -->\nThis is a list of all book versions of the ''[[Wikipedia:Wikipedia Signpost|Wikipedia Signpost]]''.\n";
$current_year = '0';
$current_month = '0';
msg('Building book list');
foreach($timed_pages as $timestamp => $page)
{
$year = date('Y', $timestamp);
$month = date('F', $timestamp);
$human_date = date('j F Y', $timestamp);
if($year != $current_year)
{
$book_page .= "\n== $year ==";
$current_year = $year;
$current_month = '';
}
if($month != $current_month)
{
$book_page .= "\n;$month\n";
$current_month = $month;
}
$book_page .= "*[[$page|$human_date]]\n";
}
$book_page .= "\n{{disambig}}\n";
msg('Editing...');
$bot->edit('Book:Wikipedia Signpost', $book_page, 'Updating book pages directory for [[Wikipedia:Wikipedia Signpost|Wikipedia Signpost]] ([[WP:BOT|BOT]])');
msg('Done!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment