buscar.php.md: improved search according to platform
This commit is contained in:
parent
4e47debdec
commit
c057dcf6fe
@ -1,13 +1,18 @@
|
||||
Author: Jorge Maldonado Ventura
|
||||
Date: 2017-04-22 20:38
|
||||
Modified: 2018-01-11 16:38
|
||||
Modified: 2020-06-11 11:38
|
||||
Save_as: buscar.php
|
||||
Status: hidden
|
||||
Title: Resultados
|
||||
|
||||
<div id="tipue_search_content">
|
||||
|
||||
<main class="row main-videos">
|
||||
<div class="col-md-12">
|
||||
<div class="row">
|
||||
<?php
|
||||
$STOP_WORDS = ['a', 'un', 'una', 'unas', 'unos', 'uno', 'sobre', 'todo',
|
||||
$STOP_WORDS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'ñ', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'un', 'una', 'unas', 'unos', 'uno', 'sobre', 'todo',
|
||||
'también', 'tras', 'otro', 'algun', 'alguno', 'alguna', 'algunos',
|
||||
'algunas', 'ser', 'es', 'soy', 'eres', 'somos', 'sois', 'estoy', 'esta',
|
||||
'estamos', 'estais', 'estan', 'como', 'en', 'para', 'atras', 'porque',
|
||||
@ -32,9 +37,6 @@ $STOP_WORDS = ['a', 'un', 'una', 'unas', 'unos', 'uno', 'sobre', 'todo',
|
||||
'trabajas', 'trabaja', 'trabajamos', 'trabajais', 'trabajan', 'podria',
|
||||
'podrias', 'podriamos', 'podrian', 'podriais', 'yo', 'aquel'];
|
||||
|
||||
$DESCRIPTION_LENGTH = 20;
|
||||
$HALF_DESCRIPTION_LENGTH = floor($DESCRIPTION_LENGTH / 2);
|
||||
|
||||
$web_content = json_decode(file_get_contents('tipuesearch_content.json'), true);
|
||||
$stop_words_ignored = false;
|
||||
|
||||
@ -43,7 +45,7 @@ if (isset($_GET['q'])) {
|
||||
$search_str = trim($_REQUEST['q']);
|
||||
|
||||
$keywords = explode(' ', $search_str);
|
||||
$keywords_temp = '';
|
||||
$keywords_temp = NULL;
|
||||
foreach ($keywords as $keyword) {
|
||||
$is_stop_word = false;
|
||||
foreach ($STOP_WORDS as $stop_word) {
|
||||
@ -80,14 +82,25 @@ if (isset($_GET['q'])) {
|
||||
if ($stop_words_ignored == 1) {
|
||||
$page['description'] = preg_replace("/$word/i", $word, $page['description'], -1, $match_count);
|
||||
} else {
|
||||
$page['description'] = preg_replace("/$word/i", '<span class="tipue_search_content_bold highlighted">' . $word . '</span>', $page['description'], -1, $match_count);
|
||||
$page['description'] = preg_replace("/$word/i", $word, $page['description'], -1, $match_count);
|
||||
}
|
||||
if ($match_count > 0) {
|
||||
$score += 10 * $match_count;
|
||||
}
|
||||
|
||||
}
|
||||
if ($score != 0) {
|
||||
$found_results[] = ['score' => $score, 'title' => $page['title'], 'url' => $page['url'], 'description' => $page['description']];
|
||||
$found_results[] = [
|
||||
'score' => $score,
|
||||
'title' => $page['title'],
|
||||
'time' => $page['time'],
|
||||
'videoThumbnail' => $page['videoThumbnail'],
|
||||
'url' => $page['url'],
|
||||
'published' => $page['published'],
|
||||
'publishedText' => $page['publishedText'],
|
||||
'author' => $page['author'],
|
||||
'authorUrl' => $page['authorUrl'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,72 +112,69 @@ if (isset($_GET['q'])) {
|
||||
return 0;
|
||||
}
|
||||
return ($a['score'] > $b['score']) ? -1 : 1;
|
||||
};
|
||||
|
||||
$stop_results = $found_results;
|
||||
if ($stop_words_ignored) {
|
||||
printf('<div id="tipue_search_warning">%s</div>', 'Las palabras comunes se ignoran en gran parte');
|
||||
$stop_results = NULL;
|
||||
}
|
||||
if ($stop_words_ignored) {
|
||||
printf('<div id="tipue_search_warning">%s</div>', 'Las palabras comunes se ignoran en gran parte');
|
||||
}
|
||||
|
||||
$found_results_count = count($found_results);
|
||||
|
||||
if ($found_results_count > 0) {
|
||||
usort($found_results, 'comp_result_score');
|
||||
if ($found_results_count == 1) {
|
||||
$found_results_count_str = '1 resultado';
|
||||
} else {
|
||||
$found_results_count_str = $found_results_count . ' resultados';
|
||||
$found_results_count_str = NULL;
|
||||
}
|
||||
} else if ($found_results_count == 0) {
|
||||
$found_results_count_str = '';
|
||||
$found_results_count_str = NULL;
|
||||
printf('<div id="tipue_search_warning">%s</div>', 'No se ha encontrado nada');
|
||||
}
|
||||
|
||||
printf('<div id="tipue_search_results_count">%s</div>', $found_results_count_str);
|
||||
|
||||
foreach ($found_results as $found_result) {
|
||||
printf('<div class="tipue_search_content_title"><a class="is-cyan" href="%s">%s</a></div>', $found_result['url'], $found_result['title']);
|
||||
printf('<div class="tipue_search_content_url"><a href="%s">%s</a></div>', $found_result['url'], $found_result['url']);
|
||||
|
||||
$description_words = explode(' ', $found_result['description']);
|
||||
$description_words_count = count($description_words);
|
||||
$first_match = false;
|
||||
for ($i = 0; $i < $description_words_count; $i++) {
|
||||
if ($description_words[$i] == '<span>') {
|
||||
$first_match = $i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($first_match !== false) {
|
||||
echo '<div class="tipue_search_content_text">';
|
||||
if ($first_match - 12 <= 0) {
|
||||
for ($i = 0; $i < $DESCRIPTION_LENGTH; $i++) {
|
||||
echo $description_words[$i] . ' ';
|
||||
}
|
||||
echo '...';
|
||||
} else if($first_match + 12 >= $description_words_count) {
|
||||
echo '...';
|
||||
for ($i = $first_match - 12; $i < $description_words_count; $i++) {
|
||||
echo ' ' . $description_words[$i];
|
||||
}
|
||||
}
|
||||
else {
|
||||
echo '...';
|
||||
for ($i = $first_match - 12; $i <= $first_match + 12; $i++) {
|
||||
echo $description_words[$i] . ' ';
|
||||
}
|
||||
echo '...';
|
||||
}
|
||||
echo '</div>';
|
||||
} elseif ( $description_words_count === 1 ) {
|
||||
printf('<div class="tipue_search_content_text"><p>%s</p></div>', 'No hay palabras en el artículo');
|
||||
break;
|
||||
} else {
|
||||
echo '<div class="tipue_search_content_text">';
|
||||
for ($i = 0; $i < $description_words_count - floor($description_words_count/2); $i++) {
|
||||
echo $description_words[$i] . ' ';
|
||||
}
|
||||
if ($description_words_count > $DESCRIPTION_LENGTH) {
|
||||
echo '...';
|
||||
}
|
||||
echo '</div>';
|
||||
if (!empty($stop_results)) {
|
||||
foreach ($found_results as $found_result) {
|
||||
printf('
|
||||
<article class="col-md-3 video" itemscope itemtype="https://schema.org/Movie">
|
||||
<a href="%s">
|
||||
<div class="area">
|
||||
<div class="mask">
|
||||
<div class="vertical-align">
|
||||
<i class="soumaicon play"><svg><use href="/theme/images/svg/master.svg#play"></use></svg></i>
|
||||
</div>
|
||||
</div>
|
||||
<img itemprop="image" src="%s" alt="%s" class="img-fluid">
|
||||
<span class="duration">%s</span>
|
||||
</div>
|
||||
</a>
|
||||
<h2 itemprop="name">
|
||||
<a href="%s" rel="bookmark" title="Ver %s">%s</a>
|
||||
</h2>
|
||||
<!-- post footer -->
|
||||
<footer class="card-content-footer mb-4">
|
||||
<span class="soumaicon text-info">
|
||||
<svg>
|
||||
<use href="/theme/images/svg/master.svg#calendar"></use>
|
||||
</svg>
|
||||
</span>
|
||||
<time class="entry-date published" datetime="%s" itemprop="datePublished">
|
||||
<small>%s</small>
|
||||
</time>
|
||||
<span class="soumaicon text-info">
|
||||
<svg>
|
||||
<use href="/theme/images/svg/master.svg#user"></use>
|
||||
</svg>
|
||||
</span>
|
||||
<small itemprop="director" itemscope itemtype="https://schema.org/Person">
|
||||
<a href="%s" title="Autor del artículo" itemprop="name">%s</a>
|
||||
</small>
|
||||
</footer>
|
||||
<!-- end of post footer -->
|
||||
</article>',
|
||||
$found_result['url'], $found_result['videoThumbnail'], $found_result['title'],
|
||||
$found_result['time'], $found_result['url'], $found_result['title'],
|
||||
$found_result['title'], $found_result['published'], $found_result['publishedText'],
|
||||
$found_result['authorUrl'], $found_result['author']);
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,4 +182,6 @@ if (isset($_GET['q'])) {
|
||||
printf('<div id="tipue_search_warning">%s</div>', 'Aún no has buscado');
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
Loading…
x
Reference in New Issue
Block a user