changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / lib / pluginmetadata.php
1 <?php
2 /*
3    Utility code for handling plugin .meta data files (pmd) and their
4    dependencies.
5 */
6
7
8 #-- read a single .meta file
9 function ewiki_pmd_read($fn) {
10    $info = array();
11    if ($src = file_get_contents($fn)) {
12       preg_match_all("/^(\w+):[ \t]*(([^\n]*(\n )?)+)/m", $src, $uu);
13       foreach ($uu[1] as $i=>$tmp) {
14          $info[$uu[1][$i]] = $uu[2][$i];
15       }
16    }
17    return($info);
18 }
19
20
21 #-- scan all .meta files or read from cache file
22 function ewiki_pmd($read_cached=0, $else_write=1, $warn=0, $gauge_callback="") {
23    global $ewiki_pmd;
24    $basedir = dirname(dirname(__FILE__));
25    $metaf = "$basedir/meta.bin";
26
27    #-- cached data
28    if ($read_cached && filesize($metaf)) {
29       $ewiki_pmd = unserialize((file_get_contents($metaf)));
30    }
31    #-- load from .meta files
32    else {
33       $ewiki_pmd = array();
34       $files = glob("$basedir/*.meta") + glob("$basedir/*/*.meta");
35       foreach ($files as $num=>$fn) {
36          if ($gauge_callback) {
37             $gauge_callback($num, count($files));
38          }
39          $id = basename($fn, ".meta");
40
41          #-- read
42          $add = ewiki_pmd_read($fn);
43          if ($add) {
44          
45             #-- .php script
46             $php = substr($fn, 0, -5) . ".php";
47             if (file_exists($php) && ($l = strpos($php, "/plugins/"))) {
48                $php = substr($php, 1 + $l);
49                $add["fn"] = $php;
50             }
51             
52             #-- plugin id:
53             if ($add["id"]) {
54                $id = "$add[id]";
55             }
56             
57             #-- provides:
58             foreach(explode(",", "$id,$add[provides],$add[delivers]") as $p) {
59                if ($p = trim($p)) {
60                   $ewiki_pmd[".provides"][$p][] = $id;  // only the first gets used
61                }
62             }
63
64             #-- append to list 
65             if ($warn && isset($ewiki_pmd[$id])) {
66                echo "WARNING: a plugin with the name '$id' is already registered\n";
67             }
68             $ewiki_pmd[$id] = $add;
69          }
70       }
71
72       #-- save
73       if ($else_write && is_writeable($metaf)) {
74          file_put_contents($metaf, (serialize($ewiki_pmd)));
75       }
76    }
77    return($ewiki_pmd);  //redundant
78 }
79
80
81 #-- apply dependencies on plugin list
82 function ewiki_pmd_resolve_dependencies(&$list, $add_suggested=1) {
83    global $ewiki_pmd;
84    $error = array();
85    
86    #-- how to handle fields
87    $fields = array(
88        "depends" => +1,
89        "conflicts" => -1,
90        "replaces" => -1,
91        "recommends" => $add_suggested,
92    );
93
94    #-- check all entries in name list
95    foreach ($list as $list_i_id=>$id) {
96       #-- fields
97       foreach ($fields as $name=>$action) {
98          if ($action && !empty($ewiki_pmd[$id][$name]))
99          foreach (explode(",",$ewiki_pmd[$id][$name]) as $dep)
100          {  // multiple dependencies may be given
101             $dep = trim($dep);
102
103             #-- add
104             if ($action >= +1) {
105                if (!in_array($dep, $list)) {
106                   if (!isset($ewiki_pmd[".provides"][$dep])) {
107                      // ooops
108                      unset($list[$list_i_id]);
109                      $error[$id] = "unmet dependency: $dep";
110                   }
111                   else {
112                      // always adds only the very first entry
113                      $list[] = $ewiki_pmd[".provides"][$dep][0];
114                   }
115                }
116             }
117             #-- remove
118             elseif ($action <= -1) {
119                foreach ($ewiki_pmd[".provides"][$dep] as $dep) {
120                   if (in_array($dep, $list)) {
121                      unset($list[array_search($dep, $list)]);
122                   }
123                }
124             }
125          }
126       }
127
128       #-- does such a plugin exist at all?
129       if (!$ewiki_pmd[$id]) {
130          $error[$id] = "plugin '$id' does not exist";
131       }
132    }
133
134    return($error);  //ouch, if you forget that this isn't a result
135 }
136
137
138 #-- 
139 function ewiki_pmd_get_config_settings($list) {
140 }
141
142
143 #-- get include_once() filenames
144 function ewiki_pmd_get_plugin_files($list) {
145    global $ewiki_pmd;
146    $r = array();
147    $n = 2001;
148    foreach($list as $id) {
149       $fn = $ewiki_pmd[$id]["fn"];
150       $type = $ewiki_pmd[$id]["type"];
151       ($sort = $ewiki_pmd[$id]["sort"]) or ($sort = 0);
152       $n--;
153       if ($fn && ($type != "R") && !in_array($fn, $r)) {
154          $r["$sort.$n"] = $fn;
155       }
156    }
157    ksort($r);
158 #   $r = array_values($r);
159    return($r);
160 }
161
162
163 #-- plugin sorting
164 function ewiki_pmd_by_category() {
165    global $ewiki_pmd;
166    $r = array();
167    foreach ($ewiki_pmd as $id=>$row) {
168       if ($id[0] != ".") {
169          ($cat = $row["category"]) or ($cat = "else");
170          $r[$cat][$id] = $row;
171       }
172    }
173    return($r);
174 }
175
176
177 #-- flip array by entry
178 function ewiki_pmd_by($field) {
179    global $ewiki_pmd;
180    $r = array();
181    foreach ($ewiki_pmd as $id=>$row) {
182       if (isset($row[$field])) {
183          $r[$row[$field]] = $id;
184       }
185    }
186    return($r);
187 }
188
189
190 #-- unwanted plugins
191 function ewiki_pmd_hidden($row) {
192    return
193      !strlen($row["type"]) || ($row["type"] == "R")
194      || !strlen($row["title"])
195      || ($row["priority"] == "never")
196      || ($row["priority"] == "deprecated");
197 }
198
199
200
201 ?>