Index: includes/SMW_InlineQueries.php =================================================================== --- includes/SMW_InlineQueries.php (revision 22372) +++ includes/SMW_InlineQueries.php (working copy) @@ -92,6 +92,7 @@ function smwfRegisterInlineQueries( $semantic, $mediawiki, $rules ) { global $wgParser; $wgParser->setHook( 'ask', 'smwfProcessInlineQueries' ); + $wgParser->setFunctionHook( 'ask', 'smwfProcessInlineQueriesParserFunction' ); return true; // always return true, in order not to stop MW's hook processing! } @@ -111,6 +112,44 @@ } } +/** + * Performs regular expression search or replacement. + * @param Parser $parser Instance of running Parser. + * @return String Result of executing query. + */ +function smwfProcessInlineQueriesParserFunction( &$parser ) { + global $smwgIQEnabled; + if (!$smwgIQEnabled) return wfMsgForContent('smw_iq_disabled'); + $params = func_get_args(); + array_shift( $params ); + $query = ''; + $args = array(); + foreach ($params as $key=>$param) { + if (strpos($param,'=')===false) { + $param = "query=$param"; + } + list($name, $val) = split('=',$param,2); + $name = strtolower(trim($name)); + $args[$name] = trim($val); + if ($name=='query') $query .= $val; + } + $iq = new SMWInlineQuery($args, true, 'wiki'); + return $iq->getHTMLResult($query); +} + +/** + * Adds magic words for SMW parser functions + * @param Array $magicWords + * @param $langCode + * @return Boolean Always true + */ +function smwfParserFunctionMagic( &$magicWords, $langCode ) { + $magicWords['ask'] = array( 0, 'ask' ); + return true; +} + + + // Constants for distinguishing various modes of printout; // used to record what part of a query should be printed; see mPrint field // below @@ -215,12 +254,14 @@ // other stuff private $mHTMLPrinter; // is the current printer expecting HTML (true) or wikitext (false) for labels? + private $mPrintContext; // context in which results are to be printed: 'html'(default) or 'wiki' private $mLinker; // we make our own linker for creating the links -- TODO: is this bad? - public function SMWInlineQuery($param = array(), $inline = true) { + public function SMWInlineQuery($param = array(), $inline = true, $printContext = 'html') { global $smwgIQDefaultLimit, $smwgIQDefaultLinking; $this->mInline = $inline; + $this->mPrintContext = $printContext; $this->mLimit = $smwgIQDefaultLimit; $this->mOffset = 0; @@ -469,7 +510,7 @@ if (!isset($smwgIQRunningNumber)) { $smwgIQRunningNumber = 0; } else { $smwgIQRunningNumber++; } - + // This should be the proper way of substituting templates in a safe and comprehensive way $parser = new Parser(); $parserOptions = new ParserOptions(); @@ -557,11 +598,12 @@ switch ($this->mFormat) { case 'table': case 'broadtable': - $printer = new SMWTablePrinter($this,$sq); + if ($this->mPrintContext=='html') { + $printer = new SMWTablePrinter($this,$sq); + } else { + $printer = new SMWWikitextTablePrinter($this,$sq); + } break; - case 'ul': case 'ol': case 'list': - $printer = new SMWListPrinter($this,$sq); - break; case 'timeline': case 'eventline': $printer = new SMWTimelinePrinter($this,$sq); break; @@ -571,7 +613,13 @@ case 'template': $printer = new SMWTemplatePrinter($this,$sq); break; - default: $printer = new SMWListPrinter($this,$sq); + case 'ul': case 'ol': case 'list': default: + if ($this->mPrintContext=='html') { + $printer = new SMWListPrinter($this,$sq); + } else { + $printer = new SMWWikitextListPrinter($this,$sq); + } + break; } $result = $printer->printResult(); $this->dbr->freeResult($this->mQueryResult); // Things that should be free: #42 "Possibly large query results" @@ -962,14 +1010,19 @@ if ($title === NULL) { return $text; // TODO maybe report an error here? } elseif ( $linked ) { - if ( NULL === $label ) $label = $title->getText(); - if ($this->mHTMLPrinter) { - if ($exists) - return $this->mLinker->makeKnownLinkObj($title, $label); - else return $this->mLinker->makeLinkObj($title, $label); - } else { - return '[[' . $title->getPrefixedText() . '|' . $label . ']]'; - } + if ($this->mPrintContext=='wiki') { + if (in_array($title->getNameSpace(), array(NS_IMAGE, NS_CATEGORY))) $prefix = ':'; + return '[['.$prefix.$title->getPrefixedText().'|'.$title->getText().']]'; + } else { + if ( NULL === $label ) $label = $title->getText(); + if ($this->mHTMLPrinter) { + if ($exists) + return $this->mLinker->makeKnownLinkObj($title, $label); + else return $this->mLinker->makeLinkObj($title, $label); + } else { + return '[[' . $title->getPrefixedText() . '|' . $label . ']]'; + } + } } else { return $title->getText(); // TODO: shouldn't this default to $label? } @@ -1091,4 +1144,4 @@ } } -?> \ No newline at end of file +?> Index: includes/SMW_GlobalFunctions.php =================================================================== --- includes/SMW_GlobalFunctions.php (revision 22372) +++ includes/SMW_GlobalFunctions.php (working copy) @@ -36,7 +36,7 @@ * does not adhere to the naming conventions. */ function enableSemantics($namespace = "", $complete = false) { - global $smwgNamespace, $wgExtensionFunctions; + global $smwgNamespace, $wgExtensionFunctions, $wgHooks; $smwgNamespace = $namespace; // The dot tells that the domain is not complete. It will be completed @@ -44,6 +44,7 @@ // it is not needed in many cases. if (!$complete && !($smwgNamespace === '')) $smwgNamespace = ".$smwgNamespace"; $wgExtensionFunctions[] = 'smwgSetupExtension'; + $wgHooks['LanguageGetMagic'][] = 'smwfParserFunctionMagic'; return true; } Index: includes/SMW_QueryPrinters.php =================================================================== --- includes/SMW_QueryPrinters.php (revision 22372) +++ includes/SMW_QueryPrinters.php (working copy) @@ -80,6 +80,67 @@ } /** + * Wikitext printer for tabular data. + */ +class SMWWikitextTablePrinter implements SMWQueryPrinter { + private $mIQ; // the querying object that called the printer + private $mQuery; // the query that was executed and whose results are to be printed + + public function SMWWikitextTablePrinter($iq, $query) { + $this->mIQ = $iq; + $this->mQuery = $query; + } + + public function printResult() { + global $smwgIQRunningNumber; + + // print header + if ('broadtable' == $this->mIQ->getFormat()) + $widthpara = ' width="100%"'; + else $widthpara = ''; + $result = $this->mIQ->getIntro() . "{| class=\"smwtable\"$widthpara id=\"querytable" . $smwgIQRunningNumber . "\"\n"; + if ($this->mIQ->showHeaders()) { + #$result .= "|-"; + foreach ($this->mQuery->mPrint as $print_data) { + $result .= "! " . $print_data[0] . "\n"; + } + } + + // print all result rows + while ( $row = $this->mIQ->getNextRow() ) { + $result .= "|- \n"; + $firstcol = true; + foreach ($this->mQuery->mPrint as $print_data) { + $iterator = $this->mIQ->getIterator($print_data,$row,$firstcol); + $result .= "| "; + $first = true; + while ($cur = $iterator->getNext()) { + if ($first) $first = false; else $result .= '
'; + $result .= $cur[0]; + } + $firstcol = false; + $result .= "\n"; + } + } + + if ($this->mIQ->isInline() && $this->mIQ->hasFurtherResults()) { + $label = $this->mIQ->getSearchLabel(); + if ($label === NULL) { //apply default + $label = wfMsgForContent('smw_iq_moreresults'); + } + if ($label != '') { + $result .= "|- \n| class=\"sortbottom\" colspan=\"" . count($this->mQuery->mPrint) . '\" | [' . $this->mIQ->getQueryURL() . ' ' . $label . ']'."\n\n"; + } + } + + // print footer + $result .= "|}\n"; + + return $result; + } +} + +/** * Printer for list data. Somewhat confusing code, since one has to iterate through lists, * inserting texts in between their elements depending on whether the element is the first * that is printed, the first that is printed in parentheses, or the last that will be printed. @@ -212,6 +273,106 @@ } /** + * Printer for wikitext list data. Somewhat confusing code, since one has to iterate through lists, + * inserting texts in between their elements depending on whether the element is the first + * that is printed, the first that is printed in parentheses, or the last that will be printed. + * Maybe one could further simplify this. + */ +class SMWWikitextListPrinter implements SMWQueryPrinter { + private $mIQ; // the querying object that called the printer + private $mQuery; // the query that was executed and whose results are to be printed + + public function SMWWikitextListPrinter($iq, $query) { + $this->mIQ = $iq; + $this->mQuery = $query; + } + + public function printResult() { + // print header + $result = $this->mIQ->getIntro(); + if ( ('ul' == $this->mIQ->getFormat()) || ('ol' == $this->mIQ->getFormat()) ) { + $result .= '';//'<' . $this->mIQ->getFormat() . '>'; + $footer = '';//'mIQ->getFormat() . '>'; + $rowstart = ('ul' == $this->mIQ->getFormat()?'* ':'# '); + $rowend = "\n"; + $plainlist = false; + } else { + $params = $this->mIQ->getParameters(); + if (array_key_exists('sep', $params)) { + $listsep = htmlspecialchars(str_replace('_', ' ', $params['sep'])); + $finallistsep = $listsep; + } else { // default list ", , , and, " + $listsep = ', '; + $finallistsep = wfMsgForContent('smw_finallistconjunct') . ' '; + } + $footer = ''; + $rowstart = ''; + $rowend = ''; + $plainlist = true; + } + + // print all result rows + $first_row = true; + $row = $this->mIQ->getNextRow(); + while ( $row ) { + $nextrow = $this->mIQ->getNextRow(); // look ahead + if ( !$first_row && $plainlist ) { + if ($nextrow) $result .= $listsep; // the comma between "rows" other than the last one + else $result .= $finallistsep; + } else $result .= $rowstart; + + $first_col = true; + $found_values = false; // has anything but the first coolumn been printed? + foreach ($this->mQuery->mPrint as $print_data) { + $iterator = $this->mIQ->getIterator($print_data,$row,$first_col); + $first_value = true; + while ($cur = $iterator->getNext()) { + if (!$first_col && !$found_values) { // first values after first column + $result .= ' ('; + $found_values = true; + } elseif ($found_values || !$first_value) { + // any value after '(' or non-first values on first column + $result .= ', '; + } + if ($first_value) { // first value in any column, print header + $first_value = false; + if ( $this->mIQ->showHeaders() && ('' != $print_data[0]) ) { + $result .= $print_data[0] . ' '; + } + } + $result .= $cur[0]; // actual output value + } + $first_col = false; + } + if ($found_values) $result .= ')'; + $result .= $rowend; + $first_row = false; + $row = $nextrow; + } + + if ($this->mIQ->isInline() && $this->mIQ->hasFurtherResults()) { + $label = $this->mIQ->getSearchLabel(); + if ($label === NULL) { //apply defaults + if ('ol' == $this->mIQ->getFormat()) $label = ''; + else $label = wfMsgForContent('smw_iq_moreresults'); + } + if (!$first_row && !in_array($this->mIQ->getFormat(), array('ol','ul'))) $result .= ' '; // relevant for list + if ($label != '') { + global $wgServer; + $result .= $rowstart . '[' . $wgServer . $this->mIQ->getQueryURL() . ' ' . $label . ']' . $rowend; + } + } + + // print footer + $result .= $footer; + + return $result; + } + + +} + +/** * Printer for timeline data. */ class SMWTimelinePrinter implements SMWQueryPrinter {