r92157 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92156‎ | r92157 | r92158 >
Date:13:31, 14 July 2011
Author:devayon
Status:deferred
Tags:
Comment:
Fixed sorting, follow-up to r92051
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php
@@ -13,6 +13,9 @@
1414 * @author Sanyam Goyal
1515 * @author Jeroen De Dauw
1616 * @author Devayon Das
 17+ *
 18+ * @ingroup SMWSpecialPage
 19+ * @ingroup SpecialPage
1720 */
1821 abstract class SMWQueryUI extends SpecialPage {
1922 /**
@@ -33,15 +36,18 @@
3437 const DISABLE_AUTO_SUGGEST = false;
3538
3639 /**
37 - * Initialises the page. Sets the property $uiCore to the appropriate helper object.
 40+ * Initialises the page. Sets the property $uiCore to the appropriate helper
 41+ * object.
3842 *
39 - * To create a custom UI, adding changes to makePage() is usually enough, but one might want to
40 - * overload this method to get better handling of form parameters.
 43+ * To create a custom UI, adding changes to makePage() is usually enough,
 44+ * but one might want to overload this method to get better handling of form
 45+ * parameters.
4146 *
4247 * @global OutputPage $wgOut
4348 * @global WebRequest $wgRequest
4449 * @global boolean $smwgQEnabled
4550 * @param string $p the sub-page string
 51+ * @todo: using processXXXBox() methods here goes against the general architecture.
4652 */
4753 public function execute( $p ) {
4854 global $wgOut, $wgRequest, $smwgQEnabled;
@@ -61,8 +67,9 @@
6268 array(
6369 'format' => $wgRequest->getVal( 'format' ),
6470 'offset' => $wgRequest->getVal( 'offset', '0' ),
65 - 'limit' => $wgRequest->getVal( 'limit', '20' )
66 - ), $this->processFormatSelectBox( $wgRequest ) );
 71+ 'limit' => $wgRequest->getVal( 'limit', '20' ) ),
 72+ $this->processSortingFormBox( $wgRequest ),
 73+ $this->processFormatSelectBox( $wgRequest ) );
6774 $this->uiCore = SMWQueryUIHelper::makeForUI(
6875 $this->processQueryFormBox( $wgRequest ),
6976 $params,
@@ -71,8 +78,7 @@
7279 if ( $this->uiCore->getQueryString() != "" ) {
7380 $this->uiCore->execute( $p );
7481 }
75 - }
76 - else {
 82+ } else {
7783 // the user has entered this page from a wiki-page using an infolink,
7884 // or no query has been set
7985 $this->uiCore = SMWQueryUIHelper::makeForInfoLink( $p );
@@ -85,13 +91,14 @@
8692 }
8793
8894 /**
89 - * The main entrypoint for your UI. Call the various methods of SMWQueryUI and
90 - * SMWQueryUIHelper to build ui elements and to process them.
 95+ * The main entrypoint for your UI. Call the various methods of SMWQueryUI
 96+ * and SMWQueryUIHelper to build ui elements and to process them.
9197 */
9298 protected abstract function makePage( $p );
9399
94100 /**
95 - * Builds a read-only #ask embed code of the given query.
 101+ * Builds a read-only #ask embed code of the given query. The code is
 102+ * presented in html code.
96103 *
97104 * @return string
98105 */
@@ -112,7 +119,7 @@
113120 * @global OutputPage $wgOut
114121 * @global boolean $smwgJQueryIncluded
115122 */
116 - private function enableJQuery(){
 123+ private function enableJQuery() {
117124 global $wgOut, $smwgJQueryIncluded;
118125 if ( !$smwgJQueryIncluded ) {
119126 $realFunction = array( 'OutputPage', 'includeJQuery' );
@@ -206,8 +213,8 @@
207214 /**
208215 * Build the navigation bar for some given query result.
209216 *
210 - * UI may overload this for a different layout. The navigation bar
211 - * can be hidden by overloading usesNavigationBar(). To change the url format,
 217+ * UI may overload this for a different layout. The navigation bar can
 218+ * be hidden by overloading usesNavigationBar(). To change the url format,
212219 * one may overload getUrlTail();
213220 *
214221 * @global int $smwgQMaxInlineLimit
@@ -304,8 +311,8 @@
305312 * @param string $errors
306313 * @return string
307314 */
308 - protected function getQueryFormBox( $content, $errors = "" ) {
309 - $result = "";
 315+ protected function getQueryFormBox( $content, $errors = '' ) {
 316+ $result = '';
310317 $result = Html::element( 'textarea', array( 'name' => 'q', 'id' => 'querybox', 'rows' => '6' ), $content );
311318 // TODO:enable/disable on checking for errors; perhaps show error messages right below the box
312319 return $result;
@@ -313,13 +320,14 @@
314321
315322 /**
316323 * Decodes form data sent through form-elements generated by
317 - * its complement, getQueryFormBox. UIs may overload both to change form parameters.
 324+ * its complement, getQueryFormBox. UIs may overload both to change form
 325+ * parameters.
318326 *
319327 * @param WebRequest $wgRequest
320328 * @return string
321329 */
322330 protected function processQueryFormBox( WebRequest $wgRequest ) {
323 - $query = "";
 331+ $query = '';
324332 if ( $wgRequest->getCheck( 'q' ) ) $query = $wgRequest->getVal( 'q' );
325333 return $query;
326334 }
@@ -331,17 +339,19 @@
332340 *
333341 * @return string
334342 */
335 - protected function addSortingFormBox() {
336 - global $smwgQSortingSupport, $wgRequest, $wgOut, $smwgJQueryIncluded;
 343+ protected function getSortingFormBox() {
 344+ global $smwgQSortingSupport, $wgRequest, $wgOut;
 345+
337346 if ( !$smwgQSortingSupport ) return '';
 347+ $params = $this->uiCore->getParameters();
338348
339349 $result = '';
340 - if ( ! array_key_exists( 'sort', $this->m_params ) || ! array_key_exists( 'order', $this->m_params ) ) {
 350+ if ( array_key_exists( 'sort', $params ) && array_key_exists( 'order', $params ) ) {
 351+ $sorts = explode( ',', $params['sort'] );
 352+ $orders = explode( ',', $params['order'] );
 353+ reset( $sorts );
 354+ } else {
341355 $orders = array(); // do not even show one sort input here
342 - } else {
343 - $sorts = explode( ',', $this->m_params['sort'] );
344 - $orders = explode( ',', $this->m_params['order'] );
345 - reset( $sorts );
346356 }
347357
348358 foreach ( $orders as $i => $order ) {
@@ -356,7 +366,7 @@
357367 $result .= "</div>\n";
358368 }
359369
360 - $result .= '<div id="sorting_starter" style="display: none">' . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort_num" size="35" />' . "\n";
 370+ $result .= '<div id="sorting_starter" style="display: none">' . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" size="35" />' . "\n";
361371 $result .= ' <select name="order_num">' . "\n";
362372 $result .= ' <option value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . "</option>\n";
363373 $result .= ' <option value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option>\n</select>\n";
@@ -364,23 +374,23 @@
365375 $result .= '<div id="sorting_main"></div>' . "\n";
366376 $result .= '<a href="javascript:addInstance(\'sorting_starter\', \'sorting_main\')">' . wfMsg( 'smw_add_sortcondition' ) . '</a>' . "\n";
367377
368 - $this->m_num_sort_values = 0;
 378+ $num_sort_values = 0;
369379
370 - if ( !array_key_exists( 'sort', $this->m_params ) ) {
 380+ if ( !array_key_exists( 'sort', $params ) ) {
371381 $sort_values = $wgRequest->getArray( 'sort' );
372382 if ( is_array( $sort_values ) ) {
373 - $this->m_params['sort'] = implode( ',', $sort_values );
374 - $this->m_num_sort_values = count( $sort_values );
 383+ $params['sort'] = implode( ',', $sort_values );
 384+ $num_sort_values = count( $sort_values );
375385 }
376386 }
377387 // Javascript code for handling adding and removing the "sort" inputs
378388 $delete_msg = wfMsg( 'delete' );
379389
380 -
 390+ $this->enableJQuery();
381391 $javascript_text = <<<EOT
382392 <script type="text/javascript">
383393 // code for handling adding and removing the "sort" inputs
384 -var num_elements = {$this->m_num_sort_values};
 394+var num_elements = {$num_sort_values};
385395
386396 function addInstance(starter_div_id, main_div_id) {
387397 var starter_div = document.getElementById(starter_div_id);
@@ -420,43 +430,51 @@
421431 EOT;
422432
423433 $wgOut->addScript( $javascript_text );
424 -
425 - if ( !$smwgJQueryIncluded ) {
426 - $realFunction = array( 'OutputPage', 'includeJQuery' );
427 - if ( is_callable( $realFunction ) ) {
428 - $wgOut->includeJQuery();
429 - } else {
430 - $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js";
431 - }
432 -
433 - $smwgJQueryIncluded = true;
434 - }
435 -
436434 return $result;
437435 }
438436
 437+ /**
 438+ * Decodes form Sorting options sent through form-elements generated by
 439+ * its complement, getSortingFormBox(). UIs may overload both to change form
 440+ * parameters.
 441+ *
 442+ * @global boolean $smwgQSortingSupport
 443+ * @param WebRequest $wgRequest
 444+ * @return string
 445+ * @todo build in validation for sorting
 446+ */
439447 protected function processSortingFormBox( WebRequest $wgRequest ) {
 448+ global $smwgQSortingSupport;
 449+ if ( !$smwgQSortingSupport ) return array();
 450+
 451+ $params = array();
440452 $order_values = $wgRequest->getArray( 'order' );
441 -
442453 if ( is_array( $order_values ) ) {
443454 $params['order'] = '';
444 -
445455 foreach ( $order_values as $order_value ) {
446456 if ( $order_value == '' ) {
447457 $order_value = 'ASC';
448458 }
449459 $params['order'] .= ( $params['order'] != '' ? ',' : '' ) . $order_value;
450460 }
451 - return $params;
452 - } else {
453 - return array();
454461 }
 462+
 463+ $sort_values = $wgRequest->getArray( 'sort' );
 464+ if ( is_array( $sort_values ) ) {
 465+ $params['sort'] = '';
 466+ foreach ( $sort_values as $sort_value ) {
 467+ $params['sort'] .= ( $params['sort'] != '' ? ',' : '' ) . $sort_value;
 468+ }
 469+ }
 470+ return $params;
 471+
455472 }
456473
457474 /**
458475 * Generates the form element(s) for PrintOuts.
459476 * Use its complement processPOFormBox() to decode data sent through these
460 - * form elements. UIs may overload both to change the form parameter or the html elements.
 477+ * form elements. UIs may overload both to change the form parameter or the
 478+ * html elements.
461479 *
462480 * @param string $content The content expected to appear in the box
463481 * @param boolean $enableAutocomplete If set to true, adds the relevant JS and CSS to the page
@@ -1236,7 +1254,7 @@
12371255 }
12381256
12391257 if ( !isset( $this->parameters[$desckey] ) ) {
1240 - /// @bug The current SMWStore will never return SMWConceptValue (an SMWDataValue) here; it might return SMWDIConcept (an SMWDataItem)
 1258+ // / @bug The current SMWStore will never return SMWConceptValue (an SMWDataValue) here; it might return SMWDIConcept (an SMWDataItem)
12411259 $dv = end( smwfGetStore()->getPropertyValues( SMWWikiPageValue::makePageFromTitle( $concept ), new SMWDIProperty( '_CONC' ) ) );
12421260 if ( $dv instanceof SMWConceptValue ) {
12431261 $this->parameters[$desckey] = $dv->getDocu();
@@ -1345,7 +1363,7 @@
13461364 * handling the "further results" links in wiki-pages that use #ask. If
13471365 * your search UI handles form parameters only, then consider using
13481366 * makeForUI().
1349 - *
 1367+ *
13501368 * If any errors do occur while parsing parameters, they may be accessed
13511369 * from hasError() and getErrors().
13521370 *

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r92051Sorting fixes and some cleanup, follow-up to r91979devayon06:59, 13 July 2011