type_requete; $id_table = $table_des_tables[$type]; $id_field = $id_table . "." . $table_primary[$type]; switch($type) { case 'articles': $boucle->from[] = "spip_articles"; if (lire_meta("post_dates") == 'non') { $boucle->where[] = "$id_table.datewhere[] = "$id_table.statut='publie'"; break; case 'auteurs': $boucle->from[] = "spip_auteurs"; // 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'; $boucle->where[] = "lien.id_auteur=$id_table.id_auteur"; $boucle->where[] = "lien.id_article=spip_articles.id_article"; $boucle->where[] = "spip_articles.statut='publie'"; $boucle->group = "\nGROUP BY " . $id_field; } // pas d'auteurs poubellises $boucle->where[] = "NOT($id_table.statut='5poubelle')"; break; case 'breves': $boucle->from[] = "spip_breves"; $boucle->where[] = "$id_table.statut='publie'"; break; case 'forums': $boucle->from[] = "spip_forum"; // Par defaut, selectionner uniquement les forums sans pere if (!$boucle->tout AND !$boucle->plat) { $boucle->where[] = "$id_table.id_parent=0"; } $boucle->where[] = "$id_table.statut='publie'"; break; case 'signatures': $boucle->from[] = "spip_signatures"; $boucle->from[] = 'spip_petitions'; $boucle->from[] = 'spip_articles'; $boucle->where[] = 'spip_petitions.id_article=spip_articles.id_article'; $boucle->where[] = "spip_petitions.id_article=$id_table.id_article"; $boucle->where[] = "$id_table.statut='publie'"; $boucle->group = "\nGROUP BY " . $id_field; break; case 'documents': $boucle->from[] = "spip_documents"; $boucle->from[] = "spip_types_documents AS types_d"; $boucle->where[] = "$id_table.id_type = types_d.id_type"; $boucle->where[] = "$id_table.taille > 0"; break; case 'types_documents': $boucle->from[] = "spip_types_documents"; break; case 'groupes_mots': $boucle->from[] = "spip_groupes_mots"; break; case 'mots': $boucle->from[] = "spip_mots"; $boucle->where[] = "$id_table.titre<>'kawax'"; break; case 'rubriques': $boucle->from[] = "spip_rubriques"; $boucle->where[] = "$id_table.statut='publie'"; break; case 'hierarchie': $boucle->from[] = "spip_rubriques"; $boucle->where[] = 'spip_rubriques.id_rubrique IN ($hierarchie)'; $boucle->select[] = 'FIELD(spip_rubriques.id_rubrique, $hierarchie) AS _field'; $boucle->order = "\nORDER BY _field"; break; case 'syndication': $boucle->from[] = "spip_syndic"; $boucle->where[] = "$id_table.statut='publie'"; break; case 'syndic_articles': $boucle->from[] = "spip_syndic_articles AS $id_table"; $boucle->from[] = 'spip_syndic AS syndic'; $boucle->where[] = "$id_table.id_syndic=syndic.id_syndic"; $boucle->where[] = "$id_table.statut='publie'"; $boucle->where[] = "syndic.statut='publie'"; $boucle->select[]='syndic.nom_site AS nom_site'; # dérogation zarbi $boucle->select[]='syndic.url_site AS url_site'; # idem break; } // fin du switch return ("\nSELECT\t". # si pas de champ ca doit etre juste un décompte ((!$boucle->select) ? $id_field : 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, $table_des_tables, $table_date, $boucles; $boucle = &$boucles[$idb]; $id_table = $table_des_tables[$type]; $id_field = $id_table . "." . $table_primary[$type]; if (is_array($params)) { reset($params); while (list(, $param) = each($params)) { spip_log("idb $idb, para $param"); if ($param == 'exclus') { $boucle->where[] = "$id_field!='\"." . index_pile($boucles[$idb]->id_parent, $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->partie = $match[1]; $boucle->total_parties = $match[2]; $boucle->mode_partie = '/'; } else if (ereg('^(([0-9]+)|n)(-([0-9]+))?,(([0-9]+)|n)(-([0-9]+))?$', $param, $match)) { if (($match[2]!='') && ($match[6]!='')) $boucle->limit = $match[2].','.$match[6]; else { $boucle->partie = ($match[1] != 'n') ? $match[1] : ($match[4] ? $match[4] : 0); $boucle->total_parties = ($match[5] != 'n') ? $match[5] : ($match[8] ? $match[8] : 0); $boucle->mode_partie = ($match[1] == 'n') ? '-' : '+'; } } 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, '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, '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, '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, ((($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, $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->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 = "forum"; else if ($type == 'syndication') $col_lien = "syndic"; else $col_lien = $type; $boucle->from[] = "spip_mots_$col_lien AS lien_mot"; $boucle->from[] = 'spip_mots AS mots'; $boucle->where[] = "$id_field=lien_mot." . $table_primary[$type]; $boucle->where[] = "lien_mot.id_mot=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 = 'syndic'; // 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) ."'"; 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é } } } ?>