#!/usr/share/php
<?php
PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
$BASE = "http://www.php.net";

function ce(DOMDocument $d, $name, $value, array $attrs = array(), DOMNode $to = null) {
	if ($value) {
		$n = $d->createElement($name, $value);
	} else {
		$n = $d->createElement($name);
	}
	foreach((array)$attrs as $k => $v) {
		$n->setAttribute($k, $v);
	}
	if ($to) {
		return $to->appendChild($n);
	}
	return $n;
}

$y = date("Y");
$today = date("Y-m-d");
$file = "archive/$y.xml";
file_exists($file) or die("Can't find $file, are you sure you are in phpweb/?\n");

$dom = new DOMDocument("1.0", "utf-8");
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->load($file);

$dom->getElementsByTagName("updated")->item(0)->nodeValue = date(DATE_ATOM);

$first = $dom->getElementsByTagName("entry")->item(0);

$xp = new DOMXpath($dom);
$xp->registerNamespace("a", "http://www.w3.org/2005/Atom");
$count = $xp->query('/a:feed/a:entry/a:id[contains(text(), "'.$today.'")]')->length + 1;
$id = $today . '-' .$count;

$item = $dom->createElementNs("http://www.w3.org/2005/Atom", "entry");

do {
	fwrite(STDOUT, "Please type in the title: ");
	$title = rtrim(fgets(STDIN));
} while(strlen($title)<3);

ce($dom, "title", $title, array(), $item);

$categories = array(
	array("frontpage"   => "PHP.net frontpage news"),
	array("releases"    => "New PHP release"),
	array("conferences" => "Conference announcement"),
	array("cfp"         => "Call for Papers"),
);
$confs = array(2, 3);

do {
	fwrite(STDOUT, "Categories:\n");
	foreach($categories as $n => $category) {
		fprintf(STDOUT, "\t%d: %s\t [%s]\n", $n, key($category), current($category));
	}
	fwrite(STDOUT, "Please select appropriate categories, seperated with space: ");

	$cat = explode(" ", rtrim(fgets(STDIN)));

	if ($cat) {
		break;
	}
	fwrite(STDERR, "You have to pick at least one category\n");
} while(1);


$via = $archive = $BASE . "/archive/$y.php#id" .$id;

ce($dom, "id", $archive, array(), $item);
ce($dom, "published", date(DATE_ATOM), array(), $item);
ce($dom, "updated", date(DATE_ATOM), array(), $item);


$conf = false;
foreach($cat as $keys) {
	if (in_array($keys, $confs)) {
		$conf = true;
		break;
	}
}
if ($conf) {
	/* /conferences news item */
	$href = $BASE . "/conferences/index.php";

	do {
		fwrite(STDOUT, "When does the conference start/cfp end? (strtotime() compatible syntax): ");
		$t = strtotime(fgets(STDIN));
		if ($t) {
			break;
		}

		fwrite(STDERR, "I told you I would run it through strtotime()!\n");
	} while(1);
	$item->appendChild($dom->createElementNs("http://php.net/ns/news", "finalTeaserDate", date("Y-m-d", $t)));
} else {
	$href = $BASE . "/index.php";
}

foreach($cat as $n) {
	if (isset($categories[$n])) {
		ce($dom, "category", null, array("term" => key($categories[$n]), "label" => current($categories[$n])), $item);
	} else {
		fprintf(STDERR, "Unkown category %d\n", $n);
	}
}

ce($dom, "link", null, array("href" => "$href#id$id", "rel"  => "alternate", "type" => "text/html"), $item);

fwrite(STDOUT, "Will a picture be accompanying this entry? ");
$yn = fgets(STDIN);
if (strtoupper($yn[0]) == "Y") {
	do {
		fwrite(STDOUT, "Enter the image name (note: the image has to exist in './images/news'): ");
		$path = basename(rtrim(fgets(STDIN)));
	} while(!file_exists("./images/news/$path"));

	fwrite(STDOUT, "Image title: ");
	$title = rtrim(fgets(STDIN));

	fwrite(STDOUT, "Link (when clicked on the image): ");
	$via = rtrim(fgets(STDIN));

	$image = $item->appendChild($dom->createElementNs("http://php.net/ns/news", "newsImage", $path));
	$image->setAttribute("link", $via);
	$image->setAttribute("title", $title);
}
ce($dom, "link", null, array("href" => $via, "rel"  => "via", "type" => "text/html"), $item);

$content = ce($dom, "content", null, array(), $item);

fwrite(STDOUT, "And at last; paste/write your news item here.\nTo end it, hit <enter>.<enter>\n");
$news = "";
while(($line = rtrim(fgets(STDIN))) != ".") {
	$news .= $line;
}

$tdoc = new DOMDocument("1.0", "utf-8");
$tdoc->formatOutput = true;
if ($tdoc->loadXML('<div>'.$news.'</div>')) {
	$content->setAttribute("type", "xhtml");
	$div = $content->appendChild($dom->createElement("div"));
	$div->setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
	foreach($tdoc->firstChild->childNodes as $node) {
		$div->appendChild($dom->importNode($node, true));
	}
} else {
	fwrite(STDERR, "There is something wrong with your xhtml, falling back to html");
	$content->setAttribute("type", "html");
	$content->nodeValue = $news;
}


$item = $first->parentNode->insertBefore($item, $first);

$dom->save($file);

fwrite(STDOUT, "File saved. Please cvs diff $file to sanity-check the changes before committing\n");

