r93168 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93167‎ | r93168 | r93169 >
Date:12:21, 26 July 2011
Author:devayon
Status:deferred
Tags:
Comment:
preliminary work combining PO and sorting
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php
@@ -47,7 +47,7 @@
4848 * @global WebRequest $wgRequest
4949 * @global boolean $smwgQEnabled
5050 * @param string $p the sub-page string
51 - * @todo: using processXXXBox() methods here goes against the general architecture.
 51+ * @todo: using processXXXBox() methods here goes against the general architecture. Move this to the UI implementation
5252 */
5353 public function execute( $p ) {
5454 global $wgOut, $wgRequest, $smwgQEnabled, $wgFeedClasses;
@@ -68,7 +68,7 @@
6969 'format' => $wgRequest->getVal( 'format' ),
7070 'offset' => $wgRequest->getVal( 'offset', '0' ),
7171 'limit' => $wgRequest->getVal( 'limit', '20' ) ),
72 - $this->processSortingFormBox( $wgRequest ),
 72+ $this->processSortedPOFormBox( $wgRequest ),
7373 $this->processFormatSelectBox( $wgRequest ) );
7474 $this->uiCore = SMWQueryUIHelper::makeForUI(
7575 $this->processQueryFormBox( $wgRequest ),
@@ -93,7 +93,7 @@
9494 $wgOut->addFeedLink('rss', $href);
9595 }
9696
97 - $this->makepage( $p );
 97+ $this->makePage( $p );
9898 }
9999 }
100100
@@ -352,6 +352,174 @@
353353 }
354354
355355 /**
 356+ * Generates the forms elements(s) for choosing printouts and sorting
 357+ * options. Use its complement processSortedPOFormBox() to decode data
 358+ * sent by these elements.
 359+ *
 360+ * @return string
 361+ */
 362+ protected function getSortedPOFormBox( $enableAutocomplete = SMWQueryUI::ENABLE_AUTO_SUGGEST ) {
 363+ global $smwgQSortingSupport, $wgRequest, $wgOut;
 364+
 365+ if ( !$smwgQSortingSupport ) return '';
 366+
 367+ $result = '';
 368+
 369+ //START: fetch sorting order, if defined earlier
 370+ $params = $this->uiCore->getParameters();
 371+ if ( array_key_exists( 'sort', $params ) && array_key_exists( 'order', $params ) ) {
 372+ $sorts = explode( ',', $params['sort'] );
 373+ $orders = explode( ',', $params['order'] );
 374+ reset( $sorts );
 375+ } else {
 376+ $orders = array(); // do not even show one sort input here
 377+ }
 378+ $num_sort_values = 0;
 379+
 380+ if ( !array_key_exists( 'sort', $params ) ) {
 381+ $sort_values = $wgRequest->getArray( 'sort' );
 382+ if ( is_array( $sort_values ) ) {
 383+ $params['sort'] = implode( ',', $sort_values );
 384+ $num_sort_values = count( $sort_values );
 385+ }
 386+ }
 387+
 388+ foreach ( $orders as $i => $order ) {
 389+ $result .= "<div id=\"sort_div_$i\">" . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort[' . $i . ']" value="' .
 390+ htmlspecialchars( $sorts[$i] ) . "\" size=\"35\"/>\n" . '<select name="order[' . $i . ']"><option ';
 391+ if ( $order == 'ASC' ) $result .= 'selected="selected" ';
 392+ $result .= 'value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . '</option><option ';
 393+ if ( $order == 'DESC' ) $result .= 'selected="selected" ';
 394+
 395+ $result .= 'value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option></select>\n";
 396+ $result .= '[<a href="javascript:removePOInstance(\'sort_div_' . $i . '\')">' . wfMsg( 'delete' ) . '</a>]' . "\n";
 397+ $result .= "</div>\n";
 398+ }
 399+ //END: fetch sorting order, if defined earlier
 400+
 401+ $result .= '<div id="sorting_starter" style="display: none">' . 'Property' . //TODO: add i18n
 402+ ' <input type="text" size="35" name="property_num" />' . "\n";
 403+ $result .= ' <select name="order_num">' . "\n";
 404+ $result .= ' <option value="NONE"> No Sorting </option>'."\n"; //TODO add i18n
 405+ $result .= ' <option value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . "</option>\n";
 406+ $result .= ' <option value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option>\n</select>\n";
 407+ $result .= 'show in results: <input type="checkbox" checked name="display_num" value="yes">'."\n"; //TODO: add i18n
 408+ $result .= "</div>\n";
 409+ $result .= '<div id="sorting_main"></div>' . "\n";
 410+ $result .= '<a href="javascript:addPOInstance(\'sorting_starter\', \'sorting_main\')">' . '[Add additional properties]' . '</a>' . "\n";
 411+
 412+ // Javascript code for handling adding and removing the "sort" inputs
 413+ $delete_msg = wfMsg( 'delete' );
 414+
 415+ $this->enableJQuery();
 416+ $this->addAutocompletionJavascriptAndCSS();
 417+ $javascript_text = <<<EOT
 418+<script type="text/javascript">
 419+// code for handling adding and removing the "sort" inputs
 420+var num_elements = {$num_sort_values};
 421+
 422+function addPOInstance(starter_div_id, main_div_id) {
 423+ var starter_div = document.getElementById(starter_div_id);
 424+ var main_div = document.getElementById(main_div_id);
 425+
 426+ //Create the new instance
 427+ var new_div = starter_div.cloneNode(true);
 428+ var div_id = 'sort_div_' + num_elements;
 429+ new_div.className = 'multipleTemplate';
 430+ new_div.id = div_id;
 431+ new_div.style.display = 'block';
 432+
 433+ var children = new_div.getElementsByTagName('*');
 434+ var x;
 435+ for (x = 0; x < children.length; x++) {
 436+ if (children[x].name)
 437+ children[x].name = children[x].name.replace(/_num/, '[' + num_elements + ']');
 438+ }
 439+
 440+ //Create 'delete' link
 441+ var remove_button = document.createElement('span');
 442+ remove_button.innerHTML = '[<a href="javascript:removePOInstance(\'sort_div_' + num_elements + '\')">{$delete_msg}</a>]';
 443+ new_div.appendChild(remove_button);
 444+
 445+ //Add the new instance
 446+ main_div.appendChild(new_div);
 447+
 448+ //add autocomplete
 449+ jQuery('[name*="property"]').autocomplete({
 450+ minLength: 2,
 451+ source: function(request, response) {
 452+ url=wgScriptPath+'/api.php?action=opensearch&limit=10&namespace='+wgNamespaceIds['property']+'&format=jsonfm';
 453+
 454+ jQuery.getJSON(url, 'search='+request.term, function(data){
 455+ //remove the namespace prefix 'Property:' from returned data
 456+ for(i=0;i<data[1].length;i++) data[1][i]=data[1][i].substr(data[1][i].indexOf(':')+1);
 457+ response(data[1]);
 458+ });
 459+ }
 460+ });
 461+
 462+ num_elements++;
 463+
 464+}
 465+
 466+function removePOInstance(div_id) {
 467+ var olddiv = document.getElementById(div_id);
 468+ var parent = olddiv.parentNode;
 469+ parent.removeChild(olddiv);
 470+}
 471+</script>
 472+
 473+EOT;
 474+
 475+ $wgOut->addScript( $javascript_text );
 476+ return $result;
 477+ }
 478+
 479+ /**
 480+ * Decodes printouts and sorting - related form options generated by its
 481+ * complement, getSortedPOFormBox(). UIs may overload both to change form
 482+ * parameters.
 483+ *
 484+ * @global boolean $smwgQSortingSupport
 485+ * @param WebRequest $wgRequest
 486+ * @return string
 487+ */
 488+ protected function processSortedPOFormBox( WebRequest $wgRequest ) {
 489+ global $smwgQSortingSupport;
 490+ if ( !$smwgQSortingSupport ) return array();
 491+
 492+ $params = array();
 493+ $order_values = $wgRequest->getArray( 'order' );
 494+ $property_values = $wgRequest->getArray( 'property' );
 495+ if ( is_array( $property_values ) ) {
 496+ $params['sort'] = '';
 497+ $params['order'] = '';
 498+ foreach ( $order_values as $key => $order_value ) {
 499+ if($order_value!='NONE'){
 500+ $params['sort'] .= ($params['sort']!=''?',':'') . $property_values[$key];
 501+ $params['order'] .= ($params['order']!=''?',':''). $order_values[$key];
 502+ }
 503+ }
 504+ }
 505+
 506+ $display_values = $wgRequest->getArray( 'display' );
 507+ $po = array();
 508+ if( is_array($display_values) ) {
 509+ foreach ($display_values as $key => $value) {
 510+ if($value == 'yes'){
 511+ $po[] = '?'.$property_values[$key];
 512+ }
 513+
 514+ }
 515+ }
 516+
 517+ $params=array_merge($params,$po);
 518+
 519+ return $params;
 520+
 521+ }
 522+
 523+ /**
356524 * Generates the forms elements(s) for adding sorting options. Use its
357525 * complement processSortingFormBox() to decode sorting data sent
358526 * by these elements.

Follow-up revisions

RevisionCommit summaryAuthorDate
r93177cleanup, follow-up to r93168devayon13:51, 26 July 2011
r93259follow-up to r93168devayon11:50, 27 July 2011
r93421small bug fixes and follow-up to r93168devayon19:52, 28 July 2011
r93454added messages created in r93452. Introduced UI controls made in r93168.devayon08:02, 29 July 2011
r93455refactored methods created in r93168. Removed some dead code.devayon08:05, 29 July 2011