Skip to content

Instantly share code, notes, and snippets.

@foxel
Last active June 7, 2019 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxel/94f07f5c4f7c36082e50760f10141048 to your computer and use it in GitHub Desktop.
Save foxel/94f07f5c4f7c36082e50760f10141048 to your computer and use it in GitHub Desktop.
CSV Data MediaWiki extension
<?php
/*
* CSV Data MediaWiki extension.
*
* Additional parameters allowed in the tag are:
* sep Specify a separator; ',' is default.
* head Specify a heading; "head=top" makes the first row a heading,
* "head=left" makes the first column a heading, "head=topleft"
* does both.
* Based on http://www.mediawiki.org/wiki/Extension:CsvTable
*/
$wgExtensionFunctions[] = 'wfCsvTable';
$wgExtensionCredits['parserhook'][] = array(
'name'=>'CSVTable',
'author'=>'Andre F. Kupreychik',
'url'=>'',
'description'=>'Convert csv/tsv data into a Wiki table',
);
/**
* Setup CsvTable extension.
* Sets a parser hook for <tab></tab>.
*/
function wfCsvTable() {
new CsvTable();
}
/**
* Class CsvTable
*
* @author Andrey F. Kupreychik
*/
class CsvTable {
/**
* The named separators. Simplifies special chars usage.
*/
private $_namedSeparators = array(
'tab' => "\t",
);
/**
* Construct the extension and install it as a parser hook.
*/
public function __construct() {
global $wgParser;
$wgParser->setHook('csv', array(&$this, 'hook'));
}
/**
* The hook function. Handles <csv></csv>.
* Receives the table content and <csv> parameters.
*/
public function hook($csvData, $argv, $parser) {
// The default field separator.
$sep = ',';
// Default value for using table headings.
$head = null;
// Build the table parameters string from the tag parameters.
// The 'sep' and 'head' parameters are special, and are handled
// here, not passed to the table.
$params = '';
foreach ($argv as $key => $val) {
if ($key == 'sep') {
$sep = $val;
} else if ($key == 'head') {
$head = $val;
} else {
$params .= ' ' . $key . '="' . $val . '"';
}
}
if (array_key_exists($sep, $this->_namedSeparators)) {
$sep = $this->_namedSeparators[$sep];
}
// Parse and convert the table body.
$wikiText = $this->convertTable($csvData, $head, $sep);
// Wrap the body in table tags, with the table parameters.
$wikiTable = "{|" . $params . "\n" . $wikiText . "|}";
// Done. Parse the result, so that the table can contain Wiki text.
$ret = $parser->parse(
$wikiTable,
$parser->mTitle,
$parser->mOptions,
false,
false
);
$HTML = trim( str_replace("</table>\n\n", "</table>", $ret->getText()) );
return $HTML;
}
/**
* Convert tabbed data into a Wiki-markup table body.
*/
private function convertTable($csvData, $head, $sep) {
$wikiTab = '';
// Remove initial and final newlines.
$csvData = trim($csvData);
$buff = fopen('php://memory', 'w+');
fwrite($buff, $csvData);
rewind($buff);
// Split the input into lines, and convert each line to table format.
$row = 0;
while ($line = fgetcsv($buff, null, $sep, '"', '\\')) {
$wikiTab .= "|-\n";
$bar = strpos($head, 'top') !== false && $row == 0 ? '!' : '|';
$col = 0;
foreach ($line as $field) {
$cbar = strpos($head, 'left') !== false && $col == 0 ? '!' : $bar;
$wikiTab .= $cbar . " " . $field . "\n";
$col++;
}
$row++;
}
fclose($buff);
return $wikiTab;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment