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,47 +7,49 @@ error_reporting(-1); ...@@ -7,47 +7,49 @@ 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 // fallback "universal" values
$has_template = False ; $title = try_attrs_until_you_find($doc, ['title', 'tit', 't']);
if ($has_template) { $source = try_attrs_until_you_find($doc, ['source', 'journal', 'j']);
$keywords = try_attrs_until_you_find($doc, ['keywords', 'keyword', 'kw']);
$author = try_attrs_until_you_find($doc, ['authors', 'author', 'auth', 'au']);
$doccontent = try_attrs_until_you_find($doc, ['text', 'txt', 'abstract', 'content']);
} $date = try_attrs_until_you_find($doc, ['pubdate', 'publication_year', 'date']);
else {
// fallback "universal" values // detailed dates, gargantext-compatible
$title = try_attrs_until_you_find($doc, ['title', 'tit', 't']); $month = try_attrs_until_you_find($doc, ['publication_month']);
$source = try_attrs_until_you_find($doc, ['source', 'journal', 'j']); $day = try_attrs_until_you_find($doc, ['publication_day']);
$keywords = try_attrs_until_you_find($doc, ['keywords', 'keyword', 'kw']); if ($month) { $date .= "/".$month;
$author = try_attrs_until_you_find($doc, ['authors', 'author', 'auth', 'au']); if ($day) { $date .= "/".$day; } }
$doccontent = try_attrs_until_you_find($doc, ['text', 'txt', 'abstract', 'content']);
// "universal" template
$date = try_attrs_until_you_find($doc, ['pubdate', 'publication_year', 'date']); if ($outmode == 'html') {
$output = "<li class='searchhit' title='".$score."'>";
// detailed dates, gargantext-compatible
$month = try_attrs_until_you_find($doc, ['publication_month']);
$day = try_attrs_until_you_find($doc, ['publication_day']);
if ($month) { $date .= "/".$month;
if ($day) { $date .= "/".$day; } }
// "universal" template
$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>";
}
// json mode
else {
$output = array(
"tit" => $title,
"au" => $author,
"src" => $source,
"kws" => $keywords,
"txt" => $doccontent,
"date" => $date
);
} }
$output.="</li>";
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