forked from php/web-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateNewsEntry
executable file
·173 lines (141 loc) · 5.1 KB
/
createNewsEntry
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env php
<?php
PHP_SAPI == 'cli' or die("Please run this script using the cli sapi");
$BASE = "http://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 git diff $archivefile sanity-check the changes before committing\n");
fwrite(STDOUT, "NOTE: Remeber to git add $file && git commit $file && git push!!\n");