changed git call from https to git readonly
[atutor.git] / mods / wiki / tools / mkpageplugin
1 #!/usr/local/bin/php -qC
2 <?php
3
4 #  This cmdline tool makes a "page_" plugin from a "StaticPage" (usually
5 #  found in the spages/ subdir). This is useful if you want to merge such
6 #  a (dynamic) "StaticPage" into a "monsterwiki.php" script (using the
7 #  'mkhuge' tool or the cat/type command).
8
9
10 if ($_SERVER["argc"] < 2) {
11    echo "\nsyntax:  mkpageplugin spages/PageOne spages/DynamicPage.php\n\n"
12       . "Makes ewiki page plugins from the 'StaticPage' files usually found in spages/,\n"
13       . "what is useful for merging those generated plugin scripts into a monsterwiki\n"
14       . "file as described in the README.\n\n";
15 }
16 else {
17    $files = $_SERVER["argv"];
18    array_shift($files);
19
20    foreach ($files as $file) {
21
22       #-- read contents
23       $c = implode("", file($file));
24
25       #-- check type (.htm / .txt / .php)
26       $ext = strtolower(substr($file, strrpos($file, ".")));
27       switch(substr($ext, 1, 3)) {
28          case "htm":
29             $type = 1;
30             if (strpos($c, "<?") !== false) {
31                $type = 2;
32             }
33             break;
34             
35          case "php":
36             $type = 2;
37             break;
38
39          case "txt":
40          default:
41             $text = 0;
42       }
43
44       #-- generate pagename from filename
45       ($id = substr($file, 0, strrpos($file, ".")))
46       or ($id = $file);
47       $id = strtr($id, DIRECTORY_SEPARATOR, "/");
48       if (($r = strrpos($id, "/")) !== false) {
49          $id = substr($id, $r + 1);
50       }
51      
52
53       #-- generate output script
54       $func2 = "ewiki_mkpageplugin_".$id;
55       $cn2 = "<"."?php\n\n"
56            . "# This ewiki page plugin was generated from the StaticPage file\n"
57            . "# '$file' using tools/mkpageplugin.\n\n\n"
58            . '$ewiki_plugins["page"]["'.$id.'"] = "'.$func2.'";'."\n\n\n"
59            . "function $func2(\$id, &\$data, \$action) {\n\n";
60       if ($type==0) {
61          $cn2 .= "   return ewiki_format(<<<END_OF_WIKIPAGE\n"
62                . $c
63                . "\nEND_OF_WIKIPAGE\n   );";
64       }
65       elseif ($type==1) {
66          $cn2 .= "   return<<<END_OF_HTML\n"
67                . $c
68                . "\nEND_OF_HTML;";
69       }
70       else {
71          $c = preg_replace("/\bheader\((.+?)\);/ims", '$ewiki_headers[] = \\1;', $c);
72          $cn2 .= '   global $ewiki_plugins, $ewiki_id, $ewiki_title, $ewiki_author, $ewiki_ring, $ewiki_t, $ewiki_config, $ewiki_action, $_EWIKI, $ewiki_auth_user;'
73                . "\n\n"
74                . "   \$ewiki_headers = array();\n"
75                . "   \$headers = &\$ewiki_headers;\n\n";
76                . "   ob_start();\n"
77                . "   ob_implicit_flush(0);\n\n"
78                . "?".">"
79                . $c
80                . "<"."?php\n\n"
81                . "   \$o = ob_get_contents();\n"
82                . "   ob_end_clean();\n\n"
83                . "   headers(implode(\"\n\", \$ewiki_headers));\n"
84                . "   return(\$o);";
85       }
86       $cn2.= "\n\n}\n\n?".">";
87
88       #-- output file name
89       $fn2 = "page_{$id}.php";
90       if (($r = strrpos($file, "/")) !== false) {
91          $fn2 = substr($file, 0, $r + 1) . $fn2;
92       }
93
94       #-- write dest plugin
95       echo "converting $file to $fn2 plugin...\n";
96       if ($f = fopen($fn2, "w")) {
97          fwrite($f, $cn2);
98          fclose($f);
99       }
100       else {
101          echo "ERROR: cannot write to '$fn2'\n";
102       }
103    }
104 }
105
106
107 ?>