6df3e73d830e54e91ee857b70d4c4c976469bf08
[atutor.git] / mods / wiki / plugins / mpi / mpi.php
1 <?php
2
3 /*
4    The so called "mpi" plugins (== markup plugin interface), can be
5    invoked from inside WikiPages using following syntax:
6
7       <?plugin PluginName option1=attribute ... ?>
8       <?plugin-link ToThisPlugin ?>
9
10    The plugins (mpi_*.php) are loaded on demand from the plugins/mpi/
11    directory. This behaviour can however be disabled by defining the
12    _MPI_DEMANDLOAD constant to 0 - but then you had to load the wanted
13    mpi plugins together with include("plugins/mpi/mpi.php") yourself
14    and beforehand (from config.php or so).
15 */
16
17 define("EWIKI_MPI_DEMANDLOAD", 1);
18 define("EWIKI_MPI_AUTOLOAD_DIR", dirname(__FILE__));
19
20 define("EWIKI_MPI_FILE_PREFIX", "mpi_");    # better do not change
21 define("EWIKI_MPI_FILE_POSTFIX", ".php");
22 define("EWIKI_MPI_MARKUP_REGEX", "/&lt;\\??(plugin:|e?wiki:|mpi:|plugin(?:-link|-form|-doc|-input|)\s)\s*(.*?)\\??&gt;/i");
23
24
25
26 #-- register at ewiki pluginterface
27 //$ewiki_plugins["format_source"][] = "ewiki_mpi_fixsyntax";
28 $ewiki_plugins["action"]["mpi"] = "ewiki_mpi_action";
29 $ewiki_plugins["format_block"]["mpi"][] = "ewiki_mpi_call";
30 $ewiki_config["format_block"]["mpi"] = array("&lt;?plugin", "?&gt;", false, 0x0020);
31
32
33
34
35 #-- changes old plugin call syntax to new one (finally disabled with R1.02b)
36 function ewiki_mpi_fixsyntax(&$src) {
37    $src = preg_replace('/&lt;plugin(.+?)\?&gt;/s', '&lt;?plugin\\1?&gt;', $src);
38    global $ewiki_config;
39    $uu = $ewiki_config["format_block"]["mpi"];   // give all other
40    unset($ewiki_config["format_block"]["mpi"]);  // block plugins
41    $ewiki_config["format_block"]["mpi"] = $uu;   // precedence
42 }
43
44
45
46 #-- called from inside ewiki_format() engine
47 function ewiki_mpi_call(&$str, &$in, &$iii, &$s) {
48
49    global $ewiki_plugins;
50
51    #-- split out $mpi-action
52    $str = trim($str);
53    if ($str[0] == "-") {
54       $mpi_action = substr(strtolower(strtok($str, " :\n\t\f\r")), 1);
55       $str = ltrim(strtok("\000"));
56    }
57    switch ($mpi_action) {
58       case "doc":
59       case "desc":
60       case "link":
61          break;
62       default:
63          $mpi_action = "html";
64    }    
65
66    #-- split mpi plugin name from arguments
67    $mpi_name = trim(strtok($str, " \n\t\f\r"));
68    $str = strtok("\000");
69
70    #-- split args
71    $mpi_args = array();
72    $mpi_args["_"] = $str;
73    ewiki_stripentities($mpi_args["_"]);
74    if (preg_match_all('/(\w+)="(.+)(?<![\\\\])"|(\w+)=([^\s]+)|([^\s"=]+)/', $mpi_args["_"], $uu)) {
75       $pos = 0;
76       foreach ($uu[5] as $i=>$d) {
77          if (strlen($d)) {
78             $mpi_args[$pos] = $d;
79             $pos++;
80          }
81          elseif ($uu[$f=1][$i] || $uu[$f=3][$i]) {
82             $mpi_args[$uu[$f][$i]] = stripslashes($uu[$f+1][$i]);
83          }
84       }
85       #-- std arg
86       isset($mpi_args["id"])
87       or ($mpi_args["id"] = $mpi_args[0])
88       or ($mpi_args["id"] = $mpi_args["page"]);
89    }
90
91    #-- plugin-link
92    if ($action == "link") {
93       $str = '<a href="' . ewiki_script("mpi", $name, $args) . '">' . $name . '</a>';
94       $iii[$in][1] = 0x0010|0x0020;  # InlineBlock+ScanForWikiWords
95    }
96    else {
97       $str = ewiki_mpi_exec($mpi_action, $mpi_name, $mpi_args, $iii, $s);
98    }
99 }
100  
101
102
103 function ewiki_mpi_exec($action, $rname, &$args, &$iii, &$s) {
104
105    global $ewiki_plugins, $ewiki_t, $ewiki_config;
106
107    #-- select plugin function
108    $name = strtolower($rname);
109    $pf = $ewiki_plugins["mpi"][$name];
110
111    #-- load plugin
112    if (!function_exists($pf) && EWIKI_MPI_DEMANDLOAD) {
113
114       $mpi_file = EWIKI_MPI_AUTOLOAD_DIR . "/" . EWIKI_MPI_FILE_PREFIX
115                 . $name . EWIKI_MPI_FILE_POSTFIX;
116       @include_once($mpi_file);
117
118       $pf = $ewiki_plugins["mpi"][$name];
119    }
120
121    #-- execute plugin
122    if (function_exists($pf)) {
123       return($pf($action, $args, $iii, $s));
124    }
125    else {
126       return("<!-- referenced mpi '$rname' not available -->");
127    }
128 }
129
130
131
132
133 function ewiki_mpi_action($id, $data, $action) {
134    global $ewiki_plugins;
135    return(ewiki_mpi_exec("html", $id, $_REQUEST, $uu, $uu));
136 }
137
138
139
140 ?>