type_requete; $id_table = $tables_tables[$type]; $id_field = $id_table . "." . $table_primary[$type]; switch($type) { case 'articles': $boucle->from[] = "spip_articles AS $id_table"; if (lire_meta("post_dates") == 'non') { $boucle->where[] = "$id_table.dateselect[] = "$id_table.date" ; } $boucle->where[] = "$id_table.statut='publie'"; $boucle->select[] = "$id_table.statut"; break; case 'auteurs': $boucle->from[] = "spip_auteurs AS $id_table"; // Si pas de lien avec un article, selectionner // uniquement les auteurs d'un article publie if (!$boucle->lien) { $boucle->from[] = 'spip_auteurs_articles AS lien'; $boucle->from[] = 'spip_articles AS articles'; $boucle->where[] = "lien.id_auteur=$id_table.id_auteur"; $boucle->select[] = "lien.id_auteur"; $boucle->select[] = "$id_table.id_auteur"; $boucle->where[] = "lien.id_article=articles.id_article"; $boucle->select[] = "lien.id_article"; $boucle->select[] = "articles.id_article"; $boucle->where[] = "articles.statut='publie'"; $boucle->select[] = "articles.statut"; $boucle->group = "\nGROUP BY " . $id_field; } // pas d'auteurs poubellises $boucle->where[] = "NOT($id_table.statut='5poubelle')"; $boucle->select[] = "$id_table.statut"; break; case 'breves': $boucle->from[] = "spip_breves AS $id_table"; $boucle->where[] = "$id_table.statut='publie'"; $boucle->select[] = "$id_table.statut"; break; case 'forums': $boucle->from[] = "spip_forum AS $id_table"; // Par defaut, selectionner uniquement les forums sans pere if (!$boucle->tout AND !$boucle->plat) { $boucle->where[] = "$id_table.id_parent=0"; $boucle->select[] = "$id_table.id_parent"; } $boucle->where[] = "$id_table.statut='publie'"; $boucle->select[] = "$id_table.statut"; break; case 'signatures': $boucle->from[] = "spip_signatures AS $id_table"; $boucle->from[] = 'spip_petitions AS petitions'; $boucle->from[] = 'spip_articles AS articles'; $boucle->where[] = "petitions.id_article=articles.id_article"; $boucle->select[] = "petitions.id_article"; $boucle->select[] = "articles.id_article"; $boucle->where[] = "petitions.id_article=$id_table.id_article"; $boucle->select[] = "$id_table.id_article"; $boucle->where[] = "$id_table.statut='publie'"; $boucle->select[] = "$id_table.statut"; $boucle->group = "\nGROUP BY " . $id_field; break; case 'documents': $boucle->from[] = "spip_documents AS $id_table"; $boucle->from[] = "spip_types_documents AS types_d"; $boucle->where[] = "$id_table.id_type = types_d.id_type"; $boucle->select[] = "$id_table.id_type"; $boucle->select[] = "types_d.id_type"; $boucle->where[] = "$id_table.taille > 0"; $boucle->select[] = "$id_table.taille"; break; case 'types_documents': $boucle->from[] = "spip_types_documents AS $id_table"; break; case 'groupes_mots': $boucle->from[] = "spip_groupes_mots AS $id_table"; break; case 'mots': $boucle->from[] = "spip_mots AS $id_table"; $boucle->where[] = "$id_table.titre<>'kawax'"; $boucle->select[] = "$id_table.titre"; break; case 'rubriques': $boucle->from[] = "spip_rubriques AS $id_table"; $boucle->where[] = "$id_table.statut='publie'"; $boucle->select[] = "$id_table.statut"; break; case 'hierarchie': $boucle->from[] = "spip_rubriques AS rubriques"; $boucle->where[] = 'rubriques.id_rubrique IN ($hierarchie)'; $boucle->select[] = 'rubriques.id_rubrique'; $boucle->select[] = 'FIELD(rubriques.id_rubrique, $hierarchie) AS _field'; $boucle->order = "\nORDER BY _field"; break; case 'syndication': $boucle->from[] = "spip_syndic AS $id_table"; $boucle->where[] = "$id_table.statut='publie'"; $boucle->select[] = "$id_table.statut"; break; case 'syndic_articles': $boucle->from[] = "spip_syndic_articles AS $id_table"; $boucle->from[] = "spip_syndic AS source"; $boucle->where[] = "$id_table.id_syndic=source.id_syndic"; $boucle->select[] = "$id_table.id_syndic"; $boucle->select[] = "source.id_syndic"; $boucle->where[] = "$id_table.statut='publie'"; $boucle->select[] = "$id_table.statut"; $boucle->where[] = "source.statut='publie'"; $boucle->select[] = "source.statut"; $boucle->select[]='syndic.nom_site AS nom_site'; # dérogation zarbi $boucle->select[]='syndic.url_site AS url_site'; # idem $boucle->from[]='spip_syndic AS syndic'; $boucle->where[] = "syndic.id_syndic=$id_table.id_syndic"; $boucle->select[] = "syndic.id_syndic"; $boucle->select[] = "$id_table.id_syndic"; break; } // fin du switch return ("\nSELECT\t". # provisoirement on met * si pas de champ ((!$boucle->select) ? "*" : join(",\n\t", array_unique($boucle->select))). "\nFROM\t". join(",\n\t", $boucle->from) . ((!$boucle->where) ? "" : ("\nWHERE\t".join("\n AND\t", $boucle->where))) . $boucle->group . $boucle->order . ((!$boucle->limit) ? "" : ("\nLIMIT\t".$boucle->limit))); } function calculer_params($type, $params, $idb) { global $tables_relations, $table_primary, $tables_tables, $table_date, $contextes, $boucles; $boucle = &$boucles[$idb]; $id_table = $tables_tables[$type]; $id_field = $id_table . "." . $table_primary[$type]; if (is_array($params)) { reset($params); while (list(, $param) = each($params)) { if ($param == 'exclus') { $boucle->where[] = "$id_field!='\"." . index_pile($boucles[$idb]->id_parent, $contextes, $table_primary[$type]) . ".\"'"; $boucle->select[] = "$id_field"; } else if ($param == 'unique' OR $param == 'doublons') { $boucle->doublons = ($type == 'hierarchie') ? 'rubriques' : $type; $boucle->where[] = "$id_field NOT IN (\$id_doublons[" . $boucle->doublons . "])"; $boucle->select[] = $id_field; } else if (ereg('^lang_select(=(oui|non))?$', $param, $match)) { if (!$boucles[$idb]->lang_select = $match[2]) $boucles[$idb]->lang_select = 'oui'; } else if (ereg('^([0-9]+),([0-9]*)$', $param, $match)) { $boucle->limit = $match[1].','.$match[2]; } else if (ereg('^debut([-_a-zA-Z0-9]+),([0-9]*)$', $param, $match)) { $debut_lim = "debut".$match[1]; $boucle->limit = '".intval($GLOBALS[\'HTTP_GET_VARS\'][\''.$debut_lim.'\']).",'.$match[2]; } else if ($param == 'recherche') { if ($type == 'syndication') $boucle->from[] = "spip_index_syndic AS idx"; else $boucle->from[] = "spip_index_$type AS idx"; $boucle->select[] = "SUM(idx.points) AS points"; $boucle->where[] = "idx.". $table_primary[$type] . "=$id_field"; $boucle->select[] = "$id_field"; $boucle->group = "\nGROUP BY $id_field"; $boucle->where[] = "idx.hash IN (\$hash_recherche)"; $boucles[$idb]->hash = true; } // Classement par ordre inverse else if ($param == 'inverse') { if ($boucle->order) $boucle->order .= ' DESC'; } // Gerer les traductions else if ($param == 'traduction') { $req_where[] = "$id_table.id_trad > 0 AND $id_table.id_trad ='\"." . index_pile($boucles[$idb]->id_parent, $contextes, 'id_trad') . ".\"'"; } else if ($param == 'origine_traduction') { $req_where[] = "$id_table.id_trad = $id_table.id_article"; } // Special rubriques else if ($param == 'meme_parent') { $boucle->where[] = "$id_table.id_parent='\"." . index_pile($boucles[$idb]->id_parent, $contextes, 'id_parent') . ".\"'"; $boucle->select[] = "$id_table.id_parent"; if ($type == 'forums') { $boucle->where[] = "$id_table.id_parent > 0"; $boucle->plat = true; } } else if ($param == 'racine') { $boucle->where[] = "$id_table.id_parent=0"; $boucle->select[] = "$id_table.id_parent"; } else if ($param == 'branche') { $boucle->where[] = "$id_table.id_rubrique IN (\" . calcul_branche(" . index_pile($boucles[$idb]->id_parent, $contextes, 'id_rubrique') . ").\")"; $boucle->select[] = "$id_table.id_rubrique"; } else if ($type == 'hierarchie') { // Hack spécifique; cf complément dans calculer_boucle $boucle->tout = index_pile($boucles[$idb]->id_parent, $contextes, ((($param == 'id_article') || ($param == 'id_syndic')) ? 'id_rubrique' : 'id_parent')); } // Restriction de valeurs (implicite ou explicite) else if (ereg('^([a-zA-Z_]+) *((!?)(<=?|>=?|==?) *"?([^<>=!"]*))?"?$', $param, $match)) { // Variable comparee $col = $match[1]; $col_table = $id_table; // Valeur de comparaison if ($match[2]) $val = $match[5]; else { $val = $match[1]; // Si id_parent, comparer l'id_parent avec l'id_objet de la boucle superieure if ($val == 'id_parent') $val = $table_primary[$type]; // Si id_enfant, comparer l'id_objet avec l'id_parent de la boucle superieure else if ($val == 'id_enfant') $val = 'id_parent'; $val = index_pile($boucles[$idb]->id_parent, $contextes, $val) ; } // Traitement general des relations externes if ($s = $tables_relations[$type][$col]) { $col_table = "rel_$type"; $boucle->from[] = "$s AS $col_table"; $boucle->where[] = "$id_field=$col_table." . $table_primary[$type]; $boucle->select[] = "$id_field"; $boucle->select[] = "$col_table." . $table_primary[$type]; $boucle->group = "\nGROUP BY $id_field"; $boucle->lien = true; } // Cas particulier pour les raccourcis 'type_mot' et 'titre_mot' else if ($type != 'mots' AND ($col == 'type_mot' OR $col == 'titre_mot' OR $col == 'id_groupe')) { if ($type == 'forums') $col_lien = "spip_mots_forum"; else if ($type == 'syndication') $col_lien = "spip_mots_syndic"; else $col_lien = 'spip_mots_'.$type; $boucle->from[] = "$col_lien AS lien_mot"; $boucle->from[] = 'spip_mots AS mots'; $boucle->where[] = "$id_field=lien_mot." . $table_primary[$type]; $boucle->select[] = "$id_field"; $boucle->select[] = "lien_mot." . $table_primary[$type]; $boucle->where[] = "lien_mot.id_mot=mots.id_mot"; $boucle->select[] = "lien_mot.id_mot"; $boucle->select[] = "mots.id_mot"; $boucle->group = "\nGROUP BY $id_field"; $col_table = 'mots'; $boucle->lien = true; if ($col == 'type_mot') $col = 'type'; else if ($col == 'titre_mot') $col = 'titre'; else if ($col == 'id_groupe') $col = 'id_groupe'; } // Cas particulier : selection des documents selon l'extension if ($type == 'documents' AND $col == 'extension') { $col_table = 'types_d'; } // HACK : selection des documents selon mode 'image' (a creer en dur dans la base) else if ($type == 'documents' AND $col == 'mode' AND $val == 'image') { $val = 'vignette'; } // Cas particulier : lier les articles syndiques au site correspondant else if ($type == 'syndic_articles' AND $col<>'id_syndic_article') $col_table = 'source'; // Cas particulier : id_enfant => utiliser la colonne id_objet if ($col == 'id_enfant') $col = $table_primary[$type]; // Cas particulier : id_secteur = id_rubrique pour certaines tables else if (($type == 'breves' OR $type == 'forums') AND $col == 'id_secteur') $col = 'id_rubrique'; // Cas particulier : expressions de date if (ereg("^(date|mois|annee|age|age_relatif|jour_relatif|mois_relatif|annee_relatif)(_redac)?$", $col, $regs)) { $col = $regs[1]; if ($regs[2]) { $date_orig = "date_redac"; $date_compare = 'date_redac'; } else { $date_orig = $table_date[$type]; $date_compare = 'date'; } $date_orig = "$id_table.$date_orig"; $boucle->select[] = $date_orig; if ($col == 'date') $col = $date_orig; else if ($col == 'mois') { $col = "MONTH($date_orig)"; $col_table = ''; } else if ($col == 'annee') { $col = "YEAR($date_orig)"; $col_table = ''; } else if ($col == 'age') { $col = "(LEAST((UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP($date_orig))/86400, TO_DAYS(now())-TO_DAYS($date_orig), DAYOFMONTH(now())-DAYOFMONTH($date_ orig)+30.4368*(MONTH(now())-MONTH($date_orig))+365.2422*(YEAR(now())-YEAR($date_orig))))"; $col_table = ''; } else if ($col == 'age_relatif') { $col = "LEAST((UNIX_TIMESTAMP('\$$date_compare')-UNIX_TIMESTAMP($date_orig))/86400, TO_DAYS('\$$date_compare')-TO_DAYS($date_orig), DAYOFMONTH('\$$date_compare')-DAYOFMONTH($date_orig)+30.4368*(MONTH('\$$date_compare')-MONTH($date_orig))+365.2422*(YEAR('\$$date_compare')-YEAR($date_orig)))"; $col_table = ''; } else if ($col == 'jour_relatif') { $col = "LEAST(TO_DAYS('\$$date_compare')-TO_DAYS($date_orig), DAYOFMONTH('\$$date_compare')-DAYOFMONTH($date_orig)+30.4368*(MONTH('\$$date_compare')-MONTH($date_orig))+365.2422*(YEAR('\$$date_compare')-YEAR($date_orig)))"; $col_table = ''; } else if ($col == 'mois_relatif') { $col = "(MONTH('\$$date_compare')-MONTH($date_orig)+12*(YEAR('\$$date_compare')-YEAR($date_orig)))"; $col_table = ''; } else if ($col == 'annee_relatif') { $col = "YEAR('\$$date_compare')-YEAR($date_orig)"; $col_table = ''; } } if ($type == 'forums' AND ($col == 'id_parent' OR $col == 'id_forum')) $boucle->plat = true; // Operateur de comparaison if ($match[4]) { $op = $match[4]; if ($op == '==') $op = ' REGEXP '; } else { $op = '='; } if ($col_table) $col_table .= '.'; // bug de php: "$x[1]->y" différent de $x[1]->y ! if (ereg('^\$',$val)) $where = "$col_table$col$op'\".".$val.".\"'"; else $where = "$col_table$col$op'" . addslashes($val) ."'"; $boucle->select[] = "$col_table$col"; if ($match[3] == '!') $where = "NOT ($where)"; $boucle->where[] = $where; } // Selection du classement else if (ereg('^par[[:space:]]+([^}]*)$', $param, $match)) { $tri = trim($match[1]); if ($tri == 'hasard') { // par hasard $boucle->select[] = "MOD($id_field * UNIX_TIMESTAMP(), 32767) & UNIX_TIMESTAMP() AS alea"; $boucle->order = "\nORDER BY alea"; } else if ($tri == 'titre_mot'){ // par titre_mot $boucle->order= "\nORDER BY mots.titre"; } else if ($tri == 'type_mot'){ // par type_mot $boucle->order= "\nORDER BY mots.type"; } else if ($tri == 'points'){ // par points $boucle->order= "\nORDER BY points"; } else if (ereg("^num[[:space:]]+([^,]*)(,.*)?",$tri, $match2)) { // par num champ $boucle->select[] = "0+$id_table.".$match2[1]." AS num"; $boucle->order = "\nORDER BY num".$match2[2]; } else if (ereg("^[a-z0-9]+$", $tri)) { // par champ $col = $tri; if ($col == 'date') $col = $table_date[$type]; $boucle->order = "\nORDER BY $id_table.$col"; } else { // tris bizarres, par formule composee, virgules, etc. $boucle->order = "\nORDER BY ".$tri; } } # pas de else, ça a du etre évacué lors du phrasé } } } ?>