Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.php |
— | — | @@ -151,7 +151,7 @@ |
152 | 152 | /** |
153 | 153 | * Return the main text representation of the next result object |
154 | 154 | * (Title or SMWDataValue) as HTML. Convenience method that would |
155 | | - * not be required if Titles would be but special SMWDataValues. |
| 155 | + * be simpler if Titles would be but special SMWDataValues. |
156 | 156 | * |
157 | 157 | * The parameter $linker controls linking of title values and should |
158 | 158 | * be some Linker object (or NULL for no linking). At some stage its |
— | — | @@ -178,6 +178,31 @@ |
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
| 182 | + * Return the main text representation of the next result object |
| 183 | + * (Title or SMWDataValue) as Wikitext. Convenience method that would |
| 184 | + * be simpler if Titles would be but special SMWDataValues. |
| 185 | + * |
| 186 | + * The parameter $linked controls linking of title values and should |
| 187 | + * be non-NULL and non-false if this is desired. |
| 188 | + */ |
| 189 | + public function getNextWikiText($linked = NULL) { |
| 190 | + $object = current($this->content); |
| 191 | + next($this->content); |
| 192 | + if ($object instanceof SMWDataValue) { //print data values |
| 193 | + return $object->getStringValue(); |
| 194 | + } elseif ($object instanceof Title) { // print Title objects |
| 195 | + if ( ($linked === NULL) || ($linked === false) ) { |
| 196 | + return $object->getPrefixedText(); |
| 197 | + } else { |
| 198 | + return '[[' . $object->getPrefixedText() . '|' . $object->getText() . ']]'; |
| 199 | + } |
| 200 | + } else { |
| 201 | + return false; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + /** |
182 | 207 | * Would there be more query results that were |
183 | 208 | * not shown due to a limit? |
184 | 209 | */ |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinters.php |
— | — | @@ -143,7 +143,7 @@ |
144 | 144 | foreach ($row as $field) { |
145 | 145 | $result .= "<td>"; |
146 | 146 | $first = true; |
147 | | - while ( ($text = $field->getNextHTMLText($this->getLinker($firstcol))) !== false) { |
| 147 | + while ( ($text = $field->getNextHTMLText($this->getLinker($firstcol))) !== false ) { |
148 | 148 | if ($first) $first = false; else $result .= '<br />'; |
149 | 149 | $result .= $text; |
150 | 150 | } |
— | — | @@ -178,31 +178,129 @@ |
179 | 179 | */ |
180 | 180 | class SMWListResultPrinter extends SMWResultPrinter { |
181 | 181 | |
| 182 | + protected $mSep = ''; |
| 183 | + protected $mTemplate = ''; |
| 184 | + |
| 185 | + protected function readParameters($params) { |
| 186 | + SMWResultPrinter::readParameters($params); |
| 187 | + |
| 188 | + if (array_key_exists('sep', $params)) { |
| 189 | + $this->mSep = htmlspecialchars($params['sep']); |
| 190 | + } |
| 191 | + if (array_key_exists('template', $params)) { |
| 192 | + $this->mSep = $params['template']; |
| 193 | + } |
| 194 | + } |
| 195 | + |
182 | 196 | protected function getHTML($res) { |
183 | | - ///TODO implement |
184 | | - |
185 | | - // DEBUG |
186 | | - $result = "Test: " . $res->getCount() . " <br/>"; |
187 | | - while ($row = $res->getNext()) { |
188 | | - $result .= "Row: "; |
189 | | - foreach ($row as $field) { |
190 | | - $result .= " (" . $field->getPrintRequest()->getLabel() . ': '; |
191 | | - if ($field->getPrintRequest()->getMode() === SMW_PRINT_ATTS) { //print data values |
192 | | - foreach ($field->getContent() as $value) { |
193 | | - $result .= $value->getUserValue() . " /"; |
| 197 | + global $wgTitle,$smwgStoreActive; |
| 198 | + // print header |
| 199 | + $result = $this->mIntro; |
| 200 | + if ( ('ul' == $this->mFormat) || ('ol' == $this->mFormat) ) { |
| 201 | + $result .= '<' . $this->mFormat . '>'; |
| 202 | + $footer = '</' . $this->mFormat . '>'; |
| 203 | + $rowstart = "\n\t<li>"; |
| 204 | + $rowend = '</li>'; |
| 205 | + $plainlist = false; |
| 206 | + } else { |
| 207 | + if ($this->mSep != '') { |
| 208 | + $listsep = $this->mSep; |
| 209 | + $finallistsep = $listsep; |
| 210 | + } else { // default list ", , , and, " |
| 211 | + $listsep = ', '; |
| 212 | + $finallistsep = wfMsgForContent('smw_finallistconjunct') . ' '; |
| 213 | + } |
| 214 | + $footer = ''; |
| 215 | + $rowstart = ''; |
| 216 | + $rowend = ''; |
| 217 | + $plainlist = true; |
| 218 | + } |
| 219 | + |
| 220 | + if ($this->mTemplate != '') { |
| 221 | + $parser_options = new ParserOptions(); |
| 222 | + $parser_options->setEditSection(false); // embedded sections should not have edit links |
| 223 | + $parser = new Parser(); |
| 224 | + $usetemplate = true; |
| 225 | + } else { |
| 226 | + $usetemplate = false; |
| 227 | + } |
| 228 | + |
| 229 | + // print all result rows |
| 230 | + $first_row = true; |
| 231 | + $row = $res->getNext(); |
| 232 | + while ( $row !== false ) { |
| 233 | + $nextrow = $res->getNext(); // look ahead |
| 234 | + if ( !$first_row && $plainlist ) { |
| 235 | + if ($nextrow !== false) $result .= $listsep; // the comma between "rows" other than the last one |
| 236 | + else $result .= $finallistsep; |
| 237 | + } else $result .= $rowstart; |
| 238 | + |
| 239 | + $first_col = true; |
| 240 | + if ($usetemplate) { // build template code |
| 241 | + $wikitext = ''; |
| 242 | + foreach ($row as $field) { |
| 243 | + $wikitext .= "|"; |
| 244 | + $first_value = true; |
| 245 | + while ( ($text = $field->getNextWikiText($this->getLinker($first_col))) !== false ) { |
| 246 | + if ($first_value) $first_value = false; else $wikitext .= ', '; |
| 247 | + $wikitext .= $text; |
194 | 248 | } |
195 | | - } else { // print Title objects |
196 | | - foreach ($field->getContent() as $title) { |
197 | | - $result .= $title->getPrefixedText() . " /"; |
| 249 | + $first_col = false; |
| 250 | + } |
| 251 | + $result .= '{{' . $this->mTemplate . str_replace('=','|', $wikitext) . '}}'; // encode '=' for use in templates (templates fail otherwise) |
| 252 | + } else { // build simple list |
| 253 | + $first_col = true; |
| 254 | + $found_values = false; // has anything but the first column been printed? |
| 255 | + foreach ($row as $field) { |
| 256 | + $first_value = true; |
| 257 | + while ( ($text = $field->getNextHTMLText($this->getLinker($first_col))) !== false ) { |
| 258 | + if (!$first_col && !$found_values) { // first values after first column |
| 259 | + $result .= ' ('; |
| 260 | + $found_values = true; |
| 261 | + } elseif ($found_values || !$first_value) { |
| 262 | + // any value after '(' or non-first values on first column |
| 263 | + $result .= ', '; |
| 264 | + } |
| 265 | + if ($first_value) { // first value in any column, print header |
| 266 | + $first_value = false; |
| 267 | + if ( $this->mShowHeaders && ('' != $field->getPrintRequest()->getLabel()) ) { |
| 268 | + $result .= $field->getPrintRequest()->getLabel() . ' '; |
| 269 | + } |
| 270 | + } |
| 271 | + $result .= $text; // actual output value |
198 | 272 | } |
| 273 | + $first_col = false; |
199 | 274 | } |
200 | | - $result .= ")"; |
| 275 | + if ($found_values) $result .= ')'; |
201 | 276 | } |
202 | | - print "<br/>\n"; |
| 277 | + $result .= $rowend; |
| 278 | + $first_row = false; |
| 279 | + $row = $nextrow; |
203 | 280 | } |
204 | | - if ($res->hasFurtherResults()) { |
205 | | - $result .= "(more results ...)<br/>\n"; |
| 281 | + |
| 282 | + if ($usetemplate) { |
| 283 | + $old_smwgStoreActive = $smwgStoreActive; |
| 284 | + $smwgStoreActive = false; // no annotations stored, no factbox printed |
| 285 | + $parserOutput = $parser->parse($result, $wgTitle, $parser_options); |
| 286 | + $result = $parserOutput->getText(); |
| 287 | + $smwgStoreActive = $old_smwgStoreActive; |
206 | 288 | } |
| 289 | + |
| 290 | + if ($this->mInline && $res->hasFurtherResults()) { |
| 291 | + $label = $this->mSearchlabel; |
| 292 | + if ($label === NULL) { //apply defaults |
| 293 | + if ('ol' == $this->mFormat) $label = ''; |
| 294 | + else $label = wfMsgForContent('smw_iq_moreresults'); |
| 295 | + } |
| 296 | + if (!$first_row) $result .= ' '; // relevant for list, unproblematic for ul/ol |
| 297 | + if ($label != '') { |
| 298 | + $result .= $rowstart . '<a href="' . $res->getQueryURL() . '">' . $label . '</a>' . $rowend; |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + // print footer |
| 303 | + $result .= $footer; |
| 304 | + |
207 | 305 | return $result; |
208 | 306 | } |
209 | 307 | } |