48aacc2aa7cc4ec5f8a36a0f1ffd0a84fb2921e0
[atutor.git] / mods / wiki / tools / mkxpi
1 #!/usr/local/bin/php -Cq
2 <?php
3
4 /*
5    this utility assembles a .xpi/.jpi file (installable ewiki plugin)
6
7    usage:
8       mkxpi xpi-source-file
9
10    The 'xpi-source-file' holds a rfc822-style list of settings and text
11    parts:
12     | id: ThePluginIdOrPageName          (REQ)
13     | type: page                         (REQ; "page" or "init")
14     | license: PublicDomain              (OPT)
15     | author: ...                        (OPT)
16     | version: ...                       (OPT)
17     | description: ...                   (OPT; notes/comments about plugin)
18     | code: ...                          (REQ; multiline field)
19
20    All entries, but especially 'description' and 'code' can be multiline
21    entries. The 'code' entry is followed by PHP code, with optional
22    surrounding <?php and ?> tags.
23    For "page" plugins it is simply the code (no function definition!) that
24    would normally be put into a page plugin func, but that you cannot use
25    return() and instead throw all output via the '$o' variable (workaround).
26
27    For "init" (any type: string will do) xpi plugins, you can add function
28    definitions and ewiki_plugin[] registry settings as usual. The execution
29    of that code is however not guaranteed to happen in global scope - most
30    of the ewiki_* vars are however accessible.
31
32    The special "jpi" type plugins are "page" plugins, but you must use
33    JavaScript code, so it can safely be executed in a sandbox (phpjs)
34    with the limited WikiApi functionality. Then however everybody can
35    install those .jpi plugins. (not yet ready)
36 */
37
38
39 function srcxpi2xpi($str) {
40
41    $xpi = array(
42       "XPI" => "0.1",
43       "engine" => "ewiki",
44    );
45
46    $uu = preg_split('/^(\w+):/m', $str, $uu, PREG_SPLIT_DELIM_CAPTURE);
47    for ($i=1; $i<count($uu); $i+=2) {
48
49       $id = strtolower(trim($uu[$i]));
50       $val = trim($uu[$i+1]);
51
52       if ($id && $val) {
53          $xpi[$id] = $val;
54       }
55    }
56    if (strpos($xpi["code"], '<?')===0) {
57       $xpi["code"] = trim(preg_replace('/<\?(php|js)+|<\?|\?>/', '', $xpi["code"]));
58    }
59
60    #-- .jpi
61    if ($xpi["type"] == "jpi") {
62       $xpi["JPI"] = $xpi["XPI"];
63    }
64
65    return($xpi);
66 }
67
68
69 if ($file = $_SERVER["argv"][1]) {
70    echo "reading '$file'...";
71    if ($src = implode("", file($file))) {
72       $r = srcxpi2xpi($src);
73       if ($fn = $r["id"]) {
74          echo " passed.\n";
75          $fn .= ($r["JPI"] ? ".jpi" : ".xpi");
76          echo "writing '\e[33m".$fn."\e[37m'...";
77          if ($f = gzopen($fn, "wb9")) {
78             gzwrite($f, serialize($r));
79             gzclose($f);
80             echo " \e[32mdone\e[37m.";
81          }
82          echo "\n";
83       }
84       else {
85          echo " \e[31minvalid sorce file\e[37m.\n";
86       }
87    }
88 }
89 else {
90    echo "syntax: mkxpi  xpi-src-file\n";
91 }
92
93 ?>