r23332 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23331‎ | r23332 | r23333 >
Date:18:42, 24 June 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Improved error handling/reporting for inline queries.
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinters.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
@@ -73,7 +73,7 @@
7474 $qp->setDefaultNamespaces($smwgIQSearchNamespaces);
7575 $desc = $qp->getQueryDescription($querystring);
7676 if ($desc === NULL) { //abort with failure
77 - return $gp->getErrorString();
 77+ return $qp->getErrorString();
7878 }
7979
8080 if (array_key_exists('mainlabel', $params)) {
@@ -259,13 +259,13 @@
260260 public function getErrorString() {
261261 $result = '';
262262 $first = true;
263 - foreach ($qp->getError() as $e) {
 263+ foreach ($this->m_errors as $e) {
264264 if ($first) {
265265 $first = false;
266266 } else {
267267 $result .= ', ';
268268 }
269 - $result .= '<span class="smwwarning">' . htmlspecialchars($e) . '</span>';
 269+ $result .= '<span class="smwwarning">' . $e . '</span>';
270270 }
271271 return $result;
272272 }
@@ -315,11 +315,9 @@
316316 switch ($chunk) {
317317 case '[[': // start new link block
318318 $ld = $this->getLinkDescription($setsubNS, $label);
319 - if ($ld === NULL) {
320 - return NULL;
321 - } elseif ($ld instanceof SMWPrintRequest) {
 319+ if ($ld instanceof SMWPrintRequest) {
322320 $printrequests[] = $ld;
323 - } else {
 321+ } elseif ($ld instanceof SMWDescription) {
324322 $conjunction = $this->addDescription($conjunction,$ld);
325323 }
326324 break;
@@ -479,6 +477,10 @@
480478 protected function getRelationDescription($relname, &$setNS, &$label) {
481479 $innerdesc = NULL;
482480 $rel = Title::newFromText($relname, SMW_NS_RELATION);
 481+ if ($rel === NULL) {
 482+ $this->m_errors[] .= 'Sorry, but ' . htmlspecialchars($relname) . ' is no valid page title.'; // TODO internationalise
 483+ return NULL;///TODO: read some more chunks and try to finish [[ ]]
 484+ }
483485
484486 $this->readChunk(); // consume seperator "::"
485487 $continue = true;
@@ -528,11 +530,15 @@
529531 $continue = ($chunk == '||');
530532 }
531533
532 - if ($innerdesc !== NULL) {
533 - $result = new SMWSomeRelation($rel,$innerdesc);
534 - } else {
535 - $result = NULL;
 534+ if ($innerdesc === NULL) { // make a wildcard search
 535+ if ($this->m_defaultns !== NULL) {
 536+ $innerdesc = $this->addDescription($innerdesc, $this->m_defaultns, false);
 537+ } else {
 538+ $innerdesc = $this->addDescription($innerdesc, new SMWThingDescription(), false);
 539+ }
 540+ $this->m_errors[] = 'Value of relation ' . $rel->getText() . ' was not understood.'; // TODO internationalise
536541 }
 542+ $result = new SMWSomeRelation($rel,$innerdesc);
537543
538544 return $this->finishLinkDescription($chunk, false, $result, $setNS, $label);
539545 }
@@ -546,6 +552,10 @@
547553 protected function getAttributeDescription($attname, &$setNS, &$label) {
548554 $this->readChunk(); // consume seperator ":="
549555 $att = Title::newFromText($attname, SMW_NS_ATTRIBUTE);
 556+ if ($att === NULL) {
 557+ $this->m_errors[] .= 'Sorry, but ' . htmlspecialchars($attname) . ' is no valid page title.'; // TODO internationalise
 558+ return NULL; ///TODO: read some more chunks and try to finish [[ ]]
 559+ }
550560 ///TODO: currently no support for disjunctions in data values (needs extension of query processor)
551561
552562 // get values, including values with internal [[...]]
@@ -684,40 +694,41 @@
685695
686696 protected function finishLinkDescription($chunk, $hasNamespaces, $result, &$setNS, &$label) {
687697 if ($result === NULL) { // no useful information or concrete error found
688 - $this->m_errors[] = 'Syntax error in part of query.'; //TODO internationalise
689 - return NULL;
690 - }
691 -
692 - if (!$hasNamespaces && $setNS && ($this->m_defaultns !== NULL) ) {
 698+ $this->m_errors[] = 'Some part [#x005B;&hellip]] of the query was not understood.';
 699+ //TODO internationalise
 700+ } elseif (!$hasNamespaces && $setNS && ($this->m_defaultns !== NULL) ) {
693701 $result = $this->addDescription($result, $this->m_defaultns);
694702 $hasNamespaces = true;
695703 }
696704 $setNS = $hasNamespaces;
697705
698706 // terminate link (assuming that next chunk was read already)
699 - if ($chunk == '|') { // label, TODO
 707+ if ($chunk == '|') {
700708 $chunk = $this->readChunk('\]\]');
701709 if ($chunk != ']]') {
702710 $label .= $chunk;
703711 $chunk = $this->readChunk('\]\]');
704 - } else {
705 - // empty label does not add to overall label
 712+ } else { // empty label does not add to overall label
706713 $chunk = ']]';
707714 }
708715 }
709 - if ($chunk == ']]') { // expected termination
710 - return $result;
711 - } else {
 716+ if ($chunk != ']]') {
712717 // What happended? We found some chunk that could not be processed as
713718 // link content (as in [[Category:Test<q>]]) and there was no label to
714719 // eat it. Or the closing ]] are just missing entirely.
715 - if ($chunk != '') { //TODO: internationalise errors
716 - $this->m_errors[] = 'The symbol \'' . $chunk . '\' was used in a place where it is not useful.';
717 - } else {
718 - $this->m_errors[] = 'Some use of \'[[\' in your query was not closed by a matching \']]\'.';
 720+ if ($chunk != '') {
 721+ $this->m_errors[] = 'The symbol \'' . htmlspecialchars($chunk) . '\' was used in a place where it is not useful.';
 722+ // try to find a later closing ]] to finish this misshaped subpart
 723+ $chunk = $this->readChunk('\]\]');
 724+ if ($chunk != ']]') {
 725+ $chunk = $this->readChunk('\]\]');
 726+ }
719727 }
720 - return NULL;
 728+ if ($chunk == '') {
 729+ $this->m_errors[] = 'Some use of \'[&#x005B;\' in your query was not closed by a matching \']]\'.';
 730+ }
721731 }
 732+ return $result;
722733 }
723734
724735 /**
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php
@@ -587,10 +587,10 @@
588588 $row = $db->fetchObject($res);
589589 return $row->count;
590590 // TODO: report query errors?
591 - } elseif ($query->querymode == SMWQuery::MODE_DEBUG) {
 591+ } elseif ($query->querymode == SMWQuery::MODE_DEBUG) { /// TODO: internationalise
592592 list( $startOpts, $useIndex, $tailOpts ) = $db->makeSelectOptions( $sql_options );
593593 $result = '<div style="border: 1px dotted black; background: #A1FB00; padding: 20px; ">' .
594 - '<b>Generated Wiki Query</b><br />' .
 594+ '<b>Generated Wiki-Query</b><br />' .
595595 htmlspecialchars($query->getDescription()->getQueryString()) . '<br />' .
596596 '<b>SQL-Query</b><br />' .
597597 "SELECT DISTINCT $pagetable.page_title as title, $pagetable.page_namespace as namespace" .
@@ -599,6 +599,10 @@
600600 foreach ($sql_options as $key => $value) {
601601 $result .= " $key=$value";
602602 }
 603+ $result .= '<br /><b>Errors and Warnings</b><br />';
 604+ foreach ($query->getErrors() as $error) {
 605+ $result .= $error . '<br />';
 606+ }
603607 $result .= '</div>';
604608 /// TODO: report query errors!
605609 return $result;
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinters.php
@@ -108,6 +108,31 @@
109109 }
110110 }
111111
 112+ /**
 113+ * Provides a simple formatted string of all the error messages that occurred.
 114+ * Can be used if not specific error formatting is desired. Compatible with HTML
 115+ * and Wiki.
 116+ *
 117+ * TODO: use joined code for this and the similar method in SMWQueryParser?
 118+ */
 119+ protected function getErrorString($res) {
 120+ $result = '';
 121+ $first = true;
 122+ foreach ($res->getErrors() as $e) {
 123+ if ($first) {
 124+ $first = false;
 125+ } else {
 126+ $result .= ', ';
 127+ }
 128+ $result .= '<span class="smwwarning">' . $e . '</span>';
 129+ }
 130+ if ($result != '') {
 131+ $result = '(' . $result . ')';
 132+ }
 133+ return $result;
 134+ }
 135+
 136+
112137 }
113138
114139 /**
@@ -160,6 +185,7 @@
161186 }
162187 }
163188 $result .= "\t</table>"; // print footer
 189+ $result .= $this->getErrorString($res); // just append error messages
164190 return $result;
165191 }
166192 }
@@ -296,6 +322,7 @@
297323
298324 // print footer
299325 $result .= $footer;
 326+ $result .= $this->getErrorString($res); // just append error messages
300327
301328 return $result;
302329 }
@@ -485,6 +512,7 @@
486513
487514 // print footer
488515 $result .= "</div>";
 516+ $result .= $this->getErrorString($res); // just append error messages
489517 return $result;
490518 }
491519 }
@@ -557,6 +585,7 @@
558586 }
559587
560588 $smwgStoreActive = $old_smwgStoreActive;
 589+ $result .= $this->getErrorString($res); // just append error messages
561590 return $result;
562591 }
563592 }
@@ -668,6 +697,7 @@
669698 }
670699 }
671700 $result .= $footer;
 701+ $result .= $this->getErrorString($res); // just append error messages
672702
673703 $smwgStoreActive = $old_smwgStoreActive;
674704 return $result;

Status & tagging log