Talk:WikiDB/Repeat tag syntax/Archive
Contents
Table Formatting
Moved from Talk:WikiDB • diff
Hi. I love this use for the Wiki, but I've had one frustration - getting formatted tables. To that end, I have made a modification for my own use, and am passing it on. Feel free to include, ignore, whatever. I just thought it might be useful. I have added two internal tags to the repeat command so that there can be footers and headers added to the output produced from the repeat tag.
The trouble was produced because something like:
{| <repeat table="foo"> |{{{colA}}} |{{{colB}}} |- </repeat> |}
gives totally useless results, and after looking at the MediaWiki Parser.php file, it turns out that unless that is rewritten, it always will - the table parsing is all one-pass, and doesn't preserve state while in an extension.
So, I have turned that into:
{| <repeat table="foo"> <header> {| </header> |{{{colA}}} |{{{colB}}} |- <footer> |} </footer> </repeat> |}
Which now gives the right results. Of course, this requires the following code I added (I know it is rudimentary, but it works...)
Change to WikiDB.php
$Data = $Table->PerformQuery($Where, $Sort); if (trim($Input) == "") $Output = $Table->FormatTableData($Data); else { $outputDef = new WikiDB_OutputFormatDef($Input); $Output = ""; $Data = $Table->NormaliseData($Data); // print_r($Data); if($outputDef->hasHeader()) { $Output .= $outputDef->getHeader() . "\n"; } $RowFormat = $outputDef->getRow(); foreach ($Data as $Row) { $Output .= pWikiDB_ExpandVariables($RowFormat, $Row); } if($outputDef->hasFooter()) { $Output .= $outputDef->getFooter() . "\n"; } // print($Output); }
Also, you must include the following file:
classWikiDB_OutputFormatDef.php
<?php class WikiDB_OutputFormatDef { var $sHeader; var $sFooter; var $sRow; function WikiDB_OutputFormatDef($contents) { $this->sHeader = $this->stripTagItem($contents, 'header'); $this->sFooter = $this->stripTagItem($contents, 'footer'); $this->sRow = $contents; } function hasHeader(){ return strlen($this->sHeader) > 0; } function hasFooter(){ return strlen($this->sFooter) > 0; } function getHeader(){ return $this->sHeader; } function getFooter(){ return $this->sFooter; } function getRow() { return $this->sRow; } function stripTagItem(&$text, $tag) { $tagLen = strlen($tag); $start = stripos($text, '<' . $tag . '>'); if($start == FALSE) { return ''; } $end = stripos($text, '</' . $tag . '>'); if($end == FALSE) { return ''; } $tagTextStart = $start+$tagLen+2; $tagTextLength = $end - $tagTextStart - 1; $tagTextEnd = $end + $tagLen + 3; $tagContents = substr($text, $tagTextStart, $tagTextLength); //strip out the tag $text = substr($text, 0, $start) . substr($text, $tagTextEnd); return $tagContents; } } //END: Class WikiDB_OutputFormatDef ?>
Thanks!
-Jacob 15:50, 8 April 2007 (BST)
Slight Modifications
I integrated your code into my working wiki, because I too saw the uselessness of the table formatting with the repeat tag. However, I made a couple other changes:
- the table begin and end before and after the repeat are not needed, correct? (the header and footer tags print them)
- It seems like the table code was inserting an extra line break in the raw input, which was causing a "br" tag in the output, which was skewing the last column of each table row. To change this, I added the last line here:
if (trim($Input) == "") $Output = $Table->FormatTableData($Data); else { $Input = trim($Input); // get rid of extra line breaks
- However, the trim broke the tag processing in the OutputFormatDef file. On the lines "if $start == FALSE" and "if $end == false", I changed the "==" to "===" to be a more correct equality test, since after the trim those tags for me were in position 0 (== FALSE, but not ===FALSE).
- Finally, "stripos" only works on PHP5, so I changed the 2 lines that use strpos as follows:
OLD: stripos($text, '<' . $tag . '>')); NEW: strpos($text, strtolower('<' . $tag . '>'));
Thanks for your help and work on this. It's a cool extension.
-- Joe Clark (a guest, joeclark AT joeclarkia DOT net)
Using the <repeat> tag to display filtered/sorted Tables
Moved from Talk:WikiDB • diff
I have a suggestion to efficiently display tables using the <repeat> tag when a table has a very large number of fields that a user may want to only see a small portion of the fields.
Expand the parameters of the <repeat> tag such that somehow a user can specify which fields to display in the rendered wiki page. Perhaps this need be only one additional parameter that contains a comma separated list of field names to display whilst not displaying all other fields in the specified table.
Example:
Suppose a table called "Stock Investments" contains the following fields: "stock_symbol", "name", "ceo_name", "address", "main_phone", "annual_revenue", "website", "current_stock_price", "my_shares_count".
There are 9 fields in this table such that when the current <repeat> tag was used to display a sorted and/or filtered table of this table, the rendered table in the browser may proceed beyond the right edge of the screen, so perhaps the <repeat> tag could have a new parameter, filter_columns, that is used as such:
<repeat table="Stock Investments" sort="stock_symbol" filter_columns="stock_symbol,name,current_stock_price,my_shares_count,annual_revenue"></repeat>
And this usage of the <repeat> tag would yield a table such as the following with ONLY the columns specified in the filter_columns parameter AND in the order specified.
stock_symbol | name | current_stock_price | my_shares_count | annual_revenue |
---|---|---|---|---|
AMD | AMD, Inc | 14.30 | 100 | 5.649B |
KEI | Keithley Instruments, Inc | 20 | 12.55 | 0.155B |
MSFT | Microsoft, Inc | 29.47 | 25 | 44.282B |
This feature would be very useful and I recommend including support for this feature in WikiDB.
Thanks! -- Mdrayman 22:02, 30 June 2007 (BST)
- There are a lot of syntax issues with the repeat tag at present, so I need to get those resolved first. It may be that after the fix-up this kind of thing will be sufficiently simple not to warrant a separate syntax, however if this is not the case then it is a good suggestion, and one I will revisit when the other issues are fixed. --HappyDog 13:35, 4 July 2007 (BST)
Adding table properties within repeat tag
Moved from Talk:WikiDB • diff
Could you please provide a patch which enables extensions to <repeat> tag for adding custom attributes for <table> generated by default? This can be the alternative to missing table formating within <repeat>. Especially, I need "style" and "class" attributes to be added to default table output so I can format this table as required and apply the sortable property.
- Hi - The table formatting code is in a bit of a mess at the moment (as is quite a lot of the code...!) and I am in the process of tidying it all up at the moment. I'm quite a long way through this process, but haven't touched on the formatting yet. When I do I'll make it a bit more flexible. All of the major elements that the extension outputs will have a class defined for them, so you can style these as you need. If you need the ability to add your own custom classes as well then I don't see a problem with adding that feature. --HappyDog 12:05, 5 February 2008 (GMT)
A hacked TabbedData solution
I created a somewhat messy but effective hack that allows me to display data from a repeat tag as a table. It is basically a change the repeat tag parser in ParserTags.php. It basically follows the SimpleTable (formerly TabbedData) extension idea. You can see the old code I commented out in the if(0). Please ignore various custom syntax related stuff I've added. It's easy to get the idea.
This hack proved itself as sufficient for almost all my needs, and I have a table with several thousands of entries which is accessed from almost any page in my wiki using a repeat tag.
I think this can be solved if WikiDB hadn't used braces for field markers like regular templates do. not being an expert on the MW code package. I don't know if that's possible, and if indeed that would solve the problem. But I think it's worth giving some though
Here's the code
function wfWikiDB_RepeatTag($Input, $Args, $Parser = NULL) { global $wgOut; global $wgpWikiDBParsingForSave; // If the page is being parsed for a save operation, then do nothing. if ($wgpWikiDBParsingForSave) return ""; // Get $Parser if it was not supplied. if (!$Parser) $Parser =& $GLOBALS['wgParser']; // Repeat tags change all the time, so disable caching. $Parser->disableCache(); // Get table argument (required) if (isset($Args['table'])) $TableName = $Args['table']; else $TableName = ""; // If no table argument, then there is no way of retrieving the data, so we // print a warning and exit. if ($TableName == "") return WikiDB_Parse("''No table specified in <repeat> tag!''", $Parser); // Get sort argument (optional - default to unsorted) if (isset($Args['sort'])) $Sort = $Args['sort']; else $Sort = ""; // Get criteria argument (optional - default to unfiltered) if (isset($Args['criteria'])) $Where = $Args['criteria']; else $Where = ""; if (isset($Args['header'])) $Header = $Args['header']; else $Header = ""; if (isset($Args['bgcolor'])) $Bgcolor = $Args['bgcolor']; else $Bgcolor = ""; if (isset($Args['width'])) $Width = $Args['width']; else $Width = "90%"; // Perform the query and get the resulting data set. $Query = new WikiDB_Query($TableName, $Where, $Sort); $Data = $Query->Run(); // If returned data is not an array, then an error occurred, so we output the // error. if (!is_array($Data)) return $Data; // TODO: Should not be needed here. Rework later code so it doesn't need a // table object, as the query may be run on several tables (JOIN). $Table = new WikiDB_Table($TableName); // If tag contents are empty, display in the standard format. // TODO: FormatTableData should be a static function! if (trim($Input) == "") $Output = $Table->FormatTableData($Data); // If tag contents are non-empty then use it as a template for each row // of the returned data, expanding variables where necessary. else { if(0){ $Output = ""; $Data = $Table->NormaliseData($Data); // print_r($Data); foreach ($Data as $Row) $Output .= pWikiDB_ExpandVariables($Input, $Row); // print($Output); } else{ $Output = '<table style=\"background:#FF0000;\" border="1" cellpadding="2" cellspacing="0" width=' . $Width .' align=center>' . "\n"; $Data = $Table->NormaliseData($Data); // print_r($Data); if ($Bgcolor ==""){ $temp = '<tr BGCOLOR=\'#FF0000\'>' ; } else{ $temp = '<tr BGCOLOR=\'#'. $Bgcolor .'\'>' ; } $temp .= "<th>" . preg_replace('/\t/', '</th><th align=center >', $Header) . "</th>\n"; $temp .= '</tr>'; $Output .= $temp; foreach ($Data as $Row){ $temp = pWikiDB_ExpandVariables($Input, $Row); if($temp != Null && !strstr($temp, "year")){ $temp = str_replace('p>', 'td>', $temp); $wikitab = "<td>" . preg_replace('/\t/', '</td><td>', $temp) . "</td>\n"; $Output .= '<tr>' . $wikitab . '</tr>'; //$Output .= $temp; } } // print($Output); //$Output .= "</td></tr><tr><td>$Input</td></tr></table>\n"; $Output .= "</table>\n"; } } // Return the resulting output, parsed as wiki text. return WikiDB_Parse($Output, $Parser); }
Osishkin 17:38, 1 September 2010 (BST)