Index: trunk/extensions/SemanticForms/includes/SF_FormClasses.php |
— | — | @@ -72,84 +72,136 @@ |
73 | 73 | var $max_allowed; |
74 | 74 | var $fields; |
75 | 75 | |
| 76 | + /** |
| 77 | + * For a field name and its attached property name located in the |
| 78 | + * template text, add the two to the $templateFields array, and |
| 79 | + * then 'x' them out in the template text to prevent them from being |
| 80 | + * parsed twice. |
| 81 | + */ |
| 82 | + function handlePropertySettingInTemplate( $fieldName, $propertyName, $isList, &$templateFields, $fullFieldText, &$templateText ) { |
| 83 | + global $wgContLang; |
| 84 | + $templateField = SFTemplateField::create( $fieldName, $wgContLang->ucfirst( $fieldName ) ); |
| 85 | + $templateField->setSemanticProperty( $propertyName ); |
| 86 | + $templateField->is_list = $isList; |
| 87 | + $cur_pos = stripos( $templateText, $fieldName ); |
| 88 | + $templateFields[$cur_pos] = $templateField; |
| 89 | + $replacement = str_repeat( "x", strlen( $fullFieldText ) ); |
| 90 | + $templateText = str_replace( $fullFieldText, $replacement, $templateText ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get the fields of the template, along with the semantic property |
| 95 | + * attached to each one (if any), by parsing the text of the template. |
| 96 | + */ |
76 | 97 | function getAllFields() { |
77 | 98 | global $wgContLang; |
78 | | - $template_fields = array(); |
79 | | - $field_names_array = array(); |
| 99 | + $templateFields = array(); |
| 100 | + $fieldNamesArray = array(); |
80 | 101 | |
81 | | - // Get the fields of the template, both semantic and otherwise, by parsing |
82 | | - // the text of the template. |
83 | | - // The way this works is that fields are found and then stored in an |
84 | | - // array based on their location in the template text, so that they |
85 | | - // can be returned in the order in which they appear in the template, even |
86 | | - // though they were found in a different order. |
87 | | - // Some fields can be found more than once (especially if they're part |
88 | | - // of an "#if" statement), so they're only recorded the first time they're |
89 | | - // found. Also, every field gets replaced with a string of x's after |
90 | | - // being found, so it doesn't interfere with future parsing. |
| 102 | + // The way this works is that fields are found and then stored |
| 103 | + // in an array based on their location in the template text, so |
| 104 | + // that they can be returned in the order in which they appear |
| 105 | + // in the template, even though they were found in a different |
| 106 | + // order. |
| 107 | + // Some fields can be found more than once (especially if |
| 108 | + // they're part of an "#if" statement), so they're only |
| 109 | + // recorded the first time they're found. Also, every field |
| 110 | + // gets replaced with a string of x's after being found, so it |
| 111 | + // doesn't interfere with future parsing. |
91 | 112 | $template_title = Title::makeTitleSafe( NS_TEMPLATE, $this->template_name ); |
92 | 113 | $template_article = null; |
93 | 114 | if ( isset( $template_title ) ) $template_article = new Article( $template_title ); |
94 | 115 | if ( isset( $template_article ) ) { |
95 | | - $template_text = $template_article->getContent(); |
96 | | - // ignore 'noinclude' sections and 'includeonly' tags |
97 | | - $template_text = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $template_text ); |
98 | | - $template_text = strtr( $template_text, array( '<includeonly>' => '', '</includeonly>' => '' ) ); |
| 116 | + $templateText = $template_article->getContent(); |
| 117 | + // Ignore 'noinclude' sections and 'includeonly' tags. |
| 118 | + $templateText = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $templateText ); |
| 119 | + $templateText = strtr( $templateText, array( '<includeonly>' => '', '</includeonly>' => '' ) ); |
99 | 120 | |
100 | | - // first, look for "arraymap" parser function calls that map a |
101 | | - // property onto a list |
102 | | - if ( preg_match_all( '/{{#arraymap:{{{([^|}]*:?[^|}]*)[^\[]*\[\[([^:=]*:?[^:=]*)(:[:=])/mis', $template_text, $matches ) ) { |
103 | | - // this is a two-dimensional array; we need the last three of the four |
104 | | - // sub-arrays; we also have to remove redundant values |
| 121 | + // First, look for "arraymap" parser function calls |
| 122 | + // that map a property onto a list. |
| 123 | + if ( preg_match_all( '/{{#arraymap:{{{([^|}]*:?[^|}]*)[^\[]*\[\[([^:=]*:?[^:=]*)(:[:=])/mis', $templateText, $matches ) ) { |
| 124 | + // This is a two-dimensional array; we need |
| 125 | + // the last three of the four sub-arrays; we |
| 126 | + // also have to remove redundant values. |
105 | 127 | foreach ( $matches[1] as $i => $field_name ) { |
106 | | - $semantic_property = $matches[2][$i]; |
107 | | - $full_field_text = $matches[0][$i]; |
108 | | - if ( ! in_array( $field_name, $field_names_array ) ) { |
109 | | - $template_field = SFTemplateField::create( $field_name, $wgContLang->ucfirst( $field_name ) ); |
110 | | - $template_field->setSemanticProperty( $semantic_property ); |
111 | | - $template_field->is_list = true; |
112 | | - $cur_pos = stripos( $template_text, $full_field_text ); |
113 | | - $template_fields[$cur_pos] = $template_field; |
114 | | - $field_names_array[] = $field_name; |
115 | | - $replacement = str_repeat( "x", strlen( $full_field_text ) ); |
116 | | - $template_text = str_replace( $full_field_text, $replacement, $template_text ); |
| 128 | + if ( ! in_array( $field_name, $fieldNamesArray ) ) { |
| 129 | + $propertyName = $matches[2][$i]; |
| 130 | + $full_field_text = $matches[0][$i]; |
| 131 | + self::handlePropertySettingInTemplate( $field_name, $propertyName, true, &$templateFields, $full_field_text, &$templateText ); |
| 132 | + $fieldNamesArray[] = $field_name; |
117 | 133 | } |
118 | 134 | } |
119 | 135 | } |
120 | 136 | |
121 | | - // second, look for normal property calls |
122 | | - if ( preg_match_all( '/\[\[([^:=]*:*?[^:=]*)(:[:=]){{{([^\]\|}]*).*?\]\]/mis', $template_text, $matches ) ) { |
123 | | - // this is a two-dimensional array; we need the last three of the four |
124 | | - // sub-arrays; we also have to remove redundant values |
125 | | - foreach ( $matches[1] as $i => $semantic_property ) { |
| 137 | + // Second, look for normal property calls. |
| 138 | + if ( preg_match_all( '/\[\[([^:=]*:*?[^:=]*)(:[:=]){{{([^\]\|}]*).*?\]\]/mis', $templateText, $matches ) ) { |
| 139 | + // This is a two-dimensional array; we need |
| 140 | + // the last three of the four sub-arrays; we |
| 141 | + // also have to remove redundant values. |
| 142 | + foreach ( $matches[1] as $i => $propertyName ) { |
126 | 143 | $field_name = $matches[3][$i]; |
127 | | - $full_field_text = $matches[0][$i]; |
128 | | - if ( ! in_array( $field_name, $field_names_array ) ) { |
129 | | - $template_field = SFTemplateField::create( $field_name, $wgContLang->ucfirst( $field_name ) ); |
130 | | - $template_field->setSemanticProperty( $semantic_property ); |
131 | | - $cur_pos = stripos( $template_text, $full_field_text ); |
132 | | - $template_fields[$cur_pos] = $template_field; |
133 | | - $field_names_array[] = $field_name; |
134 | | - $replacement = str_repeat( "x", strlen( $full_field_text ) ); |
135 | | - $template_text = str_replace( $full_field_text, $replacement, $template_text ); |
| 144 | + if ( ! in_array( $field_name, $fieldNamesArray ) ) { |
| 145 | + $full_field_text = $matches[0][$i]; |
| 146 | + self::handlePropertySettingInTemplate( $field_name, $propertyName, false, &$templateFields, $full_field_text, &$templateText ); |
| 147 | + $fieldNamesArray[] = $field_name; |
136 | 148 | } |
137 | 149 | } |
138 | 150 | } |
| 151 | + |
| 152 | + // Then, get calls to #set and #set_internal |
| 153 | + // (thankfully, they have basically the same syntax). |
| 154 | + if ( preg_match_all( '/#(set|set_internal):(.*?}}})\s*}}/mis', $templateText, $matches ) ) { |
| 155 | + foreach ( $matches[2] as $i => $match ) { |
| 156 | + $match = str_replace( '{{{', '', $match ); |
| 157 | + $match = str_replace( '}}}', '', $match ); |
| 158 | + $setValues = explode( '|', $match ); |
| 159 | + foreach( $setValues as $valuePair ) { |
| 160 | + $keyAndVal = explode( '=', $valuePair ); |
| 161 | + if ( count( $keyAndVal ) == 2) { |
| 162 | + $propertyName = trim( $keyAndVal[0] ); |
| 163 | + $fieldName = trim( $keyAndVal[1] ); |
| 164 | + if ( ! in_array( $fieldName, $fieldNamesArray ) ) { |
| 165 | + $full_field_text = $matches[0][$i]; |
| 166 | + self::handlePropertySettingInTemplate( $fieldName, $propertyName, false, &$templateFields, $full_field_text, &$templateText ); |
| 167 | + $fieldNamesArray[] = $fieldName; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // Then, get calls to #declare. |
| 175 | + if ( preg_match_all( '/#declare:(.*?)}}/mis', $templateText, $matches ) ) { |
| 176 | + foreach ( $matches[1] as $i => $match ) { |
| 177 | + $setValues = explode( '|', $match ); |
| 178 | + foreach( $setValues as $valuePair ) { |
| 179 | + $keyAndVal = explode( '=', $valuePair ); |
| 180 | + if ( count( $keyAndVal ) == 2) { |
| 181 | + $propertyName = trim( $keyAndVal[0] ); |
| 182 | + $fieldName = trim( $keyAndVal[1] ); |
| 183 | + if ( ! in_array( $fieldName, $fieldNamesArray ) ) { |
| 184 | + $full_field_text = $matches[0][$i]; |
| 185 | + self::handlePropertySettingInTemplate( $fieldName, $propertyName, false, &$templateFields, $full_field_text, &$templateText ); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
139 | 191 | |
140 | | - // finally, get any non-semantic fields defined |
141 | | - if ( preg_match_all( '/{{{([^|}]*)/mis', $template_text, $matches ) ) { |
| 192 | + // Finally, get any non-semantic fields defined. |
| 193 | + if ( preg_match_all( '/{{{([^|}]*)/mis', $templateText, $matches ) ) { |
142 | 194 | foreach ( $matches[1] as $i => $field_name ) { |
143 | 195 | $full_field_text = $matches[0][$i]; |
144 | | - if ( ( $full_field_text != '' ) && ( ! in_array( $field_name, $field_names_array ) ) ) { |
145 | | - $cur_pos = stripos( $template_text, $full_field_text ); |
146 | | - $template_fields[$cur_pos] = SFTemplateField::create( $field_name, $wgContLang->ucfirst( $field_name ) ); |
147 | | - $field_names_array[] = $field_name; |
| 196 | + if ( ( $full_field_text != '' ) && ( ! in_array( $field_name, $fieldNamesArray ) ) ) { |
| 197 | + $cur_pos = stripos( $templateText, $full_field_text ); |
| 198 | + $templateFields[$cur_pos] = SFTemplateField::create( $field_name, $wgContLang->ucfirst( $field_name ) ); |
| 199 | + $fieldNamesArray[] = $field_name; |
148 | 200 | } |
149 | 201 | } |
150 | 202 | } |
151 | 203 | } |
152 | | - ksort( $template_fields ); |
153 | | - return $template_fields; |
| 204 | + ksort( $templateFields ); |
| 205 | + return $templateFields; |
154 | 206 | } |
155 | 207 | |
156 | 208 | static function create( $name, $label, $allow_multiple, $max_allowed = null ) { |