68a282835b7e3e082513ded6a065fdb95187af42
[atutor.git] / mods / wiki / tools / wiki2html
1 #!/usr/local/bin/php -Cq
2 <?php
3
4   #  please keep this script inside of the tools/ directory (one
5   #  subdirectory below the ewiki.php and config.php)
6
7
8   #-- make wiki links filesystem local
9   define("EWIKI_SCRIPT", "%s.htm");
10   define("EWIKI_SCRIPT_BINARY", "bin/%s");
11
12   #-- load ewiki library / open database
13   $PWD=getcwd();
14   chdir(dirname(__FILE__));
15   foreach (array("config.php", "ewiki.php", "t_config.php") as $inc) {
16     foreach (array('./', '../') as $dir) {
17       @include("$dir$inc");
18       if (function_exists("ewiki_database")) break 2;
19     }
20   }
21   chdir($PWD);
22   if (!function_exists("ewiki_database")) {
23      echo "You cannot move around this utility, it needs to be located nereby the\nother ewiki tools/ (or at least ewiki.php or some config.php)!\n";
24   }
25
26
27   #-- cmdline options
28   $config = regex_getopts(
29   array(
30      "help" => "/^-+(h|help)$/i",
31      "dirs" => "/^-+(d|dir.*|create.*)$/i",
32      "ext" => "/^-+(e|ext.*)$/i",
33      "keepref" => "/^-+(k|keep.*|ref.*)$/i",
34   ));
35
36   #-- main
37   if ($config["help"]) {
38      echo "\e[21mwiki2html\e[27m converts your ewiki database into .html body part files.
39 These files miss the <html> and <head> markup and only contain the rendered
40 wiki content.
41 If you rather want a static version of your site please choose 'wget' or a
42 similar utility instead.
43
44 --help   prints this help screen
45 --dirs   creates the bin/ and img/ subdirectories (auto)
46 --ext    extension for the created pages (.htm is default)
47 --keep   keep references to external (but cached) images (NYI)
48
49 ";
50   }
51   else {
52
53      #-- working vars
54      $dest = "wiki2html-".time();
55      mkdir($dest);
56      ($ext = $config["ext"]) || ($ext = "htm");
57      $ewiki_config["script"] = "%s." . trim($ext, ".");
58      $ewiki_config["script_binary"] = "bin/%s";
59
60      #-- page names
61      $result = ewiki_db::GETALL(array("flags"));
62
63      #-- loop
64      while ($row = $result->get()) {
65
66         $id = $row["id"];
67         $row = ewiki_db::GET($id);
68
69         #-- pages
70         if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_TEXT) {
71             
72            $html = ewiki_format($row["content"]);
73
74            $f = fopen("$dest/" . ewiki_script("", $id), "wb");
75            fwrite($f, $html); fclose($f);
76         }
77
78         #-- images
79         elseif (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_BINARY) {
80
81            if (!file_exists("$dest/bin")) {
82               mkdir("$dest/bin");
83            }
84
85            $f = fopen("$dest/bin/" . ewiki_script("", $id), "wb");
86            fwrite($f, $row["content"]); fclose($f);
87         }
88
89      }
90   }
91
92
93   #------------------------------------------------------------------------
94
95   function regex_getopts($regexopts) {
96      if (empty($_SERVER)) {
97         $_SERVER = $GLOBALS["HTTP_SERVER_VARS"];
98      }
99      if (!empty($GLOBALS["argc"])) {
100         $_SERVER["argc"] = $GLOBALS["argc"];
101         $_SERVER["argv"] = $GLOBALS["argv"];
102      }
103      $opts = array();
104      for ($n = 1; $n < $_SERVER["argc"]; $n++) {
105         foreach ($regexopts as $opts_id => $optsregex) {
106            if (preg_match($optsregex, $_SERVER["argv"][$n])) {
107               $value = 1;
108               if (($next = @$_SERVER['argv'][$n+1]) && ($next[0] != "-")) {
109                  $value = $next;
110                  $n++;
111               }
112               $opts[$opts_id] = $value;
113               continue 2;
114            }
115         }
116         $opts[] = $_SERVER["argv"][$n];
117      }
118      return($opts);
119   }
120   #-------------------------------------------------------------------------
121   
122
123 ?>