JargonWiki:BulkPageCreator.php
From JargonWiki
Note: SVN access, most recent versions, documentation, version history, and more are available at the Google Code site for this project:
<?php
# PHP MediaWiki Bulk Page Creator
# Version: 1.1
# Author: Jonathan Cutrer
# Website: http://jon.cutrer.org/
#
# Modifications: Charlie File
# Website: http://charlesfile.com/
# Modified for: http://jargonwiki.com
#
#
# This program must have the Snoopy Class Library to run.
# http://sourceforge.net/projects/snoopy/
#
#
# Syntax:
#
# Invoked via a Command Line Interface (CLI):
# > php BulkPageCreator.php inputfile.txt
#
# - or -
#
# Invoked via a Uniform Resource Identifier (URI):
# http://jargonwiki.com/wiki/BulkPageCreator.php?file=inputfile.txt
#
#
#
#
# This is basically a requirement for anything more than a handful of articles.
# Batch submits of thousands of articles to a remote host can take hours.
set_time_limit(0); //0 = no time limit.
include "./w/includes/Snoopy.class.php";
$snoopy = new Snoopy;
$wikiroot = "http://jargonwiki.com/wiki";
$login_url = $wikiroot . "/index.php?title=Special:Userlogin&action=submitlogin";
$submit_url = $wikiroot . "/index.php?title=Special:Userlogin&action=submitlogin";
# Set the username and password below:
$login_vars['wpName'] = 'JargonBot';
$login_vars['wpPassword'] = '';
$login_vars['wpRemember'] = '1';
# Login to Wiki
$snoopy->submit($login_url,$login_vars);
# Open Source File and Read into $contents
if (count($_GET) > 0) {
# Via URI:
$fp = fopen($_GET['file'], 'r');
$contents = fread($fp, filesize($_GET['file']));
} else {
# Via CLI:
$fp = fopen($argv[1], "r");
$contents = fread($fp, filesize($argv[1]));
}
fclose($fp);
# Split $contents in $pages array
$pages = split("--ENDPAGE--", $contents);
# Loop for each item in pages array
# During loop we will get edit page for token then submit form to create page
foreach ($pages as $key => $value) {
list($title, $body) = split("--ENDTITLE--", $value);
echo $title;
# Get rid of newlines in title
$title = str_replace("\n", "", $title);
# Make Safetitle for URL
$safetitle = rawurlencode(str_replace(" ", "_", $title));
# Lets make sure $title contains something other than null
if ($title) {
# Submit to edit page for $title and get contents into $editpage
if($snoopy->submit($wikiroot . "/index.php?title=" . $safetitle . "&action=edit",$login_vars)) {
$editpage = $snoopy->results;
}
# Pick out Edit Token into $token
#echo "$editpage"; //DEBUG
$ans = preg_match('/.*value="(.*?)".*name="wpEditToken"/',$editpage, $matches);
$token = $matches[1];
echo $token;
# Set Post Variables before submitting
$submit_vars['wpTextbox1'] = $body;
$submit_vars['wpSummary'] = "";
$submit_vars['wpSection'] = "";
$submit_vars['wpEdittime'] = "";
$submit_vars['wpMinoredit'] = "1";
$submit_vars['wpSave'] = "Save page";
$submit_vars['wpEditToken'] = $token;
# Submit or Post to create the page
echo "Final Submit goes to:" . $wikiroot . "/index.php?title=" . $safetitle . "&action=submit";
if($snoopy->submit($wikiroot . "/index.php?title=" . $safetitle . "&action=submit", $submit_vars)) {
$finalresults = $snoopy->results;
}
echo $finalresults;
# End If Loop
}
# End ForEach Loop
}
exit;
?>
[edit]
External links
- Google Code site for this project: http://code.google.com/p/jargonwiki/

