3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
14 * Fonctions relatives aux objets éditoriaux et SQL
16 * @package SPIP\Core\SQL\Tables
19 if (!defined('_ECRIRE_INC_VERSION')) {
24 * Merge dans un tableau une de ses clés avec une valeur
27 * Tableau dont on veut compléter une clé
28 * @param string $index
29 * Clé du tableau que l'on souhaite compléter
30 * @param array $valeur
31 * Sous tableau à merger dans la clé.
34 function array_set_merge(&$table, $index, $valeur) {
35 if (!isset($table[$index])) {
36 $table[$index] = $valeur;
38 $table[$index] = array_merge($table[$index], $valeur);
43 * Lister les infos de toutes les tables sql declarées
45 * Si un argument est fourni, on ne renvoie que les infos de cette table.
46 * Elle est auto-declarée si inconnue jusqu'alors.
49 * @param string $table_sql
50 * table_sql demandee explicitement
52 * description connue de la table sql demandee
55 function lister_tables_objets_sql($table_sql = null, $desc = array()) {
56 static $deja_la = false;
57 static $infos_tables = null;
59 static $plugin_hash = null;
61 // plugins hash connu ? non si _CACHE_PLUGINS_OPT est pas encore chargé.
62 $_PLUGINS_HASH = defined('_PLUGINS_HASH') ? _PLUGINS_HASH
: '!_CACHE_PLUGINS_OPT';
64 // prealablement recuperer les tables_principales
65 if (is_null($infos_tables) or $plugin_hash !== $_PLUGINS_HASH) {
66 // pas de reentrance (cas base/serial)
68 spip_log('Re-entrance anormale sur lister_tables_objets_sql : '
69 . var_export(debug_backtrace(), true), _LOG_CRITIQUE
);
71 return ($table_sql === '::md5' ?
$md5 : array());
74 $plugin_hash = $_PLUGINS_HASH; // avant de lancer les pipelines
76 // recuperer les declarations explicites ancienne mode
77 // qui servent a completer declarer_tables_objets_sql
78 base_serial($GLOBALS['tables_principales']);
79 base_auxiliaires($GLOBALS['tables_auxiliaires']);
80 $infos_tables = array(
81 'spip_articles' => array(
83 'texte_retour' => 'icone_retour_article',
84 'texte_modifier' => 'icone_modifier_article',
85 'texte_creer' => 'icone_ecrire_article',
86 'texte_objets' => 'public:articles',
87 'texte_objet' => 'public:article',
88 'texte_signale_edition' => 'texte_travail_article',
89 'info_aucun_objet' => 'info_aucun_article',
90 'info_1_objet' => 'info_1_article',
91 'info_nb_objets' => 'info_nb_articles',
92 'texte_logo_objet' => 'logo_article',
93 'texte_langue_objet' => 'titre_langue_article',
94 'texte_definir_comme_traduction_objet' => 'trad_lier',
95 'titre' => 'titre, lang',
97 'principale' => 'oui',
98 'introduction_longueur' => '500',
99 'champs_editables' => array(
111 'champs_versionnes' => array(
125 'id_article' => 'bigint(21) NOT NULL',
126 'surtitre' => "text DEFAULT '' NOT NULL",
127 'titre' => "text DEFAULT '' NOT NULL",
128 'soustitre' => "text DEFAULT '' NOT NULL",
129 'id_rubrique' => "bigint(21) DEFAULT '0' NOT NULL",
130 'descriptif' => "text DEFAULT '' NOT NULL",
131 'chapo' => "mediumtext DEFAULT '' NOT NULL",
132 'texte' => "longtext DEFAULT '' NOT NULL",
133 'ps' => "mediumtext DEFAULT '' NOT NULL",
134 'date' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
135 'statut' => "varchar(10) DEFAULT '0' NOT NULL",
136 'id_secteur' => "bigint(21) DEFAULT '0' NOT NULL",
137 'maj' => 'TIMESTAMP',
138 'export' => "VARCHAR(10) DEFAULT 'oui'",
139 'date_redac' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
140 'visites' => "integer DEFAULT '0' NOT NULL",
141 'referers' => "integer DEFAULT '0' NOT NULL",
142 'popularite' => "DOUBLE DEFAULT '0' NOT NULL",
143 'accepter_forum' => "CHAR(3) DEFAULT '' NOT NULL",
144 'date_modif' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
145 'lang' => "VARCHAR(10) DEFAULT '' NOT NULL",
146 'langue_choisie' => "VARCHAR(3) DEFAULT 'non'",
147 'id_trad' => "bigint(21) DEFAULT '0' NOT NULL",
148 'nom_site' => "tinytext DEFAULT '' NOT NULL",
149 'url_site' => "text DEFAULT '' NOT NULL",
150 'virtuel' => "text DEFAULT '' NOT NULL",
153 'PRIMARY KEY' => 'id_article',
154 'KEY id_rubrique' => 'id_rubrique',
155 'KEY id_secteur' => 'id_secteur',
156 'KEY id_trad' => 'id_trad',
157 'KEY lang' => 'lang',
158 'KEY statut' => 'statut, date',
161 'id_article' => 'id_article',
162 'id_rubrique' => 'id_rubrique'
164 'rechercher_champs' => array(
175 'rechercher_jointures' => array(
176 'auteur' => array('nom' => 10),
181 'publie' => 'publie',
182 'previsu' => 'publie,prop,prepa/auteur',
183 'post_date' => 'date',
184 'exception' => array('statut', 'tout')
187 'statut_titres' => array(
188 'prepa' => 'info_article_redaction',
189 'prop' => 'info_article_propose',
190 'publie' => 'info_article_publie',
191 'refuse' => 'info_article_refuse',
192 'poubelle' => 'info_article_supprime'
194 'statut_textes_instituer' => array(
195 'prepa' => 'texte_statut_en_cours_redaction',
196 'prop' => 'texte_statut_propose_evaluation',
197 'publie' => 'texte_statut_publie',
198 'refuse' => 'texte_statut_refuse',
199 'poubelle' => 'texte_statut_poubelle',
201 'texte_changer_statut' => 'texte_article_statut',
202 'aide_changer_statut' => 'artstatut',
203 'tables_jointures' => array(
204 'profondeur' => 'rubriques',
205 #'id_auteur' => 'auteurs_liens' // declaration generique plus bas
208 'spip_auteurs' => array(
210 'texte_retour' => 'icone_retour',
211 'texte_ajouter' => 'titre_ajouter_un_auteur',
212 'texte_modifier' => 'admin_modifier_auteur',
213 'texte_objets' => 'icone_auteurs',
214 'texte_objet' => 'public:auteur',
215 'info_aucun_objet' => 'info_aucun_auteur',
216 'info_1_objet' => 'info_1_auteur',
217 'info_nb_objets' => 'info_nb_auteurs',
218 'texte_logo_objet' => 'logo_auteur',
219 'texte_creer_associer' => 'creer_et_associer_un_auteur',
220 'titre' => "nom AS titre, '' AS lang",
222 'principale' => 'oui',
223 'champs_editables' => array('nom', 'email', 'bio', 'nom_site', 'url_site', 'imessage', 'pgp'),
224 'champs_versionnes' => array('nom', 'bio', 'email', 'nom_site', 'url_site', 'login'),
226 'id_auteur' => 'bigint(21) NOT NULL',
227 'nom' => "text DEFAULT '' NOT NULL",
228 'bio' => "text DEFAULT '' NOT NULL",
229 'email' => "tinytext DEFAULT '' NOT NULL",
230 'nom_site' => "tinytext DEFAULT '' NOT NULL",
231 'url_site' => "text DEFAULT '' NOT NULL",
232 'login' => 'VARCHAR(255) BINARY',
233 'pass' => "tinytext DEFAULT '' NOT NULL",
234 'low_sec' => "tinytext DEFAULT '' NOT NULL",
235 'statut' => "varchar(255) DEFAULT '0' NOT NULL",
236 'webmestre' => "varchar(3) DEFAULT 'non' NOT NULL",
237 'maj' => 'TIMESTAMP',
238 'pgp' => "TEXT DEFAULT '' NOT NULL",
239 'htpass' => "tinytext DEFAULT '' NOT NULL",
240 'en_ligne' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
241 'alea_actuel' => 'tinytext',
242 'alea_futur' => 'tinytext',
244 'cookie_oubli' => 'tinytext',
245 'source' => "VARCHAR(10) DEFAULT 'spip' NOT NULL",
246 'lang' => "VARCHAR(10) DEFAULT '' NOT NULL"
249 'PRIMARY KEY' => 'id_auteur',
250 'KEY login' => 'login',
251 'KEY statut' => 'statut',
252 'KEY en_ligne' => 'en_ligne',
255 'id_auteur' => 'id_auteur',
258 'rechercher_champs' => array(
266 // 2 conditions pour les auteurs : statut!=poubelle,
267 // et avoir des articles publies
271 'publie' => '!5poubelle',
272 'previsu' => '!5poubelle',
273 'exception' => 'statut'
277 array('spip_auteurs_liens', 'id_auteur'),
280 array('id_objet', 'id_article', 'objet', 'article')
284 'publie' => 'publie',
286 'post_date' => 'date',
287 'exception' => array('statut', 'lien', 'tout')
290 'statut_images' => array(
291 'auteur-6forum-16.png',
292 '0minirezo' => 'auteur-0minirezo-16.png',
293 '1comite' => 'auteur-1comite-16.png',
294 '6forum' => 'auteur-6forum-16.png',
295 '5poubelle' => 'auteur-5poubelle-16.png',
298 'statut_titres' => array(
299 'titre_image_visiteur',
300 '0minirezo' => 'titre_image_administrateur',
301 '1comite' => 'titre_image_redacteur_02',
302 '6forum' => 'titre_image_visiteur',
303 '5poubelle' => 'titre_image_auteur_supprime',
305 'tables_jointures' => array(#'auteurs_liens' // declaration generique plus bas
308 'spip_rubriques' => array(
309 'page' => 'rubrique',
310 'url_voir' => 'rubrique',
311 'url_edit' => 'rubrique_edit',
312 'texte_retour' => 'icone_retour',
313 'texte_objets' => 'public:rubriques',
314 'texte_objet' => 'public:rubrique',
315 'texte_modifier' => 'icone_modifier_rubrique',
316 'texte_creer' => 'icone_creer_rubrique',
317 'texte_ajouter' => 'titre_ajouter_une_rubrique',
318 'texte_creer_associer' => 'creer_et_associer_une_rubrique',
319 'info_aucun_objet' => 'info_aucun_rubrique',
320 'info_1_objet' => 'info_1_rubrique',
321 'info_nb_objets' => 'info_nb_rubriques',
322 'texte_logo_objet' => 'logo_rubrique',
323 'texte_langue_objet' => 'titre_langue_rubrique',
324 'texte_definir_comme_traduction_objet' => 'texte_definir_comme_traduction_rubrique',
325 'titre' => 'titre, lang',
327 'principale' => 'oui',
328 'introduction_longueur' => '600',
329 'champs_editables' => array('titre', 'texte', 'descriptif', 'extra'),
330 'champs_versionnes' => array('titre', 'descriptif', 'texte'),
332 'id_rubrique' => 'bigint(21) NOT NULL',
333 'id_parent' => "bigint(21) DEFAULT '0' NOT NULL",
334 'titre' => "text DEFAULT '' NOT NULL",
335 'descriptif' => "text DEFAULT '' NOT NULL",
336 'texte' => "longtext DEFAULT '' NOT NULL",
337 'id_secteur' => "bigint(21) DEFAULT '0' NOT NULL",
338 'maj' => 'TIMESTAMP',
339 'statut' => "varchar(10) DEFAULT '0' NOT NULL",
340 'date' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
341 'lang' => "VARCHAR(10) DEFAULT '' NOT NULL",
342 'langue_choisie' => "VARCHAR(3) DEFAULT 'non'",
343 'statut_tmp' => "varchar(10) DEFAULT '0' NOT NULL",
344 'date_tmp' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
345 'profondeur' => "smallint(5) DEFAULT '0' NOT NULL"
348 'PRIMARY KEY' => 'id_rubrique',
349 'KEY lang' => 'lang',
350 'KEY id_parent' => 'id_parent',
352 'rechercher_champs' => array(
360 'publie' => 'publie',
362 'exception' => array('statut', 'tout')
365 'tables_jointures' => array(#'id_auteur' => 'auteurs_liens' // declaration generique plus bas
368 // toutes les tables ont le droit a une jointure sur les auteurs
369 array('tables_jointures' => array('id_auteur' => 'auteurs_liens'))
372 // avant d'appeller les pipeline qui peuvent generer une reentrance a l'install
373 // initialiser la signature
374 $md5 = md5(serialize($infos_tables));
376 $GLOBALS['tables_principales'] = pipeline('declarer_tables_principales', $GLOBALS['tables_principales']);
377 $GLOBALS['tables_auxiliaires'] = pipeline('declarer_tables_auxiliaires', $GLOBALS['tables_auxiliaires']);
378 $infos_tables = pipeline('declarer_tables_objets_sql', $infos_tables);
380 // completer les informations manquantes ou implicites
382 foreach (array_keys($infos_tables) as $t) {
383 // les cles numeriques servent a declarer
384 // les proprietes applicables a tous les objets
385 // on les mets de cote
386 if (is_numeric($t)) {
387 $all = array_merge_recursive($all, $infos_tables[$t]);
388 unset($infos_tables[$t]);
390 $infos_tables[$t] = renseigner_table_objet_sql($t, $infos_tables[$t]);
394 // repercuter les proprietes generales communes a tous les objets
395 foreach (array_keys($infos_tables) as $t) {
396 foreach ($all as $i => $v) {
397 if (in_array($i, array('tables_jointures', 'champs_versionnes'))) {
399 // eviter les doublons de declaration de table jointure (ex des mots sur auteurs)
400 // pour les declarations generiques avec cles numeriques
401 if ($i == 'tables_jointures' and isset($infos_tables[$t][$i]) and count($infos_tables[$t][$i])) {
402 $doublons = array_intersect($infos_tables[$t][$i], $add);
403 foreach ($doublons as $d) {
404 if (is_numeric(array_search($d, $infos_tables[$t][$i]))
405 and is_numeric($k = array_search($d, $add))
411 $infos_tables[$t][$i] = array_merge(isset($infos_tables[$t][$i]) ?
$infos_tables[$t][$i] : array(), $add);
413 $infos_tables[$t][$i] = array_merge_recursive(
414 isset($infos_tables[$t][$i]) ?
$infos_tables[$t][$i] : array(),
421 // completer les tables principales et auxiliaires
422 // avec celles declarees uniquement dans declarer_table_objets_sql
423 // pour assurer la compat en transition
424 foreach ($infos_tables as $table => $infos) {
425 $principale_ou_auxiliaire = ($infos['principale'] ?
'tables_principales' : 'tables_auxiliaires');
426 // memoriser des champs eventuels declares par des plugins dans le pipeline tables_xxx
427 // qui a ete appelle avant
428 $mem = (isset($GLOBALS[$principale_ou_auxiliaire][$table]) ?
$GLOBALS[$principale_ou_auxiliaire][$table] : array());
429 // l'ajouter au tableau
430 $GLOBALS[$principale_ou_auxiliaire][$table] = array();
431 if (isset($infos['field']) and isset($infos['key'])) {
432 foreach (array('field', 'key', 'join') as $k) {
433 if (isset($infos_tables[$table][$k])) {
434 $GLOBALS[$principale_ou_auxiliaire][$table][$k] = &$infos_tables[$table][$k];
438 // ici on ne renvoie que les declarations, donc RIEN
439 // pour avoir la vrai description en base, il faut passer par trouver_table
440 $GLOBALS[$principale_ou_auxiliaire][$table] = array();
443 foreach (array_keys($mem) as $k) {
444 if (isset($GLOBALS[$principale_ou_auxiliaire][$table][$k])) {
445 $GLOBALS[$principale_ou_auxiliaire][$table][$k] = array_merge(
446 $GLOBALS[$principale_ou_auxiliaire][$table][$k],
450 $GLOBALS[$principale_ou_auxiliaire][$table][$k] = $mem[$k];
456 // recuperer les interfaces (table_titre, table_date)
457 // on ne le fait que dans un second temps pour que table_objet soit fonctionnel
458 // dans le pipeline de declarer_tables_interfaces
459 include_spip('public/interfaces');
460 foreach (array_keys($infos_tables) as $t) {
461 $infos_tables[$t] = renseigner_table_objet_interfaces($t, $infos_tables[$t]);
466 $md5 = md5(serialize($infos_tables));
468 if ($table_sql === '::md5') {
471 if ($table_sql and !isset($infos_tables[$table_sql])) {
472 #$desc = renseigner_table_objet_sql($table_sql,$desc);
473 $desc = renseigner_table_objet_interfaces($table_sql, $desc);
478 return isset($infos_tables[$table_sql]) ?
$infos_tables[$table_sql] : array();
481 return $infos_tables;
486 * Déclare les tables principales du Core
488 * Tables principales, hors objets éditoriaux.
490 * @param array $tables_principales
491 * Description des tables principales déjà déclarées
494 function base_serial(&$tables_principales) {
497 'id_job' => 'bigint(21) NOT NULL',
498 'descriptif' => "text DEFAULT '' NOT NULL",
499 'fonction' => 'varchar(255) NOT NULL', //nom de la fonction
500 'args' => "longblob DEFAULT '' NOT NULL", // arguments
501 'md5args' => "char(32) NOT NULL default ''", // signature des arguments
502 'inclure' => 'varchar(255) NOT NULL', // fichier a inclure ou path/ pour charger_fonction
503 'priorite' => 'smallint(6) NOT NULL default 0',
504 'date' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL", // date au plus tot
505 'status' => 'tinyint NOT NULL default 1',
508 $spip_jobs_key = array(
509 'PRIMARY KEY' => 'id_job',
510 'KEY date' => 'date',
511 'KEY status' => 'status',
514 /// Attention: mes_fonctions peut avoir deja defini cette variable
515 /// il faut donc rajouter, mais pas reinitialiser
516 $tables_principales['spip_jobs'] = array('field' => &$spip_jobs, 'key' => &$spip_jobs_key);
521 * Déclare les tables auxiliaires du Core
523 * @param array $tables_auxiliaires
524 * Description des tables auxiliaires déjà déclarées
527 function base_auxiliaires(&$tables_auxiliaires) {
528 $spip_resultats = array(
529 'recherche' => "char(16) DEFAULT '' NOT NULL",
530 'id' => 'INT UNSIGNED NOT NULL',
531 'points' => "INT UNSIGNED DEFAULT '0' NOT NULL",
532 'table_objet' => "varchar(30) DEFAULT '' NOT NULL",
533 'serveur' => "char(16) DEFAULT '' NOT NULL", // hash md5 partiel du serveur de base ('' pour le serveur principal)
537 $spip_resultats_key = array(// pas de cle ni index, ca fait des insertions plus rapides et les requetes jointes utilisees en recheche ne sont pas plus lentes ...
540 $spip_auteurs_liens = array(
541 'id_auteur' => "bigint(21) DEFAULT '0' NOT NULL",
542 'id_objet' => "bigint(21) DEFAULT '0' NOT NULL",
543 'objet' => "VARCHAR (25) DEFAULT '' NOT NULL",
544 'vu' => "VARCHAR(6) DEFAULT 'non' NOT NULL"
547 $spip_auteurs_liens_key = array(
548 'PRIMARY KEY' => 'id_auteur,id_objet,objet',
549 'KEY id_auteur' => 'id_auteur',
550 'KEY id_objet' => 'id_objet',
551 'KEY objet' => 'objet',
555 'nom' => 'VARCHAR (255) NOT NULL',
556 'valeur' => "text DEFAULT ''",
557 'impt' => "ENUM('non', 'oui') DEFAULT 'oui' NOT NULL",
561 $spip_meta_key = array(
562 'PRIMARY KEY' => 'nom'
565 $spip_jobs_liens = array(
566 'id_job' => "bigint(21) DEFAULT '0' NOT NULL",
567 'id_objet' => "bigint(21) DEFAULT '0' NOT NULL",
568 'objet' => "VARCHAR (25) DEFAULT '' NOT NULL",
571 $spip_jobs_liens_key = array(
572 'PRIMARY KEY' => 'id_job,id_objet,objet',
573 'KEY id_job' => 'id_job'
576 $tables_auxiliaires['spip_auteurs_liens'] = array(
577 'field' => &$spip_auteurs_liens,
578 'key' => &$spip_auteurs_liens_key
581 $tables_auxiliaires['spip_meta'] = array(
582 'field' => &$spip_meta,
583 'key' => &$spip_meta_key
585 $tables_auxiliaires['spip_resultats'] = array(
586 'field' => &$spip_resultats,
587 'key' => &$spip_resultats_key
589 $tables_auxiliaires['spip_jobs_liens'] = array(
590 'field' => &$spip_jobs_liens,
591 'key' => &$spip_jobs_liens_key
598 * Auto remplissage des informations non explicites
599 * sur un objet d'une table sql
602 * - table_objet_surnoms
612 * - texte_creer_associer
622 * - texte_langue_objet
623 * - texte_definir_comme_traduction_objet
626 * - champs_contenu : utlisé pour générer l'affichage par défaut du contenu
628 * - champs_editables : utilisé pour prendre en compte le post lors de l'édition
630 * - champs_versionnes
632 * L'objet doit définir de lui même ces champs pour gérer des statuts :
636 * - statut_textes_instituer
637 * - texte_changer_statut
638 * - aide_changer_statut
640 * - modeles : permet de declarer les modeles associes a cet objet
642 * Les infos non renseignées sont auto-déduites par conventions
645 * @param string $table_sql
646 * @param array $infos
649 function renseigner_table_objet_sql($table_sql, &$infos) {
650 if (!isset($infos['type'])) {
651 // si on arrive de base/trouver_table, on a la cle primaire :
652 // s'en servir pour extrapoler le type
653 if (isset($infos['key']['PRIMARY KEY'])) {
654 $primary = $infos['key']['PRIMARY KEY'];
655 $primary = explode(',', $primary);
656 $primary = reset($primary);
657 $infos['type'] = preg_replace(',^spip_|^id_|s$,', '', $primary);
659 $infos['type'] = preg_replace(',^spip_|s$,', '', $table_sql);
662 if (!isset($infos['type_surnoms'])) {
663 $infos['type_surnoms'] = array();
666 if (!isset($infos['table_objet'])) {
667 $infos['table_objet'] = preg_replace(',^spip_,', '', $table_sql);
669 if (!isset($infos['table_objet_surnoms'])) {
670 $infos['table_objet_surnoms'] = array();
673 if (!isset($infos['principale'])) {
674 $infos['principale'] = (isset($GLOBALS['tables_principales'][$table_sql]) ?
'oui' : false);
677 // normaliser pour pouvoir tester en php $infos['principale']?
678 // et dans une boucle {principale=oui}
679 $infos['principale'] = (($infos['principale'] and $infos['principale'] != 'non') ?
'oui' : false);
681 // declarer et normaliser pour pouvoir tester en php $infos['editable']?
682 // et dans une boucle {editable=oui}
683 if (!isset($infos['editable'])) {
684 $infos['editable'] = 'oui';
687 $infos['editable'] = (($infos['editable'] and $infos['editable'] != 'non') ?
'oui' : false);
689 // les urls publiques sont par defaut page=type pour les tables principales, et rien pour les autres
690 // seules les exceptions sont donc a declarer
691 if (!isset($infos['page'])) {
692 $infos['page'] = ($infos['principale'] ?
$infos['type'] : '');
695 if (!isset($infos['url_voir'])) {
696 $infos['url_voir'] = $infos['type'];
698 if (!isset($infos['url_edit'])) {
699 $infos['url_edit'] = $infos['url_voir'] . ($infos['editable'] ?
'_edit' : '');
701 if (!isset($infos['icone_objet'])) {
702 $infos['icone_objet'] = $infos['type'];
706 // par defaut : objet:icone_xxx_objet
707 if (!isset($infos['texte_retour'])) {
708 $infos['texte_retour'] = 'icone_retour';
710 if (!isset($infos['texte_modifier'])) {
711 $infos['texte_modifier'] = $infos['type'] . ':' . 'icone_modifier_' . $infos['type'];
713 if (!isset($infos['texte_creer'])) {
714 $infos['texte_creer'] = $infos['type'] . ':' . 'icone_creer_' . $infos['type'];
716 if (!isset($infos['texte_creer_associer'])) {
717 $infos['texte_creer_associer'] = $infos['type'] . ':' . 'texte_creer_associer_' . $infos['type'];
719 if (!isset($infos['texte_ajouter'])) {
721 $infos['texte_ajouter'] = $infos['type'] . ':' . 'texte_ajouter_' . $infos['type'];
723 if (!isset($infos['texte_objets'])) {
724 $infos['texte_objets'] = $infos['type'] . ':' . 'titre_' . $infos['table_objet'];
726 if (!isset($infos['texte_objet'])) {
727 $infos['texte_objet'] = $infos['type'] . ':' . 'titre_' . $infos['type'];
729 if (!isset($infos['texte_logo_objet'])) {
730 // objet:titre_logo_objet "Logo de ce X"
731 $infos['texte_logo_objet'] = $infos['type'] . ':' . 'titre_logo_' . $infos['type'];
733 if (!isset($infos['texte_langue_objet'])) {
734 // objet:texte_langue_objet "Langue de ce X"
735 $infos['texte_langue_objet'] = $infos['type'] . ':' . 'titre_langue_' . $infos['type'];
737 if (!isset($infos['texte_definir_comme_traduction_objet'])) {
738 // "Ce X est une traduction du X numéro :"
739 $infos['texte_definir_comme_traduction_objet'] = $infos['type'] . ':' . 'texte_definir_comme_traduction_' . $infos['type'];
742 // objet:info_aucun_objet
743 if (!isset($infos['info_aucun_objet'])) {
744 $infos['info_aucun_objet'] = $infos['type'] . ':' . 'info_aucun_' . $infos['type'];
746 // objet:info_1_objet
747 if (!isset($infos['info_1_objet'])) {
748 $infos['info_1_objet'] = $infos['type'] . ':' . 'info_1_' . $infos['type'];
750 // objet:info_nb_objets
751 if (!isset($infos['info_nb_objets'])) {
752 $infos['info_nb_objets'] = $infos['type'] . ':' . 'info_nb_' . $infos['table_objet'];
755 if (!isset($infos['champs_editables'])) {
756 $infos['champs_editables'] = array();
758 if (!isset($infos['champs_versionnes'])) {
759 $infos['champs_versionnes'] = array();
761 if (!isset($infos['rechercher_champs'])) {
762 $infos['rechercher_champs'] = array();
764 if (!isset($infos['rechercher_jointures'])) {
765 $infos['rechercher_jointures'] = array();
768 if (!isset($infos['modeles'])) {
769 $infos['modeles'] = array($infos['type']);
776 * Renseigner les infos d'interface compilateur pour les tables objets
777 * complete la declaration precedente
788 function renseigner_table_objet_interfaces($table_sql, &$infos) {
789 if (!isset($infos['titre'])) {
790 if (isset($infos['table_objet']) and isset($GLOBALS['table_titre'][$infos['table_objet']])) {
791 $infos['titre'] = $GLOBALS['table_titre'][$infos['table_objet']];
793 $infos['titre'] = ((isset($infos['field']['titre'])) ?
'titre,' : "'' as titre,");
794 $infos['titre'] .= ((isset($infos['field']['lang'])) ?
'lang' : "'' as lang");
797 if (!isset($infos['date'])) {
798 if (isset($infos['table_objet']) and isset($GLOBALS['table_date'][$infos['table_objet']])) {
799 $infos['date'] = $GLOBALS['table_date'][$infos['table_objet']];
801 $infos['date'] = ((isset($infos['field']['date'])) ?
'date' : '');
804 if (!isset($infos['statut'])) {
805 $infos['statut'] = isset($GLOBALS['table_statut'][$table_sql]) ?
$GLOBALS['table_statut'][$table_sql] : '';
807 if (!isset($infos['tables_jointures'])) {
808 $infos['tables_jointures'] = array();
810 if (isset($GLOBALS['tables_jointures'][$table_sql])) {
811 $infos['tables_jointures'] = array_merge($infos['tables_jointures'], $GLOBALS['tables_jointures'][$table_sql]);
818 * Retourne la liste des tables principales et leurs descriptions
822 * Liste et descriptions des tables principales
824 function lister_tables_principales() {
825 static $done = false;
826 if (!$done or !count($GLOBALS['tables_principales'])) {
827 lister_tables_objets_sql();
831 return $GLOBALS['tables_principales'];
835 * Retourne la liste des tables auxiliaires et leurs descriptions
839 * Liste et descriptions des tables auxiliaires
841 function lister_tables_auxiliaires() {
842 static $done = false;
843 if (!$done or !count($GLOBALS['tables_auxiliaires'])) {
844 lister_tables_objets_sql();
848 return $GLOBALS['tables_auxiliaires'];
852 * Recenser les surnoms de table_objet
856 function lister_tables_objets_surnoms() {
857 static $surnoms = null;
860 or $md5 != lister_tables_objets_sql('::md5')
862 // passer dans un pipeline qui permet aux plugins de declarer leurs exceptions
863 // pour compatibilite, car il faut dorenavent utiliser
864 // declarer_table_objets_sql
866 'declarer_tables_objets_surnoms',
870 'doc' => 'documents',
871 'img' => 'documents',
872 'emb' => 'documents',
875 $infos_tables = lister_tables_objets_sql();
876 foreach ($infos_tables as $t => $infos) {
877 // cas de base type=>table
878 // et preg_replace(',^spip_|^id_|s$,',table)=>table
879 if ($infos['table_objet']) { // securite, si la fonction est appelee trop tot, c'est vide
880 // optimisations pour table_objet
881 //$surnoms[$infos['type']] = $infos['table_objet'];
882 $surnoms[preg_replace(',^spip_|^id_|s$,', '', $infos['table_objet'])] = $infos['table_objet'];
883 $surnoms[preg_replace(',^spip_|^id_|s$,', '', $infos['type'])] = $infos['table_objet'];
884 if (is_array($infos['table_objet_surnoms']) and count($infos['table_objet_surnoms'])) {
885 foreach ($infos['table_objet_surnoms'] as $surnom) {
886 $surnoms[$surnom] = $infos['table_objet'];
891 $md5 = lister_tables_objets_sql('::md5');
898 * Recenser les surnoms de table_objet
902 function lister_types_surnoms() {
903 static $surnoms = null;
906 or $md5 != lister_tables_objets_sql('::md5')
908 // passer dans un pipeline qui permet aux plugins de declarer leurs exceptions
909 // pour compatibilite, car il faut dorenavent utiliser
910 // declarer_table_objets_sql
911 $surnoms = pipeline('declarer_type_surnoms', array('racine-site' => 'site'));
912 $infos_tables = lister_tables_objets_sql();
913 foreach ($infos_tables as $t => $infos) {
914 if ($infos['type']) { // securite, si la fonction est appelee trop tot, c'est vide
915 // optimisations pour objet_type
916 //$surnoms[$infos['type']] = $infos['type'];
917 $surnoms[preg_replace(',^spip_|^id_|s$,', '', $infos['table_objet'])] = $infos['type'];
918 $surnoms[preg_replace(',^spip_|^id_|s$,', '', $infos['type'])] = $infos['type'];
920 if (is_array($infos['type_surnoms']) and count($infos['type_surnoms'])) {
921 foreach ($infos['type_surnoms'] as $surnom) {
922 $surnoms[$surnom] = $infos['type'];
927 $md5 = lister_tables_objets_sql('::md5');
934 * Retourne la liste des tables SQL qui concernent SPIP
936 * Cette liste n'est calculée qu'une fois par serveur pour l'ensemble du hit
938 * @param string $serveur
939 * Nom du fichier de connexion à la base de données
941 * Couples (nom de la table SQL => même nom, sans 'spip_' devant)
943 function lister_tables_spip($serveur = '') {
944 static $tables = array();
945 if (!isset($tables[$serveur])) {
946 $tables[$serveur] = array();
947 if (!function_exists('sql_alltable')) {
948 include_spip('base/abstract_sql');
950 $ts = sql_alltable(null, $serveur); // toutes les tables "spip_" (ou prefixe perso)
951 $connexion = $GLOBALS['connexions'][$serveur ?
$serveur : 0];
952 $spip = $connexion['prefixe'] . '_';
953 foreach ($ts as $t) {
954 $t = substr($t, strlen($spip));
955 $tables[$serveur]["spip_$t"] = $t;
959 return $tables[$serveur];
964 * Retourne la liste des tables SQL, Spip ou autres
966 * Cette liste n'est calculée qu'une fois par serveur pour l'ensemble du hit
968 * @param string $serveur
969 * Nom du fichier de connexion à la base de données
971 * Couples (nom de la table SQL => même nom)
973 function lister_toutes_tables($serveur){
974 static $tables = array();
975 if (!isset($tables[$serveur])){
976 $tables[$serveur] = array();
977 if (!function_exists("sql_alltable"))
978 include_spip("base/abstract_sql");
979 $ts = sql_alltable('%',$serveur); // toutes les tables
981 $tables[$serveur][$t] = $t;
984 return $tables[$serveur];
988 * Retrouve le nom d'objet à partir de la table
990 * - spip_articles -> articles
991 * - id_article -> articles
992 * - article -> articles
995 * @param string $type
996 * Nom de la table SQL (le plus souvent)
997 * Tolère un nom de clé primaire.
998 * @param string $serveur
1003 function table_objet($type, $serveur = '') {
1004 $surnoms = lister_tables_objets_surnoms();
1005 $type = preg_replace(',^spip_|^id_|s$,', '', $type);
1009 if (isset($surnoms[$type])) {
1010 return $surnoms[$type];
1013 if ($serveur !== false) {
1014 $t = lister_tables_spip($serveur);
1015 $trouver_table = charger_fonction('trouver_table', 'base');
1016 $typetrim = rtrim($type, 's') . 's';
1017 if ((isset($t[$typetrim]) or in_array($typetrim, $t))
1018 and ($desc = $trouver_table(rtrim($type, 's') . 's', $serveur))
1020 return $desc['id_table'];
1021 } elseif ((isset($t[$type]) or in_array($type, $t))
1022 and ($desc = $trouver_table($type, $serveur))
1024 return $desc['id_table'];
1027 spip_log('table_objet(' . $type . ') calculee sans verification');
1028 #spip_log(debug_backtrace(),'db');
1031 return rtrim($type, 's') . 's'; # cas historique ne devant plus servir, sauf si $serveur=false
1035 * Retrouve la table sql à partir de l'objet ou du type
1037 * - articles -> spip_articles
1038 * - article -> spip_articles
1039 * - id_article -> spip_articles
1042 * @param string $type
1043 * Nom ou type de l'objet
1044 * Tolère un nom de clé primaire.
1045 * @param string $serveur
1048 * Nom de la table SQL
1050 function table_objet_sql($type, $serveur = '') {
1052 $nom = table_objet($type, $serveur);
1053 if (!isset($GLOBALS['table_des_tables']['articles'])) {
1054 // eviter de multiples inclusions
1055 include_spip('public/interfaces');
1057 if (isset($GLOBALS['table_des_tables'][$nom])) {
1058 $nom = $GLOBALS['table_des_tables'][$nom];
1061 $infos_tables = lister_tables_objets_sql();
1062 if (isset($infos_tables["spip_$nom"])) {
1064 } elseif ($serveur !== false) {
1065 $t = lister_tables_spip($serveur);
1066 if (isset($t[$nom]) or in_array($nom, $t)) {
1067 $trouver_table = charger_fonction('trouver_table', 'base');
1068 if ($desc = $trouver_table($nom, $serveur)) {
1069 return $desc['table_sql'];
1079 * Retrouve la clé primaire à partir du nom d'objet ou de table
1081 * - articles -> id_article
1082 * - article -> id_article
1083 * - spip_articles -> id_article
1086 * @param string $type
1087 * Nom de la table SQL ou de l'objet
1088 * @param string $serveur
1091 * Nom de la clé primaire
1093 function id_table_objet($type, $serveur = '') {
1094 static $trouver_table = null;
1095 $type = objet_type($type, $serveur);
1099 $t = table_objet($type);
1100 if (!$trouver_table) {
1101 $trouver_table = charger_fonction('trouver_table', 'base');
1104 $ts = lister_tables_spip($serveur);
1107 or in_array($t, lister_toutes_tables($serveur))
1109 $desc = $trouver_table($t, $serveur);
1110 if (isset($desc['key']['PRIMARY KEY'])) {
1111 return $desc['key']['PRIMARY KEY'];
1113 if (!$desc or isset($desc['field']["id_$type"])) {
1116 // sinon renvoyer le premier champ de la table...
1117 $keys = array_keys($desc['field']);
1119 return array_shift($keys);
1126 * Retrouve le type d'objet à partir du nom d'objet ou de table
1128 * - articles -> article
1129 * - spip_articles -> article
1130 * - id_article -> article
1133 * @param string $table_objet
1134 * Nom de l'objet ou de la table SQL
1135 * @param string $serveur
1140 function objet_type($table_objet, $serveur = '') {
1141 if (!$table_objet) {
1144 $surnoms = lister_types_surnoms();
1147 // le type est decline a partir du nom de la table en enlevant le prefixe eventuel
1148 // et la marque du pluriel
1149 // on accepte id_xx en entree aussi
1150 $type = preg_replace(',^spip_|^id_|s$,', '', $table_objet);
1151 if (isset($surnoms[$type])) {
1152 return $surnoms[$type];
1155 // securite : eliminer les caracteres non \w
1156 $type = preg_replace(',[^\w-],', '', $type);
1158 // si le type redonne bien la table c'est bon
1159 // oui si table_objet ressemblait deja a un type
1160 if ($type == $table_objet
1161 or (table_objet($type, $serveur) == $table_objet)
1162 or (table_objet_sql($type, $serveur) == $table_objet)
1167 // si on ne veut pas chercher en base
1168 if ($serveur === false) {
1172 // sinon on passe par la cle primaire id_xx pour trouver le type
1173 // car le s a la fin est incertain
1174 // notamment en cas de pluriel derogatoire
1175 // id_jeu/spip_jeux id_journal/spip_journaux qui necessitent tout deux
1176 // une declaration jeu => jeux, journal => journaux
1177 // dans le pipeline declarer_tables_objets_surnoms
1178 $trouver_table = charger_fonction('trouver_table', 'base');
1179 $ts = lister_tables_spip($serveur);
1181 if (in_array($table_objet, $ts)) {
1182 $desc = $trouver_table($table_objet);
1184 if (!$desc and in_array($table_objet = table_objet($type, $serveur), $ts)) {
1185 $desc = $trouver_table($table_objet, $serveur);
1187 // si le type est declare : bingo !
1188 if ($desc and isset($desc['type'])) {
1189 return $desc['type'];
1192 // on a fait ce qu'on a pu
1197 * Determininer si un objet est publie ou non
1199 * On se base pour cela sur sa declaration de statut
1200 * pour des cas particuliers non declarables, on permet de fournir une fonction
1201 * base_xxxx_test_si_publie qui sera appele par la fonction
1203 * @param string $objet
1204 * @param int $id_objet
1205 * @param string $serveur
1208 function objet_test_si_publie($objet, $id_objet, $serveur = '') {
1209 // voir si une fonction est definie pour faire le boulot
1210 // elle a la priorite dans ce cas
1211 if ($f = charger_fonction($objet . '_test_si_publie', 'base', true)) {
1212 return $f($objet, $id_objet, $serveur);
1215 // sinon on se fie a la declaration de l'objet si presente
1216 $id_table = $table_objet = table_objet($objet);
1217 $id_table_objet = id_table_objet($objet, $serveur);
1218 $trouver_table = charger_fonction('trouver_table', 'base');
1219 if ($desc = $trouver_table($table_objet, $serveur)
1220 and isset($desc['statut'])
1223 $boucle = new Boucle();
1224 $boucle->show
= $desc;
1225 $boucle->nom
= 'objet_test_si_publie';
1226 $boucle->id_boucle
= $id_table;
1227 $boucle->id_table
= $id_table;
1228 $boucle->sql_serveur
= $serveur;
1229 $boucle->select
[] = $id_table_objet;
1230 $boucle->from
[$table_objet] = table_objet_sql($objet, $serveur);
1231 $boucle->where
[] = $id_table . '.' . $id_table_objet . '=' . intval($id_objet);
1233 include_spip('public/compiler');
1234 include_spip('public/composer');
1235 instituer_boucle($boucle, false, true);
1236 $res = calculer_select(
1250 if (sql_fetch($res)) {
1257 // si pas d'info statut ni de fonction : l'objet est publie