r82772 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82771‎ | r82772 | r82773 >
Date:00:37, 25 February 2011
Author:yaron
Status:deferred
Tags:
Comment:
Turned doSpecialCreateTemplate() function into a class method, printCreateTemplateForm(); replaced some hardcoded HTML with calls to the Xml class
Modified paths:
  • /trunk/extensions/SemanticForms/specials/SF_CreateTemplate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/specials/SF_CreateTemplate.php
@@ -24,7 +24,7 @@
2525
2626 public function execute( $query ) {
2727 $this->setHeaders();
28 - doSpecialCreateTemplate();
 28+ self::printCreateTemplateForm();
2929 }
3030
3131 public static function getAllPropertyNames() {
@@ -52,29 +52,32 @@
5353 }
5454
5555 public static function printPropertiesDropdown( $all_properties, $id, $selected_property ) {
56 - $dropdown_str = "<select name=\"semantic_property_$id\">\n";
57 - $dropdown_str .= "<option value=\"\"></option>\n";
58 -
 56+ $selectBody = "<option value=\"\"></option>\n";
5957 foreach ( $all_properties as $prop_name ) {
60 - $selected = ( $selected_property == $prop_name ) ? "selected" : "";
61 - $dropdown_str .= "<option value=\"$prop_name\" $selected>$prop_name</option>\n";
 58+ $optionAttrs = array( 'value' => $prop_name );
 59+ if ( $selected_property == $prop_name ) { $optionAttrs['selected'] = 'selected'; }
 60+ $selectBody .= Xml::element( 'option', $optionAttrs, $prop_name ) . "\n";
6261 }
63 -
64 - $dropdown_str .= "</select>\n";
65 -
66 - return $dropdown_str;
 62+ return Xml::tags( 'select', array( 'name' => "semantic_property_$id" ), $selectBody ) . "\n";
6763 }
6864
6965 public static function printFieldEntryBox( $id, $f, $all_properties ) {
7066 SFUtils::loadMessages();
71 - $dropdown_html = SFCreateTemplate::printPropertiesDropdown( $all_properties, $id, $f->semantic_property );
7267
73 - $text = ' <div class="fieldBox">' . "\n";
74 - $text .= ' <p>' . wfMsg( 'sf_createtemplate_fieldname' ) . ' <input size="15" name="name_' . $id . '" value="' . $f->field_name . '">' . "\n";
75 - $text .= ' ' . wfMsg( 'sf_createtemplate_displaylabel' ) . ' <input size="15" name="label_' . $id . '" value="' . $f->label . '">' . "\n";
76 - $text .= ' ' . wfMsg( 'sf_createtemplate_semanticproperty' ) . ' ' . $dropdown_html . "</p>\n";
 68+ $text = "\t" . '<div class="fieldBox">' . "\n";
 69+ $text .= "\t<p>" . wfMsg( 'sf_createtemplate_fieldname' ) . ' ' .
 70+ Xml::element( 'input',
 71+ array( 'size' => '15', 'name' => 'name_' . $id, 'value' => $f->field_name ), null
 72+ ) . "\n";
 73+ $text .= "\t" . wfMsg( 'sf_createtemplate_displaylabel' ) . ' ' .
 74+ Xml::element( 'input',
 75+ array( 'size' => '15', 'name' => 'label_' . $id, 'value' => $f->label ), null
 76+ ) . "\n";
 77+
 78+ $dropdown_html = self::printPropertiesDropdown( $all_properties, $id, $f->semantic_property );
 79+ $text .= "\t" . wfMsg( 'sf_createtemplate_semanticproperty' ) . ' ' . $dropdown_html . "</p>\n";
7780 $checked_str = ( $f->is_list ) ? " checked" : "";
78 - $text .= ' <p><input type="checkbox" name="is_list_' . $id . '"' . $checked_str . '> ' . wfMsg( 'sf_createtemplate_fieldislist' ) . "\n";
 81+ $text .= "\t<p>" . '<input type="checkbox" name="is_list_' . $id . '"' . $checked_str . '> ' . wfMsg( 'sf_createtemplate_fieldislist' ) . "\n";
7982
8083 if ( $id != "new" ) {
8184 $text .= ' &#160;&#160;<input name="del_' . $id . '" type="submit" value="' . wfMsg( 'sf_createtemplate_deletefield' ) . '">' . "\n";
@@ -87,94 +90,104 @@
8891 END;
8992 return $text;
9093 }
91 -
92 -}
9394
94 -function doSpecialCreateTemplate() {
95 - global $wgOut, $wgRequest, $wgUser, $sfgScriptPath, $wgContLang;
 95+ static function printCreateTemplateForm() {
 96+ global $wgOut, $wgRequest, $wgUser, $sfgScriptPath, $wgContLang;
9697
97 - SFUtils::loadMessages();
 98+ SFUtils::loadMessages();
9899
99 - $all_properties = SFCreateTemplate::getAllPropertyNames();
100 -
101 - $template_name = $wgRequest->getVal( 'template_name' );
102 - $template_name_error_str = "";
103 - $category = $wgRequest->getVal( 'category' );
104 - $cur_id = 1;
105 - $fields = array();
106 - # cycle through the query values, setting the appropriate local variables
107 - foreach ( $wgRequest->getValues() as $var => $val ) {
108 - $var_elements = explode( "_", $var );
109 - // we only care about query variables of the form "a_b"
110 - if ( count( $var_elements ) != 2 )
111 - continue;
112 - list ( $field_field, $old_id ) = $var_elements;
113 - if ( $field_field == "name" ) {
114 - if ( $old_id != "new" || ( $old_id == "new" && $val != "" ) ) {
115 - if ( $wgRequest->getVal( 'del_' . $old_id ) != '' ) {
116 - # do nothing - this field won't get added to the new list
117 - } else {
118 - $field = SFTemplateField::create( $val, $wgRequest->getVal( 'label_' . $old_id ) );
119 - $field->semantic_property = $wgRequest->getVal( 'semantic_property_' . $old_id );
120 - $field->is_list = $wgRequest->getCheck( 'is_list_' . $old_id );
121 - $fields[] = $field;
 100+ $all_properties = self::getAllPropertyNames();
 101+
 102+ $template_name = $wgRequest->getVal( 'template_name' );
 103+ $template_name_error_str = "";
 104+ $category = $wgRequest->getVal( 'category' );
 105+ $cur_id = 1;
 106+ $fields = array();
 107+ # cycle through the query values, setting the appropriate local variables
 108+ foreach ( $wgRequest->getValues() as $var => $val ) {
 109+ $var_elements = explode( "_", $var );
 110+ // we only care about query variables of the form "a_b"
 111+ if ( count( $var_elements ) != 2 )
 112+ continue;
 113+ list ( $field_field, $old_id ) = $var_elements;
 114+ if ( $field_field == "name" ) {
 115+ if ( $old_id != "new" || ( $old_id == "new" && $val != "" ) ) {
 116+ if ( $wgRequest->getVal( 'del_' . $old_id ) != '' ) {
 117+ // Do nothing - this field won't get added to the new list
 118+ } else {
 119+ $field = SFTemplateField::create( $val, $wgRequest->getVal( 'label_' . $old_id ) );
 120+ $field->semantic_property = $wgRequest->getVal( 'semantic_property_' . $old_id );
 121+ $field->is_list = $wgRequest->getCheck( 'is_list_' . $old_id );
 122+ $fields[] = $field;
 123+ }
122124 }
123125 }
124126 }
125 - }
126 - $aggregating_property = $wgRequest->getVal( 'semantic_property_aggregation' );
127 - $aggregation_label = $wgRequest->getVal( 'aggregation_label' );
128 - $template_format = $wgRequest->getVal( 'template_format' );
 127+ $aggregating_property = $wgRequest->getVal( 'semantic_property_aggregation' );
 128+ $aggregation_label = $wgRequest->getVal( 'aggregation_label' );
 129+ $template_format = $wgRequest->getVal( 'template_format' );
129130
130 - $text = "";
131 - $save_button_text = wfMsg( 'savearticle' );
132 - $preview_button_text = wfMsg( 'preview' );
133 - $save_page = $wgRequest->getCheck( 'wpSave' );
134 - $preview_page = $wgRequest->getCheck( 'wpPreview' );
135 - if ( $save_page || $preview_page ) {
136 - # validate template name
137 - if ( $template_name == '' ) {
138 - $template_name_error_str = wfMsg( 'sf_blank_error' );
139 - } else {
140 - // redirect to wiki interface
141 - $wgOut->setArticleBodyOnly( true );
142 - $title = Title::makeTitleSafe( NS_TEMPLATE, $template_name );
143 - $full_text = SFTemplateField::createTemplateText( $template_name, $fields, $category, $aggregating_property, $aggregation_label, $template_format );
144 - $text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null );
145 - $wgOut->addHTML( $text );
146 - return;
 131+ $text = "";
 132+ $save_button_text = wfMsg( 'savearticle' );
 133+ $preview_button_text = wfMsg( 'preview' );
 134+ $save_page = $wgRequest->getCheck( 'wpSave' );
 135+ $preview_page = $wgRequest->getCheck( 'wpPreview' );
 136+ if ( $save_page || $preview_page ) {
 137+ # validate template name
 138+ if ( $template_name == '' ) {
 139+ $template_name_error_str = wfMsg( 'sf_blank_error' );
 140+ } else {
 141+ // redirect to wiki interface
 142+ $wgOut->setArticleBodyOnly( true );
 143+ $title = Title::makeTitleSafe( NS_TEMPLATE, $template_name );
 144+ $full_text = SFTemplateField::createTemplateText( $template_name, $fields, $category, $aggregating_property, $aggregation_label, $template_format );
 145+ $text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null );
 146+ $wgOut->addHTML( $text );
 147+ return;
 148+ }
147149 }
148 - }
149150
150 - $text .= ' <form action="" method="post">' . "\n";
151 - // set 'title' field, in case there's no URL niceness
152 - $mw_namespace_labels = $wgContLang->getNamespaces();
153 - $special_namespace = $mw_namespace_labels[NS_SPECIAL];
154 - $text .= ' <input type="hidden" name="title" value="' . $special_namespace . ':CreateTemplate">' . "\n";
155 - $text .= ' <p>' . wfMsg( 'sf_createtemplate_namelabel' ) . ' <input size="25" name="template_name" value="' . $template_name . '"> <font color="red">' . $template_name_error_str . '</font></p>' . "\n";
156 - $text .= ' <p>' . wfMsg( 'sf_createtemplate_categorylabel' ) . ' <input size="25" name="category" value="' . $category . '"></p>' . "\n";
157 - $text .= " <fieldset>\n";
158 - $text .= ' ' . Xml::element( 'legend', null, wfMsg( 'sf_createtemplate_templatefields' ) ) . "\n";
159 - $text .= ' ' . Xml::element( 'p', null, wfMsg( 'sf_createtemplate_fieldsdesc' ) ) . "\n";
 151+ $text .= ' <form action="" method="post">' . "\n";
 152+ // set 'title' field, in case there's no URL niceness
 153+ $mw_namespace_labels = $wgContLang->getNamespaces();
 154+ $special_namespace = $mw_namespace_labels[NS_SPECIAL];
 155+ $text .= "\t" . '<input type="hidden" name="title" value="' . $special_namespace . ':CreateTemplate">' . "\n";
 156+ $text .= "\t<p>" . wfMsg( 'sf_createtemplate_namelabel' ) . ' <input size="25" name="template_name" value="' . $template_name . '"> <font color="red">' . $template_name_error_str . '</font></p>' . "\n";
 157+ $text .= "\t<p>" . wfMsg( 'sf_createtemplate_categorylabel' ) . ' <input size="25" name="category" value="' . $category . '"></p>' . "\n";
 158+ $text .= "\t<fieldset>\n";
 159+ $text .= "\t" . Xml::element( 'legend', null, wfMsg( 'sf_createtemplate_templatefields' ) ) . "\n";
 160+ $text .= "\t" . Xml::element( 'p', null, wfMsg( 'sf_createtemplate_fieldsdesc' ) ) . "\n";
160161
161 - foreach ( $fields as $i => $field ) {
162 - $text .= SFCreateTemplate::printFieldEntryBox( $i + 1, $field, $all_properties );
163 - }
164 - $new_field = new SFTemplateField();
165 - $text .= SFCreateTemplate::printFieldEntryBox( "new", $new_field, $all_properties );
 162+ foreach ( $fields as $i => $field ) {
 163+ $text .= self::printFieldEntryBox( $i + 1, $field, $all_properties );
 164+ }
 165+ $new_field = new SFTemplateField();
 166+ $text .= self::printFieldEntryBox( "new", $new_field, $all_properties );
166167
167 - $text .= ' <p><input type="submit" value="' . wfMsg( 'sf_createtemplate_addfield' ) . '"></p>' . "\n";
168 - $text .= " </fieldset>\n";
169 - $text .= " <fieldset>\n";
170 - $text .= ' ' . Xml::element( 'legend', null, wfMsg( 'sf_createtemplate_aggregation' ) ) . "\n";
171 - $text .= ' ' . Xml::element( 'p', null, wfMsg( 'sf_createtemplate_aggregationdesc' ) ) . "\n";
172 - $text .= ' <p>' . wfMsg( 'sf_createtemplate_semanticproperty' ) . " " . SFCreateTemplate::printPropertiesDropdown( $all_properties, "aggregation", $aggregating_property ) . "</p>\n";
173 - $text .= ' <p>' . wfMsg( 'sf_createtemplate_aggregationlabel' ) . ' <input size="25" name="aggregation_label" value="' . $aggregation_label . '"></p>' . "\n";
174 - $text .= " </fieldset>\n";
175 - $text .= ' <p>' . wfMsg( 'sf_createtemplate_outputformat' ) . "\n";
176 - $text .= ' <input type="radio" name="template_format" checked value="standard" />' . wfMsg( 'sf_createtemplate_standardformat' ) . "\n";
177 - $text .= ' <input type="radio" name="template_format" value="infobox" />' . wfMsg( 'sf_createtemplate_infoboxformat' ) . "</p>\n";
178 - $text .= <<<END
 168+ $text .= "\t<p>" . Xml::element( 'input',
 169+ array( 'type' => 'submit', 'value' => wfMsg( 'sf_createtemplate_addfield' ) ), null ) . "</p>\n";
 170+ $text .= "\t</fieldset>\n";
 171+ $text .= "\t<fieldset>\n";
 172+ $text .= "\t" . Xml::element( 'legend', null, wfMsg( 'sf_createtemplate_aggregation' ) ) . "\n";
 173+ $text .= "\t" . Xml::element( 'p', null, wfMsg( 'sf_createtemplate_aggregationdesc' ) ) . "\n";
 174+ $text .= "\t<p>" . wfMsg( 'sf_createtemplate_semanticproperty' ) . ' ' .
 175+ self::printPropertiesDropdown( $all_properties, "aggregation", $aggregating_property ) . "</p>\n";
 176+ $text .= "\t<p>" . wfMsg( 'sf_createtemplate_aggregationlabel' ) . ' ' .
 177+ Xml::element( 'input',
 178+ array( 'size' => '25', 'name' => 'aggregation_label', 'value' => $aggregation_label ), null ) .
 179+ "</p>\n";
 180+ $text .= "\t</fieldset>\n";
 181+ $text .= "\t<p>" . wfMsg( 'sf_createtemplate_outputformat' ) . "\n";
 182+ $text .= "\t" . Xml::element( 'input', array(
 183+ 'type' => 'radio',
 184+ 'name' => 'template_format',
 185+ 'checked' => 'checked',
 186+ 'value' => 'standard'
 187+ ), null ) . ' ' . wfMsg( 'sf_createtemplate_standardformat' ) . "\n";
 188+ $text .= "\t" . Xml::element( 'input',
 189+ array( 'type' => 'radio', 'name' => 'template_format', 'value' => 'infobox'), null ) .
 190+ ' ' . wfMsg( 'sf_createtemplate_infoboxformat' ) . "</p>\n";
 191+ $text .= <<<END
179192 <div class="editButtons">
180193 <input type="submit" id="wpSave" name="wpSave" value="$save_button_text" />
181194 <input type="submit" id="wpPreview" name="wpPreview" value="$preview_button_text" />
@@ -182,11 +195,13 @@
183196 </form>
184197
185198 END;
186 - $sk = $wgUser->getSkin();
187 - $create_property_link = SFUtils::linkForSpecialPage( $sk, 'CreateProperty' );
188 - $text .= " <br /><hr /><br />\n";
189 - $text .= " " . Xml::tags( 'p', null, $create_property_link . '.' ) . "\n";
 199+ $sk = $wgUser->getSkin();
 200+ $create_property_link = SFUtils::linkForSpecialPage( $sk, 'CreateProperty' );
 201+ $text .= "\t<br /><hr /><br />\n";
 202+ $text .= "\t" . Xml::tags( 'p', null, $create_property_link . '.' ) . "\n";
190203
191 - $wgOut->addExtensionStyle( $sfgScriptPath . "/skins/SemanticForms.css" );
192 - $wgOut->addHTML( $text );
 204+ $wgOut->addExtensionStyle( $sfgScriptPath . "/skins/SemanticForms.css" );
 205+ $wgOut->addHTML( $text );
 206+ }
 207+
193208 }