Author : Daniel Renfro
Introduced in version 0.8, the loader class houses methods to load data into tables in the wiki.
Reading the page on Using Subversion with the wikis is a good reference, you’ll need some background in SVN.
The loader currently resides in the SVN repository at
svn://tetramer.tamu.edu/wiki-extensions/trunk/TableEdit/
The loader is a PHP class which must be instantiated to use.
See the Example script below.
The loader has a few options that can be set in the instantiation code.
option | input value | explanation |
---|---|---|
setVerbose() | none | Will force the loader to tell you more about what is going on behind the scenes. It’s not very pretty output because it is activating print statements throughout the code, but it will tell you more than normal. |
setUser() | A valid Username from the wiki | This will use the given user’s id as the owner of the loaded rows. It defaults to the WikiEntryBot user (which makes the rows public). |
debug() | none | Tells the loader to go through all the steps of loading, but not actually do anything permanent. This has the effect of making things run much faster because the wiki isn’t accessing the database to do writes. |
printInfo() | true/false | Will print a few lines at the beginning of execution to tell you about what wiki and what user you’re working with. |
Name | Info | Code / Usage Ex. |
---|---|---|
do_misc_features() |
This is where you put code to interpret things in the 7th column. To make a string that goes in that column use the PHP function http_build_query() . This string then gets cut up into an array by parse_str() . |
code |
appendRow() |
This method takes care of when the incoming row gets appended to the box. | code |
mergeRows() |
This method gets called when a new row is trying to be merged into an existing box. It can be <a href=”http://en.wikipedia.org/wiki/Overriding” class=”external text” | |
rel=”nofollow”>overridden</a> to behave differently. | code | |
clearOldRows() |
This method gets called with the flag “clear” gets put in the update_type field of IFALT (col 6.) It will delete all the rows in a box. Useful for emptying tables for reloads. | code |
TableEdit ships with a sample script, but I’ll add it here, too. This is a basic script which will:
loadFromFile()
)<?php
/*
Example loader script for TableEdit.
*/
// Setup to use the wiki from the command line
$options = getopt('w:');
if(isset($options['w'])){
if( is_file($options['w'] . "/AdminSettings.php") ){
require_once($options['w'] . "/maintenance/commandLine.inc");
} else {
die("Cannot find AdminSettings.php in {$options['w']}. Please check the paths.\n");
}
} else {
die("Please use the -w flag to set a path to the wiki.\n");
}
// Instantiate the object
$loader = new TableEdit_Loader;
// set some options
$loader->setVerbose();
$loader->setUser()
$loader->printInfo();
// load pretty much anything into tables from either an *.ifalt file.
$loader->loadFromFile(array_pop($argv));
?>
Once you have a script like above, with the right variables and paths, you can then run that script to do the loading. In a command-line window, browse to the path where your script lives, and run it using the command
php5 loader.sample.php /path/to/input_file
If you want to save the output into a file for later viewing, you can pipe the output like so:
php5 loader.sample.php >& output.txt
You can also separate the output based on STDOUT and STDERR with this type of command:
(php5 loader.sample.php > load.out) >& load.err
It takes a path to a formatted input file, and has a few options.
The loader currently will accept one type of input:
On the commandline the loader will tell you what page it’s working on. It will print any error messages to STDERR.
There is a key method in the loader class which compares two rows. There are four return values:
Below is a table that explains these cases in more depth:
The new row and the old row match exactly. |
|
|
A = B |
true |
The new row is a superset of the old row. |
|
|
A is a subset of B |
$new_row_obj |
The new row and the old row dont' match at any field. |
|
|
Pr(A ∩ B) = 0 |
false |
The new row only matches part of the old row. |
|
|
A ∩ B = ø |
false |
public function do_misc_features( $array ){
foreach ($array as $name => $feature) {
// Here is where you can add code to define what to do for things in the 7th column.
if ($name == 'row_style') $this->new_row->row_style = trim($feature);
print "added row style";
return true;
}
}
public function appendRow($new_row_obj){
$this->box_obj->rows[] = $new_row_obj;
if ($this->verbose) print "\tappended row, \n";
return;
}
public function mergeRows($new_row_obj){
// compare the new row to each of the existing rows
$results = array();
foreach ($this->box_obj->rows as $old_row) {
$results[] = $this->compareRows($old_row, $new_row_obj);
}
if (in_array('exact', $results)) {
// skip, this row is already in the box
return;
} elseif (in_array('subset', $results)) {
for($i=0, $c=count($results); $i<$c; $i++){
if($results[$i] == 'subset'){
foreach($this->box_obj->rows[$i]->row_metadata as $old_row_metadata_obj){
$new_row_obj->metadata[] = $old_row_metadata_obj;
}
$this->box_obj->delete_row($i);
}
}
$this->box_obj->rows[] = $new_row_obj;
return;
} else {
$this->appendRow($new_row_obj);
}
}
function clearOldRows($box, $time){
// compare timestamps and delete row if older
if(!$this->debug){
foreach($box->rows as $row){
if($time > $row->timestamp){
$row->delete_row();
}
}
}
return;
}