Index: trunk/extensions/SemanticMediaWiki/includes/SMW_InlineQueries.php |
— | — | @@ -123,7 +123,7 @@ |
124 | 124 | |
125 | 125 | /** |
126 | 126 | * A simple data container for storing the essential components of SQL queries. |
127 | | - * Used to have a struct as convenient reutrn value to functions that construct queries. |
| 127 | + * Used to have a struct as convenient return value to functions that construct queries. |
128 | 128 | */ |
129 | 129 | class SMWSQLQuery { |
130 | 130 | public $mConditions; // SQL conditions as a string |
— | — | @@ -132,9 +132,9 @@ |
133 | 133 | public $mOrderBy; // NULL or name of a single field by which the result should be ordered |
134 | 134 | public $mFixedSubject; // true if the subject of the query has been given explicitly |
135 | 135 | public $mPrint; // array of things to print; format: id=>array(label,mode[,namestring[,datavalue]]) |
136 | | - |
| 136 | + |
137 | 137 | public $mDebug; // some field for debugging //DEBUG |
138 | | - |
| 138 | + |
139 | 139 | function SMWSQLQuery() { |
140 | 140 | $this->mConditions = ''; |
141 | 141 | $this->mTables = ''; |
— | — | @@ -167,7 +167,7 @@ |
168 | 168 | * mainlabel -- Label to use for the column that shows the main subjects. Also used to indicate that |
169 | 169 | * the subject should be displayed in cases where it would normally be hidden. |
170 | 170 | * format -- Either 'list', 'ul' (for unordered bullet list), 'ol' (ordered and numbered list), |
171 | | - * 'table', 'broadtable', 'timeline', 'eventline', 'embedded', or 'auto' (default). |
| 171 | + * 'table', 'broadtable', 'timeline', 'eventline', 'embedded', 'template' or 'auto' (default). |
172 | 172 | * Some formats have additional parameters: |
173 | 173 | * sep (list only) -- Customized separator string. |
174 | 174 | */ |
— | — | @@ -178,7 +178,7 @@ |
179 | 179 | * formats. The formats 'table' and 'list' are defaults that cannot be disabled. The format 'broadtable' |
180 | 180 | * should not be disabled either in order not to break Special:ask. |
181 | 181 | */ |
182 | | - static $formats = array('table','list','ol','ul','broadtable','embedded','timeline','eventline'); |
| 182 | + static $formats = array('table','list','ol','ul','broadtable','embedded','timeline','eventline','template'); |
183 | 183 | |
184 | 184 | private $mInline; // is this really an inline query, i.e. are results used in an article or not? (bool) |
185 | 185 | |
— | — | @@ -214,6 +214,7 @@ |
215 | 215 | private $mQueryResult; // retrieved query result |
216 | 216 | |
217 | 217 | // other stuff |
| 218 | + private $mHTMLPrinter; // is the current printer expecting HTML (true) or wikitext (false) for labels? |
218 | 219 | private $mLinker; // we make our own linker for creating the links -- TODO: is this bad? |
219 | 220 | |
220 | 221 | public function SMWInlineQuery($param = array(), $inline = true) { |
— | — | @@ -274,6 +275,15 @@ |
275 | 276 | if ( !in_array($this->mFormat,SMWInlineQuery::$formats) ) |
276 | 277 | $this->mFormat = 'auto'; // If it is an unknown format, default to list again |
277 | 278 | } |
| 279 | + // TODO: of course the printer should specify the following, but links are currently |
| 280 | + // created during query parsing, while printers might be selected only after this. |
| 281 | + // This will be fixed by cleaning up the query parsing code to not create full links |
| 282 | + // at a later stage. |
| 283 | + if ($this->mFormat == 'template') { |
| 284 | + $this->mHTMLPrinter = false; |
| 285 | + } else { |
| 286 | + $this->mHTMLPrinter = true; |
| 287 | + } |
278 | 288 | if (array_key_exists('intro', $param)) { |
279 | 289 | $this->mIntro = htmlspecialchars(str_replace('_', ' ', $param['intro'])); |
280 | 290 | } |
— | — | @@ -540,6 +550,9 @@ |
541 | 551 | case 'embedded': |
542 | 552 | $printer = new SMWEmbeddedPrinter($this,$sq); |
543 | 553 | break; |
| 554 | + case 'template': |
| 555 | + $printer = new SMWTemplatePrinter($this,$sq); |
| 556 | + break; |
544 | 557 | default: $printer = new SMWListPrinter($this,$sq); |
545 | 558 | } |
546 | 559 | $result = $printer->printResult(); |
— | — | @@ -901,14 +914,14 @@ |
902 | 915 | |
903 | 916 | /** |
904 | 917 | * Create output string for an article title (possibly including namespace) |
905 | | - * as given by $text. |
| 918 | + * as given by $text. |
906 | 919 | * |
907 | 920 | * $subject states whether the given title is the subject (to which special |
908 | 921 | * settings for linking apply). |
909 | 922 | * If $label is null the standard label of the given article will be used. |
910 | 923 | * If $label is the empty string, an empty string is returned. |
911 | 924 | * $linked states whether the result should be a hyperlink |
912 | | - * $exists states whether $text is known to be anexisting article, in which |
| 925 | + * $exists states whether $text is known to be an existing article, in which |
913 | 926 | * case we can save a DB lookup when creating links. |
914 | 927 | */ |
915 | 928 | public function makeTitleString($text,$label,$linked,$exists=false) { |
— | — | @@ -918,9 +931,13 @@ |
919 | 932 | return $text; // TODO maybe report an error here? |
920 | 933 | } elseif ( $linked ) { |
921 | 934 | if ( NULL === $label ) $label = $title->getText(); |
922 | | - if ($exists) |
923 | | - return $this->mLinker->makeKnownLinkObj($title, $label); |
924 | | - else return $this->mLinker->makeLinkObj($title, $label); |
| 935 | + if ($this->mHTMLPrinter) { |
| 936 | + if ($exists) |
| 937 | + return $this->mLinker->makeKnownLinkObj($title, $label); |
| 938 | + else return $this->mLinker->makeLinkObj($title, $label); |
| 939 | + } else { |
| 940 | + return '[[' . $title->getPrefixedText() . '|' . $label . ']]'; |
| 941 | + } |
925 | 942 | } else { |
926 | 943 | return $title->getText(); // TODO: shouldn't this default to $label? |
927 | 944 | } |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinters.php |
— | — | @@ -419,7 +419,7 @@ |
420 | 420 | } |
421 | 421 | $article_namespace = $cur[1]; |
422 | 422 | $article_title = $cur[2]; // TODO: ouch .. we need to check whether this exists |
423 | | - if ((NULL != $article_title) and ($article_title != '')) { |
| 423 | + if ( (NULL != $article_title) and ('' != $article_title) ) { |
424 | 424 | $title_text = $article_namespace; |
425 | 425 | if ($title_text != '') $title_text .= ':'; |
426 | 426 | $title_text .= $article_title; |
— | — | @@ -453,4 +453,67 @@ |
454 | 454 | } |
455 | 455 | } |
456 | 456 | |
| 457 | +/** |
| 458 | + * Printer for template data. Passes a result a result row as anonymous parameters to |
| 459 | + * a given template (which might ignore them or not) and prints the result. Values |
| 460 | + */ |
| 461 | +class SMWTemplatePrinter implements SMWQueryPrinter { |
| 462 | + private $mIQ; // the querying object that called the printer |
| 463 | + private $mQuery; // the query that was executed and whose results are to be printed |
| 464 | + |
| 465 | + public function SMWTemplatePrinter($iq, $query) { |
| 466 | + $this->mIQ = $iq; |
| 467 | + $this->mQuery = $query; |
| 468 | + } |
| 469 | + |
| 470 | + public function printResult() { |
| 471 | + // handle factbox |
| 472 | + global $smwgStoreActive, $wgTitle; |
| 473 | + $old_smwgStoreActive = $smwgStoreActive; |
| 474 | + $smwgStoreActive = false; // no annotations stored, no factbox printed |
| 475 | + |
| 476 | + // print all result rows |
| 477 | + $result = $this->mIQ->getIntro(); |
| 478 | + $params = $this->mIQ->getParameters(); |
| 479 | + if (array_key_exists('template', $params)) { |
| 480 | + $templatename = $params['template']; |
| 481 | + } else { |
| 482 | + return 'Please set parameter "template" for <ask> to work.'; // TODO: internationalise |
| 483 | + } |
| 484 | + |
| 485 | + $parser_options = new ParserOptions(); |
| 486 | + $parser_options->setEditSection(false); // embedded sections should not have edit links |
| 487 | + $parser = new Parser(); |
| 488 | + while ( $row = $this->mIQ->getNextRow() ) { |
| 489 | + $wikitext = ''; |
| 490 | + $firstcol = true; |
| 491 | + foreach ($this->mQuery->mPrint as $print_data) { |
| 492 | + $iterator = $this->mIQ->getIterator($print_data,$row,$firstcol); |
| 493 | + $wikitext .= "|"; |
| 494 | + $first = true; |
| 495 | + while ($cur = $iterator->getNext()) { |
| 496 | + if ($first) $first = false; else $wikitext .= ', '; |
| 497 | + $wikitext .= $cur[0]; |
| 498 | + } |
| 499 | + $firstcol = false; |
| 500 | + } |
| 501 | + $parserOutput = $parser->parse('{{' . $templatename . $wikitext . '}}', $wgTitle, $parser_options); |
| 502 | + $result .= $parserOutput->getText(); |
| 503 | + } |
| 504 | + // show link to more results |
| 505 | + if ($this->mIQ->isInline() && $this->mIQ->hasFurtherResults()) { |
| 506 | + $label = $this->mIQ->getSearchLabel(); |
| 507 | + if ($label === NULL) { //apply defaults |
| 508 | + $label = wfMsgForContent('smw_iq_moreresults'); |
| 509 | + } |
| 510 | + if ($label != '') { |
| 511 | + $result .= '<a href="' . $this->mIQ->getQueryURL() . '">' . $label . '</a>'; |
| 512 | + } |
| 513 | + } |
| 514 | + |
| 515 | + $smwgStoreActive = $old_smwgStoreActive; |
| 516 | + return $result; |
| 517 | + } |
| 518 | +} |
| 519 | + |
457 | 520 | ?> |
\ No newline at end of file |