<TEI:TOK>

Main Menu


Powered by <TEI:TOK>
Maarten Janssen, 2014-

TEITOK Help Pages

Build your own module

TEITOK has a modular design, in which it is easy to add custom functionality. Custom modules are PHP scripts, that are not placed inside the common TEITOK folder, but in the Sources folder inside the project you want to add the module to. The only requirement on a custom module is that it places all the output it generates into a variable maintext, after which the system will put it into the right place. So the hello world module (named Sources/helloworld.php in the project folder) looks as follows:

<?xml
    $maintext = "<h1>Hello World!</h1>";
?>

If the module presents a different view on the XML file, you can define it in the views sections of your settings.xml file. That will provide users automatically with the option to switch to your view from any XML file view. The name of the XML file is passed as a URL variable cid, which points to the file inside the folder xmlfiles. The output does not have to be generated by PHP, but can come from outside: if we have a program tei2txt in our /usr/local/bin that converts TEI to plain text, we can use that to easily create a module that provide a plain-text version of the text as follows:

<?php
    
    $raw = shell_exec("/usr/local/bin/tei2txt xmlfiles/{$_GET['cid']}");
    
    $maintext = "<h1>Raw text</h1>
        <pre>$raw</pre>";
    
?>

The name of the custom PHP script is used directly in the URL - so our helloworld.php would be accessed as index.php?action=helloworld. If you give the PHP script the same name as an existing script, it will overrule the standard function associated with it. This should ideally only be done if the custom script is an adaptation of an existing function, otherwise it is more recommendable to not use an existing name.

Easy XML Display

To make interacting with XML files easier, you can use the TTXML class. This will automatically open your XML file so that you can build tools on top of it. The standard way in which the XML file is displayed in TEITOK is to just put the raw XML into an element with the id "mtxt", which will make CSS take care of the rest. So we can use the following code to display the full text, with the number of paragraphs in the text printed above it:

<?php
    
    require ("$ttroot/common/Sources/ttxml.php");
    $ttxml = new TTXML();
	
    $maintext .= "<p>Paragraph count: ".count($ttxml->xml->xpath("//text//p"))."</p>";

    $maintext .= "<div id='mtxt'>".$ttxml->asXML()."</div>";

?>

Easy Corpus Access

To make interacting with CQP corpus easier, you can use the CWCQP class. This will automatically open your corpus and let you query it. The following short code will display how many tokens each text in the corpus contains:

<?php
    
    require ("$ttroot/common/Sources/cwcqp.php");
    $cqp = new CQP();

    $cqp->exec('Matches = [word=".*"]');
    $results = $cqp->exec('group Matches match text_id');
    foreach ( explode("\n", $results) as $line ) {
        list ( $id, $count ) = explode ( "\t", $line );
        $maintext .= "<p>Text $id: $count tokens"
    };

?>

Back to index