changed git call from https to git readonly
[atutor.git] / mods / wiki / tools / t_textinsert.php
1 <?php
2   include("t_config.php");
3 ?>
4 <html>
5 <head>
6  <title>insert plain text files as pages into ewiki db</title>
7  <link rel="stylesheet" type="text/css" href="t_config.css">
8 </head>
9 <body bgcolor="#ffffff" text="#000000">
10 <h1>text file insertion util</h1>
11
12 <?php
13
14
15   if (empty($_REQUEST["from"])) {
16
17      ?>
18
19  Use this tool to (re)insert text files as pages into the database (like the
20  automatic initialization if ewiki.php, when it is run for the very first
21  time).  Before it overwrites anything you must first select the files to be
22  inserted from the list of found files, in the subdirectory you can select
23  now: <br><br>
24
25  <FORM ACTION="t_textinsert.php" METHOD="GET">
26  subdir with page files: <INPUT NAME="from" VALUE="./init-pages">
27  <br><br>
28  <INPUT type="submit" VALUE="&nbsp;  list files  &nbsp;">
29  </FORM>
30
31      <?php
32
33   }
34   elseif (empty($_REQUEST["insert"])) {
35
36      if (strstr(substr($from, 3), "/")) die("unallowed subdir name");
37
38      $files = array();
39      if ($dh = opendir($from = $_REQUEST["from"] . "/")) {
40
41         while ($fn = readdir($dh)) {
42
43            if (is_file($from . $fn)) {
44               $files[]  = $fn;
45            }
46
47         }
48         closedir($dh);
49      }
50
51      echo '<FORM ACTION="t_textinsert.php" METHOD="GET">' .
52           '<INPUT NAME="from" TYPE="hidden" VALUE="'.rtrim($from, "/").'">';
53
54      echo '<TABLE BORDER="0" CELLSPACING="3" CELLPADDING="2">';
55
56      foreach ($files as $fn) {
57
58         $sel = 0;
59         $reason = "";
60
61         $data = ewiki_db::GET($fn);
62
63         if (empty($data)) {
64            $sel = 1;
65            $reason .= "<b>not yet in database</b><br>\n";
66         }
67         elseif (!strlen(trim($data["content"]))) {
68            $sel = 1;
69            $reason .= "<b>database entry empty</b><br>\n";
70         }
71         elseif ($data["lastmodified"] < filemtime($from.$fn)) {
72            $sel = 1;
73            $reason .= "<b>database entry is older</b><br>\n";
74         }
75         elseif (strlen($data["content"]) < filesize($from.$fn)) {
76            $sel = 1;
77            $reason .= "<b>database entry is shorter</b><br>\n";
78         }
79
80         if (strlen($data["content"]) > filesize($from.$fn)) {
81            $sel = 0;
82            $reason .= "database entry is <i>longer</i>!<br>\n";
83         }
84
85         if ($data["lastmodified"] >= filemtime($from.$fn)) {
86            $sel = 0;
87            $reason .= "database entry is <i>newer</i>!<br>\n";
88         }
89
90         if (!filesize($from . $fn)) {
91            $sel = 0;
92            $reason .= "empty file<br>\n";
93         }
94
95         echo '<TR><TD BGCOLOR="#9090B0">';
96         echo '<INPUT NAME="file[' . $fn . ']" TYPE="checkbox" VALUE="1" ' . ($sel ? " CHECKED" : "") . '>';
97         echo " " . $fn;
98         echo "</TD>\n" . '<TD BGCOLOR="#9090B0">';
99         echo $reason . "</TD></TR>\n";
100
101      }
102      
103      echo '</TABLE>' .
104           '<INPUT TYPE="submit" NAME="insert" VALUE="&nbsp;  insert files  &nbsp;">' .
105           '</FORM>';     
106
107   }
108   else {
109
110      $from = $_REQUEST["from"];
111      $files = $_REQUEST["file"];
112      if (strstr(substr($from, 3), "/")) die("unallowed subdir name");
113      $from .= "/";
114
115      foreach ($files as $fn => $uu) {
116
117         if (strstr($fn, "/") ||strstr($fn, ".") || (!$uu)) {
118            echo "filename '$fn' not allowed (NOTE: no versioned pages!)...<br>\n";
119            continue;
120         }
121
122         $ctime = filectime($from . $fn);
123         $content = implode("", file($from . $fn));
124
125         $prev = ewiki_db::GET($fn);
126
127         $data = array(
128            "id" => $fn,
129            "version" => 1+ @$prev["version"],
130            "author" => ewiki_author("ewiki_backdown"),
131            "flags" => EWIKI_DB_F_TEXT | @$prev["flags"],
132            "content" => $content,
133            "created" => $ctime,
134            "lastmodified" => time(),
135            "refs" => "\n",
136            "meta" => "",
137            "hits" => 0+ @$prev["hits"],
138         );
139
140         $r = (ewiki_db::WRITE($data) ? "ok" : "error");
141
142         echo "writing '$fn'... [$r]<br>\n";
143      }
144
145   }
146
147 ?>
148 </body>
149 </html>