Commit 90ad614a authored by Romain Loth's avatar Romain Loth

json output mode for php (1/2: for our new csv queries)

parent 099ce9c5
...@@ -7,22 +7,13 @@ error_reporting(-1); ...@@ -7,22 +7,13 @@ error_reporting(-1);
// DISPLAY THE RESULTS // DISPLAY THE RESULTS
function displayDoc($docId, $score, $base) { function displayDoc($docId, $score, $base, $outmode) {
$output = "";
// POSS score should have a data-score attribute // POSS score should have a data-score attribute
$output = "<li class='searchhit' title='".$score."'>";
// shortcut to doc // shortcut to doc
$doc = $base[$docId]; $doc = $base[$docId];
// £TODO new concept: use searchhit-templates
$has_template = False ;
if ($has_template) {
}
else {
// fallback "universal" values // fallback "universal" values
$title = try_attrs_until_you_find($doc, ['title', 'tit', 't']); $title = try_attrs_until_you_find($doc, ['title', 'tit', 't']);
$source = try_attrs_until_you_find($doc, ['source', 'journal', 'j']); $source = try_attrs_until_you_find($doc, ['source', 'journal', 'j']);
...@@ -39,15 +30,26 @@ function displayDoc($docId, $score, $base) { ...@@ -39,15 +30,26 @@ function displayDoc($docId, $score, $base) {
if ($day) { $date .= "/".$day; } } if ($day) { $date .= "/".$day; } }
// "universal" template // "universal" template
if ($outmode == 'html') {
$output = "<li class='searchhit' title='".$score."'>";
$output.="<p><b>$title</b> by <span class='author'>$author</span>, <i>$source</i> [$date]</p>"; $output.="<p><b>$title</b> by <span class='author'>$author</span>, <i>$source</i> [$date]</p>";
if (strlen($keywords)) { if (strlen($keywords)) {
$output.="<p><b>keywords:</b> $keywords</p>"; $output.="<p><b>keywords:</b> $keywords</p>";
} }
$output.="<p><span class='hit-text'>$doccontent</span></p>"; $output.="<p><span class='hit-text'>$doccontent</span></p>";
}
$output.="</li>"; $output.="</li>";
}
// json mode
else {
$output = array(
"tit" => $title,
"au" => $author,
"src" => $source,
"kws" => $keywords,
"txt" => $doccontent,
"date" => $date
);
}
return $output; return $output;
} }
...@@ -67,6 +69,7 @@ function try_attrs_until_you_find($doc_obj, $attr_names_array) { ...@@ -67,6 +69,7 @@ function try_attrs_until_you_find($doc_obj, $attr_names_array) {
$htmlout = "<ul class=infoitems>\n"; $htmlout = "<ul class=infoitems>\n";
$jsonout = array();
$nb_displayed = 0; $nb_displayed = 0;
foreach ($sims as $doc => $freq) { foreach ($sims as $doc => $freq) {
// doc limit // doc limit
...@@ -75,16 +78,30 @@ foreach ($sims as $doc => $freq) { ...@@ -75,16 +78,30 @@ foreach ($sims as $doc => $freq) {
} }
$rowid = ltrim($doc, 'd'); $rowid = ltrim($doc, 'd');
$thisdoc = displayDoc($rowid, $freq, $base); $thisdoc = displayDoc($rowid, $freq, $base, $output_mode);
// echodump("doc", $thisdoc); // echodump("doc", $thisdoc);
$htmlout .= $thisdoc."\n";
if ($output_mode == "html") {
$htmlout .= $thisdoc."\n";
}
else {
array_push($jsonout, $thisdoc);
}
// doc limit // doc limit
$nb_displayed++; $nb_displayed++;
} }
$htmlout .= "</ul>\n"; $htmlout .= "</ul>\n";
echo '<br/><h4><font color="#0000FF"> Full text of top '.$max_item_displayed."/".count($sims).' related publications:</font></h4>'.$htmlout; if ($output_mode == "html") {
echo '<br/><h4><font color="#0000FF"> Full text of top '.$nb_displayed."/".count($sims).' related publications:</font></h4>';
echo $htmlout;
}
else {
echo json_encode(array(
'hits' => $jsonout,
'nhits' => $nb_displayed
));
}
// to see the entire results array: // to see the entire results array:
// -------------------------------- // --------------------------------
......
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
// GLOBAL PARAMS // GLOBAL PARAMS
// ------------- // -------------
// 0 - output mode: 'json' or 'html'
// > json is for new use cases where styling is applied via template import in the js
// > html is the classical use where data is styled in this php scripts
$output_mode = "json";
// 1 - relative urls // 1 - relative urls
$our_php_root="twbackends/phpAPI"; // our php scripts relative URL $our_php_root="twbackends/phpAPI"; // our php scripts relative URL
$our_libs_root="twbackends/phpAPI"; // for our few icons and jquery-ui $our_libs_root="twbackends/phpAPI"; // for our few icons and jquery-ui
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment