r78929 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78928‎ | r78929 | r78930 >
Date:22:00, 23 December 2010
Author:yaron
Status:deferred
Tags:
Comment:
Improved parsing of templates to determine their fields and properties, especially for #set and #set_internal but also in general. Among other changes, the old ':=' syntax for setting properties is no longer supported; only '::' is.
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormClasses.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormClasses.php
@@ -74,19 +74,16 @@
7575
7676 /**
7777 * 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.
 78+ * template text, create an SFTemplateField object out of it, and
 79+ * add it to the $templateFields array.
8180 */
82 - function handlePropertySettingInTemplate( $fieldName, $propertyName, $isList, &$templateFields, $fullFieldText, &$templateText ) {
 81+ function handlePropertySettingInTemplate( $fieldName, $propertyName, $isList, &$templateFields, $templateText ) {
8382 global $wgContLang;
8483 $templateField = SFTemplateField::create( $fieldName, $wgContLang->ucfirst( $fieldName ) );
8584 $templateField->setSemanticProperty( $propertyName );
8685 $templateField->is_list = $isList;
8786 $cur_pos = stripos( $templateText, $fieldName );
8887 $templateFields[$cur_pos] = $templateField;
89 - $replacement = str_repeat( "x", strlen( $fullFieldText ) );
90 - $templateText = str_replace( $fullFieldText, $replacement, $templateText );
9188 }
9289
9390 /**
@@ -101,13 +98,10 @@
10299 // The way this works is that fields are found and then stored
103100 // in an array based on their location in the template text, so
104101 // 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.
 102+ // in the template, not the order in which they were found.
107103 // Some fields can be found more than once (especially if
108104 // 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.
 105+ // recorded the first time they're found.
112106 $template_title = Title::makeTitleSafe( NS_TEMPLATE, $this->template_name );
113107 $template_article = null;
114108 if ( isset( $template_title ) ) $template_article = new Article( $template_title );
@@ -119,30 +113,23 @@
120114
121115 // First, look for "arraymap" parser function calls
122116 // 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.
 117+ if ( preg_match_all( '/{{#arraymap:{{{([^|}]*:?[^|}]*)[^\[]*\[\[([^:]*:?[^:]*)::/mis', $templateText, $matches ) ) {
127118 foreach ( $matches[1] as $i => $field_name ) {
128119 if ( ! in_array( $field_name, $fieldNamesArray ) ) {
129120 $propertyName = $matches[2][$i];
130 - $full_field_text = $matches[0][$i];
131 - self::handlePropertySettingInTemplate( $field_name, $propertyName, true, &$templateFields, $full_field_text, &$templateText );
 121+ self::handlePropertySettingInTemplate( $field_name, $propertyName, true, &$templateFields, $templateText );
132122 $fieldNamesArray[] = $field_name;
133123 }
134124 }
135125 }
136126
137127 // 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.
 128+ if ( preg_match_all( '/\[\[([^:|\[\]]*:*?[^:|\[\]]*)::{{{([^\]\|}]*).*?\]\]/mis', $templateText, $matches ) ) {
142129 foreach ( $matches[1] as $i => $propertyName ) {
143 - $field_name = $matches[3][$i];
 130+ $field_name = trim( $matches[2][$i] );
144131 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 );
 132+ $propertyName = trim( $propertyName );
 133+ self::handlePropertySettingInTemplate( $field_name, $propertyName, false, &$templateFields, $templateText );
147134 $fieldNamesArray[] = $field_name;
148135 }
149136 }
@@ -152,17 +139,12 @@
153140 // (thankfully, they have basically the same syntax).
154141 if ( preg_match_all( '/#(set|set_internal):(.*?}}})\s*}}/mis', $templateText, $matches ) ) {
155142 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] );
 143+ if ( preg_match_all( '/([^|{]*?)=\s*{{{([^|}]*)/mis', $match, $matches2 ) ) {
 144+ foreach ( $matches2[1] as $i => $propertyName ) {
 145+ $fieldName = trim( $matches2[2][$i] );
164146 if ( ! in_array( $fieldName, $fieldNamesArray ) ) {
165 - $full_field_text = $matches[0][$i];
166 - self::handlePropertySettingInTemplate( $fieldName, $propertyName, false, &$templateFields, $full_field_text, &$templateText );
 147+ $propertyName = trim( $propertyName );
 148+ self::handlePropertySettingInTemplate( $fieldName, $propertyName, false, &$templateFields, $templateText );
167149 $fieldNamesArray[] = $fieldName;
168150 }
169151 }
@@ -180,8 +162,8 @@
181163 $propertyName = trim( $keyAndVal[0] );
182164 $fieldName = trim( $keyAndVal[1] );
183165 if ( ! in_array( $fieldName, $fieldNamesArray ) ) {
184 - $full_field_text = $matches[0][$i];
185 - self::handlePropertySettingInTemplate( $fieldName, $propertyName, false, &$templateFields, $full_field_text, &$templateText );
 166+ self::handlePropertySettingInTemplate( $fieldName, $propertyName, false, &$templateFields, $templateText );
 167+ $fieldNamesArray[] = $fieldName;
186168 }
187169 }
188170 }
@@ -190,12 +172,12 @@
191173
192174 // Finally, get any non-semantic fields defined.
193175 if ( preg_match_all( '/{{{([^|}]*)/mis', $templateText, $matches ) ) {
194 - foreach ( $matches[1] as $i => $field_name ) {
195 - $full_field_text = $matches[0][$i];
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;
 176+ foreach ( $matches[1] as $i => $fieldName ) {
 177+ $fieldName = trim( $fieldName );
 178+ if ( !empty( $fieldName ) && ( ! in_array( $fieldName, $fieldNamesArray ) ) ) {
 179+ $cur_pos = stripos( $templateText, $fieldName );
 180+ $templateFields[$cur_pos] = SFTemplateField::create( $fieldName, $wgContLang->ucfirst( $fieldName ) );
 181+ $fieldNamesArray[] = $fieldName;
200182 }
201183 }
202184 }

Status & tagging log