changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / lib / wikiapi.php
1 <?php
2
3 /*
4    this provides the limited WikiApi used by the XML+RPC interfaces and
5    also the WikiScript interpreter (sandboxed server-side JavaScript)
6 */
7
8 $wikiapi = (array)@$wikiapi + array(
9    "wiki.supportedVersion"      => "wikiapi_supportedVersion",
10    "wiki.getPage"               => "wikiapi_getPage",
11    "wiki.getPageVersion"        => "wikiapi_getPage",
12    "wiki.getPageHtml"           => "wikiapi_getPageHtml",
13    "wiki.getPageHtmlVersion"    => "wikiapi_getPageHtml",
14    "wiki.getPageInfo"           => "wikiapi_getPageInfo",
15    "wiki.getPageInfoVersion"    => "wikiapi_getPageInfo",
16    "wiki.getAllPages"           => "wikiapi_getAllPages",
17    "wiki.getRecentChanges"      => "wikiapi_getRecentChanges",
18    "wiki.listLinks"             => "wikiapi_listLinks",
19    "wiki.putPage"               => "wikiapi_putPage",
20    "ewiki.getBackLinks"         => "ewiki_get_backlinks",
21    "ewiki.getLinks"             => "ewiki_get_links",
22 // "ewiki.linkDatabase"         => "wikiapi_linkdatabase",
23 // "ewiki.searchPages"          => "wikiapi_search",
24 // "ewiki.titleSearch"          => "wikiapi_search_titles",
25    "ewiki.url"                  => "ewiki_script",
26    "ewiki.ahref"                => "ewiki_link",
27    "ewiki.getInterWikiUrl"      => "ewiki_interwiki",
28    "ewiki.log"                  => "ewiki_log",
29 );
30 $wikiapi_encode = 0; 
31
32
33 # API switch helper functions (XML+RPC needs encoded fields)
34 #
35 function wikiapi_b64encode($str) {
36    global $wikiapi_encode;
37    if ($wikiapi_encode) {
38       return(new xmlrpc_base64(base64_encode($str)));
39              // NOTE: possible Wiki XML-RPC bugs everywhere:
40              // this text snippet will be in L1 charset, as <base64>
41              // is for binary transportation (as opposed to text
42              // <string>s)
43    }
44    else return($str);
45 }
46
47 function wikiapi_time($t) {
48    global $wikiapi_encode;
49    if ($wikiapi_encode) {
50       return(new xmlrpc_datetime($t));
51    }
52    else return($str);
53 }
54
55
56 #---------------------------------------------------------------------------
57 # API implementation
58
59
60
61 // we're now version TWO - no more base64+urlencoding!!
62 function wikiapi_supportedVersion() {
63    return(2);
64 }
65
66
67 function wikiapi_getAllPages() {
68    return(wikiapi_getRecentChanges(NULL));
69 }
70
71
72 # helper function
73 #
74 function wikiapi_pageinfo(&$data) {
75    $author = strtok($data["author"], "(,|  ");
76    $res = array(
77       "name"            => $data["id"],
78       "lastModified"    => wikiapi_time($data["modified"]),
79       "author"          => $author,
80       "version"         => $data["version"],
81    );
82    return($res);
83 }
84
85 function wikiapi_getRecentChanges($since=UNIX_MILLENNIUM) {
86    $r = array();
87    $result = ewiki_db::GETALL(array("flags", "lastmodified"));
88    while ($row = $result->get(0, 0x0020)) { 
89       if (EWIKI_DB_F_TEXT == ($row["flags"] & EWIKI_DB_F_TYPE)) {
90          if (!isset($since)) {
91             $r[] = $row["id"];
92          }
93          elseif ($row["lastmodified"] >= $since) {
94             $r[] = wikiapi_pageinfo($data);
95          }
96       }
97    }
98    return($r);
99 }
100
101
102 function wikiapi_getPageInfo($id, $ver=NULL) {
103    $data = ewiki_db::GET($id, $ver);
104    if ($data) {
105       $res = wikiapi_pageinfo($data);
106       return($res);
107    }
108 }
109
110
111 function wikiapi_getPage($id, $ver=NULL) {
112    $data = ewiki_db::GET($id, $ver);
113    if ($data) {
114       return($data["content"]);  #wikiapi_b64encode()
115    }
116 }
117
118
119 function wikiapi_getPageHtml($id, $ver=NULL) {
120    $data = ewiki_db::GET($id, $ver);
121    if ($data) {
122       $render_args = array(
123          "scan_links" => 1,
124          "html" => (EWIKI_ALLOW_HTML||(@$data["flags"]&EWIKI_DB_F_HTML)),
125       );
126       $res = ewiki_format($data["content"], $render_args);
127       return($res);  #wikiapi_b64encode()
128    }
129 }
130
131
132 function wikiapi_listLinks($id, $ver=NULL) {
133    global $ewiki_config;
134    $iw = &$ewiki_config["interwiki"];
135
136    $data = ewiki_db::GET($id, $ver);
137    if ($data) {
138       $r = array();
139       $refs = explode("\n", trim($data["refs"]));
140       foreach ($refs as $link) {
141          #-- interwiki
142          if (($m = strtok($link, ":")) && ($p = strtok("\000"))) {
143             if ($url = ewiki_array($iw, $m)) {
144                while (($uu = ewiki_array($iw, $url)) !== NULL) {
145                   $url = $uu;
146                }
147                $link = $url . $p;
148             }
149          }
150          #-- absolute URLs
151          if (strpos($link, "://")) {
152             if (strpos($link, EWIKI_IDF_INTERNAL) === 0) {
153                $link = ewiki_script_binary("", $link);
154                # BAD NEWS: we can't get absolute URLs for
155                # internal:// image links
156             }
157             $r[] = array(
158                "page" => $link,
159                "type" => "external",
160                "href" => $href,
161             );
162          }
163          #-- internal WikiLinks
164          else {
165             $r[] = array(
166                "page" => $link,
167                "type" => "local",
168                "href" => ewiki_script("", $link),
169             );
170          }
171       }
172       return($r);
173    }
174 }
175
176
177 function wikiapi_putPage($id, $raw) {
178    if (EWIKI_PROTECTED_MODE) {
179       return(false);
180    }
181    return(false);
182 }
183
184
185 ?>