MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / lib / htmlpurifier / HTMLPurifier / ContentSets.php
blob7baf7a3101d1989cdc2aabad8254a61d1de7fa83
1 <?php
3 // common defs that we'll support by default
4 require_once 'HTMLPurifier/ChildDef.php';
5 require_once 'HTMLPurifier/ChildDef/Empty.php';
6 require_once 'HTMLPurifier/ChildDef/Required.php';
7 require_once 'HTMLPurifier/ChildDef/Optional.php';
8 require_once 'HTMLPurifier/ChildDef/Custom.php';
10 // NOT UNIT TESTED!!!
12 class HTMLPurifier_ContentSets
15 /**
16 * List of content set strings (pipe seperators) indexed by name.
17 * @public
19 var $info = array();
21 /**
22 * List of content set lookups (element => true) indexed by name.
23 * @note This is in HTMLPurifier_HTMLDefinition->info_content_sets
24 * @public
26 var $lookup = array();
28 /**
29 * Synchronized list of defined content sets (keys of info)
31 var $keys = array();
32 /**
33 * Synchronized list of defined content values (values of info)
35 var $values = array();
37 /**
38 * Merges in module's content sets, expands identifiers in the content
39 * sets and populates the keys, values and lookup member variables.
40 * @param $modules List of HTMLPurifier_HTMLModule
42 function HTMLPurifier_ContentSets($modules) {
43 if (!is_array($modules)) $modules = array($modules);
44 // populate content_sets based on module hints
45 // sorry, no way of overloading
46 foreach ($modules as $module_i => $module) {
47 foreach ($module->content_sets as $key => $value) {
48 if (isset($this->info[$key])) {
49 // add it into the existing content set
50 $this->info[$key] = $this->info[$key] . ' | ' . $value;
51 } else {
52 $this->info[$key] = $value;
56 // perform content_set expansions
57 $this->keys = array_keys($this->info);
58 foreach ($this->info as $i => $set) {
59 // only performed once, so infinite recursion is not
60 // a problem
61 $this->info[$i] =
62 str_replace(
63 $this->keys,
64 // must be recalculated each time due to
65 // changing substitutions
66 array_values($this->info),
67 $set);
69 $this->values = array_values($this->info);
71 // generate lookup tables
72 foreach ($this->info as $name => $set) {
73 $this->lookup[$name] = $this->convertToLookup($set);
77 /**
78 * Accepts a definition; generates and assigns a ChildDef for it
79 * @param $def HTMLPurifier_ElementDef reference
80 * @param $module Module that defined the ElementDef
82 function generateChildDef(&$def, $module) {
83 if (!empty($def->child)) return; // already done!
84 $content_model = $def->content_model;
85 if (is_string($content_model)) {
86 $def->content_model = str_replace(
87 $this->keys, $this->values, $content_model);
89 $def->child = $this->getChildDef($def, $module);
92 /**
93 * Instantiates a ChildDef based on content_model and content_model_type
94 * member variables in HTMLPurifier_ElementDef
95 * @note This will also defer to modules for custom HTMLPurifier_ChildDef
96 * subclasses that need content set expansion
97 * @param $def HTMLPurifier_ElementDef to have ChildDef extracted
98 * @return HTMLPurifier_ChildDef corresponding to ElementDef
100 function getChildDef($def, $module) {
101 $value = $def->content_model;
102 if (is_object($value)) {
103 trigger_error(
104 'Literal object child definitions should be stored in '.
105 'ElementDef->child not ElementDef->content_model',
106 E_USER_NOTICE
108 return $value;
110 switch ($def->content_model_type) {
111 case 'required':
112 return new HTMLPurifier_ChildDef_Required($value);
113 case 'optional':
114 return new HTMLPurifier_ChildDef_Optional($value);
115 case 'empty':
116 return new HTMLPurifier_ChildDef_Empty();
117 case 'custom':
118 return new HTMLPurifier_ChildDef_Custom($value);
120 // defer to its module
121 $return = false;
122 if ($module->defines_child_def) { // save a func call
123 $return = $module->getChildDef($def);
125 if ($return !== false) return $return;
126 // error-out
127 trigger_error(
128 'Could not determine which ChildDef class to instantiate',
129 E_USER_ERROR
131 return false;
135 * Converts a string list of elements separated by pipes into
136 * a lookup array.
137 * @param $string List of elements
138 * @return Lookup array of elements
140 function convertToLookup($string) {
141 $array = explode('|', str_replace(' ', '', $string));
142 $ret = array();
143 foreach ($array as $i => $k) {
144 $ret[$k] = true;
146 return $ret;