r91979 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91978‎ | r91979 | r91980 >
Date:18:00, 12 July 2011
Author:devayon
Status:deferred
Tags:
Comment:
added #ask output, limited sorting support and squashed some bugs
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_SpecialQueryCreator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php
@@ -16,6 +16,20 @@
1717 const ENABLE_AUTO_SUGGEST = true;
1818 const DISABLE_AUTO_SUGGEST = false;
1919
 20+ /**
 21+ * Builds a read-only #ask of the given query
 22+ *
 23+ * @return string
 24+ */
 25+ protected function getAskEmbedBox() {
 26+ $result = '<div id="inlinequeryembed">
 27+ <div id="inlinequeryembedinstruct">' . wfMsg( 'smw_ask_embed_instr' ) . '</div>
 28+ <textarea id="inlinequeryembedarea" readonly="yes" cols="20" rows="6" onclick="this.select()">' .
 29+ $this->m_ui_helper->getAsk() . '</textarea></div>';
 30+
 31+ return $result;
 32+ }
 33+
2034 protected function addAutocompletionJavascriptAndCSS() {
2135 global $wgOut, $smwgScriptPath, $smwgJQueryIncluded, $smwgJQueryUIIncluded;
2236 if ( $this->autocompleteenabled == false ) {
@@ -254,7 +268,7 @@
255269 }
256270
257271 /**
258 - * A method which generates the form element(s) for the Query-string. Use its
 272+ * Generates the form element(s) for the Query-string. Use its
259273 * complement processQueryFormBox() to decode data sent through these elements.
260274 * UI's may overload both to change form parameters.
261275 *
@@ -270,7 +284,7 @@
271285 }
272286
273287 /**
274 - * A method which decodes form data sent through form-elements generated by
 288+ * Decodes form data sent through form-elements generated by
275289 * its complement, getQueryFormBox. UIs may overload both to change form parameters.
276290 *
277291 * @param WebRequest $wgRequest
@@ -283,7 +297,136 @@
284298 }
285299
286300 /**
287 - * A method which generates the form element(s) for PrintOuts.
 301+ * Generates the forms elements(s) for adding sorting options. Use its
 302+ * complement processSortingFormBox() to decode sorting data sent
 303+ * by these elements.
 304+ *
 305+ * @return string
 306+ */
 307+ protected function addSortingFormBox() {
 308+ $result = '';
 309+ if ( $smwgQSortingSupport ) {
 310+ global $wgRequest, $wgOut, $smwgJQueryIncluded;
 311+
 312+ if ( ! array_key_exists( 'sort', $this->m_params ) || ! array_key_exists( 'order', $this->m_params ) ) {
 313+ $orders = array(); // do not even show one sort input here
 314+ } else {
 315+ $sorts = explode( ',', $this->m_params['sort'] );
 316+ $orders = explode( ',', $this->m_params['order'] );
 317+ reset( $sorts );
 318+ }
 319+
 320+ foreach ( $orders as $i => $order ) {
 321+ $result .= "<div id=\"sort_div_$i\">" . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort[' . $i . ']" value="' .
 322+ htmlspecialchars( $sorts[$i] ) . "\" size=\"35\"/>\n" . '<select name="order[' . $i . ']"><option ';
 323+ if ( $order == 'ASC' ) $result .= 'selected="selected" ';
 324+ $result .= 'value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . '</option><option ';
 325+ if ( $order == 'DESC' ) $result .= 'selected="selected" ';
 326+
 327+ $result .= 'value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option></select>\n";
 328+ $result .= '[<a href="javascript:removeInstance(\'sort_div_' . $i . '\')">' . wfMsg( 'delete' ) . '</a>]' . "\n";
 329+ $result .= "</div>\n";
 330+ }
 331+
 332+ $result .= '<div id="sorting_starter" style="display: none">' . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort_num" size="35" />' . "\n";
 333+ $result .= ' <select name="order_num">' . "\n";
 334+ $result .= ' <option value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . "</option>\n";
 335+ $result .= ' <option value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option>\n</select>\n";
 336+ $result .= "</div>\n";
 337+ $result .= '<div id="sorting_main"></div>' . "\n";
 338+ $result .= '<a href="javascript:addInstance(\'sorting_starter\', \'sorting_main\')">' . wfMsg( 'smw_add_sortcondition' ) . '</a>' . "\n";
 339+
 340+
 341+
 342+
 343+ $this->m_num_sort_values = 0;
 344+
 345+ if ( !array_key_exists( 'sort', $this->m_params ) ) {
 346+ $sort_values = $wgRequest->getArray( 'sort' );
 347+ if ( is_array( $sort_values ) ) {
 348+ $this->m_params['sort'] = implode( ',', $sort_values );
 349+ $this->m_num_sort_values = count( $sort_values );
 350+ }
 351+ }
 352+ // Javascript code for handling adding and removing the "sort" inputs
 353+ $delete_msg = wfMsg( 'delete' );
 354+
 355+
 356+ $javascript_text = <<<EOT
 357+<script type="text/javascript">
 358+// code for handling adding and removing the "sort" inputs
 359+var num_elements = {$this->m_num_sort_values};
 360+
 361+function addInstance(starter_div_id, main_div_id) {
 362+ var starter_div = document.getElementById(starter_div_id);
 363+ var main_div = document.getElementById(main_div_id);
 364+
 365+ //Create the new instance
 366+ var new_div = starter_div.cloneNode(true);
 367+ var div_id = 'sort_div_' + num_elements;
 368+ new_div.className = 'multipleTemplate';
 369+ new_div.id = div_id;
 370+ new_div.style.display = 'block';
 371+
 372+ var children = new_div.getElementsByTagName('*');
 373+ var x;
 374+ for (x = 0; x < children.length; x++) {
 375+ if (children[x].name)
 376+ children[x].name = children[x].name.replace(/_num/, '[' + num_elements + ']');
 377+ }
 378+
 379+ //Create 'delete' link
 380+ var remove_button = document.createElement('span');
 381+ remove_button.innerHTML = '[<a href="javascript:removeInstance(\'sort_div_' + num_elements + '\')">{$delete_msg}</a>]';
 382+ new_div.appendChild(remove_button);
 383+
 384+ //Add the new instance
 385+ main_div.appendChild(new_div);
 386+ num_elements++;
 387+}
 388+
 389+function removeInstance(div_id) {
 390+ var olddiv = document.getElementById(div_id);
 391+ var parent = olddiv.parentNode;
 392+ parent.removeChild(olddiv);
 393+}
 394+</script>
 395+
 396+EOT;
 397+
 398+ $wgOut->addScript( $javascript_text );
 399+
 400+ if ( !$smwgJQueryIncluded ) {
 401+ $realFunction = array( 'OutputPage', 'includeJQuery' );
 402+ if ( is_callable( $realFunction ) ) {
 403+ $wgOut->includeJQuery();
 404+ } else {
 405+ $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js";
 406+ }
 407+
 408+ $smwgJQueryIncluded = true;
 409+ }
 410+ }
 411+ return $result;
 412+ }
 413+
 414+ protected function processSortingFormBox( WebRequest $wgRequest ) {
 415+ $order_values = $wgRequest->getArray( 'order' );
 416+
 417+ if ( is_array( $order_values ) ) {
 418+ $params['order'] = '';
 419+
 420+ foreach ( $order_values as $order_value ) {
 421+ if ( $order_value == '' ) $order_value = 'ASC';
 422+ $params['order'] .= ( $params['order'] != '' ? ',' : '' ) . $order_value;
 423+ }
 424+ return $params;
 425+ }
 426+ return array();
 427+ }
 428+
 429+ /**
 430+ * Generates the form element(s) for PrintOuts.
288431 * Use its complement processPOFormBox() to decode data sent through these
289432 * form elements. UIs may overload both to change the form parameter or the html elements.
290433 *
@@ -340,7 +483,7 @@
341484 }
342485
343486 /**
344 - * A method which decodes form data sent through form-elements generated by
 487+ * Decodes form data sent through form-elements generated by
345488 * its complement, getPOFormBox(). UIs may overload both to change form parameters.
346489 *
347490 * @param WebRequest $wgRequest
@@ -367,7 +510,7 @@
368511 }
369512
370513 /**
371 - * A method which generates the url parameters based on passed parameters.
 514+ * Generates the url parameters based on passed parameters.
372515 * UI implementations need to overload this if they use different form parameters.
373516 *
374517 * @return string An url-encoded string.
@@ -395,7 +538,7 @@
396539 }
397540
398541 /**
399 - * Display a form section showing the options for a given format,
 542+ * Displays a form section showing the options for a given format,
400543 * based on the getParameters() value for that format's query printer.
401544 *
402545 * @param string $format
@@ -675,8 +818,8 @@
676819 * @return boolean
677820 */
678821 protected function usesNavigationBar() {
679 - //hide if no results are found
680 - if($this->m_ui_helper->getResultCount()==0) return false;
 822+ // hide if no results are found
 823+ if ( $this->m_ui_helper->getResultCount() == 0 ) return false;
681824 else return true;
682825 }
683826
@@ -709,7 +852,7 @@
710853 * Various parameters passed by the user which control the format, limit, offset.
711854 * @var array of strings
712855 */
713 - protected $parameters = array();
 856+ protected $parameters = array();
714857
715858 /**
716859 * The additional columns to be displayed with results
@@ -719,7 +862,7 @@
720863
721864 /**
722865 * The The additional columns to be displayed with results in '?property' form
723 - *
 866+ *
724867 * @var array of strings
725868 */
726869 protected $printOutStrings = array();
@@ -745,6 +888,11 @@
746889 private $errors = array();
747890
748891 /**
 892+ * The default result printer if no format is set at the higher level
 893+ */
 894+ private $defaultResultPrinter = 'broadtable';
 895+
 896+ /**
749897 * The Query Result, if it has been fetched.
750898 *
751899 * @var SMWQueryResult
@@ -760,7 +908,7 @@
761909
762910 /**
763911 * A list of Query UIs
764 - *
 912+ *
765913 * @var array of SpecialPage
766914 */
767915 protected static $uiPages = array(); // A list of Query UIs
@@ -769,7 +917,7 @@
770918 * Although this constructor is publicly accessible, its use is discouraged.
771919 * Instantiation can instead be done by the makeForInfoLink() to handle infolink
772920 * requests or makeForUI() to handle requests from a Query form.
773 - *
 921+ *
774922 * @param mixed $context SMWQueryUIHelper::SPECIAL_PAGE | SMWQueryUIHelper::WIKI_LINK
775923 */
776924 public function __construct( $context = SMWQueryUIHelper::SPECIAL_PAGE ) {
@@ -815,11 +963,11 @@
816964
817965 /**
818966 * Would there be more query results that were not shown due to a limit?
819 - *
 967+ *
820968 * @return boolean
821969 */
822970 public function hasFurtherResults() {
823 - if ( is_a( $this->queryResult, 'SMWQueryResult' ) ) { //The queryResult may not be set
 971+ if ( is_a( $this->queryResult, 'SMWQueryResult' ) ) { // The queryResult may not be set
824972 return $this->queryResult->hasFurtherResults();
825973 }
826974 else {
@@ -834,12 +982,12 @@
835983 */
836984 public function getResultObject() {
837985 return $this->queryResult;
838 - //TODO: see if this method can be removed.
 986+ // TODO: see if this method can be removed.
839987 }
840988
841989 /**
842990 * Returns an array of errors, if any have occured.
843 - *
 991+ *
844992 * @return array of strings
845993 */
846994 public function getErrors() {
@@ -920,7 +1068,7 @@
9211069 /*
9221070 * Note: property validation is not very clearly defined yet, so validation is disabled by default
9231071 */
924 -
 1072+
9251073 $errors = array();
9261074 if ( $enable_validation ) {
9271075 foreach ( $print_outs as $key => $prop ) {
@@ -959,10 +1107,8 @@
9601108 $errors = array();
9611109
9621110 // checking for missing parameters and adding them
963 - if ( !array_key_exists( 'format', $params ) )
964 - $params[ 'format' ] = $this->getDefaultResultPrinter ();
965 - if ( !array_key_exists( 'order', $params ) )
966 - $params[ 'order' ] = '';
 1111+ if ( !array_key_exists( 'format', $params ) or ! array_key_exists ( $params['format'], $smwgResultFormats ) )
 1112+ $params[ 'format' ] = $this->defaultResultPrinter;
9671113 if ( !array_key_exists( 'limit', $params ) )
9681114 $params[ 'limit' ] = 20;
9691115 $params[ 'limit' ] = min( $params[ 'limit' ], $smwgQMaxInlineLimit );
@@ -1096,14 +1242,10 @@
10971243 }
10981244 return $result;
10991245 }
1100 -
1101 - /**
1102 - * $queryString, $parameters, $printOuts are set, returns the relevant #ask query
1103 - */
11041246
11051247 /**
11061248 * Returns the query in the #ask format
1107 - *
 1249+ *
11081250 * @return string
11091251 */
11101252 public function getAsk() {
@@ -1129,7 +1271,7 @@
11301272
11311273 /**
11321274 * Returns number of available results.
1133 - *
 1275+ *
11341276 * @return int
11351277 */
11361278 public function getResultCount() {
@@ -1142,7 +1284,7 @@
11431285
11441286 /**
11451287 * Retuens the param array
1146 - *
 1288+ *
11471289 * @return array
11481290 */
11491291 public function getParams() {
@@ -1179,6 +1321,7 @@
11801322 $result->execute();
11811323 return $result;
11821324 }
 1325+
11831326 /**
11841327 * Constructs a new SMWQueryUIHelper when arguments are extracted from the UI
11851328 *
@@ -1199,9 +1342,10 @@
12001343 // $result->execute();
12011344 return $result;
12021345 }
 1346+
12031347 /**
12041348 * Checks if $property exists in the wiki or not
1205 - *
 1349+ *
12061350 * @return bool
12071351 */
12081352 protected static function validateProperty( $property ) {
@@ -1217,16 +1361,5 @@
12181362 return false;
12191363 }
12201364 }
1221 -
1222 - /**
1223 - * Returns the result printer which should be used if not specified by the user.
1224 - *
1225 - * Overload if necessary.
1226 - *
1227 - * @return string
1228 - */
1229 - protected static function getDefaultResultPrinter() {
1230 - return 'broadtable';
1231 - }
1232 -
 1365+
12331366 }
Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_SpecialQueryCreator.php
@@ -40,7 +40,7 @@
4141 */
4242 public function execute( $p ) {
4343 global $wgOut, $wgRequest, $smwgQEnabled, $smwgIgnoreQueryErrors;
44 - $smwgIgnoreQueryErrors= false;
 44+ $smwgIgnoreQueryErrors = false;
4545 $this->setHeaders();
4646
4747 if ( !$smwgQEnabled ) {
@@ -116,7 +116,11 @@
117117 $result .= $this->addSortingOptions( $result );
118118 }
119119 $result .= "<br><br>" . $this->getFormatSelectBox( 'broadtable' );
120 - $result .= '</div>';
 120+
 121+ if ( $this->m_ui_helper->getQueryString() != '' ) // hide #ask if there isnt any query defined
 122+ $result .= $this->getAskEmbedBox();
 123+
 124+ $result .= '</div>'; // end of hidden additional options
121125 $result .= '<br /><input type="submit" value="' . wfMsg( 'smw_ask_submit' ) . '"/>' .
122126 '<input type="hidden" name="eq" value="no"/>' .
123127 "\n</form>";

Follow-up revisions

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