public
Last active

  • Download Gist
bot.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
<?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.');
}
build_dir.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
<?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!');

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.