1
0
mirror of https://github.com/php/web-php.git synced 2026-03-23 23:02:13 +01:00
Files
archived-web-php/bin/createNewsEntry
Hannes Magnusson 51a80559d4 SVNify
2009-07-30 10:42:44 +00:00

174 lines
5.1 KiB
PHP
Executable File

#!/usr/bin/env 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($attrs as $k => $v) {
$n->setAttribute($k, $v);
}
if ($to) {
return $to->appendChild($n);
}
return $n;
}
$archivefile = "archive/archive.xml";
file_exists($archivefile) or die("Can't find $archivefile, are you sure you are in phpweb/?\n");
$filename = date("Y-m-d", $_SERVER["REQUEST_TIME"]);
$count = 0;
do {
++$count;
$id = $filename. "-" . $count;
$basename = $id .".xml";
$file = "archive/entries/" . $basename;
$xincluded = "entries/" . $basename;
fprintf(STDOUT, "Trying $file\n");
} while (file_exists($file));
$dom = new DOMDocument("1.0", "utf-8");
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$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/" .date("Y", $_SERVER["REQUEST_TIME"]).".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 = "\n";
while(($line = rtrim(fgets(STDIN))) != ".") {
$news .= " $line\n";
}
$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;
}
$dom->appendChild($item);
$dom->save($file);
$arch = new DOMDocument("1.0", "utf-8");
$arch->formatOutput = true;
$arch->preserveWhiteSpace = false;
$arch->load("archive/archive.xml");
$first = $arch->createElementNs("http://www.w3.org/2001/XInclude", "xi:include");
$first->setAttribute("href", $xincluded);
$second = $arch->getElementsByTagNameNs("http://www.w3.org/2001/XInclude", "include")->item(0);
$arch->documentElement->insertBefore($first, $second);
$arch->save("archive/archive.xml");
fwrite(STDOUT, "File saved.\nPlease svn diff $archivefile sanity-check the changes before committing\n");
fwrite(STDOUT, "NOTE: Remeber to svn add $file && svn commit $file!!\n");