r91050 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91049‎ | r91050 | r91051 >
Date:04:14, 29 June 2011
Author:yaron
Status:ok (Comments)
Tags:
Comment:
Follow-up to r91021 - re-added setting of title as hidden field, this time corretly, thanks to code from Nikerabbit; also added some long-overdue fixes to formatting and class structure
Modified paths:
  • /trunk/extensions/SemanticForms/specials/SF_CreateForm.php (modified) (history)
  • /trunk/extensions/SemanticForms/specials/SF_CreateTemplate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/specials/SF_CreateForm.php
@@ -47,29 +47,30 @@
4848 }
4949 }
5050
51 -function doSpecialCreateForm() {
52 - global $wgOut, $wgRequest, $wgUser, $sfgScriptPath;
53 - $db = wfGetDB( DB_SLAVE );
 51+ function doSpecialCreateForm() {
 52+ global $wgOut, $wgRequest, $wgUser, $sfgScriptPath;
 53+ $db = wfGetDB( DB_SLAVE );
5454
55 - SFUtils::loadMessages();
 55+ SFUtils::loadMessages();
5656
57 - // Create Javascript to populate fields to let the user input
58 - // parameters for the field, based on the input type selected in the
59 - // dropdown.
60 - $skin = $wgUser->getSkin();
61 - $url = $skin->makeSpecialUrl( 'CreateForm', "showinputtypeoptions=' + this.val() + '&formfield=' + this.attr('formfieldid') + '" );
62 - foreach ( $wgRequest->getValues() as $param => $value ) {
63 - $url .= '&params[' . Xml::escapeJsString( $param ) . ']=' . Xml::escapeJsString( $value );
64 - }
 57+ // Create Javascript to populate fields to let the user input
 58+ // parameters for the field, based on the input type selected
 59+ // in the dropdown.
 60+ $skin = $wgUser->getSkin();
 61+ $url = $skin->makeSpecialUrl( 'CreateForm', "showinputtypeoptions=' + this.val() + '&formfield=' + this.attr('formfieldid') + '" );
 62+ foreach ( $wgRequest->getValues() as $param => $value ) {
 63+ $url .= '&params[' . Xml::escapeJsString( $param ) . ']=' . Xml::escapeJsString( $value );
 64+ }
6565
66 - // Only add 'collapsible' ability if the ResourceLoader exists, i.e.
67 - // for MW 1.17 - adding backwards compatibility doesn't seem worth
68 - // it for this relatively minor piece of functionality.
69 - if ( method_exists( $wgOut, 'addModules' ) ) {
70 - $wgOut->addModules( 'ext.semanticforms.collapsible' );
71 - }
 66+ // Only add 'collapsible' ability if the ResourceLoader exists,
 67+ // i.e. for MW 1.17 - adding backwards compatibility doesn't
 68+ // seem worth it for this relatively minor piece of
 69+ // functionality.
 70+ if ( method_exists( $wgOut, 'addModules' ) ) {
 71+ $wgOut->addModules( 'ext.semanticforms.collapsible' );
 72+ }
7273
73 - $wgOut->addScript("<script>
 74+ $wgOut->addScript("<script>
7475 jQuery.fn.displayInputParams = function() {
7576 inputParamsDiv = this.closest('.formField').find('.otherInputParams');
7677 jQuery.ajax({
@@ -88,200 +89,203 @@
8990 </script>");
9091
9192
92 - // Get the names of all templates on this site.
93 - $all_templates = array();
94 - $res = $db->select(
95 - 'page',
96 - 'page_title',
97 - array( 'page_namespace' => NS_TEMPLATE, 'page_is_redirect' => 0 ),
98 - array( 'ORDER BY' => 'page_title' )
99 - );
 93+ // Get the names of all templates on this site.
 94+ $all_templates = array();
 95+ $res = $db->select(
 96+ 'page',
 97+ 'page_title',
 98+ array( 'page_namespace' => NS_TEMPLATE, 'page_is_redirect' => 0 ),
 99+ array( 'ORDER BY' => 'page_title' )
 100+ );
100101
101 - if ( $db->numRows( $res ) > 0 ) {
102 - while ( $row = $db->fetchRow( $res ) ) {
103 - $template_name = str_replace( '_', ' ', $row[0] );
104 - $all_templates[] = $template_name;
 102+ if ( $db->numRows( $res ) > 0 ) {
 103+ while ( $row = $db->fetchRow( $res ) ) {
 104+ $template_name = str_replace( '_', ' ', $row[0] );
 105+ $all_templates[] = $template_name;
 106+ }
105107 }
106 - }
107108
108 - $form_templates = array();
109 - $i = 1;
110 - $deleted_template_loc = null;
 109+ $form_templates = array();
 110+ $i = 1;
 111+ $deleted_template_loc = null;
111112
112 - # handle inputs
113 - $form_name = $wgRequest->getVal( 'form_name' );
114 - foreach ( $wgRequest->getValues() as $var => $val ) {
115 - # ignore variables that are not of the right form
116 - if ( strpos( $var, "_" ) != false ) {
117 - # get the template declarations and work from there
118 - list ( $action, $id ) = explode( "_", $var, 2 );
119 - if ( $action == "template" ) {
120 - # if the button was pressed to remove this
121 - # template, just don't add it to the array
122 - if ( $wgRequest->getVal( "del_$id" ) != null ) {
123 - $deleted_template_loc = $id;
124 - } else {
125 - $form_template = SFTemplateInForm::create( $val,
126 - $wgRequest->getVal( "label_$id" ),
127 - $wgRequest->getVal( "allow_multiple_$id" ) );
128 - $form_templates[] = $form_template;
 113+ # handle inputs
 114+ $form_name = $wgRequest->getVal( 'form_name' );
 115+ foreach ( $wgRequest->getValues() as $var => $val ) {
 116+ # ignore variables that are not of the right form
 117+ if ( strpos( $var, "_" ) != false ) {
 118+ # get the template declarations and work from there
 119+ list ( $action, $id ) = explode( "_", $var, 2 );
 120+ if ( $action == "template" ) {
 121+ // If the button was pressed to remove
 122+ // this template, just don't add it to
 123+ // the array.
 124+ if ( $wgRequest->getVal( "del_$id" ) != null ) {
 125+ $deleted_template_loc = $id;
 126+ } else {
 127+ $form_template = SFTemplateInForm::create( $val,
 128+ $wgRequest->getVal( "label_$id" ),
 129+ $wgRequest->getVal( "allow_multiple_$id" ) );
 130+ $form_templates[] = $form_template;
 131+ }
129132 }
130133 }
131134 }
132 - }
133 - if ( $wgRequest->getVal( 'add_field' ) != null ) {
134 - $form_template = SFTemplateInForm::create( $wgRequest->getVal( 'new_template' ), "", false );
135 - $new_template_loc = $wgRequest->getVal( 'before_template' );
136 - if ( $new_template_loc === null ) { $new_template_loc = 0; }
137 - # hack - array_splice() doesn't work for objects, so we have to
138 - # first insert a stub element into the array, then replace that
139 - # with the actual object
140 - array_splice( $form_templates, $new_template_loc, 0, "stub" );
141 - $form_templates[$new_template_loc] = $form_template;
142 - } else {
143 - $new_template_loc = null;
144 - }
 135+ if ( $wgRequest->getVal( 'add_field' ) != null ) {
 136+ $form_template = SFTemplateInForm::create( $wgRequest->getVal( 'new_template' ), "", false );
 137+ $new_template_loc = $wgRequest->getVal( 'before_template' );
 138+ if ( $new_template_loc === null ) { $new_template_loc = 0; }
 139+ // @HACK - array_splice() doesn't work for objects, so
 140+ // we have to first insert a stub element into the
 141+ // array, then replace that with the actual object.
 142+ array_splice( $form_templates, $new_template_loc, 0, "stub" );
 143+ $form_templates[$new_template_loc] = $form_template;
 144+ } else {
 145+ $new_template_loc = null;
 146+ }
145147
146 - // Now cycle through the templates and fields, modifying each one
147 - // per the query variables.
148 - foreach ( $form_templates as $i => $ft ) {
149 - foreach ( $ft->fields as $j => $field ) {
150 - // handle the change in indexing if a new template was
151 - // inserted before the end, or one was deleted
152 - $old_i = $i;
153 - if ( $new_template_loc != null ) {
154 - if ( $i > $new_template_loc ) {
155 - $old_i = $i - 1;
156 - } elseif ( $i == $new_template_loc ) {
157 - // it's the new template; it shouldn't
158 - // get any query-string data
159 - $old_i = - 1;
 148+ // Now cycle through the templates and fields, modifying each
 149+ // one per the query variables.
 150+ foreach ( $form_templates as $i => $ft ) {
 151+ foreach ( $ft->fields as $j => $field ) {
 152+ // handle the change in indexing if a new template was
 153+ // inserted before the end, or one was deleted
 154+ $old_i = $i;
 155+ if ( $new_template_loc != null ) {
 156+ if ( $i > $new_template_loc ) {
 157+ $old_i = $i - 1;
 158+ } elseif ( $i == $new_template_loc ) {
 159+ // it's the new template; it shouldn't
 160+ // get any query-string data
 161+ $old_i = - 1;
 162+ }
 163+ } elseif ( $deleted_template_loc != null ) {
 164+ if ( $i >= $deleted_template_loc ) {
 165+ $old_i = $i + 1;
 166+ }
160167 }
161 - } elseif ( $deleted_template_loc != null ) {
162 - if ( $i >= $deleted_template_loc ) {
163 - $old_i = $i + 1;
164 - }
165 - }
166 - foreach ( $wgRequest->getValues() as $key => $value ) {
167 - if ( ( $pos = strpos( $key, '_' . $old_i . '_' . $j ) ) != false ) {
168 - $paramName = substr( $key, 0, $pos );
169 - // Spaces got replaced by underlines
170 - // in the query.
171 - $paramName = str_replace( '_', ' ', $paramName );
172 - } else {
173 - continue;
174 - }
175 -
176 - if ( $paramName == 'label' ) {
177 - $field->template_field->label = $value;
178 - } elseif ( $paramName == 'input type' ) {
179 - $input_type = $wgRequest->getVal( "input_type_" . $old_i . "_" . $j );
180 - if ( $input_type == 'hidden' ) {
181 - $field->template_field->input_type = $input_type;
182 - $field->is_hidden = true;
183 - } elseif ( substr( $input_type, 0, 1 ) == '.' ) {
184 - // It's the default input type -
185 - // don't do anything.
 168+ foreach ( $wgRequest->getValues() as $key => $value ) {
 169+ if ( ( $pos = strpos( $key, '_' . $old_i . '_' . $j ) ) != false ) {
 170+ $paramName = substr( $key, 0, $pos );
 171+ // Spaces got replaced by
 172+ // underlines in the query.
 173+ $paramName = str_replace( '_', ' ', $paramName );
186174 } else {
187 - $field->template_field->input_type = $input_type;
 175+ continue;
188176 }
189 - } else {
190 - if ( ! empty( $value ) ) {
191 - if ( $value == 'on' ) {
192 - $value = true;
 177+
 178+ if ( $paramName == 'label' ) {
 179+ $field->template_field->label = $value;
 180+ } elseif ( $paramName == 'input type' ) {
 181+ $input_type = $wgRequest->getVal( "input_type_" . $old_i . "_" . $j );
 182+ if ( $input_type == 'hidden' ) {
 183+ $field->template_field->input_type = $input_type;
 184+ $field->is_hidden = true;
 185+ } elseif ( substr( $input_type, 0, 1 ) == '.' ) {
 186+ // It's the default input type -
 187+ // don't do anything.
 188+ } else {
 189+ $field->template_field->input_type = $input_type;
193190 }
194 - $field->field_args[$paramName] = $value;
 191+ } else {
 192+ if ( ! empty( $value ) ) {
 193+ if ( $value == 'on' ) {
 194+ $value = true;
 195+ }
 196+ $field->field_args[$paramName] = $value;
 197+ }
195198 }
196199 }
197200 }
198201 }
199 - }
200 - $form = SFForm::create( $form_name, $form_templates );
 202+ $form = SFForm::create( $form_name, $form_templates );
201203
202 - // If a submit button was pressed, create the form-definition file,
203 - // then redirect.
204 - $save_page = $wgRequest->getCheck( 'wpSave' );
205 - $preview_page = $wgRequest->getCheck( 'wpPreview' );
206 - if ( $save_page || $preview_page ) {
207 - # validate form name
208 - if ( $form->form_name == "" ) {
209 - $form_name_error_str = wfMsg( 'sf_blank_error' );
210 - } else {
211 - # redirect to wiki interface
212 - $wgOut->setArticleBodyOnly( true );
213 - $title = Title::makeTitleSafe( SF_NS_FORM, $form->form_name );
214 - $full_text = $form->createMarkup();
215 - $text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null );
216 - $wgOut->addHTML( $text );
217 - return;
 204+ // If a submit button was pressed, create the form-definition
 205+ // file, then redirect.
 206+ $save_page = $wgRequest->getCheck( 'wpSave' );
 207+ $preview_page = $wgRequest->getCheck( 'wpPreview' );
 208+ if ( $save_page || $preview_page ) {
 209+ // Validate form name
 210+ if ( $form->form_name == "" ) {
 211+ $form_name_error_str = wfMsg( 'sf_blank_error' );
 212+ } else {
 213+ // Redirect to wiki interface
 214+ $wgOut->setArticleBodyOnly( true );
 215+ $title = Title::makeTitleSafe( SF_NS_FORM, $form->form_name );
 216+ $full_text = $form->createMarkup();
 217+ $text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null );
 218+ $wgOut->addHTML( $text );
 219+ return;
 220+ }
218221 }
219 - }
220222
221 - $text = ' <form action="" method="post">' . "\n";
222 - $text .= ' <p>' . wfMsg( 'sf_createform_nameinput' ) . ' ' . wfMsg( 'sf_createform_nameinputdesc' ) . ' <input size=25 name="form_name" value="' . $form_name . '">';
223 - if ( ! empty( $form_name_error_str ) )
224 - $text .= ' ' . Xml::element( 'font', array( 'color' => 'red' ), $form_name_error_str );
225 - $text .= "</p>\n";
 223+ $text = "\t" . '<form action="" method="post">' . "\n";
 224+ // Set 'title' field, in case there's no URL niceness
 225+ $text .= "\t" . Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
 226+ $text .= "\t<p>" . wfMsg( 'sf_createform_nameinput' ) . ' ' . wfMsg( 'sf_createform_nameinputdesc' ) . ' <input size=25 name="form_name" value="' . $form_name . '" />';
 227+ if ( ! empty( $form_name_error_str ) )
 228+ $text .= "\t" . Xml::element( 'font', array( 'color' => 'red' ), $form_name_error_str );
 229+ $text .= "</p>\n";
226230
227 - $text .= $form->creationHTML();
 231+ $text .= $form->creationHTML();
228232
229 - $text .= ' <p>' . wfMsg( 'sf_createform_addtemplate' ) . "\n";
 233+ $text .= "\t<p>" . wfMsg( 'sf_createform_addtemplate' ) . "\n";
230234
231 - $select_body = "";
232 - foreach ( $all_templates as $template ) {
233 - $select_body .= " " . Xml::element( 'option', array( 'value' => $template ), $template ) . "\n";
234 - }
235 - $text .= ' ' . Xml::tags( 'select', array( 'name' => 'new_template' ), $select_body ) . "\n";
236 - // if a template has already been added, show a dropdown letting the
237 - // user choose where in the list to add a new dropdown
238 - if ( count( $form_templates ) > 0 ) {
239 - $before_template_msg = wfMsg( 'sf_createform_beforetemplate' );
240 - $text .= $before_template_msg;
241235 $select_body = "";
242 - foreach ( $form_templates as $i => $ft ) {
243 - $select_body .= " " . Xml::element( 'option', array( 'value' => $i ), $ft->template_name ) . "\n";
 236+ foreach ( $all_templates as $template ) {
 237+ $select_body .= " " . Xml::element( 'option', array( 'value' => $template ), $template ) . "\n";
244238 }
245 - $final_index = count( $form_templates );
246 - $at_end_msg = wfMsg( 'sf_createform_atend' );
247 - $select_body .= ' ' . Xml::element( 'option', array( 'value' => $final_index, 'selected' => 'selected' ), $at_end_msg );
248 - $text .= Xml::tags( 'select', array( 'name' => 'before_template' ), $select_body ) . "\n";
249 - }
 239+ $text .= "\t" . Xml::tags( 'select', array( 'name' => 'new_template' ), $select_body ) . "\n";
 240+ // If a template has already been added, show a dropdown letting
 241+ // the user choose where in the list to add a new dropdown.
 242+ if ( count( $form_templates ) > 0 ) {
 243+ $before_template_msg = wfMsg( 'sf_createform_beforetemplate' );
 244+ $text .= $before_template_msg;
 245+ $select_body = "";
 246+ foreach ( $form_templates as $i => $ft ) {
 247+ $select_body .= "\t" . Xml::element( 'option', array( 'value' => $i ), $ft->template_name ) . "\n";
 248+ }
 249+ $final_index = count( $form_templates );
 250+ $at_end_msg = wfMsg( 'sf_createform_atend' );
 251+ $select_body .= "\t" . Xml::element( 'option', array( 'value' => $final_index, 'selected' => 'selected' ), $at_end_msg );
 252+ $text .= Xml::tags( 'select', array( 'name' => 'before_template' ), $select_body ) . "\n";
 253+ }
250254
251 - // disable 'save' and 'preview' buttons if user has not yet added any
252 - // templates
253 - $disabled_text = ( count( $form_templates ) == 0 ) ? "disabled" : "";
254 - $add_button_text = wfMsg( 'sf_createform_add' );
255 - $sk = $wgUser->getSkin();
256 - $create_template_link = SFUtils::linkForSpecialPage( $sk, 'CreateTemplate' );
257 - $text .= "\t" . Xml::element( 'input', array( 'type' => 'submit', 'name' => 'add_field', 'value' => $add_button_text ) );
258 - $text .= <<<END
 255+ // Disable 'save' and 'preview' buttons if user has not yet
 256+ // added any templates.
 257+ $disabled_text = ( count( $form_templates ) == 0 ) ? "disabled" : "";
 258+ $add_button_text = wfMsg( 'sf_createform_add' );
 259+ $sk = $wgUser->getSkin();
 260+ $create_template_link = SFUtils::linkForSpecialPage( $sk, 'CreateTemplate' );
 261+ $text .= "\t" . Xml::element( 'input', array( 'type' => 'submit', 'name' => 'add_field', 'value' => $add_button_text ) );
 262+ $text .= <<<END
259263 </p>
260264 <br />
261265
262266 END;
263 - $saveAttrs = array( 'type' => 'submit', 'id' => 'wpSave', 'name' => 'wpSave', 'value' => wfMsg( 'savearticle' ) );
264 - if ( count( $form_templates ) == 0 ) { $saveAttrs['disabled'] = 'disabled'; }
265 - $editButtonsText = "\t" . Xml::element( 'input', $saveAttrs ) . "\n";
266 - $previewAttrs = array( 'type' => 'submit', 'id' => 'wpPreview', 'name' => 'wpPreview', 'value' => wfMsg( 'preview' ) );
267 - if ( count( $form_templates ) == 0 ) { $previewAttrs['disabled'] = 'disabled'; }
268 - $editButtonsText .= "\t" . Xml::element( 'input', $previewAttrs ) . "\n";
269 - $text .= "\t" . Xml::tags( 'div', array( 'class' => 'editButtons' ),
270 - Xml::tags( 'p', array(), $editButtonsText ) . "\n" ) . "\n";
271 - // explanatory message if buttons are disabled because no templates
272 - // have been added
273 - if ( count( $form_templates ) == 0 ) {
274 - $text .= "\t" . Xml::element( 'p', null, "(" . wfMsg( 'sf_createtemplate_addtemplatebeforesave' ) . ")" );
275 - }
276 - $text .= <<<END
 267+ $saveAttrs = array( 'type' => 'submit', 'id' => 'wpSave', 'name' => 'wpSave', 'value' => wfMsg( 'savearticle' ) );
 268+ if ( count( $form_templates ) == 0 ) { $saveAttrs['disabled'] = 'disabled'; }
 269+ $editButtonsText = "\t" . Xml::element( 'input', $saveAttrs ) . "\n";
 270+ $previewAttrs = array( 'type' => 'submit', 'id' => 'wpPreview', 'name' => 'wpPreview', 'value' => wfMsg( 'preview' ) );
 271+ if ( count( $form_templates ) == 0 ) { $previewAttrs['disabled'] = 'disabled'; }
 272+ $editButtonsText .= "\t" . Xml::element( 'input', $previewAttrs ) . "\n";
 273+ $text .= "\t" . Xml::tags( 'div', array( 'class' => 'editButtons' ),
 274+ Xml::tags( 'p', array(), $editButtonsText ) . "\n" ) . "\n";
 275+ // Explanatory message if buttons are disabled because no
 276+ // templates have been added.
 277+ if ( count( $form_templates ) == 0 ) {
 278+ $text .= "\t" . Xml::element( 'p', null, "(" . wfMsg( 'sf_createtemplate_addtemplatebeforesave' ) . ")" );
 279+ }
 280+ $text .= <<<END
277281 </form>
278282 <hr /><br />
279283
280284 END;
281 - $text .= "\t" . Xml::tags( 'p', null, $create_template_link . '.' );
 285+ $text .= "\t" . Xml::tags( 'p', null, $create_template_link . '.' );
282286
283 - $wgOut->addExtensionStyle( $sfgScriptPath . "/skins/SemanticForms.css" );
284 - $wgOut->addHTML( $text );
285 -}
 287+ $wgOut->addExtensionStyle( $sfgScriptPath . "/skins/SemanticForms.css" );
 288+ $wgOut->addHTML( $text );
 289+ }
286290
287291 /**
288292 * Prints an input for a form-field parameter.
Index: trunk/extensions/SemanticForms/specials/SF_CreateTemplate.php
@@ -25,7 +25,7 @@
2626
2727 public function execute( $query ) {
2828 $this->setHeaders();
29 - self::printCreateTemplateForm();
 29+ $this->printCreateTemplateForm();
3030 }
3131
3232 public static function getAllPropertyNames() {
@@ -149,7 +149,7 @@
150150 $wgOut->addScript( $jsText );
151151 }
152152
153 - static function printCreateTemplateForm() {
 153+ function printCreateTemplateForm() {
154154 global $wgOut, $wgRequest, $wgUser, $sfgScriptPath;
155155
156156 SFUtils::loadMessages();
@@ -192,6 +192,8 @@
193193 }
194194
195195 $text .= ' <form id="createTemplateForm" action="" method="post">' . "\n";
 196+ // Set 'title' field, in case there's no URL niceness
 197+ $text .= "\t" . Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
196198 $text .= "\t<p id=\"template_name_p\">" . wfMsg( 'sf_createtemplate_namelabel' ) . ' <input size="25" id="template_name" name="template_name" /></p>' . "\n";
197199 $text .= "\t<p>" . wfMsg( 'sf_createtemplate_categorylabel' ) . ' <input size="25" name="category" /></p>' . "\n";
198200 $text .= "\t<fieldset>\n";

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r91021Removed setting of hidden field for 'title' value - no longer necessary (?), ...yaron21:59, 28 June 2011

Comments

#Comment by Nikerabbit (talk | contribs)   07:25, 29 June 2011

Would have been easier to review if different changes were split into two commits.

#Comment by Yaron Koren (talk | contribs)   05:20, 3 July 2011

Thanks for reviewing this, and thanks also for the comment - I never know whether to split up commits or try to keep them together.

Status & tagging log