r23326 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23325‎ | r23326 | r23327 >
Date:17:56, 24 June 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Support search for further results (generate proper search URL), store search url and error messages in query
and query result.
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Query.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.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 $qp->getError();
 77+ return $gp->getErrorString();
7878 }
7979
8080 if (array_key_exists('mainlabel', $params)) {
@@ -86,6 +86,8 @@
8787 }
8888
8989 $query = new SMWQuery($desc);
 90+ $query->setQueryString($querystring);
 91+ $query->addErrors($qp->getErrors()); // keep parsing errors for later output
9092 if ($format == '') {
9193 $format = SMWQueryProcessor::getResultFormat($params);
9294 }
@@ -151,8 +153,8 @@
152154 } else { // result for counting or debugging is just a string
153155 return $res;
154156 }
155 - } else { // error string: return escaped version
156 - return htmlspecialchars($query); ///TODO: improve error reporting format ...
 157+ } else { // error string (should be HTML-safe)
 158+ return $query; ///TODO: improve error reporting format ...
157159 }
158160 }
159161
@@ -206,7 +208,7 @@
207209
208210 protected $m_sepstack; // list of open blocks ("parentheses") that need closing at current step
209211 protected $m_curstring; // remaining string to be parsed (parsing eats query string from the front)
210 - protected $m_error; // false if all went right, string otherwise
 212+ protected $m_errors; // empty array if all went right, array of strings otherwise
211213 protected $m_label; //label of the main query result
212214 protected $m_defaultns; //description of the default namespace restriction, or NULL if not used
213215
@@ -236,7 +238,7 @@
237239 * false if there were errors.
238240 */
239241 public function getQueryDescription($querystring) {
240 - $this->m_error = false;
 242+ $this->m_errors = array();
241243 $this->m_label = '';
242244 $this->m_curstring = $querystring;
243245 $this->m_sepstack = array();
@@ -245,13 +247,30 @@
246248 }
247249
248250 /**
249 - * Return error message or false if no error occurred.
 251+ * Return array of error messages (possibly empty).
250252 */
251 - public function getError() {
252 - return $this->m_error;
 253+ public function getErrors() {
 254+ return $this->m_errors;
253255 }
254256
255257 /**
 258+ * Return error message or empty string if no error occurred.
 259+ */
 260+ public function getErrorString() {
 261+ $result = '';
 262+ $first = true;
 263+ foreach ($qp->getError() as $e) {
 264+ if ($first) {
 265+ $first = false;
 266+ } else {
 267+ $result .= ', ';
 268+ }
 269+ $result .= '<span class="smwwarning">' . htmlspecialchars($e) . '</span>';
 270+ }
 271+ return $result;
 272+ }
 273+
 274+ /**
256275 * Return label for the results of this query (which
257276 * might be empty if no such information was passed).
258277 */
@@ -333,7 +352,7 @@
334353 if ($this->popDelimiter('</q>')) {
335354 $continue = false; // leave the loop
336355 } else {
337 - $this->m_error = 'There appear to be too many occurences of \'' . $chunk . '\' in the query.';
 356+ $this->m_errors[] = 'There appear to be too many occurences of \'' . $chunk . '\' in the query.';
338357 return NULL;
339358 }
340359 } elseif ($chunk == '') {
@@ -341,7 +360,7 @@
342361 }
343362 break;
344363 default: // error: unexpected $chunk
345 - $this->m_error = 'The part \'' . $chunk . '\' in the query was not understood. Results might not be as expected.'; // TODO: internationalise
 364+ $this->m_errors[] = 'The part \'' . $chunk . '\' in the query was not understood. Results might not be as expected.'; // TODO: internationalise
346365 return NULL;
347366 }
348367 if ($setsubNS) { // namespace restrictions encountered in current conjunct
@@ -356,7 +375,7 @@
357376 $result = NULL;
358377 foreach ($disjuncts as $d) {
359378 if ($d === NULL) {
360 - $this->m_error = 'No condition in subquery.';
 379+ $this->m_errors[] = 'No condition in subquery.';
361380 $setNS = false;
362381 return NULL;
363382 } else {
@@ -364,7 +383,7 @@
365384 }
366385 }
367386 } else {
368 - $this->m_error = 'No condition in subquery.';
 387+ $this->m_errors[] = 'No condition in subquery.';
369388 $setNS = false;
370389 return NULL;
371390 }
@@ -432,7 +451,7 @@
433452 if ($chunk == ']]') {
434453 return new SMWPrintRequest(SMW_PRINT_CATS, $label);
435454 } else {
436 - $this->m_error = 'Misshaped print statement.'; //TODO: internationalise
 455+ $this->m_errors[] = 'Misshaped print statement.'; //TODO: internationalise
437456 return NULL;
438457 }
439458 break;
@@ -482,7 +501,7 @@
483502 if ($chunk == ']]') {
484503 return new SMWPrintRequest(SMW_PRINT_RELS, $label, $rel);
485504 } else {
486 - $this->m_error = 'Misshaped print statement.'; //TODO: internationalise
 505+ $this->m_errors[] = 'Misshaped print statement.'; //TODO: internationalise
487506 return NULL;
488507 }
489508 break;
@@ -532,8 +551,8 @@
533552 // get values, including values with internal [[...]]
534553 $open = 1;
535554 $value = '';
536 - $chunk = 'NONEMPTY';
537 - while ( ($open > 0) && ($chunk != '') ) {
 555+ $continue = true;
 556+ while ( ($open > 0) && ($continue) ) {
538557 $chunk = $this->readChunk('\[\[|\]\]|\|');
539558 switch ($chunk) {
540559 case '[[': // open new [[ ]]
@@ -547,12 +566,15 @@
548567 $open = 0;
549568 }
550569 break;
 570+ case '': // this is not good ... TODO:report error
 571+ $continue = false;
 572+ break;
551573 }
552574 if ($open != 0) {
553575 $value .= $chunk;
554576 }
555577 }
556 - // note that at this point, we already read one more chunk behind the value
 578+ // note that at this point, we normally already read one more chunk behind the value
557579 $list = preg_split('/^\*/',$value,2);
558580 if (count($list) == 2) { //hit
559581 $value = '*';
@@ -576,7 +598,7 @@
577599 if ($chunk == ']]') {
578600 return new SMWPrintRequest(SMW_PRINT_ATTS, $label, $att, $printmodifier);
579601 } else {
580 - $this->m_error = 'Misshaped print statement.'; //TODO: internationalise
 602+ $this->m_errors[] = 'Misshaped print statement.'; //TODO: internationalise
581603 return NULL;
582604 }
583605 break;
@@ -603,8 +625,8 @@
604626 // TODO: needs extension for n-ary values
605627 $dv = SMWDataValueFactory::newAttributeObjectValue($att, $value);
606628 if (!$dv->isValid()) {
607 - $this->m_error = $dv->getError();
608 - $vd = new SMWValueDescription(NULL, SMW_CMP_ANY);
 629+ $this->m_errors[] = $dv->getError();
 630+ $vd = new SMWValueDescription($dv, SMW_CMP_ANY);
609631 } else {
610632 $vd = new SMWValueDescription($dv, $comparator);
611633 }
@@ -628,7 +650,7 @@
629651 //$innerdesc = NULL;
630652 while ($continue) {
631653 if ($chunk == '<q>') { // no subqueries of the form [[<q>...</q>]] (not needed)
632 - $this->m_error = 'Subqueries not allowed here.'; //TODO
 654+ $this->m_errors[] = 'Subqueries not allowed here.'; //TODO
633655 return NULL;
634656 }
635657 $list = preg_split('/:/', $chunk, 3); // ":Category:Foo" "User:bar" ":baz" ":+"
@@ -662,7 +684,7 @@
663685
664686 protected function finishLinkDescription($chunk, $hasNamespaces, $result, &$setNS, &$label) {
665687 if ($result === NULL) { // no useful information or concrete error found
666 - $this->m_error = 'Syntax error in part of query.'; //TODO internationalise
 688+ $this->m_errors[] = 'Syntax error in part of query.'; //TODO internationalise
667689 return NULL;
668690 }
669691
@@ -690,9 +712,9 @@
691713 // link content (as in [[Category:Test<q>]]) and there was no label to
692714 // eat it. Or the closing ]] are just missing entirely.
693715 if ($chunk != '') { //TODO: internationalise errors
694 - $this->m_error = 'The symbol \'' . $chunk . '\' was used in a place where it is not useful.';
 716+ $this->m_errors[] = 'The symbol \'' . $chunk . '\' was used in a place where it is not useful.';
695717 } else {
696 - $this->m_error = 'Some use of \'[[\' in your query was not closed by a matching \']]\'.';
 718+ $this->m_errors[] = 'Some use of \'[[\' in your query was not closed by a matching \']]\'.';
697719 }
698720 return NULL;
699721 }
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php
@@ -38,7 +38,7 @@
3939 ///// Reading methods /////
4040
4141 function getSpecialValues(Title $subject, $specialprop, $requestoptions = NULL) {
42 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 42+ $db =& wfGetDB( DB_SLAVE ); // TODO: Is '=&' needed in PHP5?
4343
4444 // TODO: this method currently supports no ordering or boundary. This is probably best anyway ...
4545
@@ -92,7 +92,7 @@
9393 }
9494
9595 function getSpecialSubjects($specialprop, $value, $requestoptions = NULL) {
96 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 96+ $db =& wfGetDB( DB_SLAVE );
9797
9898 $result = array();
9999
@@ -169,7 +169,7 @@
170170
171171
172172 function getAttributeValues(Title $subject, Title $attribute, $requestoptions = NULL, $outputformat = '') {
173 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 173+ $db =& wfGetDB( DB_SLAVE );
174174 $result = array();
175175
176176 $id = SMWDataValueFactory::getAttributeObjectTypeID($attribute);
@@ -221,7 +221,7 @@
222222 return array();
223223 }
224224
225 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 225+ $db =& wfGetDB( DB_SLAVE );
226226 $sql = 'value_xsd=' . $db->addQuotes($value->getXSDValue()) .
227227 ' AND value_unit=' . $db->addQuotes($value->getUnit()) .
228228 ' AND attribute_title=' . $db->addQuotes($attribute->getDBKey()) .
@@ -243,7 +243,7 @@
244244 }
245245
246246 function getAllAttributeSubjects(Title $attribute, $requestoptions = NULL) {
247 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 247+ $db =& wfGetDB( DB_SLAVE );
248248 $sql = 'attribute_title=' . $db->addQuotes($attribute->getDBkey()) .
249249 $this->getSQLConditions($requestoptions,'subject_title','subject_title');
250250
@@ -278,7 +278,7 @@
279279 }
280280
281281 function getAttributes(Title $subject, $requestoptions = NULL) {
282 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 282+ $db =& wfGetDB( DB_SLAVE );
283283 $sql = 'subject_id=' . $db->addQuotes($subject->getArticleID()) . $this->getSQLConditions($requestoptions,'attribute_title','attribute_title');
284284
285285 $result = array();
@@ -305,7 +305,7 @@
306306 }
307307
308308 function getRelationObjects(Title $subject, Title $relation, $requestoptions = NULL) {
309 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 309+ $db =& wfGetDB( DB_SLAVE );
310310 $sql = 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
311311 ' AND relation_title=' . $db->addQuotes($relation->getDBKey()) .
312312 $this->getSQLConditions($requestoptions,'object_title','object_title');
@@ -326,7 +326,7 @@
327327 }
328328
329329 function getRelationSubjects(Title $relation, Title $object, $requestoptions = NULL) {
330 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 330+ $db =& wfGetDB( DB_SLAVE );
331331 $sql = 'object_namespace=' . $db->addQuotes($object->getNamespace()) .
332332 ' AND object_title=' . $db->addQuotes($object->getDBKey()) .
333333 ' AND relation_title=' . $db->addQuotes($relation->getDBKey()) .
@@ -348,7 +348,7 @@
349349 }
350350
351351 function getAllRelationSubjects(Title $relation, $requestoptions = NULL) {
352 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 352+ $db =& wfGetDB( DB_SLAVE );
353353 $sql = 'relation_title=' . $db->addQuotes($relation->getDBkey()) .
354354 $this->getSQLConditions($requestoptions,'subject_title','subject_title');
355355
@@ -368,7 +368,7 @@
369369 }
370370
371371 function getOutRelations(Title $subject, $requestoptions = NULL) {
372 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 372+ $db =& wfGetDB( DB_SLAVE );
373373 $sql = 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
374374 $this->getSQLConditions($requestoptions,'relation_title','relation_title');
375375
@@ -388,7 +388,7 @@
389389 }
390390
391391 function getInRelations(Title $object, $requestoptions = NULL) {
392 - $db =& wfGetDB( DB_MASTER ); // TODO: can we use SLAVE here? Is '=&' needed in PHP5?
 392+ $db =& wfGetDB( DB_SLAVE );
393393 $sql = 'object_namespace=' . $db->addQuotes($object->getNamespace()) .
394394 ' AND object_title=' . $db->addQuotes($object->getDBKey()) .
395395 $this->getSQLConditions($requestoptions,'relation_title','relation_title');
@@ -555,7 +555,7 @@
556556 // Build main query
557557 $this->m_sortkey = $query->sortkey;
558558 $this->m_sortfield = false;
559 -
 559+
560560 $pagetable = $db->tableName('page');
561561 $from = $pagetable;
562562 $where = '';
@@ -586,6 +586,7 @@
587587 $sql_options );
588588 $row = $db->fetchObject($res);
589589 return $row->count;
 590+ // TODO: report query errors?
590591 } elseif ($query->querymode == SMWQuery::MODE_DEBUG) {
591592 list( $startOpts, $useIndex, $tailOpts ) = $db->makeSelectOptions( $sql_options );
592593 $result = '<div style="border: 1px dotted black; background: #A1FB00; padding: 20px; ">' .
@@ -599,6 +600,7 @@
600601 $result .= " $key=$value";
601602 }
602603 $result .= '</div>';
 604+ /// TODO: report query errors!
603605 return $result;
604606 } // else: continue
605607
@@ -621,7 +623,7 @@
622624
623625 // Create result by executing print statements for everything that was fetched
624626 ///TODO: use limit (and offset?) values for printouts?
625 - $result = new SMWQueryResult($prs, ( ($count > $query->limit) && ($query->limit >= 0) ) );
 627+ $result = new SMWQueryResult($prs, $query, ( ($count > $query->limit) && ($query->limit >= 0) ) );
626628 foreach ($qr as $qt) {
627629 $row = array();
628630 foreach ($prs as $pr) {
@@ -761,7 +763,7 @@
762764 protected function getSQLConditions($requestoptions, $valuecol, $labelcol = NULL) {
763765 $sql_conds = '';
764766 if ($requestoptions !== NULL) {
765 - $db =& wfGetDB( DB_MASTER ); // TODO: use slave?
 767+ $db =& wfGetDB( DB_SLAVE );
766768 if ($requestoptions->boundary !== NULL) { // apply value boundary
767769 if ($requestoptions->ascending) {
768770 if ($requestoptions->include_boundary) {
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.php
@@ -18,18 +18,26 @@
1919 * returned by this object has the same number of elements (columns).
2020 */
2121 class SMWQueryResult {
22 - protected $content; //an array (table) of arrays (rows) of arrays (fields, SMWResultArray)
23 - protected $printrequests; //an array of SMWPrintRequest objects, indexed by their natural hash keys
24 - protected $furtherres;
 22+ protected $m_content; //an array (table) of arrays (rows) of arrays (fields, SMWResultArray)
 23+ protected $m_printrequests; //an array of SMWPrintRequest objects, indexed by their natural hash keys
 24+ protected $m_furtherres;
 25+ protected $m_errors; // error array (simple string messages, possibly empty)
 26+ protected $m_querystring; // string (inline query) version of query
 27+ protected $m_ascending; // order ascending?
 28+ protected $m_sortkey; // by which property to sort (false: do not sort)
2529
2630 /**
2731 * Initialise the object with an array of SMWPrintRequest objects, which
2832 * define the structure of the result "table" (one for each column).
2933 */
30 - public function SMWQueryResult($printrequests, $furtherres=false) {
31 - $this->content = array();
32 - $this->printrequests = $printrequests;
33 - $this->furtherres = $furtherres;
 34+ public function SMWQueryResult($printrequests, $query, $furtherres=false) {
 35+ $this->m_content = array();
 36+ $this->m_printrequests = $printrequests;
 37+ $this->m_furtherres = $furtherres;
 38+ $this->m_errors = $query->getErrors();
 39+ $this->m_querystring = $query->getQueryString();
 40+ $this->m_ascending = $query->ascending;
 41+ $this->m_sortkey = $query->sortkey;
3442 }
3543
3644 /**
@@ -39,8 +47,8 @@
4048 */
4149 public function addRow($row) {
4250 reset($row);
43 - reset($this->printrequests);
44 - $pr = current($this->printrequests);
 51+ reset($this->m_printrequests);
 52+ $pr = current($this->m_printrequests);
4553 $ra = current($row);
4654
4755 while ( $pr !== false ) {
@@ -51,14 +59,14 @@
5260 if ($pr->getHash() !== $ra->getPrintRequest()->getHash()) {
5361 return false;
5462 }
55 - $pr = next($this->printrequests);
 63+ $pr = next($this->m_printrequests);
5664 $ra = next($row);
5765 }
5866 if ($ra !== false) {
5967 return false;
6068 }
61 - $this->content[] = $row;
62 - reset($this->content);
 69+ $this->m_content[] = $row;
 70+ reset($this->m_content);
6371 return true;
6472 }
6573
@@ -68,8 +76,8 @@
6977 * SMWResultArray objects.
7078 */
7179 public function getNext() {
72 - $result = current($this->content);
73 - next($this->content);
 80+ $result = current($this->m_content);
 81+ next($this->m_content);
7482 return $result;
7583 }
7684
@@ -77,7 +85,7 @@
7886 * Return number of available results.
7987 */
8088 public function getCount() {
81 - return count($this->content);
 89+ return count($this->m_content);
8290 }
8391
8492 /**
@@ -85,7 +93,7 @@
8694 * in this result set contains.
8795 */
8896 public function getColumnCount() {
89 - return count($this->printrequests);
 97+ return count($this->m_printrequests);
9098 }
9199
92100 /**
@@ -93,7 +101,7 @@
94102 * property labels).
95103 */
96104 public function getPrintRequests() {
97 - return $this->printrequests;
 105+ return $this->m_printrequests;
98106 }
99107
100108 /**
@@ -101,16 +109,34 @@
102110 * not shown due to a limit?
103111 */
104112 public function hasFurtherResults() {
105 - return $this->furtherres;
 113+ return $this->m_furtherres;
106114 }
107115
108116 /**
 117+ * Return error array, possibly empty.
 118+ */
 119+ public function getErrors() {
 120+ return $this->m_errors;
 121+ }
 122+
 123+ /**
109124 * Return URL of a page that displays those search results
110125 * (and enables browsing results, and is accessible even without
111126 * JavaScript enabled browsers).
112127 */
113128 public function getQueryURL() {
114129 /// TODO implement (requires some way of generating/maintaining this URL as part of the query, and setting it when creating this result)
 130+ $title = Title::makeTitle(NS_SPECIAL, 'ask');
 131+ $params = 'query=' . urlencode($this->m_querystring);
 132+ if ($this->m_sortkey != false) {
 133+ $params .= '&sort=' . urlencode($this->m_sortkey);
 134+ if ($this->m_ascending) {
 135+ $params .= '&order=ASC';
 136+ } else {
 137+ $params .= '&order=DESC';
 138+ }
 139+ }
 140+ return $title->getLocalURL($params);
115141 }
116142 }
117143
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Query.php
@@ -32,9 +32,12 @@
3333 public $querymode = SMWQuery::MODE_INSTANCES;
3434
3535 protected $m_description;
 36+ protected $m_errors; // keep any errors that occurred so far
 37+ protected $m_querystring = false; // string (inline query) version (if fixed and known)
3638
3739 public function SMWQuery($description = NULL) {
3840 $this->m_description = $description;
 41+ $this->m_errors = array();
3942 }
4043
4144 public function setDescription(SMWDescription $description) {
@@ -44,6 +47,29 @@
4548 public function getDescription() {
4649 return $this->m_description;
4750 }
 51+
 52+ public function getErrors() {
 53+ return $this->m_errors;
 54+ }
 55+
 56+ public function addErrors($errors) {
 57+ $this->m_errors = array_merge($this->m_errors, $errors);
 58+ }
 59+
 60+ public function setQueryString($querystring) {
 61+ $this->m_querystring = $querystring;
 62+ }
 63+
 64+ public function getQueryString() {
 65+ if ($this->m_querystring !== false) {
 66+ return $this->m_querystring;
 67+ } elseif ($this->m_description !== NULL) {
 68+ return $this->m_description->getQueryString();
 69+ } else {
 70+ return '';
 71+ }
 72+ }
 73+
4874 }
4975
5076 ?>
\ No newline at end of file

Status & tagging log