r96900 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96899‎ | r96900 | r96901 >
Date:20:46, 12 September 2011
Author:yaron
Status:deferred
Tags:
Comment:
Rewrote some of the Page Schemas-interfacing code: names and structure of the hooks changed
Modified paths:
  • /trunk/extensions/SemanticDrilldown/SemanticDrilldown.php (modified) (history)
  • /trunk/extensions/SemanticDrilldown/includes/SD_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticDrilldown/includes/SD_Utils.php
@@ -35,136 +35,110 @@
3636 return true;
3737 }
3838
39 - public static function getFilledHtmlTextForPS( $pageSchemaObj, &$text_extensions ){
40 - $text = "";
41 - $name_label = wfMsg( 'sd_createfilter_name' );
42 - $property_label = wfMsg( 'sd_createfilter_property' );
43 - $label_label = wfMsg( 'sd_createfilter_label' );
44 - $values_from_property_label = wfMsg( 'sd_createfilter_usepropertyvalues' );
45 - $values_from_category_label = wfMsg( 'sd_createfilter_usecategoryvalues' );
46 - $date_values_label = wfMsg( 'sd_createfilter_usedatevalues' );
47 - $enter_values_label = wfMsg( 'sd_createfilter_entervalues' );
 39+ public static function getFieldHTMLForPS( $field, &$text_extensions ){
 40+ //$property_label = wfMsg( 'sd_createfilter_property' );
 41+ //$label_label = wfMsg( 'sd_createfilter_label' );
4842 // need both label and value, in case user's language is different
4943 // from wiki's
50 - $year_label = wfMsg( 'sd_filter_year' );
51 - $year_value = wfMsgForContent( 'sd_filter_year' );
52 - $month_label = wfMsg( 'sd_filter_month' );
53 - $month_value = wfMsgForContent( 'sd_filter_month' );
54 - $input_type_label = wfMsg( 'sd_createfilter_inputtype' );
55 - $values_list_label = wfMsg( 'sd_createfilter_listofvalues' );
56 - // same as for time values
57 - $combo_box_label = wfMsg( 'sd_filter_combobox' );
58 - $combo_box_value = wfMsgForContent( 'sd_filter_combobox' );
59 - $date_range_label = wfMsg( 'sd_filter_daterange' );
60 - $date_range_value = wfMsgForContent( 'sd_filter_daterange' );
61 - $require_filter_label = wfMsg( 'sd_createfilter_requirefilter' );
 44+ //$require_filter_label = wfMsg( 'sd_createfilter_requirefilter' );
6245
63 - $template_fields = array();
64 - $html_text = "";
65 - $template_all = $pageSchemaObj->getTemplates();
66 - $html_text_array = array();
67 - foreach ( $template_all as $template ) {
68 - $field_all = $template->getFields();
69 - $field_count = 0; //counts the number of fields
70 - foreach( $field_all as $field ) {
71 - $html_text .= '<fieldset style="background: #FDD;"><legend>Filter</legend>';
72 - $field_count++;
73 - $sd_array = $field->getObject('semanticdrilldown_Filter');//this returns an array with property values filled
74 - $filter_array = $sd_array['sd'];
75 - $html_text .= '<p>'.$name_label.' <input size="25" name="sd_filter_name_starter" value="'.$filter_array['Label'].'" ></p>';
76 - $html_text .= '<p><input type="radio" name="sd_values_source_starter" checked value="property">'.
77 - $values_from_property_label.'
78 - </p>';
79 - $categories = SDUtils::getTopLevelCategories();
80 - $option_html_text = "";
81 - foreach ( $categories as $category ) {
82 - $category = str_replace( '_', ' ', $category );
83 - $option_html_text .= " <option>$category</option>\n";
84 - }
85 - if( $filter_array['ValuesFromCategory'] != null){
86 - $html_text .= '<p><input type="radio" name="sd_values_source_starter" checked value="category">'.
87 - $values_from_category_label.'
88 - <select id="category_dropdown" name="sd_category_name_starter">';
89 - $option_html_text = "";
90 - foreach ( $categories as $category ) {
91 - $category = str_replace( '_', ' ', $category );
92 - if( $category == $filter_array["ValuesFromCategory"]) {
93 - $option_html_text .= ' <option selected>'.$category.'</option>\n';
94 - }else{
95 - $option_html_text .= ' <option>'.$category.'</option>\n';
96 - }
97 - }
98 - } else {
99 - $html_text .= '<p><input type="radio" name="sd_values_source_starter" value="category">'.
100 - $values_from_category_label.'
101 - <select id="category_dropdown" name="sd_category_name_starter">';
102 - }
103 - $html_text .= $option_html_text;
104 - $html_text .= '</select></p>';
 46+ if ( !is_null( $field ) ) {
 47+ $sd_array = $field->getObject('semanticdrilldown_Filter');
 48+ $filter_array = $sd_array['sd'];
 49+ } else {
 50+ $filter_array = array();
 51+ }
10552
106 - if ( $filter_array["TimePeriod"] != null ){
107 - $html_text .= '<p><input type="radio" name="sd_values_source_starter" checked value="dates">'.
108 - $date_values_label.'
109 - <select id="time_period_dropdown" name="sd_time_period_starter">';
 53+ $html_text = '<p>' . wfMsg( 'sd_createfilter_name' ) . ' <input size="25" name="sd_filter_name_num" value="' . $filter_array['Label'] . '" ></p>';
 54+ $html_text .= '<p><input type="radio" name="sd_values_source_num" checked value="property"> '.
 55+ wfMsg( 'sd_createfilter_usepropertyvalues' ) . "</p>\n";
 56+ $html_text .= "\t<p>\n";
 57+ $fromCategoryAttrs = array();
 58+ if ( $filter_array['ValuesFromCategory'] != null ) {
 59+ $selectedCategory = $filter_array['ValuesFromCategory'];
 60+ $fromCategoryAttrs['checked'] = true;
 61+ } else {
 62+ $selectedCategory = '';
 63+ }
 64+ $html_text .= Html::input( 'sd_values_source_num', 'category', 'radio', $fromCategoryAttrs ) . "\n";
 65+ $html_text .= "\t" . wfMsg( 'sd_createfilter_usecategoryvalues' ) . "\n";
 66+ $categories = SDUtils::getTopLevelCategories();
 67+ $option_html_text = "";
 68+ foreach ( $categories as $category ) {
 69+ $category = str_replace( '_', ' ', $category );
 70+ if ( $category == $selectedCategory) {
 71+ $option_html_text .= ' <option selected>' . $category . "</option>\n";
 72+ } else {
 73+ $option_html_text .= ' <option>' . $category . "</option>\n";
 74+ }
 75+ }
 76+ $html_text .= "\t" . '<select id="category_dropdown" name="sd_category_name_num">' . "\n";
 77+ $html_text .= $option_html_text;
 78+ $html_text .= '</select></p>';
11079
111 - if($filter_array['TimePeriod'] == $year_value ){
112 - $html_text .= '<option selected value="'.$year_value.'">'.$year_label.'</option>
113 - <option value="'.$month_value.'">'.$month_label.'</option>';
114 - }else{
115 - $html_text .= '<option value="'.$year_value.'">'.$year_label.'</option>
116 - <option selected value="'.$month_value.'">'.$month_label.'</option>';
117 - }
 80+ $html_text .= "\t<p>\n";
 81+ $dateRangesAttrs = array();
 82+ if ( array_key_exists( 'TimePeriod', $filter_array ) ) {
 83+ $dateRangesAttrs['checked'] = true;
 84+ }
 85+ $html_text .= "\t" . Html::input( 'sd_values_source_num', 'dates', 'radio', $dateRangesAttrs ) . "\n";
 86+ $html_text .= "\t" . wfMsg( 'sd_createfilter_usedatevalues' ) . "\n";
 87+ $html_text .= '<select id="time_period_dropdown" name="sd_time_period_num">' . "\n";
11888
 89+ $year_value = wfMsgForContent( 'sd_filter_year' );
 90+ $yearOptionAttrs = array( 'value' => $year_value );
 91+ $month_value = wfMsgForContent( 'sd_filter_month' );
 92+ $monthOptionAttrs = array( 'value' => $month_value );
 93+ if ( $filter_array["TimePeriod"] != null ) {
 94+ if ( $filter_array['TimePeriod'] == $year_value ) {
 95+ $yearOptionAttrs['selected'] = true;
 96+ } else {
 97+ $monthOptionAttrs['selected'] = true;
 98+ }
 99+ }
 100+ $html_text .= Html::element( 'option', $yearOptionAttrs, wfMsg( 'sd_filter_year' ) ) . "\n";
 101+ $html_text .= Html::element( 'option', $monthOptionAttrs, wfMsg( 'sd_filter_month' ) ) . "\n";
 102+ $html_text .= '</select>
 103+ </p>
 104+ <p>';
 105+ $manualSourceAttrs = array();
 106+ $filterValuesAttrs = array( 'size' => 40 );
 107+ if ( !is_null( $filter_array['Values'] ) ) {
 108+ $manualSourceAttrs['checked'] = true;
 109+ $values_array = $filter_array['Values'];
 110+ $filterValuesStr = implode( ', ', $values_array );
 111+ }
 112+ $html_text .= "\t" . Html::input( 'sd_values_source_num', 'manual', 'radio', $manualSourceAttrs ) . "\n";
 113+ $html_text .= "\t" . wfMsg( 'sd_createfilter_entervalues' ) . "\n";
 114+ $html_text .= "\t" . Html::input( 'sd_filter_values_num', $filterValuesStr, 'text', $filterValuesAttrs ) . "\n";
 115+ $html_text .= "\t</p>\n";
 116+ $html_text .= '<p>' . wfMsg( 'sd_createfilter_inputtype' ) . '
 117+ <select id="input_type_dropdown" name="sd_input_type_num">' . "\n";
119118
120 - } else {
121 - $html_text .= '<p><input type="radio" name="sd_values_source_starter" value="dates">'.
122 - $date_values_label.'
123 - <select id="time_period_dropdown" name="sd_time_period_starter">
124 - <option value="'.$year_value.'">'.$year_label.'</option>
125 - <option value="'.$month_value.'">'.$month_label.'</option>';
126 - }
127 - $html_text .= '</select>
128 - </p>';
129 - if( $filter_array['Values'] != null){
130 - $values_array = $filter_array['Values'];
131 - $values_str = "";
132 - foreach($values_array as $value) {
133 - $values_str .= $value.', ';
134 - }
135 - $html_text .= '<p><input type="radio" name="sd_values_source_starter" checked value="manual">'.
136 - $enter_values_label.' <input size="40" name="sd_filter_values_starter" value=".'.$values_str.'" >
137 - </p>';
138 - }else{
139 - $html_text .= '<p><input type="radio" name="sd_values_source_starter" value="manual">'.
140 - $enter_values_label.' <input size="40" name="sd_filter_values_starter" value="">
141 - </p>';
142 - }
143 - $input_type_val = $filter_array['InputType'];
144 - $html_text .= '<p>'.$input_type_label.'
145 - <select id="input_type_dropdown" name="sd_input_type_starter">
146 - <option selected value="">'.$values_list_label.'</option>';
147 - if ( $input_type_val == $combo_box_value ) {
148 - $html_text .= '<option selected value="'.$combo_box_value.'">'.$combo_box_label.'</option>
149 - <option value="'.$date_range_value.'">'.$date_range_label.'</option>';
150 - } elseif ( $input_type_val == $date_range_value ) {
151 - $html_text .= '<option value="'.$combo_box_value.'">'.$combo_box_label.'</option>
152 - <option selected value="'.$date_range_value.'">'.$date_range_label.'</option>';
153 - } else {
154 - $html_text .= '<option value="'.$combo_box_value.'">'.$combo_box_label.'</option>
155 - <option value="'.$date_range_value.'">'.$date_range_label.'</option>';
156 - }
157 - $html_text .= <<<END
 119+ $input_type_val = $filter_array['InputType'];
 120+ $combo_box_value = wfMsgForContent( 'sd_filter_combobox' );
 121+ $date_range_value = wfMsgForContent( 'sd_filter_daterange' );
 122+ $valuesListAttrs = array( 'value' => '' );
 123+ $comboBoxAttrs = array( 'value' => $combo_box_value );
 124+ $dateRangeAttrs = array( 'value' => $date_range_value );
 125+ if ( $input_type_val == $combo_box_value ) {
 126+ $comboBoxAttrs['selected'] = true;
 127+ } elseif ( $input_type_val == $date_range_value ) {
 128+ $dateRangeAttrs['selected'] = true;
 129+ } else {
 130+ $valuesListAttrs['selected'] = true;
 131+ }
 132+ $html_text .= "\t" . Html::element( 'option', $valuesListAttrs, wfMsg( 'sd_createfilter_listofvalues' ) ) . "\n";
 133+ $html_text .= "\t" . Html::element( 'option', $comboBoxAttrs, wfMsg( 'sd_filter_combobox' ) ) . "\n";
 134+ $html_text .= "\t" . Html::element( 'option', $dateRangeAttrs, wfMsg( 'sd_filter_daterange' ) ) . "\n";
 135+ $html_text .= <<<END
158136 </select>
159137 </p>
160 - </fieldset>
161138
162139 END;
163140
164 - $html_text_array[] = $html_text;
165 - $html_text = "";
166 - }
167 - }
168 - $text_extensions['sd'] = $html_text_array;
 141+ $text_extensions['sd'] = array( 'Filter', '#FDD', $html_text );
 142+
169143 return true;
170144 }
171145
@@ -174,30 +148,30 @@
175149 foreach ( $request->getValues() as $var => $val ) {
176150 if ( substr( $var, 0, 15 ) == 'sd_filter_name_' ) {
177151 $xml = '<semanticdrilldown_Filter>';
178 - $templateNum = substr($var,15,1);
 152+ $templateNum = substr( $var, 15 );
179153 $xml .= '<Label>'.$val.'</Label>';
180154 } elseif ( substr( $var, 0, 17 ) == 'sd_values_source_') {
181155 if ( $val == 'category' ) {
182 - $xml .= '<ValuesFromCategory>'.$wgRequest->getText('sd_category_name_'.$templateNum).'</ValuesFromCategory>';
 156+ $xml .= '<ValuesFromCategory>' . $request->getText('sd_category_name_' . $templateNum) . '</ValuesFromCategory>';
183157 } elseif ( $val == 'dates' ) {
184 - $xml .= '<TimePeriod>'.$wgRequest->getText('sd_time_period_'.$templateNum).'</TimePeriod>';
 158+ $xml .= '<TimePeriod>' . $request->getText('sd_time_period_' . $templateNum) . '</TimePeriod>';
185159 } elseif ( $val == 'manual' ) {
186 - $filter_mannual_values_str = $wgRequest->getText('sd_filter_values_'.$templateNum);
 160+ $filter_manual_values_str = $request->getText('sd_filter_values_'.$templateNum);
187161 // replace the comma substitution character that has no chance of
188162 // being included in the values list - namely, the ASCII beep
189163 $listSeparator = ',';
190 - $filter_mannual_values_str = str_replace( "\\$listSeparator", "\a", $filter_mannual_values_str );
191 - $filter_mannual_values_array = explode( $listSeparator, $filter_mannual_values_str );
 164+ $filter_manual_values_str = str_replace( "\\$listSeparator", "\a", $filter_manual_values_str );
 165+ $filter_manual_values_array = explode( $listSeparator, $filter_manual_values_str );
192166 $xml .= '<Values>';
193 - foreach ( $filter_mannual_values_array as $i => $value ) {
 167+ foreach ( $filter_manual_values_array as $i => $value ) {
194168 // replace beep back with comma, trim
195169 $value = str_replace( "\a", $listSeparator, trim( $value ) );
196170 $xml .= '<Value>'.$value.'</Value>';
197171 }
198172 $xml .= '</Values>';
199173 }
200 - } elseif ( substr($var,0,14) == 'sd_input_type_') {
201 - $xml .= '<InputType>'.$val.'</InputType>';
 174+ } elseif ( substr( $var, 0, 14 ) == 'sd_input_type_' ) {
 175+ $xml .= '<InputType>' . $val . '</InputType>';
202176 $xml .= '</semanticdrilldown_Filter>';
203177 $xmlPerField[] = $xml;
204178 }
@@ -207,78 +181,6 @@
208182 return true;
209183 }
210184
211 - public static function getHtmlTextForPS( &$js_extensions ,&$text_extensions ) {
212 - global $wgContLang;
213 -
214 - $text = "";
215 - $text .= '<fieldset style="background: #FDD;"><legend>Filter</legend>';
216 - $name_label = wfMsg( 'sd_createfilter_name' );
217 - $property_label = wfMsg( 'sd_createfilter_property' );
218 - $label_label = wfMsg( 'sd_createfilter_label' );
219 - $text .= '
220 - <p>'.$name_label.' <input size="25" name="sd_filter_name_starter" value=""></p> ';
221 -
222 - $values_from_property_label = wfMsg( 'sd_createfilter_usepropertyvalues' );
223 - $values_from_category_label = wfMsg( 'sd_createfilter_usecategoryvalues' );
224 - $date_values_label = wfMsg( 'sd_createfilter_usedatevalues' );
225 - $enter_values_label = wfMsg( 'sd_createfilter_entervalues' );
226 - // need both label and value, in case user's language is different
227 - // from wiki's
228 - $year_label = wfMsg( 'sd_filter_year' );
229 - $year_value = wfMsgForContent( 'sd_filter_year' );
230 - $month_label = wfMsg( 'sd_filter_month' );
231 - $month_value = wfMsgForContent( 'sd_filter_month' );
232 - $input_type_label = wfMsg( 'sd_createfilter_inputtype' );
233 - $values_list_label = wfMsg( 'sd_createfilter_listofvalues' );
234 - // same as for time values
235 - $combo_box_label = wfMsg( 'sd_filter_combobox' );
236 - $combo_box_value = wfMsgForContent( 'sd_filter_combobox' );
237 - $date_range_label = wfMsg( 'sd_filter_daterange' );
238 - $date_range_value = wfMsgForContent( 'sd_filter_daterange' );
239 - $require_filter_label = wfMsg( 'sd_createfilter_requirefilter' );
240 - $text .= <<<END
241 -
242 - <p><input type="radio" name="sd_values_source_starter" checked value="property">
243 - $values_from_property_label
244 - </p>
245 - <p><input type="radio" name="sd_values_source_starter" value="category">
246 - $values_from_category_label
247 - <select id="category_dropdown" name="sd_category_name_starter">
248 -
249 -END;
250 -
251 - $categories = SDUtils::getTopLevelCategories();
252 - foreach ( $categories as $category ) {
253 - $category = str_replace( '_', ' ', $category );
254 - $text .= " <option>$category</option>\n";
255 - }
256 - $text .= <<<END
257 - </select>
258 - </p>
259 - <p><input type="radio" name="sd_values_source_starter" value="dates">
260 - $date_values_label
261 - <select id="time_period_dropdown" name="sd_time_period_starter">
262 - <option value="$year_value">$year_label</option>
263 - <option value="$month_value">$month_label</option>
264 - </select>
265 - </p>
266 - <p><input type="radio" name="sd_values_source_starter" value="manual">
267 - $enter_values_label <input size="40" name="sd_filter_values_starter" value="">
268 - </p>
269 - <p>$input_type_label
270 - <select id="input_type_dropdown" name="sd_input_type_starter">
271 - <option value="">$values_list_label</option>
272 - <option value="$combo_box_value">$combo_box_label</option>
273 - <option value="$date_range_value">$date_range_label</option>
274 - </select>
275 - </p>
276 - </fieldset>
277 -
278 -END;
279 -
280 - $text_extensions['sd'] = $text;
281 - return true;
282 - }
283185 /**
284186 * This function parses the Field elements in the xml of the pages. Hooks for Page Schemas extension
285187 */
Index: trunk/extensions/SemanticDrilldown/SemanticDrilldown.php
@@ -67,11 +67,11 @@
6868
6969 $wgPageProps['hidefromdrilldown'] = 'Whether or not the page is set as HIDEFROMDRILLDOWN';
7070 $wgPageProps['showindrilldown'] = 'Whether or not the page is set as SHOWINDRILLDOWN';
71 -$wgHooks['PSParseFieldElements'][] = 'SDUtils::parseFieldElements' ; //Hook for creating Pages
72 -$wgHooks['PageSchemasGetObject'][] = 'SDUtils::createPageSchemasObject' ; //Hook for returning PageSchema(extension) object from a given xml
73 -$wgHooks['getHtmlTextForFieldInputs'][] = 'SDUtils::getHtmlTextForPS' ; //Hook for retuning html text to PS schema
74 -$wgHooks['PageSchemasGetFieldXML'][] = 'SDUtils::getFieldXMLForPS' ; //Hook for retuning html text to PS schema
75 -$wgHooks['getFilledHtmlTextForFieldInputs'][] = 'SDUtils::getFilledHtmlTextForPS' ; //Hook for retuning html text to PS schema
 71+$wgHooks['PSParseFieldElements'][] = 'SDUtils::parseFieldElements';
 72+$wgHooks['PageSchemasGetObject'][] = 'SDUtils::createPageSchemasObject';
 73+$wgHooks['PageSchemasGetFieldHTML'][] = 'SDUtils::getFieldHTMLForPS';
 74+$wgHooks['PageSchemasGetStarterFieldHTML'][] = 'SDUtils::getStarterFieldHTMLForPS';
 75+$wgHooks['PageSchemasGetFieldXML'][] = 'SDUtils::getFieldXMLForPS';
7676
7777
7878 # ##