Skip to content

Commit

Permalink
[PHP] - Optimized SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
timovn committed May 14, 2016
1 parent 96a77c9 commit ae0ed09
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 38 deletions.
10 changes: 7 additions & 3 deletions admin/commentaires.php
Expand Up @@ -60,11 +60,15 @@ traiter_form_commentaire($comment, 'admin');
$param_makeup['menu_theme'] = 'for_article';
$article_id = $_GET['post_id'];

$article_title = get_entry($GLOBALS['db_handle'], 'articles', 'bt_title', $article_id, 'return');
$query = "SELECT * FROM commentaires WHERE bt_article_id=? ORDER BY bt_id";

$query = "SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id=? AND c.bt_article_id=a.bt_id ORDER BY c.bt_id";
$commentaires = liste_elements($query, array($article_id), 'commentaires');

if (!empty($commentaires)) {
$article_title = $commentaires[0]['bt_title'];
} else {
$article_title = get_entry($GLOBALS['db_handle'], 'articles', 'bt_title', $article_id, 'return');
}

$param_makeup['show_links'] = '0';

}
Expand Down
2 changes: 1 addition & 1 deletion admin/maintenance.php
Expand Up @@ -658,7 +658,7 @@ function parse_html($content) {
// get list of comments (comments that belong to selected articles only)
if ($_GET['incl-comms'] == 1) {
foreach ($data_array['articles'] as $article) {
$comments = liste_elements('SELECT * FROM commentaires WHERE bt_article_id = ? ', array($article['bt_id']), 'commentaires');
$comments = liste_elements('SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id = ? AND c.bt_article_id=a.bt_id', array($article['bt_id']), 'commentaires');
if (!empty($comments)) {
$data_array['commentaires'] = array_merge($data_array['commentaires'], $comments);
}
Expand Down
10 changes: 4 additions & 6 deletions atom.php
Expand Up @@ -61,14 +61,12 @@ function require_all() {
$GLOBALS['db_handle'] = open_base();
$article_id = htmlspecialchars($_GET['id']);

$liste = liste_elements("SELECT * FROM commentaires WHERE bt_article_id=? AND bt_statut=1 ORDER BY bt_id DESC", array($article_id), 'commentaires');
$liste = liste_elements("SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id=? AND c.bt_article_id=a.bt_id AND c.bt_statut=1 ORDER BY c.bt_id DESC", array($article_id), 'commentaires');

if (!empty($liste)) {
$query = "SELECT * FROM articles WHERE bt_id=? AND bt_date<=".date('YmdHis')." AND bt_statut=1";
$billet = liste_elements($query, array($article_id), 'articles');
$xml .= '<title>Commentaires sur '.$billet[0]['bt_title'].' - '.$GLOBALS['nom_du_site'].'</title>'."\n";
$xml .= '<link href="'.$billet[0]['bt_link'].'" />'."\n";
$xml .= '<id>'.$billet[0]['bt_link'].'</id>';
$xml .= '<title>Commentaires sur '.$liste[0]['bt_title'].' - '.$GLOBALS['nom_du_site'].'</title>'."\n";
$xml .= '<link href="'.$liste[0]['bt_link'].'" />'."\n";
$xml .= '<id>'.$liste[0]['bt_link'].'</id>';

foreach ($liste as $comment) {
$dec = decode_id($comment['bt_id']);
Expand Down
2 changes: 1 addition & 1 deletion inc/conf.php
Expand Up @@ -19,7 +19,7 @@
// GENERAL
define('BLOGOTEXT_NAME', 'BlogoText');
define('BLOGOTEXT_SITE', 'http://lehollandaisvolant.net/blogotext/');
define('BLOGOTEXT_VERSION', '3.3.9-1');
define('BLOGOTEXT_VERSION', '3.3.10-1');
define('MINIMAL_PHP_REQUIRED_VERSION', '5.5');

// FOLDERS (change this only if you know what you are doing...)
Expand Down
6 changes: 3 additions & 3 deletions inc/fich.php
Expand Up @@ -319,9 +319,9 @@ function request_external_files($feeds, $timeout, $echo_progress=false) {

function rafraichir_cache_lv1() {
creer_dossier(BT_ROOT.DIR_CACHE, 1);
$arr_a = liste_elements("SELECT * FROM articles WHERE bt_statut = 1 ORDER BY bt_date DESC LIMIT 0, 20", array(), 'articles');
$arr_c = liste_elements("SELECT * FROM commentaires WHERE bt_statut = 1 ORDER BY bt_id DESC LIMIT 0, 20", array(), 'commentaires');
$arr_l = liste_elements("SELECT * FROM links WHERE bt_statut = 1 ORDER BY bt_id DESC LIMIT 0, 20", array(), 'links');
$arr_a = liste_elements("SELECT * FROM articles WHERE bt_statut=1 ORDER BY bt_date DESC LIMIT 0, 20", array(), 'articles');
$arr_c = liste_elements("SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_statut=1 AND c.bt_article_id=a.bt_id ORDER BY c.bt_id DESC LIMIT 0, 20", array(), 'commentaires');
$arr_l = liste_elements("SELECT * FROM links WHERE bt_statut=1 ORDER BY bt_id DESC LIMIT 0, 20", array(), 'links');
$file = BT_ROOT.DIR_CACHE.'/'.'cache_rss_array.dat';
return file_put_contents($file, '<?php /* '.chunk_split(base64_encode(serialize(array('c' => $arr_c, 'a' => $arr_a, 'l' => $arr_l)))).' */');
}
Expand Down
5 changes: 3 additions & 2 deletions inc/html.php
Expand Up @@ -153,7 +153,7 @@ function moteur_recherche() {

function encart_commentaires() {
mb_internal_encoding('UTF-8');
$query = "SELECT c.bt_author, c.bt_id, c.bt_article_id, c.bt_content FROM commentaires c LEFT JOIN articles a ON a.bt_id=c.bt_article_id WHERE c.bt_statut=1 AND a.bt_statut=1 ORDER BY c.bt_id DESC LIMIT 5";
$query = "SELECT a.bt_title, c.bt_author, c.bt_id, c.bt_article_id, c.bt_content FROM commentaires c LEFT JOIN articles a ON a.bt_id=c.bt_article_id WHERE c.bt_statut=1 AND a.bt_statut=1 ORDER BY c.bt_id DESC LIMIT 5";
$tableau = liste_elements($query, array(), 'commentaires');
if (isset($tableau)) {
$listeLastComments = '<ul class="encart_lastcom">'."\n";
Expand All @@ -166,7 +166,8 @@ function encart_commentaires() {
if (strlen($comment['bt_author']) >= 30) {
$comment['bt_author'] = mb_substr($comment['bt_author'], 0, 29).'…';
}
$listeLastComments .= '<li title="'.date_formate($comment['bt_id']).'"><b>'.$comment['bt_author'].'</b> '.$GLOBALS['lang']['sur'].' <b>'.$comment['article_title'].'</b><br/><a href="'.$comment['bt_link'].'">'.$comment['contenu_abbr'].'</a>'.'</li>'."\n";
// $listeLastComments .= '<li title="'.date_formate($comment['bt_id']).'"><b>'.$comment['bt_author'].'</b> '.$GLOBALS['lang']['sur'].' <b>'.$comment['article_title'].'</b><br/><a href="'.$comment['bt_link'].'">'.$comment['contenu_abbr'].'</a>'.'</li>'."\n";
$listeLastComments .= '<li title="'.date_formate($comment['bt_id']).'"><b>'.$comment['bt_author'].'</b><br/><a href="'.$comment['bt_link'].'">'.$comment['contenu_abbr'].'</a>'.'</li>'."\n";
}
$listeLastComments .= '</ul>'."\n";
return $listeLastComments;
Expand Down
4 changes: 2 additions & 2 deletions inc/sqli.php
Expand Up @@ -233,8 +233,8 @@ function init_list_articles($article) {
function init_list_comments($comment) {
$comment['auteur_lien'] = (!empty($comment['bt_webpage'])) ? '<a href="'.$comment['bt_webpage'].'" class="webpage">'.$comment['bt_author'].'</a>' : $comment['bt_author'] ;
$comment['anchor'] = article_anchor($comment['bt_id']);
$comment['article_title'] = get_entry($GLOBALS['db_handle'], 'articles', 'bt_title', $comment['bt_article_id'], 'return');
$comment['bt_link'] = get_blogpath($comment['bt_article_id'], $comment['article_title']).'#'.$comment['anchor'];
// $comment['article_title'] = get_entry($GLOBALS['db_handle'], 'articles', 'bt_title', $comment['bt_article_id'], 'return');
$comment['bt_link'] = get_blogpath($comment['bt_article_id'], $comment['bt_title']).'#'.$comment['anchor'];
$comment = array_merge($comment, decode_id($comment['bt_id']));
return $comment;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/them.php
Expand Up @@ -268,7 +268,7 @@ function afficher_index($tableau, $type) {
$HTML_comms = '';
// get list comments
if ($billet['bt_nb_comments'] != 0) {
$query = "SELECT * FROM commentaires WHERE bt_article_id=? AND bt_statut=1 ORDER BY bt_id LIMIT ? ";
$query = "SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id=? AND c.bt_article_id=a.bt_id AND c.bt_statut=1 ORDER BY c.bt_id LIMIT ? ";
$commentaires = liste_elements($query, array($billet['bt_id'], $billet['bt_nb_comments']), 'commentaires');

// parse & apply template comments
Expand Down
48 changes: 35 additions & 13 deletions index.php
Expand Up @@ -152,26 +152,40 @@ traiter_form_commentaire($comment, 'public');
// paramètre mode : quelle table "mode" ?
if (isset($_GET['mode'])) {
switch($_GET['mode']) {
case 'blog':
$where = 'articles';
break;
case 'comments':
$query = "SELECT c.*, a.bt_title FROM ";
$where = 'commentaires';
break;
case 'links':
$where = 'links';
break;
case 'blog':
default:
$where = 'articles';
break;
}
} else {
$where = 'articles';
}
$query .= $where.' ';

// paramètre de recherche uniquement dans les articles publiés :
$query .= 'WHERE bt_statut=1 ';
switch($where) {
case 'commentaires':
$query .= "commentaires AS c, articles AS a ";
break;
default:
$query .= $where.' ';
break;
}

// paramètre de recherche uniquement dans les éléments publiés :
switch($where) {
case 'commentaires':
$query .= 'WHERE c.bt_statut=1 ';
break;
default:
$query .= 'WHERE bt_statut=1 ';
break;
}


// paramètre de date "d"
Expand All @@ -188,6 +202,9 @@ traiter_form_commentaire($comment, 'public');
case 'articles':
$sql_date = "bt_date LIKE ? ";
break;
case 'commentaires':
$sql_date = "c.bt_date LIKE ? ";
break;
default:
$sql_date = "bt_id LIKE ? ";
break;
Expand All @@ -210,7 +227,7 @@ traiter_form_commentaire($comment, 'public');
$sql_q = implode(array_fill(0, count($arr), '( bt_content || bt_title || bt_link ) LIKE ? '), 'AND ');
break;
case 'commentaires' :
$sql_q = implode(array_fill(0, count($arr), 'bt_content LIKE ? '), 'AND ');
$sql_q = implode(array_fill(0, count($arr), 'c.bt_content LIKE ? '), 'AND ');
break;
default:
$sql_q = "";
Expand All @@ -229,14 +246,14 @@ traiter_form_commentaire($comment, 'public');
$array[] = '%, '.$_GET['tag'];
break;
case 'links' :
$sql_tag = "( bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? )";
$sql_tag = "( bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? OR bt_tags LIKE ? ) ";
$array[] = $_GET['tag'];
$array[] = $_GET['tag'].', %';
$array[] = '%, '.$_GET['tag'].', %';
$array[] = '%, '.$_GET['tag'];
break;
default:
$sql_tag = "";
$sql_tag = " ";
break;
}
}
Expand All @@ -248,18 +265,24 @@ traiter_form_commentaire($comment, 'public');
case 'articles' :
$sql_order = "ORDER BY bt_date DESC ";
break;
case 'commentaires' :
$sql_order = "ORDER BY c.bt_id DESC ";
break;
default:
$sql_order = "ORDER BY bt_id DESC ";
break;
}

// paramètre de filtrage admin/public (pas un paramètre, mais ajouté quand même)
// paramètre de filtrage date (pas un paramètre, mais ajouté quand même)
switch ($where) {
case 'articles' :
$sql_a_p = "bt_date <= ".date('YmdHis')." AND bt_statut=1 ";
$sql_a_p = "bt_date <= ".date('YmdHis')." ";
break;
case 'commentaires' :
$sql_a_p = "c.bt_id <= ".date('YmdHis')." AND c.bt_article_id=a.bt_id ";
break;
default:
$sql_a_p = "bt_id <= ".date('YmdHis')." AND bt_statut=1 ";
$sql_a_p = "bt_id <= ".date('YmdHis')." ";
break;
}

Expand All @@ -285,7 +308,6 @@ traiter_form_commentaire($comment, 'public');
}

$query .= $glue.$sql_a_p.$sql_order.$sql_p;

$tableau = liste_elements($query, $array, $where);
$GLOBALS['param_pagination'] = array('nb' => count($tableau), 'nb_par_page' => $GLOBALS['max_bill_acceuil']);
afficher_index($tableau, 'list');
Expand Down
8 changes: 3 additions & 5 deletions rss.php
Expand Up @@ -62,13 +62,11 @@ function require_all() {
$GLOBALS['db_handle'] = open_base();
$article_id = htmlspecialchars($_GET['id']);

$liste = liste_elements("SELECT * FROM commentaires WHERE bt_article_id=? AND bt_statut=1 ORDER BY bt_id DESC", array($article_id), 'commentaires');
$liste = liste_elements("SELECT c.*, a.bt_title FROM commentaires AS c, articles AS a WHERE c.bt_article_id=? AND c.bt_article_id=a.bt_id AND c.bt_statut=1 ORDER BY c.bt_id DESC", array($article_id), 'commentaires');

if (!empty($liste)) {
$query = "SELECT * FROM articles WHERE bt_id=? AND bt_date<=".date('YmdHis')." AND bt_statut=1";
$billet = liste_elements($query, array($article_id), 'articles');
$xml .= '<title>Commentaires sur '.$billet[0]['bt_title'].' - '.$GLOBALS['nom_du_site'].'</title>'."\n";
$xml .= '<link>'.$billet[0]['bt_link'].'</link>'."\n";
$xml .= '<title>Commentaires sur '.$liste[0]['bt_title'].' - '.$GLOBALS['nom_du_site'].'</title>'."\n";
$xml .= '<link>'.$liste[0]['bt_link'].'</link>'."\n";
$xml .= '<description><![CDATA['.$GLOBALS['description'].']]></description>'."\n";
$xml .= '<language>fr</language>'."\n";
$xml .= '<copyright>'.$GLOBALS['auteur'].'</copyright>'."\n";
Expand Down
2 changes: 1 addition & 1 deletion themes/default/template/commentaire.html
Expand Up @@ -11,7 +11,7 @@
{commentaire_contenu}
</div>
<footer class="com-footer">
<span class="com-reply"><a href="#form-commentaire" onclick="reply('[b]@[{commentaire_auteur}|#{commentaire_ancre}] :[/b] ');">@répondre</a> <a href="#{commentaire_ancre}">#lien</a></span>
<span class="com-reply"><a href="#form-commentaire" onclick="reply('[b]@[{commentaire_auteur}|#{commentaire_ancre}] :[/b] ');">@répondre</a> <a href="{commentaire_lien}">#lien</a></span>
</footer>
</div>
</article>
Expand Down

0 comments on commit ae0ed09

Please sign in to comment.