<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class dico_tidy {
private $file;
private $DICO;
private $TIDY;
private $charset;
private $lang;
public function __construct(){
if ( ! CORE::isphpModLoaded('tidy') ){
if ( ! CORE::LoadphpMod('tidy') ){ return FALSE; }
}
}
public function load($file){
$this->DICO = tidy_parse_file($file, array(),'utf8');
$this->TIDY = tidy_get_html($this->DICO);
$this->file=$file;
if ( FALSE === $this->TIDY ){
errors::raise("Invalid Dictionnary : $file could not be loaded into Tidy", CORE_LOG_ERROR, 'LANG');
$this->unload();
return FALSE;
}
//looking for lang attribut
if ( ! isset($this->TIDY->attribute['lang']) ){
errors::raise("Invalid Dictionnary : $file, missing lang attribut in html element", CORE_LOG_ERROR, 'LANG');
$this->unload();
return FALSE;
}
$this->lang = $this->TIDY->attribute['lang'];
//looking for charset
$this->TIDY = tidy_get_head($this->DICO);
foreach($this->TIDY->child as $k => $child ){
if ( $child->name == 'meta' AND isset($child->attribute['content']) ){
$b = $child->attribute['content'];
$b = split(';', $b);
$this->charset = trim(str_replace('charset=', '', $b[1]));
$this->TIDY = tidy_get_body($this->DICO);
//errors::raise("Dictionnary $this->file = lang : $this->lang, charset : $this->charset", CORE_LOG_NOTICE, 'LANG');
return TRUE;
}
}
errors::raise("Invalid Dictionnary : $file, missing meta element with content attribut in head", CORE_LOG_ERROR, 'LANG');
$this->unload();
return FALSE;
}
public function unload(){
if ( isset($this->DICO) ){ unset($this->DICO, $this->TIDY); }
}
public function lang(){
if ( !isset($this->DICO) ){ return FALSE; }
return $this->lang;
}
public function get($ref, $args=NULL){
if ( !isset($this->DICO) ){ return FALSE; }
if ( $args === NULL ){
$o = $this->searchfor('words', $ref);
} else {
$o = $this->get_sentence($ref, $args);
}
if ( FALSE === $o ){ return $o; }
if ( $this->charset != CORE::getintcharset() ){
$o = langs::recode($o, $this->charset, CORE::getintcharset() );
}
return $o;
}
private function get_sentence($ref, $args){
$o = $this->searchfor('sentences', $ref);
if ( FALSE === $o ){ return FALSE; }
if ( is_array($args) ){
foreach($args as $k => $val ){ $o = str_replace('%'.$k.'%', $val, $o); }
}
return $o;
}
private function searchfor($node, $ref){
foreach($this->TIDY->child as $k => $child ){
if ( $child->name == 'div' AND isset($child->attribute['id']) ){
if ( $child->attribute['id'] == $node ){
foreach($child->child as $k => $entry ){
if ( $entry->name == 'p' AND isset($entry->attribute['id']) ){
if ( $entry->attribute['id'] == $ref ){ $o = $this->rebuild($entry); }
}
}
}
}
}
if ( ! isset($o) ){ return FALSE; }
return $o;
}
public function readall(){
foreach($this->TIDY->child as $k => $child ){
if ( $child->name == 'div' AND isset($child->attribute['id']) ){
if ( $child->attribute['id'] == 'words' OR $child->attribute['id'] == 'sentences' ){
if ( $child->attribute['id'] == 'words' ){
$n = 'W_';
} else {
$n = 'S_';
}
if ( is_array($child->child) ){
foreach($child->child as $k => $entry ){
if ( $entry->name == 'p' AND isset($entry->attribute['id']) ){
//$o[$n.$entry->attribute['id']] = $this->rebuild($entry);
$o[$n.md5($entry->attribute['id'])] = $this->rebuild($entry);
}
}
}
}
}
}
if ( isset($o) ){ return $o; }
return FALSE;
}
private function rebuild($tidy){
$o='';
foreach($tidy->child as $k => $node ){ $o .= $node->value; }
return $o;
}
}