r96898 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96897‎ | r96898 | r96899 >
Date:20:34, 12 September 2011
Author:yaron
Status:deferred
Tags:
Comment:
Major overhaul: rearranged methods and hooks, and rewrote some code, to try to streamline the class and avoid redundant code
Modified paths:
  • /trunk/extensions/PageSchemas/specials/PS_EditSchema.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PageSchemas/specials/PS_EditSchema.php
@@ -1,6 +1,6 @@
22 <?php
33 /**
4 - * Displays an interface to let users create all pages based on xml
 4+ * Displays an interface to let users create and edit the <PageSchema> XML.
55 *
66 * @author Ankit Garg
77 */
@@ -19,12 +19,13 @@
2020 $template_name_error_str = wfMsg( 'sf_blank_error' );
2121 $jsText =<<<END
2222 <script type="text/javascript">
23 -var fieldNum = 1;
24 -var templateNum = 1;
25 -function createTemplateAddField(template_num) {
 23+var fieldNum = 0;
 24+var templateNum = 0;
 25+// TODO - this function should be a jQuery 'fn' instead
 26+function psAddField(template_num) {
2627 fieldNum++;
2728 newField = jQuery('#starterField').clone().css('display', '').removeAttr('id');
28 - newHTML = newField.html().replace(/starter/g, fieldNum);
 29+ newHTML = newField.html().replace(/fnum/g, fieldNum);
2930 newField.html(newHTML);
3031 newField.find(".deleteField").click( function() {
3132 // Remove the encompassing div for this instance.
@@ -35,10 +36,10 @@
3637 addjQueryToCheckbox();
3738 }
3839
39 -function createAddTemplate() {
 40+function psAddTemplate() {
4041 templateNum++;
4142 newField = jQuery('#starterTemplate').clone().css('display', '').removeAttr('id');
42 - newHTML = newField.html().replace(/starter/g, templateNum);
 43+ newHTML = newField.html().replace(/tnum/g, templateNum);
4344 newField.html(newHTML);
4445 newField.find(".deleteTemplate").click( function() {
4546 // Remove the encompassing div for this instance.
@@ -48,7 +49,7 @@
4950 jQuery('#templatesList').append(newField);
5051 }
5152
52 -function updateFieldNum(field_num ) {
 53+function updateFieldNum(field_num) {
5354 fieldNum = field_num;
5455 }
5556
@@ -74,6 +75,10 @@
7576 .fadeOut('fast', function() { jQuery(this).remove(); });
7677 });
7778 addjQueryToCheckbox();
 79+ jQuery('#editPageSchemaForm').submit( function() {
 80+ jQuery('#starterTemplate').find("input, select, textarea").attr('disabled', 'disabled');
 81+ return true;
 82+ } );
7883 });
7984 </script>
8085
@@ -81,122 +86,10 @@
8287 $wgOut->addScript( $jsText );
8388 }
8489
85 - function blankFormHTML( $htmlFromExtensions ) {
86 - //$schema_name_label = wfMsg('ps-schema-name-label');
87 - $add_xml_label = wfMsg('ps-add-xml-label');
88 -
89 - $text = ' <form id="createPageSchemaForm" action="" method="post">' . "\n";
90 - //$text .= '<p>'.$schema_name_label.' <input type="text" name="s_name"/> </p> ';
91 - $text .= '<p>'.$add_xml_label.'
92 - <textarea rows=4 style="width: 100%" name="ps_add_xml"></textarea>
93 - </p> ';
94 - if ( $htmlFromExtensions['sf_form'] != null ) {
95 - $text_ex = preg_replace( '/starter/', '1', $htmlFromExtensions['sf_form'] );
96 - $text .= $text_ex;
97 - }
98 -
99 - $text .= '<div id="templatesList">';
100 - $text .= '<div class="templateBox" >';
101 - $text .= '</div></div>';
102 -
103 - $add_template_button = Xml::element( 'input',
104 - array(
105 - 'type' => 'button',
106 - 'value' => wfMsg( 'ps-add-template' ),
107 - 'onclick' => "createAddTemplate()"
108 - )
109 - );
110 - $text .= Xml::tags( 'p', null, $add_template_button ) . "\n";
111 - $text .= ' <hr />
112 - <div class="editButtons">
113 - <input type="submit" id="wpSave" name="wpSave" value="Save" />
114 - </div>';
115 - $text .= ' </form>';
116 -
117 - return $text;
118 - }
119 -
120 - function starterFieldHTML( $htmlFromExtensions ) {
121 - $template_label = wfMsg( 'ps-template' );
122 - $delimiter_label = wfMsg( 'ps-delimiter-label' );
123 - $multiple_temp_label = wfMsg( 'ps-multiple-temp-label' );
124 - $add_xml_label = wfMsg('ps-add-xml-label');
125 - $field_list_label = wfMsg( 'ps-field-list-label' );
126 -
127 - $starter_text = <<<END
128 -<div class="templateBox" id="starterTemplate" style="display: none">
129 - <fieldset style="background: #ddd;">
130 - <legend>$template_label</legend>
131 - <p>Name: <input type="text" name="t_name_starter"/></p>
132 - <p><input type="checkbox" name="is_multiple_starter"/>$multiple_temp_label</p>
133 - <div id="fieldsList_starter">
134 - </div>
135 -
136 -END;
137 - $addFieldButton = Html::input( 'add-field', wfMsg( 'ps-add-field' ), 'button',
138 - array( 'onclick' => 'createTemplateAddField(starter)' )
139 - );
140 - $starter_text .= Html::rawElement( 'p', null, $addFieldButton );
141 - $starter_text .= <<<END
142 - <hr />
143 - <p>$add_xml_label
144 - <textarea rows=4 style="width: 100%" name="t_add_xml_starter"></textarea>
145 - </p>
146 -
147 -END;
148 - $removeTemplateButton = Html::input( 'remove-template', wfMsg( 'ps-remove-template' ), 'button',
149 - array( 'class' => 'deleteTemplate' )
150 - );
151 - $starter_text .= Html::rawElement( 'p', null, $removeTemplateButton );
152 - $starter_text .= <<<END
153 - </fieldset>
154 -</div>
155 -<hr />
156 -
157 -END;
158 - $field_label = wfMsg( 'ps-field' );
159 - $display_label = wfMsg( 'ps-displaylabel' );
160 - $starter_text .= <<<END
161 -<div class="fieldBox" id="starterField" style="display: none">
162 - <fieldset style="background: #bbb;"><legend>$field_label</legend>
163 - <p>
164 - Field name: <input size="15" name="f_name_starter">
165 - $display_label <input size="15" name="f_label_starter">
166 - </p>
167 - <p><input type="checkbox" name="f_is_list_starter" class="isListCheckbox" /> $field_list_label
168 - &#160;&#160;</p>
169 - <p class="delimiterInput" style="display: none" >
170 - $delimiter_label <input type="text" name="f_delimiter_starter" />
171 - </p>
172 -
173 -END;
174 - if ($htmlFromExtensions['smw'] != null ) {
175 - $starter_text .= $htmlFromExtensions['smw'];
176 - }
177 - if ($htmlFromExtensions['sf'] != null ) {
178 - $starter_text .= $htmlFromExtensions['sf'];
179 - }
180 - if ($htmlFromExtensions['sd'] != null ) {
181 - $starter_text .= $htmlFromExtensions['sd'];
182 - }
183 - $starter_text .= <<<END
184 - <p>$add_xml_label
185 - <textarea rows=4 style="width: 100%" name="f_add_xml_starter"></textarea>
186 - </p>
187 -
188 -END;
189 - $removeFieldButton = Html::input( 'remove-field', wfMsg( 'ps-remove-field' ), 'button',
190 - array( 'class' => 'deleteField' )
191 - );
192 - $starter_text .= $removeFieldButton;
193 - $starter_text .= <<<END
194 - </fieldset>
195 -</div>
196 -
197 -END;
198 - return $starter_text;
199 - }
200 -
 90+ /**
 91+ * Creates full <PageSchema> XML text, based on what was passed in by
 92+ * the form.
 93+ */
20194 static function pageSchemaXMLFromRequest() {
20295 global $wgRequest;
20396
@@ -220,28 +113,30 @@
221114 }
222115 $indexGlobalField = 0 ; //this variable is use to index the array returned by extensions for XML.
223116 foreach ( $wgRequest->getValues() as $var => $val ) {
 117+ $suffix = substr( $var, -3 );
 118+ // Ignore fields from the hidden/starter div
224119 if ( substr( $var, 0, 7 ) == 't_name_' ) {
225 - $templateNum = substr($var,7,1);
226 - if ($wgRequest->getCheck( 'is_multiple_'.$templateNum ) ) {
 120+ $templateNum = substr( $var, 7 );
 121+ if ( $wgRequest->getCheck( 'is_multiple_' . $templateNum ) ) {
227122 $psXML .= '<Template name="'.$val.'" multiple="multiple">';
228123 } else {
229124 $psXML .= '<Template name="'.$val.'">';
230125 }
231126 } elseif ( substr( $var, 0, 7 ) == 'f_name_' ) {
 127+ $fieldNum = substr( $var, 7 );
232128 $fieldName = $val;
233 - $fieldNum = substr($var,7,1);
234 - if ( $wgRequest->getCheck( 'f_is_list_'.$fieldNum ) ) {
235 - if ( $wgRequest->getText('f_delimiter_'.$fieldNum) != '' ) {
236 - $delimiter = $wgRequest->getText('f_delimiter_'.$fieldNum);
237 - $psXML .= '<Field name="'.$fieldName.'" list="list" delimiter="'.$delimiter.'">';
 129+ if ( $wgRequest->getCheck( 'f_is_list_' . $fieldNum ) ) {
 130+ if ( $wgRequest->getText( 'f_delimiter_' . $fieldNum ) != '' ) {
 131+ $delimiter = $wgRequest->getText( 'f_delimiter_' . $fieldNum );
 132+ $psXML .= '<Field name="'.$fieldName . '" list="list" delimiter="' . $delimiter . '">';
238133 } else {
239 - $psXML .= '<Field name="'.$fieldName.'" list="list">';
 134+ $psXML .= '<Field name="'.$fieldName . '" list="list">';
240135 }
241136 } else {
242 - $psXML .= '<Field name="'.$fieldName.'">';
 137+ $psXML .= '<Field name="' . $fieldName . '">';
243138 }
244139 } elseif ( substr( $var, 0, 8 ) == 'f_label_' ) {
245 - $psXML .= '<Label>'.$val.'</Label>';
 140+ $psXML .= '<Label>' . $val . '</Label>';
246141
247142 // Get XML created by extensions
248143 foreach ( $fieldXMLFromExtensions as $extensionName => $xmlPerField ) {
@@ -262,269 +157,252 @@
263158 return $psXML;
264159 }
265160
266 - function execute( $category ) {
267 - global $wgRequest, $wgOut, $wgUser;
268 - global $wgSkin;
 161+ /**
 162+ * Displays a list of links to categories that have a page schema
 163+ * defined; for use in Special:EditSchema, if no category is specified.
 164+ */
 165+ static function showLinksToCategories() {
 166+ $cat_titles = array();
 167+ $count_title = 0;
 168+ $text = "";
 169+ $dbr = wfGetDB( DB_SLAVE );
 170+ //get the result set, query : slect page_props
 171+ $res = $dbr->select( 'page_props',
 172+ array(
 173+ 'pp_page',
 174+ 'pp_propname',
 175+ 'pp_value'
 176+ ),
 177+ array(
 178+ 'pp_propname' => 'PageSchema'
 179+ )
 180+ );
 181+ $editSchemaPage = SpecialPage::getTitleFor( 'EditSchema' );
 182+ while ( $row = $dbr->fetchRow( $res ) ) {
 183+ if ( $row[2] != null ) {
 184+ $page_id_cat = $row[0];
 185+ if ( Title::newFromId( $page_id_cat )->getNamespace() == NS_CATEGORY ) {
 186+ $cat_text = Title::newFromId( $page_id_cat )->getText();
 187+ $url = $editSchemaPage ->getFullURL() . '/' . $cat_text;
 188+ $text .= Html::element( 'a', array( 'href' => $url ), $cat_text ) . '<br />';
 189+ }
 190+ }
 191+ }
 192+ $dbr->freeResult( $res );
 193+ return $text;
 194+ }
269195
270 - $this->setHeaders();
271 - $text_3 = '<p>'.wfMsg( 'ps-page-desc-edit-schema' ).'</p>';
272 - self::addJavascript();
 196+ /*
 197+ * Returns the HTML for one section of the EditSchema form.
 198+ */
 199+ static function printFormSection( $label, $topColor, $html, $bgColor = 'white' ) {
 200+ return "<div style=\"background: $bgColor; border: 1px #999 solid; padding: 0px; margin-bottom: 10px; margin-top: 10px;\">\n" .
 201+ "<div style=\"font-weight: bold; background: $topColor; padding: 4px 7px; border-bottom: 1px #bbb solid;\">$label</div>\n" .
 202+ "<div style=\"padding: 5px 15px;\">$html</div>\n" .
 203+ "</div>\n";
 204+ }
273205
274 - $htmlFromExtensions = array(); //This var. will save the html text returned by the extensions
275 - $js_extensions = array();
276 - wfRunHooks( 'getHtmlTextForFieldInputs', array( &$js_extensions, &$htmlFromExtensions ) );
 206+ /*
 207+ * Returns the HTML for a form section coming from a specific extension.
 208+ */
 209+ static function printFieldHTMLForExtension( $valuesFromExtension ) {
 210+ list( $label, $color, $html ) = $valuesFromExtension;
 211+ return self::printFormSection( $label, $color, $html );
 212+ }
277213
278 - $save_page = $wgRequest->getCheck( 'wpSave' );
279 - if ( $save_page ) {
280 - $psXML = self::pageSchemaXMLFromRequest();
281 - $pageSchemaObj = new PSSchema( $category );
282 - $categoryTitle = Title::newFromText( $category, NS_CATEGORY );
283 - $categoryArticle = new Article( $categoryTitle );
284 - $pageText = $categoryArticle->getContent();
285 - if ( $pageSchemaObj->isPSDefined() ) {
286 - //Do some preg-replace magic
287 - $tag = "PageSchema";
288 - $pageText = preg_replace('{<'.$tag.'[^>]*>([^@]*?)</'.$tag.'>'.'}', $psXML, $pageText);
 214+ /**
 215+ * Returns the HTML for a section of the form comprising one
 216+ * template field.
 217+ */
 218+ static function printFieldSection( $field_xml = null, $pageSchemaField = null, $field_count = 'fnum' ) {
 219+ $fieldName = '';
 220+ $delimiter = '';
 221+ $fieldLabel = '';
 222+ $attrs = array();
 223+ $pAttrs = array( 'class' => 'delimiterInput' );
 224+ if ( is_null( $field_xml ) ) {
 225+ $text = '<div class="fieldBox" id="starterField" style="display: none" >';
 226+ } else {
 227+ $text = '<div class="fieldBox" >';
 228+ $fieldName = (string)$field_xml->attributes()->name;
 229+ if ( ((string)$field_xml->attributes()->delimiter) != null || ((string)$field_xml->attributes()->delimiter) != '' ) {
 230+ $delimiter = (string)$field_xml->attributes()->delimiter;
 231+ }
 232+ foreach ( $field_xml->children() as $tag => $child ) {
 233+ if ( $tag == 'Label' ) {
 234+ $fieldLabel = (string)$child;
 235+ }
 236+ }
 237+ if ( ((string)$field_xml->attributes()->list) == "list" ) {
 238+ $attrs['checked'] = 'checked';
289239 } else {
290 - $pageText = $psXML . $pageText;
 240+ $pAttrs['style'] = 'display: none';
291241 }
292 - $editSummary = $wgRequest->getVal( 'wpSummary' );
293 - $categoryArticle->doEdit( $pageText, $editSummary );
294 - $redirectURL = $categoryTitle->getLocalURL();
295 - $text .= <<<END
296 - <script type="text/javascript">
297 - window.onload = function() {
298 - window.location="$redirectURL";
299242 }
300 - </script>
 243+ $fieldHTML = '<p>Field name: ';
 244+ $fieldHTML .= Html::input( 'f_name_' . $field_count, $fieldName, 'text', array( 'size' => 15 ) ) . ' ';
 245+ $fieldHTML .= wfMsg( 'ps-displaylabel' ) . ' ';
 246+ $fieldHTML .= Html::input( 'f_label_' . $field_count, $fieldLabel, 'text', array( 'size' => 15 ) );
 247+ $fieldHTML .= "\t\t</p>\n";
 248+ $fieldIsListInput = Html::input( 'f_is_list_' . $field_count, null, 'checkbox', $attrs );
 249+ $fieldHTML .= Html::rawElement( 'p', null, $fieldIsListInput . ' ' . wfMsg( 'ps-field-list-label' ) );
 250+ $fieldDelimiterInput = Html::input ( 'f_delimiter_' . $field_count, $delimiter, 'text', null );
 251+ $fieldHTML .= Html::rawElement( 'p', $pAttrs, wfMsg( 'ps-delimiter-label' ) . ' ' . $fieldDelimiterInput );
301252
302 -END;
303 - $wgOut->addHTML( $text );
304 - return true;
 253+ // Insert HTML text from extensions
 254+ $htmlFromExtensions = array();
 255+ wfRunHooks( 'PageSchemasGetFieldHTML', array( $pageSchemaField, &$htmlFromExtensions ) );
 256+ foreach ( $htmlFromExtensions as $valuesFromExtension ) {
 257+ $html = self::printFieldHTMLForExtension( $valuesFromExtension );
 258+ // We use 'num' here, instead of 'fnum', to distinguish
 259+ // between field names from Page Schemas (which get
 260+ // their number set via Javascript) and field names from
 261+ // other extensions (which get their number set via PHP).
 262+ // Is this important to do? Probably not.
 263+ $fieldHTML .= str_replace( 'num', $field_count, $html );
305264 }
306265
307 - if ( $category == "" ) {
308 - // No category was specified - show the list of categories with a page schema defined.
309 - $cat_titles = array();
310 - $count_title = 0;
311 - $text = "";
312 - $dbr = wfGetDB( DB_SLAVE );
313 - //get the result set, query : slect page_props
314 - $res = $dbr->select( 'page_props',
315 - array(
316 - 'pp_page',
317 - 'pp_propname',
318 - 'pp_value'
319 - ),
320 - array(
321 - 'pp_propname' => 'PageSchema'
322 - )
323 - );
324 - $editSchemaPage = SpecialPage::getTitleFor( 'EditSchema' );
325 - while ( $row = $dbr->fetchRow( $res ) ) {
326 - if ( $row[2] != null ) {
327 - $page_id_cat = $row[0];
328 - if ( Title::newFromId($page_id_cat)->getNamespace() == NS_CATEGORY ) {
329 - $cat_text = Title::newFromId($page_id_cat)->getText();
330 - $url = $editSchemaPage ->getFullURL() . '/' . $cat_text;
331 - $text .= Html::element( 'a', array( 'href' => $url ), $cat_text ) . '<br />';
332 - }
333 - }
 266+ $add_xml_label = wfMsg('ps-add-xml-label');
 267+ $fieldHTML .= <<<END
 268+ <p>$add_xml_label
 269+ <textarea rows=4 style="width: 100%" name="f_add_xml_$field_count"></textarea>
 270+ </p>
 271+
 272+END;
 273+ $fieldHTML .= Html::input( 'remove-field', wfMsg( 'ps-remove-field' ), 'button',
 274+ array( 'class' => 'deleteField' )
 275+ );
 276+ $text .= self::printFormSection( wfMsg( 'ps-field' ), '#AAA', $fieldHTML, '#CCC' );
 277+ $text .= "\t</div><!-- fieldBox -->\n";
 278+ return $text;
 279+ }
 280+
 281+ /**
 282+ * Returns the HTML for a section of the form comprising one template.
 283+ */
 284+ static function printTemplateSection( $template_num = 'tnum', $template_xml = null, $pageSchemaTemplate = null ) {
 285+ if ( is_null( $pageSchemaTemplate ) ) {
 286+ $pageSchemaTemplateFields = array();
 287+ } else {
 288+ $pageSchemaTemplateFields = $pageSchemaTemplate->getFields();
 289+ }
 290+ $attrs = array();
 291+ if ( is_null( $template_xml ) ) {
 292+ $text = '<div class="templateBox" id="starterTemplate" style="display: none">';
 293+ $templateName = '';
 294+ $fields_xml_array = array( null );
 295+ } else {
 296+ $text = '<div class="templateBox" >';
 297+ $templateName = (string) $template_xml->attributes()->name;
 298+ if ( ( (string)$template_xml->attributes()->multiple ) == "multiple" ) {
 299+ $attrs['checked'] = 'checked';
334300 }
335 - $dbr->freeResult( $res );
336 - $wgOut->addHTML( $text );
337 - return true;
 301+ $fields_xml_array = $template_xml->children();
338302 }
 303+ $templateNameInput = Html::input( 't_name_' . $template_num, $templateName, 'text' );
 304+ $templateHTML = '<p>Name: ' . $templateNameInput . '</p> ';
 305+ $templateIsMultipleInput = Html::input( 'is_multiple_' . $template_num, null, 'checkbox', $attrs );
 306+ $templateHTML .= Html::rawElement( 'p', null, $templateIsMultipleInput . ' ' . wfMsg( 'ps-multiple-temp-label' ) );
 307+ $template_add_xml = "";
 308+ foreach ( $fields_xml_array as $field_xml ) {
 309+ if ( !empty( $field_xml ) && $field_xml->getName() != 'Field' ) {
 310+ $template_add_xml .= (string)$field_xml->asXML();
 311+ }
 312+ }
 313+ $templateHTML .= '<div id="fieldsList_'.$template_num.'">';
 314+ $field_count = 0;
 315+ foreach ( $fields_xml_array as $field_xml ) {
 316+ if ( empty( $field_xml ) ) {
 317+ $templateHTML .= self::printFieldSection();
 318+ } elseif ( $field_xml->getName() == "Field" ) {
 319+ $pageSchemaField = $pageSchemaTemplateFields[$field_count];
 320+ $templateHTML .= self::printFieldSection( $field_xml, $pageSchemaField, $field_count );
 321+ $field_count++;
 322+ }
 323+ }
 324+ $templateHTML .= "\t</div><!-- fieldsList -->\n";
 325+ $templateHTML .=<<<END
 326+<script type="text/javascript">
 327+ $(document).ready(function() {
 328+ updateFieldNum($field_count);
 329+ });
 330+</script>
339331
340 - // We have a category - show a form.
341 - $formHTML = self::blankFormHTML( $htmlFromExtensions );
342 - $formHTML .= self::starterFieldHTML( $htmlFromExtensions );
343 -
344 - $add_xml_label = wfMsg('ps-add-xml-label');
345 -
346 - $pageSchemaObj = new PSSchema( $category );
347 - $title = Title::newFromText( $category, NS_CATEGORY );
348 - $pageId = $title->getArticleID();
349 - $dbr = wfGetDB( DB_SLAVE );
350 - //get the result set, query : select page_props
351 - $res = $dbr->select( 'page_props',
 332+END;
 333+ $add_field_button = Xml::element( 'input',
352334 array(
353 - 'pp_page',
354 - 'pp_propname',
355 - 'pp_value'
356 - ),
357 - array(
358 - 'pp_page' => $pageId,
 335+ 'type' => 'button',
 336+ 'value' => wfMsg( 'ps-add-field' ),
 337+ 'onclick' => "psAddField($template_num)"
359338 )
360339 );
 340+ $templateHTML .= Xml::tags( 'p', null, $add_field_button ) . "\n";
 341+ $templateHTML .= '<hr />
 342+ <p>'. wfMsg('ps-add-xml-label') .'
 343+ <textarea rows=4 style="width: 100%" name="t_add_xml_'.$template_num.'">'.$template_add_xml.'</textarea>
 344+ </p>';
 345+ $templateHTML .= '<p>' . Html::input( 'remove-template', 'Remove template', 'button', array( 'class' => 'deleteTemplate' ) ) . "</p>\n";
 346+ $text .= self::printFormSection( wfMsg( 'ps-template' ), '#CCC', $templateHTML, '#EEE' );
 347+ $text .= " </div><!-- templateBox-->";
 348+ return $text;
 349+ }
361350
362 - //first row of the result set
363 - $row = $dbr->fetchRow( $res );
364 - if ( $row == null && !$title->exists() ) {
365 - // Category doesn't exist.
366 - $text = '<p>'.wfMsg( 'ps-page-desc-cat-not-exist' ).'</p>';
367 - $text .= $formHTML;
368 - $wgOut->addHTML( $text );
369 - return true;
 351+ /**
 352+ * Returns the HTML to display an entire form.
 353+ */
 354+ static function printForm( $pageSchemaObj = null, $pageXML = null ) {
 355+ $htmlForSchema = array();
 356+ wfRunHooks( 'PageSchemasGetSchemaHTML', array( $pageSchemaObj, &$htmlForSchema ) );
 357+
 358+ if ( is_null( $pageSchemaObj ) ) {
 359+ $template_all = array();
 360+ } else {
 361+ $template_all = $pageSchemaObj->getTemplates();
370362 }
371363
372 - if ( ($row[1] != 'PageSchema') || ($row[2] == null ) ) {
373 - // Category exists, but has no page schema.
374 - $text = '<p>'.wfMsg( 'ps-page-desc-ps-not-exist' ).'</p>';
375 - $text .= $formHTML;
376 - $wgOut->addHTML($text );
377 - return true;
 364+ if ( is_null( $pageXML ) ) {
 365+ $pageXMLChildren = array();
 366+ } else {
 367+ $pageXMLChildren = $pageXML->children();
378368 }
379369
380 - // If we're here, it's a category with an existing page schema - populate the
381 - // form with its values.
382 - $pageXMLstr = $row[2];
383 - $pageXML = simplexml_load_string( $pageXMLstr );
384 - $ps_add_xml = "";
385 - //$pageName = (string)$pageXML->attributes()->name;
386 - $text = '<form id="editPageSchemaForm" action="" method="post">' . "\n";
387 - //$text .= '<p>'.$schema_name_label.' <input type="text" name="s_name" value="'.$pageName.'" /> </p> ';
388 - foreach ( $pageXML->children() as $template_xml ) {
389 - if ( ( $template_xml->getName() != 'Template') && ($template_xml->getName() != 'semanticforms_Form') ) {
 370+ foreach ( $pageXMLChildren as $template_xml ) {
 371+ if ( ( $template_xml->getName() != 'Template') && ( $template_xml->getName() != 'semanticforms_Form' ) ) {
390372 $ps_add_xml .= (string)$template_xml->asXML();
391373 }
392374 }
393 - $text .= '<p>' . $add_xml_label . '
 375+
 376+ $text = '<form id="editPageSchemaForm" action="" method="post">' . "\n";
 377+ $text .= '<p>' . wfMsg('ps-add-xml-label') . '
394378 <textarea rows=4 style="width: 100%" name="ps_add_xml" >' . $ps_add_xml . '</textarea>
395379 </p> ';
396380
397 - $filledHTMLFromExtensions = array();
398 - wfRunHooks( 'getFilledHtmlTextForFieldInputs', array( $pageSchemaObj, &$filledHTMLFromExtensions ));
399 - if ( $filledHTMLFromExtensions['sf_form'] != null ) {
400 - $text_ex = preg_replace('/starter/', '1', $filledHTMLFromExtensions['sf_form']);
401 - $text .= $text_ex;
 381+ foreach ( $htmlForSchema as $valuesFromExtension ) {
 382+ $text .= self::printFieldHTMLForExtension( $valuesFromExtension );
402383 }
 384+
403385 $text .= '<div id="templatesList">';
 386+
404387 $template_num = 0;
 388+ $pageSchemaTemplate = $template_all[$template_num];
 389+
 390+ // Add 'starter', hidden template section.
 391+ $text .= self::printTemplateSection();
405392 /* index for template objects */
406 - foreach ( $pageXML->children() as $tag => $template_xml ) {
 393+ foreach ( $pageXMLChildren as $tag => $template_xml ) {
407394 if ( $tag == 'Template' ) {
408 - $template_add_xml = "";
409395 $template_num++;
410 - $field_count = 0;
411 - if ( count($template_xml->children()) > 0 ) {
412 - $text .= '<div class="templateBox" >';
413 - $text .= '<fieldset style="background: #ddd;"><legend>Template</legend> ';
414 - $templateName = (string) $template_xml->attributes()->name;
415 - $templateNameInput = Html::input( 't_name_' . $template_num, $templateName, 'text' );
416 - $text .= '<p>Name: ' . $templateNameInput . '</p> ';
417 - $attrs = array();
418 - if ( ((string) $template_xml->attributes()->multiple) == "multiple" ) {
419 - $attrs['checked'] = 'checked';
420 - }
421 - $templateIsMultipleInput = Html::input( 'is_multiple_' . $template_num, null, 'checkbox', $attrs );
422 - $text .= Html::rawElement( 'p', null, $templateIsMultipleInput . ' ' . wfMsg( 'ps-multiple-temp-label' ) );
423 - foreach ( $template_xml->children() as $field_xml ) {
424 - if ( $field_xml->getName() != 'Field' ) {
425 - $template_add_xml .= (string)$field_xml->asXML();
426 - }
427 - }
428 - $text .= '<div id="fieldsList_'.$template_num.'">';
429 - foreach ( $template_xml->children() as $field_xml ) {
430 - if ( $field_xml->getName() == "Field" ) {
431 - $fieldName = (string)$field_xml->attributes()->name;
432 - $text .= '<div class="fieldBox" >';
433 - $text .= '<fieldset style="background: #bbb;"><legend>Field</legend> ';
434 - if ( ((string)$field_xml->attributes()->delimiter) != null || ((string)$field_xml->attributes()->delimiter) != '' ) {
435 - $delimiter = (string)$field_xml->attributes()->delimiter;
436 - }
437 - foreach ($field_xml->children() as $tag => $child ) {
438 - if ( $tag == 'Label' ) {
439 - $fieldLabel = (string)$child;
440 - }
441 - }
442 - $text .= '<p>Field name: <input size="15" name="f_name_'.$field_count.'" value="'.$fieldName.'" />';
443 - $display_label = wfMsg( 'ps-displaylabel' );
444 - $text .= $display_label . ' ' . Html::input( 'f_label_'.$field_count, $fieldLabel, 'text', array( 'size' => 15 ) );
445 - $text .= "\t\t</p>\n";
446 - $attrs = array();
447 - $pAttrs = array( 'class' => 'delimiterInput' );
448 - if ( ((string)$field_xml->attributes()->list) == "list" ) {
449 - $attrs['checked'] = 'checked';
450 - } else {
451 - $pAttrs['style'] = 'display: none';
452 - }
453 - $fieldIsListInput = Html::input( 'f_is_list_' . $field_count, null, 'checkbox', $attrs );
454 - $text .= Html::rawElement( 'p', null, $fieldIsListInput . ' ' . wfMsg( 'ps-field-list-label' ) );
455 - $fieldDelimiterInput = Html::input ( 'f_delimiter_' . $field_count, $delimiter, 'text', null );
456 - $text .= Html::rawElement( 'p', $pAttrs, wfMsg( 'ps-delimiter-label' ) . ' ' . $fieldDelimiterInput );
457 -
458 - // Insert HTML text from extensions
459 - if ( $filledHTMLFromExtensions['smw'] != null ) {
460 - $text_ex_array = $filledHTMLFromExtensions['smw'];
461 - if ( $text_ex_array[$field_count] != null ) {
462 - $text_ex = preg_replace('/starter/', $field_count, $text_ex_array[$field_count]);
463 - $text .= $text_ex;
464 - }
465 - }
466 - if ( $filledHTMLFromExtensions['sf'] != null ) {
467 - $text_ex_array = $filledHTMLFromExtensions['sf'];
468 - if ( $text_ex_array[$field_count] != null ) {
469 - $text_ex = preg_replace('/starter/', $field_count, $text_ex_array[$field_count]);
470 - $text .= $text_ex;
471 - }
472 - }
473 - if ( $filledHTMLFromExtensions['sd'] != null ) {
474 - $text_ex_array = $filledHTMLFromExtensions['sd'];
475 - if ( $text_ex_array[$field_count] != null ) {
476 - $text_ex = preg_replace('/starter/', $field_count, $text_ex_array[$field_count]);
477 - $text .= $text_ex;
478 - }
479 - }
480 -
481 - $text .= <<<END
482 - <p>$add_xml_label
483 - <textarea rows=4 style="width: 100%" name="f_add_xml_$field_count"></textarea>
484 - </p>
485 -
486 -END;
487 - $removeFieldButton = Html::input( 'remove-field', wfMsg( 'ps-remove-field' ), 'button',
488 - array( 'class' => 'deleteField' )
489 - );
490 - $text .= $removeFieldButton;
491 - $text .= <<<END
492 - </fieldset><!-- for fields -->
493 - </div><!-- fieldBox -->
494 -
495 -END;
496 - $field_count++;
497 - $text .= '<script type="text/javascript">
498 - updateFieldNum('.$field_count.');
499 - </script>';
500 - }
501 - }
502 - $text .= '</div><!-- fieldsList -->';
503 - $add_field_button = Xml::element( 'input',
504 - array(
505 - 'type' => 'button',
506 - 'value' => wfMsg( 'ps-add-field' ),
507 - 'onclick' => "createTemplateAddField($template_num)"
508 - )
509 - );
510 - $text .= Xml::tags( 'p', null, $add_field_button ) . "\n";
511 - $text .= '<hr />
512 - <p>'.$add_xml_label.'
513 - <textarea rows=4 style="width: 100%" name="t_add_xml_'.$template_num.'">'.$template_add_xml.'</textarea>
514 - </p>
515 - <p><input type="button" value="Remove template" class="deleteTemplate" /></p>
516 - </fieldset><!-- for template --> </div><!-- templateBox-->';
517 - }
 396+ $text .= self::printTemplateSection( $template_num, $template_xml, $pageSchemaTemplate );
518397 }
519398 }
520399 $add_template_button = Xml::element( 'input',
521400 array(
522401 'type' => 'button',
523402 'value' => wfMsg( 'ps-add-template' ),
524 - 'onclick' => "createAddTemplate()"
 403+ 'onclick' => "psAddTemplate()"
525404 )
526405 );
527 - $text .= '
528 - </div><!-- templatesList -->';
 406+ $text .= "\t</div><!-- templatesList -->\n";
529407 $text .= Xml::tags( 'p', null, $add_template_button ) . "\n";
530408 $text .= "\t\t<hr />\n";
531409 $label = wfMsg( 'summary' );
@@ -543,9 +421,93 @@
544422 $saveButton = Html::input( 'wpSave', wfMsg( 'savearticle' ), 'submit', $attr );
545423 $text .= "\t\t" . Html::rawElement( 'div', array( 'class' => 'editButtons' ),
546424 $saveButton ) . "\n";
547 - $text .= ' </form>';
548 - $text .= self::starterFieldHTML( $htmlFromExtensions );
549 - $wgOut->addHTML($text);
 425+ $text .= "\t</form>\n";
 426+
 427+ return $text;
 428+ }
 429+
 430+ function execute( $category ) {
 431+ global $wgRequest, $wgOut, $wgUser;
 432+ global $wgSkin;
 433+
 434+ $this->setHeaders();
 435+ $text = '<p>' . wfMsg( 'ps-page-desc-edit-schema' ) . '</p>';
 436+ self::addJavascript();
 437+
 438+ $save_page = $wgRequest->getCheck( 'wpSave' );
 439+ if ( $save_page ) {
 440+ $psXML = self::pageSchemaXMLFromRequest();
 441+ $pageSchemaObj = new PSSchema( $category );
 442+ $categoryTitle = Title::newFromText( $category, NS_CATEGORY );
 443+ $categoryArticle = new Article( $categoryTitle );
 444+ $pageText = $categoryArticle->getContent();
 445+ if ( $pageSchemaObj->isPSDefined() ) {
 446+ // Do some preg_replace magic.
 447+ // This is necessary if the <PageSchema> tag
 448+ // accepts any attributes - which it currently
 449+ // does not, but it may well in the future.
 450+ $tag = "PageSchema";
 451+ $pageText = preg_replace( '{<' . $tag . '[^>]*>([^@]*?)</' . $tag . '>' . '}', $psXML, $pageText );
 452+ } else {
 453+ $pageText = $psXML . $pageText;
 454+ }
 455+ $editSummary = $wgRequest->getVal( 'wpSummary' );
 456+ $categoryArticle->doEdit( $pageText, $editSummary );
 457+ $redirectURL = $categoryTitle->getLocalURL();
 458+ $text = <<<END
 459+ <script type="text/javascript">
 460+ window.onload = function() {
 461+ window.location="$redirectURL";
 462+ }
 463+ </script>
 464+
 465+END;
 466+ $wgOut->addHTML( $text );
 467+ return true;
 468+ }
 469+
 470+ if ( $category == "" ) {
 471+ // No category was specified - show the list of
 472+ // categories with a page schema defined.
 473+ $text = self::showLinksToCategories();
 474+ $wgOut->addHTML( $text );
 475+ return true;
 476+ }
 477+
 478+ // We have a category - show a form.
 479+ // See if a page schema has already been defined for this category.
 480+ $title = Title::newFromText( $category, NS_CATEGORY );
 481+ $pageId = $title->getArticleID();
 482+ $dbr = wfGetDB( DB_SLAVE );
 483+ $res = $dbr->select( 'page_props',
 484+ array(
 485+ 'pp_page',
 486+ 'pp_propname',
 487+ 'pp_value'
 488+ ),
 489+ array(
 490+ 'pp_page' => $pageId,
 491+ )
 492+ );
 493+
 494+ $row = $dbr->fetchRow( $res );
 495+ if ( $row == null && !$title->exists() ) {
 496+ // Category doesn't exist.
 497+ $text = '<p>' . wfMsg( 'ps-page-desc-cat-not-exist' ) . '</p>';
 498+ $text .= self::printForm();
 499+ } elseif ( ( $row[1] != 'PageSchema' ) || ( $row[2] == null ) ) {
 500+ // Category exists, but has no page schema.
 501+ $text = '<p>' . wfMsg( 'ps-page-desc-ps-not-exist' ) . '</p>';
 502+ $text .= self::printForm();
 503+ } else {
 504+ // It's a category with an existing page schema -
 505+ // populate the form with its values.
 506+ $pageSchemaObj = new PSSchema( $category );
 507+ $pageXMLstr = $row[2];
 508+ $pageXML = simplexml_load_string( $pageXMLstr );
 509+ $text = self::printForm( $pageSchemaObj, $pageXML );
 510+ }
 511+ $wgOut->addHTML( $text );
550512 return true;
551513 }
552514 }

Status & tagging log