Create a gist now

Instantly share code, notes, and snippets.

@naoa /grn.php
Created Apr 20, 2014

Embed
What would you like to do?
<?php
$article = $argv[1];
if ($handle = opendir($article)) {
while (false !== ($file = readdir($handle))) {
echo "-------$file------\n";
echo "XML loading and importing...\n";
$xml = new XMLReader();
if(!$xml->open($article . "/" . $file)){
die('Failed to open file!');
}
$rc = 0;
$startTimeAll = microtime(true);
while ($xml->read()){
if ($xml->name === "page") {
$page = array();
$node = new SimpleXMLElement($xml->readOuterXML());
$page['id'] = (string)$node->id;
$page['title'] = (string)$node->title;
$page['text'] = (string)$node->revision->text;
if ($page['id'] != ""){
$startTime = microtime(true);
$json_doc = array(
"_key" => $page['id'],
"title" => $page['title'],
"text" => $page['text']
);
$qry = json_encode($json_doc);
$url="http://localhost:10041/d/load?table=text";
$req="POST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 10041);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$data = ob_get_contents();
ob_end_clean();
$endTime = microtime(true);
$elapsedTime = $endTime - $startTime;
echo $page['id'] . "," . $elapsedTime . "\n";
$rc++;
}
}
}
$xml->close();
$endTimeAll = microtime(true);
$elapsedTimeAll = $endTimeAll - $startTimeAll;
echo $rc . " records done. Total = " . $elapsedTimeAll . "\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment