<?
define("WS_WEB",true);
require_once( "WSInit.php" );
require_once("WikiAccess.php");
require_once("WorldStore.php");
define('SECTION_OFF',0);
define('SECTION_TERSE',1);
define('SECTION_LIST',2);
define('SECTION_EXPANDED',3);
define('SECTION_TERM',1);
define('SECTION_CONCEPT',2);
define('SECTION_RESOURCE',3);
class WikiWordPage {
var $world;
var $format;
var $csv;
function WikiWordPage ( &$world, $format = "html" ) {
$this->dict= $world;
$this->format= $format;
if ( $this->format == "csv" ) {
require_once("CSV.php");
$this->csv= new CSV();
}
}
function showStats( $lang = NULL ) {
$stats= $this->dict->getStats( $lang );
if ( $this->csv ) {
$this->csv->csvFromMap( $stats );
}
else {
$langs= $this->dict->getLanguages( $lang );
print "\n<p>Stats for: ";
if ( $lang ) {
print "<b>";
$u= "WikiWord.php?stats=1";
print "[<a href='$u'>all</a>] ";
print "</b>";
}
foreach ($langs as $l) {
$u= "WikiWord.php?stats=1&lang=$l";
if ($l == $lang) print "[<b>$l</b>] ";
else print "[<a href='$u'>$l</a>] ";
}
print "</p>\n";
print "<ul>\n";
foreach ($stats as $k => $v) {
print "\t\t<li><b>".htmlspecialchars($k).":</b> ".htmlspecialchars($v)."</li>\n";
}
print "</ul>\n";
}
}
function listTerms( $lang = NULL, $max = 100, $start = NULL ) {
$terms= $this->dict->listTerms( $lang, TYPE_PAGE, false, $max, $start );
$this->showTermList( $terms );
}
function randomTerm( $lang = NULL ) {
$t= $this->dict->randomTerm( $lang, TYPE_PAGE, false );
if (!is_array($t)) {
print "\n\t\t<div class='error'>failed to select random term</div>\n";
return;
}
extract ( $t );
print "\n\t\t<div class='term' id='term-$id'>\n";
$this->showTerm($id,$lang,$term,$type,true);
print "\n\t\t</div>\n";
}
function termHTML( $t, $mode='meanings' ) {
extract($t);
$suffix=" <span class='suffix'>[$type]</span>";
$html= htmlspecialchars($term);
if ($mode) {
$u= $_SERVER['PHP_SELF'];
$u.= '?term='.urlencode($term).'&mode='.urlencode($mode);
$html= "<a href='$u'>$html</a>";
}
$html= "\"<span class='wterm'><span class='wterm-$type'>$html$suffix</span></span>\"";
return $html;
}
function conceptHTML( $c, $mode=NULL ) {
extract($c);
$suffix=" <span class='suffix'>[$type]</span>";
$html= htmlspecialchars($name);
if ($mode) {
$u= $_SERVER['PHP_SELF'];
$u.= '?concept='.urlencode($name).'&mode='.urlencode($mode);
$html= "<a href='$u'>$html</a>";
}
$html= "<span class='wconcept'><span class='wconcept-$type'>$html$suffix</span></span>";
return $html;
}
function resourceHTML( $rc, $link=true ) {
extract($rc);
$suffix=" <span class='suffix'>[$type]</span>";
$html= htmlspecialchars($corpus_name.': '.$name);
if ($link) {
#FIXME: cache!
$u= "http://$corpus_name/w/index.php?title=".urlencode($name);
$html= "<a href='$u'>$html</a>";
}
$html= "<span class='wresource'><span class='wresource-$type'>$html$suffix</span></span>";
return $html;
}
function listTermsLike( $term, $mode, $title ) {
print "\n";
print "<h3>$title</h3>";
$res= $this->dict->queryTermsLike( $term, 100 );
$c= 0;
print '<div class="entries">';
while ($row = $this->dict->fetchRow( $res )) {
if ($expanded) $this->conceptBox( $row );
else {
print '<div class="entry">';
$this->termHTML( $row, $mode );
print '</div>';
}
$c++;
}
$this->dict->freeResult( $res );
if (!$c) {
print "<div class='error'>no terms found!</div>";
}
print '</div>';
}
function showTerm( $term ) {
if (is_array($term)) $t= $term;
else $t= $this->dict->getTerm($term);
if (!$t) {
$this->listTermsLike($term,'meanings',"Expression \"<span class='wterm'>".htmlspecialchars($term)."</span>\" not found");
}
else {
termBox( $term );
}
}
function termBox( $term, $sections = NULL ) {
if (is_null($sections)) $sections= array(
'meaning' => SECTION_EXPANDED
);
if (is_array($term)) $t= $term;
else $t= $this->dict->getTerm($term);
print "\n";
if (!$t) {
print "<div class='notice'>term not found: ".$this->termHTML($t,NULL)."</div>";
return;
}
print "<div class='bterm'>";
print "<div class='boxtitle'>".$this->termHTML($t,NULL)."</div>");
foreach ( $sections as $rel => $exp ) {
if ($exp!=SECTION_OFF) $this->printConceptSection($sect,'Meanings',$exp);
}
print "\n</div>";
}
function printConceptSection( $rel, $term, $title, $exp = SECTION_EXPANDED ) {
$res= $this->dict->queryConceptRecordsBy( $rel, $term, 100 );
$this->printSection( CONCEPT_SECTION, $res, $title, $exp );
$this->dict->freeResult( $res );
}
function printTermSection( $rel, $key, $title, $exp = SECTION_EXPANDED ) {
$res= $this->dict->queryTermRecordsBy( $rel, $key, 100 );
$this->printSection( CONCEPT_SECTION, $res, $title, $exp );
$this->dict->freeResult( $res );
}
function printSection( $type, $res, $title, $exp ) {
print "\n";
if ($exp==SECTION_EXPANDED) print "<div class='block'>";
else if ($exp==SECTION_TERSE) print "<div class='section'>";
else print "<span class='list'>";
if ($title) {
if ($exp==SECTION_EXPANDED) print "<div class='blocktitle'>$title</div>";
else if ($exp==SECTION_TERSE) print "<div class='sectiontitle'>$title</div>: ";
else print "<div class='listtitle'>$title</div>";
}
$c= 0;
while ($row = $this->dict->fetchRow( $res )) {
if ($exp==SECTION_EXPANDED && $type!=RESOURCE_SECTION) {
if ($type==CONCEPT_SECTION) $this->conceptBox( $row );
else if ($type==TERM_SECTION) $this->termBox( $row );
}
else {
if ($exp==SECTION_TERSE) {
if ($c) print ", ";
}
else print '<div class="entry">';
$this->conceptHTML( $row );
if ($type==CONCEPT_SECTION) $this->conceptHTML( $row );
else if ($type==TERM_SECTION) $this->termHTML( $row );
else if ($type==RESOURCE_SECTION) $this->resourceHTML( $row );
if ($exp!=SECTION_TERSE) print '</div>';
}
$c++;
}
if (!$c) {
if ($exp==SECTION_TERSE) print "<span class='notice'>nothing found!</span>";
else print "<div class='notice'>nothing found!</div>";
}
print "</div>";
return $c;
}
function showExpressions( $name ) {
$c= $this->dict->getConcept($name);
if (!$c) {
print "<div class='notice'>Expression \"<span class='wterm'>".htmlspecialchars($name)."</span>\" not found</div>");
return;
}
$res= $this->dict->queryConceptRecordsBy( 'meaning', $term, 100 );
$this->listConcepts( $res );
$this->dict->freeResult( $res );
}
function showMeanings( $word, $lang ) {
$word= strtolower($word);
$lang= strtolower($lang);
$e= $this->dict->getEntry( $lang, $word );
if (!$e) {
print "<p>not found!</p>";
return;
}
$terms= $this->dict->getMeanings($e);
$this->showTermList( $terms, false );
}
function showTranslations( $word, $lang, $tolang ) {
$word= strtolower($word);
$lang= strtolower($lang);
$e= $this->dict->getEntry( $lang, $word );
if (!$e) {
print "<p>not found!</p>";
return;
}
$terms= $this->dict->getMeanings($e);
foreach ($terms as $t) {
if (!$t) continue;
extract ( $t );
print "\n\t\t<div class='term' id='term-$id'>\n";
print "\t\t<h4 class='section'>";
print "[".htmlspecialchars($lang)."] ";
print "<a href='?w=".htmlspecialchars($term)."&l=".htmlspecialchars($lang)."'>".htmlspecialchars($term)."</a> ";
print "</h4>\n";
$trans= array();
$trans+= $this->dict->getRelatives( $id, REL_TRANS, true, $tolang );
$trans+= $this->dict->getRelatives( -$id, REL_TRANS, true, $tolang ); #FIXME: unique!
$c= 0;
foreach ($trans as $tr) {
if (!$tr) continue;
extract ( $tr );
if ($tolang && $lang!=$tolang) continue;
print "\n\t\t<div class='translation' id='term-$id'>\n";
print "\t\t<h5 class='translation-section'>";
print "[".htmlspecialchars($lang)."] ";
print "<a href='?w=".htmlspecialchars($term)."&l=".htmlspecialchars($lang)."'>".htmlspecialchars($term)."</a> ";
if (@$confidence) {
print " <span style='font-weight:normal; font-style:italic; font-size:66%;'>(".(int)round($confidence * 100)." %)</span>";
}
print "</h5>\n";
$syn= $this->dict->getRelatives( -$id, array( REL_LABEL, REL_NORMAL ) );
if ($syn) $this->showRelatives("Synonyms", $syn);
$meanings= $this->dict->getRelatives( $id, array( REL_LABEL, REL_NORMAL ) );
if ($meanings) $this->showRelatives("Meanings", $meanings);
print "</div>\n";
$c+= 1;
}
if (!$c) {
print "<i>no translations found, sorry</i>";
}
print "</div>\n";
}
}
function showTermList( $terms, $showMeanings = true ) {
if ( $this->csv ) {
wfHttpError( 404, "Not Found", "CSV output is not supported for the 'explore' view." );
return;
}
if ( !$terms ) {
print "<p>Nothing found, sorry</p>";
return;
}
$unparsed= array();
foreach ($terms as $t) {
if (!$t) continue;
extract ( $t );
if (!isset($score)) $score= NULL;
if (!isset($confidence)) $confidence= NULL;
if ($type==TYPE_TAG || $type==TYPE_CAT) ; //TODO: show later? smaller? not at all?
else {
print "\n\t\t<div class='term' id='term-$id'>\n";
$this->showTerm($id,$lang,$term,$type,$showMeanings,$score,$confidence);
print "\n\t\t</div>\n";
}
}
if ($unparsed) {
print "\t\t<h2><i>Unparsed matches</i></h2>\n";
$this->showRelatives(NULL,$unparsed,true,true);
}
}
function inspectTerm( $term, $lang, $distinct = true ) {
$e= $this->dict->getEntry( $lang, $term );
if ( !$e ) {
if ( $this->format != "html" ) {
wfHttpError( 404, "Not Found", "No entry found for $lang:$term" );
}
else {
print "<p>Can't find [".htmlspecialchars($lang)."] ".htmlspecialchars($term).", sorry</p>";
}
return;
}
extract( $e );
if ( $this->csv ) {
$out = $this->dict->getRelatives( $id, NULL);
$in = $this->dict->getRelatives( -$id, NULL);
$relatives= array_merge($out,$in);
$this->csv->csvFromArray( $relatives );
return;
}
print "\t\t<h3>#$id: [".htmlspecialchars($lang)."] ".htmlspecialchars($term)." [".typeName($type)."]</h3>\n";
$relatives = $this->dict->getRelatives( $id, NULL, $distinct);
$this->listRelations( "Outgoing", $relatives, false, $distinct);
$relatives = $this->dict->getRelatives( -$id, NULL, $distinct);
$this->listRelations( "Incoming", $relatives, true, $distinct);
}
function listRelations( $label, $relatives, $reverse = false, $distinct = true ) {
if ($label) print "\n<h4>$label</h4>\n";
print "<ul>";
foreach ($relatives as $r) {
print "\t\t\t<li>";
if (!$reverse) print "~ <i>".htmlspecialchars(relationName($r['rel']))."</i> ";
print "[".htmlspecialchars($r['lang'])."] ";
print "<a href='?w=".htmlspecialchars($r['term'])."&l=".htmlspecialchars($r['lang'])."&q=inspect'>".htmlspecialchars($r['term'])."</a>";
print "<small> [".htmlspecialchars(typeName($r['type']))."]</small>";
if ($reverse) print " <i>".htmlspecialchars(relationName($r['rel']))."</i> ~";
if (!is_null(@$r['confidence'])) {
print " <small>(".(int)round($r['confidence']*100)."%)</small>";
}
if ( $distinct ) {
print " <small>(".$r['count']." sources)</small>";
}
else {
if ($r['source']) {
$u= "http://{$r['source']}/wiki/".urlencode($r['sourcetitle']);
print " <small><i>";
print "(from <a href='$u'>".htmlspecialchars($r['source'])."/".htmlspecialchars($r['sourcetitle'])."</a>)";
if ( $r['sourcepid'] && $r['sourcedts']) print " (".$r['sourcedts'].") ";
else print " (pending)";
}
else {
print " (<span class='error'>missing source</span>)";
}
}
print "</i></small> ";
print "</li>\n";
}
print "</ul>\n";
}
function showTerm( $id, $lang, $term, $type, $showMeanings, $score = NULL, $confidence = NULL ) {
#if ( $type != TYPE_LABEL && !$dts ) $dts= "<i>pending</i>";
if ( $type == TYPE_PAGE ) $articles= $this->dict->getPagesForTerm($id);
else $articles= array();
print "\t\t<h4 class='section'>";
print "[".htmlspecialchars($lang)."] ";
print "<a href='?w=".htmlspecialchars($term)."&l=".htmlspecialchars($lang)."'>".htmlspecialchars($term)."</a> ";
print "[".typeName($type)."]";
if ($score) {
print " <span style='font-weight:normal; font-style:italic; font-size:66%;'>(score: $score)</span>";
}
if (@$confidence) {
print " <span style='font-weight:normal; font-style:italic; font-size:66%;'>(".(int)round($confidence * 100)." %)</span>";
}
foreach ($articles as $a) {
$domain= $a['source'];
$title= $a['title']; #TODO: namespace?!
$u= "http://$domain/wiki/".urlencode($title);
print " →<a href='$u' style='font-weight:normal; font-size:86%;'>".htmlspecialchars($domain)."/".htmlspecialchars($title)."</a> ";
print "<span style='font-weight:normal; font-style:italic; font-size:66%;'>";
if ( $a['pid'] && $a['dts']) print " (".$a['dts'].") ";
else print " (pending)";
print "</span>";
}
print "</h4>\n";
if ($showMeanings) {
$use= $this->dict->getRelatives( $id, array( REL_LABEL, REL_NORMAL, REL_HOM ), true );
#NOTE: if it's a "page", it points to itself as a meaning...
#FIXME: better rel, keep this in the database!
#FIXME: "PAGE" should not be used for terms at all. Use CONCEPT!
if ($use && $type==TYPE_PAGE) {
$e= array(
'rel' => REL_LABEL,
'lang' => $lang,
'term' => $term,
'type' => $type,
'conficence' => 1,
);
array_unshift($use, $e);
}
if ($use) $this->showRelatives("Meanings", $use);
}
if ( $type != TYPE_LABEL ) {
$syn= $this->dict->getRelatives( $id, array( REL_SYN, REL_HYPOSYN, REL_HYPERSYN, REL_LABEL, REL_NORMAL ) );
if ($syn) $this->showRelatives("Synonyms (out)", $syn);
$syn= $this->dict->getRelatives( -$id, array( REL_SYN, REL_HYPOSYN, REL_HYPERSYN, REL_LABEL, REL_NORMAL ) );
if ($syn) $this->showRelatives("Synonyms (in)", $syn);
$trans= $this->dict->getRelatives( $id, REL_TRANS );
if ($trans) $this->showRelatives("Translations (out)", $trans, true);
$trans= $this->dict->getRelatives( -$id, REL_TRANS );
if ($trans) $this->showRelatives("Translations (in)", $trans, true);
$links= $this->dict->getRelatives( $id, REL_LINK );
if ($links) $this->showRelatives("Links", $links, false, false, 'font-size:small;');
$tags= $this->dict->getRelatives( $id, array( REL_TAG, REL_CAT ) );
if ($tags) $this->showRelatives("Tags", $tags, false, true, 'font-size:small;');
}
}
function showRelatives( $label, $relatives, $showLang = false, $showType = false, $style= NULL ) {
print "\t\t\t<p class='relatives'>";
if ($label) print "<b>$label:</b> ";
$s= "";
if ($style) $s= " style='$style'";
$first= true;
foreach ($relatives as $r) {
if ($first) $first= false;
else print " / ";
$page= true;
if ( $r['type'] == TYPE_LABEL || $r['type'] == TYPE_TAG || $r['type'] == TYPE_CAT )
$page= false;
print " <span class='relative'$s>";
if (!$page) print "<i>";
if ($showLang) print "[".htmlspecialchars($r['lang'])."] ";
print "<a href='?w=".htmlspecialchars($r['term'])."&l=".htmlspecialchars($r['lang'])."'>".htmlspecialchars($r['term'])."</a>";
if ($showType) print "<sup>[".htmlspecialchars(typeName($r['type']))."]</sup>";
if (!$page) print "</i>";
print "<small> (".(int)round($r['confidence']*100)."%)</small>";
print "</span>";
}
print "</p>\n";
}
}
function wsfWikiWordOutput($word, $lang, $query = "explore", $format = "html", $tolang = NULL ) {
global $wsgWorldDatabase, $wgRequest;
if ($word) $word= strtolower($word);
if ($lang) $lang= strtolower($lang);
$dbw= openConnection( $wsgWorldDatabase );
$world= new WorldStore( $dbw );
$page= new WikiWordPage( $world, $format );
if ( $query=="stats" ) {
$page->showStats( $wgRequest->getVal('lang'));
}
else if ( $query=="inspect" ) {
$page->inspectTerm( $word, $lang, true );
}
else if ( $query=="inspect-details" ) {
$page->inspectTerm( $word, $lang, false );
}
else if ( $query=="list" ) {
$page->listTerms( $lang, 100, $wgRequest->getVal('start') );
}
else if ( $query=="random" ) {
$page->randomTerm( $lang );
}
else if ( $query=="explore" ) {
$page->exploreTerm( $word, $lang );
}
else if ( $query=="translate" ) {
$page->showTranslations( $word, $lang, $tolang );
}
else {
$page->showMeanings( $word, $lang );
}
$dbw->close();
}
$word= $wgRequest->getVal("w");
$lang= $wgRequest->getVal("l");
$tolang= $wgRequest->getVal("t");
if ( !is_null($word) ) $word= trim($word);
if ( empty($word) ) $word= NULL;
if ( !is_null($lang) ) $lang= trim($lang);
if ( empty($lang) ) $lang= NULL;
$format= $wgRequest->getVal("f","html");
$query= $wgRequest->getVal("q","meanings");
if ( $wgRequest->getVal("stats") ) $query= "stats";
else if ( $wgRequest->getVal("inspect") ) $query= "inspect";
else if ( $wgRequest->getVal("inspect-details") ) $query= "inspect-details";
else if ( $wgRequest->getVal("list") ) $query= "list";
else if ( $wgRequest->getVal("random") ) $query= "random";
else if ( $wgRequest->getVal("meanings") ) $query= "meanings";
else if ( $wgRequest->getVal("explore") ) $query= "explore";
else if ( $wgRequest->getVal("translate") ) $query= "translate";
if ( $format != "html" && ( $word || $query == "stats" ) ) {
wsfWikiWordoutput($word, $lang, $query, $format, $tolang);
wfDebug( "Request for $format ended normally\n" );
exit();
}
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>WikiWord</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<?
if ($word) print '<meta name="robots" content="noindex, nofollow"/>';
else print '<meta name="robots" content="nofollow"/>';
?>
<style type='text/css'>
div.term { border:1px solid #AAAAAA; background-color: #F8F8FF; margin:1ex; }
.term p { margin-left: 1ex; margin-right: 1ex; }
.section { border:1px solid #AAAAAA; border-width:0 0 1px 0; background-color: #F0F0FF; padding:1ex; }
h4.section { margin:0; font-size:120%; }
.error { color:red; font-weight:bold; }
div.translation { margin-left: 1ex; margin-right: 1ex; }
h5.translation-section { margin:0; font-size:110%; }
</style>
</head>
<body>
<? #wsfHackingInProgress() ?>
<h1>WikiWord<br/>
<a style="font-size:50%; font-wight:normal;" href="WikiWord.php?stats=1&l=<?=urlencode($lang)?>&w=<?=urlencode($word)?>">stats</a></h1>
<form method="get" action="WikiWord.php">
<p>
Word: <input type="text" name="w" value="<?= htmlspecialchars($word) ?>"/>
Language: <input type="text" name="l" size="4" value="<?= htmlspecialchars($lang) ?>"/>
</p>
<p>
<input type="submit" name="meanings" value="Meanings"/>
<input type="submit" name="inspect" value="Inspect"/>
<input type="submit" name="explore" value="Search"/>
</p>
<p>
<input type="submit" name="translate" value="Translate"/>
to <input type="text" name="t" size="4" value="<?= htmlspecialchars($tolang) ?>"/>
</p>
<!-- <p>
<input type="submit" name="list" value="List"/>
<input type="submit" name="random" value="Random"/>
</p> -->
</form>
<?
if ( $word || $query == "stats" || $query == "list" || $query == "random" ) {
if ( $query != "explore" && $query != "random" ) {
?>
<p><a style="font-size:90%;" href="WikiWord.php?l=<?=urlencode($lang)?>&w=<?=urlencode($word)?>&q=<?=urlencode($query)?>&f=csv">as CSV</a></p>
<?
}
wsfWikiWordoutput($word, $lang, $query, $format, $tolang);
}
wsfFooter(false);
?>
</body>
</html>
<?
wfDebug( "Request ended normally\n" );
?>
WikiWord.php.bak
text/x-php, 26244 bytes (load raw)

