2 include("t_config.php");
6 <title>WikiCommander:CommandExec</title>
7 <link rel="stylesheet" type="text/css" href="80x25.css">
9 <body bgcolor="#202527" text="#eeeeee" class="CommandExec Shell Exec" style="padding:3px;">
13 $id = $_REQUEST["id"];
15 #-- delete all page versions
16 if ($_REQUEST["delete"]) {
17 $ls = $_REQUEST["ls"];
24 #-- move page to new name
25 elseif ($_REQUEST["rename"]) {
26 cmd_mv(array($_REQUEST["id"], $_REQUEST["new_id"]));
30 elseif ($_REQUEST["copy"]) {
31 cmd_cp(array($_REQUEST["id"], $_REQUEST["new_id"]));
35 elseif ($_REQUEST["nop"]) {
38 #-- commandline actions
39 elseif ($_REQUEST["cmd"]) {
41 $args = split_input($_REQUEST["input"]);
42 $action = array_shift($args);
52 "touch" => "cmd_touch",
61 "cmd_mv" => "syntax: mv <i>FromPage ToPage</i>\n<br><br>\nrenames a page, and overwrites target page versions by that",
62 "cmd_ren" => "ren: works similar to 'mv' but refuses to overwrite existing page versions <br>\n('move' is an alias to 'ren')",
63 "cmd_ls" => "ls: lists existing pages, accepts glob parameters",
64 "cmd_ll" => "ll: long/verbose page listing (see also 'ls --help')",
65 "cmd_cp" => "cp: duplicates a page completely to another name, and overwrites any existing target page versions",
66 "cmd_rm" => "rm: completely deletes pages so that even no backup version will remain in the database (irreversible)",
67 "cmd_touch" => "touch: updates {lastmodified} field of given pages - but beware: RecentChanges may get you!",
68 "cmd_help" => "syntax: help [command]<br> or even [command] --help<br>\n<br>shows some notes on the given command",
69 "cmd_cat" => "syntax: help [command]<br> or even [command] --help<br>\n<br>shows some notes on the given command",
73 $pf = $funcs[$action];
77 elseif ($help[$pf] && (($args[0] == "--help") or ($args[0] == "-h") or ($args[0] == "-?"))) {
80 elseif ($pf && function_exists($pf)) {
83 elseif ($pf == "cmd_help") {
85 ($text = $help[$funcs[$args[0]]]) or ($text = "$args[0]: no help available");
89 echo "following commands are defined:<br>\n";
90 echo implode(", ", array_keys($funcs));
94 echo "unknown command '$action'\n";
101 echo "unknown command() call\n<br>";
108 #-- action calls / commands -----------------------------------------------
111 #-- delete page completely
112 function cmd_rm($ls) {
113 foreach ($ls as $id) {
114 echo "<em style=\"color:red\">deleting</em> page '$id' (<small>";
115 while ($data = get_almost_latest($data, $id)) {
116 $version = $data["version"];
117 if (ewiki_db::DELETE($id, $version)) {
122 echo "</small>)<br>\n";
127 #-- output page contents
128 function cmd_cat($ls) {
129 foreach ($ls as $id) {
130 if ($data = ewiki_db::GET($id)) {
131 echo htmlentities($data["content"]);
137 #-- directory listing
138 function cmd_ls($ls) {
139 if ((count($ls)==1) && ($ls[0]=="-l")) {
143 $rx = preg_glob($ls);
144 $all = ewiki_db::GETALL(array("id"));
145 while ($row = $all->get()) {
146 if ($rx && !preg_match($rx, $row["id"])) {
149 echo $row["id"] . "\n";
155 function cmd_ll($ls) {
156 echo "ll: not implemented";
161 function cmd_mv($ls, $bad=1) {
164 function cmd_ren($ls, $overwrite=0) {
165 if (count($ls) != 2) {
166 echo "mv: needs exactly two arguments\n";
171 echo htmlentities("$old -> $new ");
172 if (bad_old_new($old, $new)) {
175 while ($data = get_almost_latest($data, $old)) {
179 if (ewiki_db::WRITE($data, $overwrite)) {
180 if (ewiki_db::DELETE($old, $data["version"])) {
184 echo "<sup>(DE#$data[version])</sup>";
189 echo "<sup>(WE#$data[version])</sup>";
196 echo "- $we write errors (target page existed)<br>\n";
199 echo "- $de errors deleting source page versions (uh, bad!)<br>\n";
206 function cmd_cp($ls) {
207 if (count($ls) != 2) {
208 echo "cp: needs exactly two arguments\n";
213 echo htmlentities("$old => $new ");
214 if (bad_old_new($old, $new, "TARGET_EXIST_WARN")) {
217 while ($data = get_almost_latest($data, $old)) {
219 if (ewiki_db::GET($new, $data["version"])) {
220 echo "<sup>(OW#$data[version])</sup>";
223 if (ewiki_db::WRITE($data)) {
227 echo "<sup>(WE#$data[version])</sup>";
233 echo "- $ow target page versions overwritten<br>\n";
236 echo "- $we write errors (uh, bad!)<br>\n";
242 #-- updated {lastmodified} stamps
243 function cmd_touch($ls) {
244 foreach ($ls as $id) {
245 if ($data = ewiki_db::GET($id)) {
246 echo "$data[id]<br>\n";
247 $data["lastmodified"] = time();
248 ewiki_db::WRITE($data, $overwrite=1);
251 echo "'$id' does not exist<br>\n";
260 #-- helper calls ---------------------------------------------------------
263 function split_input($s) {
267 while (strlen($s = ltrim($s))) {
269 if (preg_match("/^\"([^\"]+|\\\")\"|^'([^']+|\\')'|^([^\s]+)/", $s, $uu)) {
270 if (($e = $uu[1]) || ($e = $uu[2])) {
271 $r[] = stripslashes($uu[1]);
281 $s = substr($s, strlen($uu[0]));
288 function preg_glob($ls=NULL) {
289 foreach ((array)$ls as $glob) {
290 $rx[] = str_replace("\\*", ".*", preg_quote($glob));
293 return( "/^(" . implode("|", $rx) . ")$/" );
298 #-- fetch version after version (decreasing),
299 # skip over remaining ones (when deleting fails)
300 function get_almost_latest($data, $id) {
302 $data = ewiki_db::GET($id);
305 $last_ver = $data["version"];
307 while ((--$last_ver) > 0) {
308 if ($data = ewiki_db::GET($id, $last_ver)) {
318 function bad_old_new($old, $new, $tt=0) {
319 if (! ewiki_db::GET($old)) {
320 echo "<br>src page not found!\n";
324 echo "<br>hmm, no - won't do that (same page name)\n";
327 if ($tt && ewiki_db::GET($new)) {
328 echo "<br><b>warning</b>: target page exists, and latest versions may not get overwritten by this copy process\n<br>\n";