1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
|
<?php
/***************************************************************************\
* SPIP, Systeme de publication pour l'internet *
* *
* Copyright (c) 2001-2019 *
* Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
* *
* Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
* Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
\***************************************************************************/
/**
* Fonctions relatives aux objets éditoriaux et SQL
*
* @package SPIP\Core\SQL\Tables
**/
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
/**
* Merge dans un tableau une de ses clés avec une valeur
*
* @param array $table
* Tableau dont on veut compléter une clé
* @param string $index
* Clé du tableau que l'on souhaite compléter
* @param array $valeur
* Sous tableau à merger dans la clé.
* @return void
**/
function array_set_merge(&$table, $index, $valeur) {
if (!isset($table[$index])) {
$table[$index] = $valeur;
} else {
$table[$index] = array_merge($table[$index], $valeur);
}
}
/**
* Lister les infos de toutes les tables sql declarées
*
* Si un argument est fourni, on ne renvoie que les infos de cette table.
* Elle est auto-declarée si inconnue jusqu'alors.
*
* @api
* @param string $table_sql
* table_sql demandee explicitement
* @param array $desc
* description connue de la table sql demandee
* @return array|bool
*/
function lister_tables_objets_sql($table_sql = null, $desc = array()) {
static $deja_la = false;
static $infos_tables = null;
static $md5 = null;
static $plugin_hash = null;
// plugins hash connu ? non si _CACHE_PLUGINS_OPT est pas encore chargé.
$_PLUGINS_HASH = defined('_PLUGINS_HASH') ? _PLUGINS_HASH : '!_CACHE_PLUGINS_OPT';
// prealablement recuperer les tables_principales
if (is_null($infos_tables) or $plugin_hash !== $_PLUGINS_HASH) {
// pas de reentrance (cas base/serial)
if ($deja_la) {
spip_log('Re-entrance anormale sur lister_tables_objets_sql : '
. var_export(debug_backtrace(), true), _LOG_CRITIQUE);
return ($table_sql === '::md5' ? $md5 : array());
}
$deja_la = true;
$plugin_hash = $_PLUGINS_HASH; // avant de lancer les pipelines
// recuperer les declarations explicites ancienne mode
// qui servent a completer declarer_tables_objets_sql
base_serial($GLOBALS['tables_principales']);
base_auxiliaires($GLOBALS['tables_auxiliaires']);
$infos_tables = array(
'spip_articles' => array(
'page' => 'article',
'texte_retour' => 'icone_retour_article',
'texte_modifier' => 'icone_modifier_article',
'texte_creer' => 'icone_ecrire_article',
'texte_objets' => 'public:articles',
'texte_objet' => 'public:article',
'texte_signale_edition' => 'texte_travail_article',
'info_aucun_objet' => 'info_aucun_article',
'info_1_objet' => 'info_1_article',
'info_nb_objets' => 'info_nb_articles',
'texte_logo_objet' => 'logo_article',
'texte_langue_objet' => 'titre_langue_article',
'texte_definir_comme_traduction_objet' => 'trad_lier',
'titre' => 'titre, lang',
'date' => 'date',
'principale' => 'oui',
'introduction_longueur' => '500',
'champs_editables' => array(
'surtitre',
'titre',
'soustitre',
'descriptif',
'nom_site',
'url_site',
'chapo',
'texte',
'ps',
'virtuel'
),
'champs_versionnes' => array(
'id_rubrique',
'surtitre',
'titre',
'soustitre',
'jointure_auteurs',
'descriptif',
'nom_site',
'url_site',
'chapo',
'texte',
'ps'
),
'field' => array(
'id_article' => 'bigint(21) NOT NULL',
'surtitre' => "text DEFAULT '' NOT NULL",
'titre' => "text DEFAULT '' NOT NULL",
'soustitre' => "text DEFAULT '' NOT NULL",
'id_rubrique' => "bigint(21) DEFAULT '0' NOT NULL",
'descriptif' => "text DEFAULT '' NOT NULL",
'chapo' => "mediumtext DEFAULT '' NOT NULL",
'texte' => "longtext DEFAULT '' NOT NULL",
'ps' => "mediumtext DEFAULT '' NOT NULL",
'date' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
'statut' => "varchar(10) DEFAULT '0' NOT NULL",
'id_secteur' => "bigint(21) DEFAULT '0' NOT NULL",
'maj' => 'TIMESTAMP',
'export' => "VARCHAR(10) DEFAULT 'oui'",
'date_redac' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
'visites' => "integer DEFAULT '0' NOT NULL",
'referers' => "integer DEFAULT '0' NOT NULL",
'popularite' => "DOUBLE DEFAULT '0' NOT NULL",
'accepter_forum' => "CHAR(3) DEFAULT '' NOT NULL",
'date_modif' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
'lang' => "VARCHAR(10) DEFAULT '' NOT NULL",
'langue_choisie' => "VARCHAR(3) DEFAULT 'non'",
'id_trad' => "bigint(21) DEFAULT '0' NOT NULL",
'nom_site' => "tinytext DEFAULT '' NOT NULL",
'url_site' => "text DEFAULT '' NOT NULL",
'virtuel' => "text DEFAULT '' NOT NULL",
),
'key' => array(
'PRIMARY KEY' => 'id_article',
'KEY id_rubrique' => 'id_rubrique',
'KEY id_secteur' => 'id_secteur',
'KEY id_trad' => 'id_trad',
'KEY lang' => 'lang',
'KEY statut' => 'statut, date',
),
'join' => array(
'id_article' => 'id_article',
'id_rubrique' => 'id_rubrique'
),
'rechercher_champs' => array(
'surtitre' => 5,
'titre' => 8,
'soustitre' => 5,
'chapo' => 3,
'texte' => 1,
'ps' => 1,
'nom_site' => 1,
'url_site' => 1,
'descriptif' => 4
),
'rechercher_jointures' => array(
'auteur' => array('nom' => 10),
),
'statut' => array(
array(
'champ' => 'statut',
'publie' => 'publie',
'previsu' => 'publie,prop,prepa/auteur',
'post_date' => 'date',
'exception' => array('statut', 'tout')
)
),
'statut_titres' => array(
'prepa' => 'info_article_redaction',
'prop' => 'info_article_propose',
'publie' => 'info_article_publie',
'refuse' => 'info_article_refuse',
'poubelle' => 'info_article_supprime'
),
'statut_textes_instituer' => array(
'prepa' => 'texte_statut_en_cours_redaction',
'prop' => 'texte_statut_propose_evaluation',
'publie' => 'texte_statut_publie',
'refuse' => 'texte_statut_refuse',
'poubelle' => 'texte_statut_poubelle',
),
'texte_changer_statut' => 'texte_article_statut',
'aide_changer_statut' => 'artstatut',
'tables_jointures' => array(
'profondeur' => 'rubriques',
#'id_auteur' => 'auteurs_liens' // declaration generique plus bas
),
),
'spip_auteurs' => array(
'page' => 'auteur',
'texte_retour' => 'icone_retour',
'texte_ajouter' => 'titre_ajouter_un_auteur',
'texte_modifier' => 'admin_modifier_auteur',
'texte_objets' => 'icone_auteurs',
'texte_objet' => 'public:auteur',
'info_aucun_objet' => 'info_aucun_auteur',
'info_1_objet' => 'info_1_auteur',
'info_nb_objets' => 'info_nb_auteurs',
'texte_logo_objet' => 'logo_auteur',
'texte_creer_associer' => 'creer_et_associer_un_auteur',
'titre' => "nom AS titre, '' AS lang",
'date' => 'date',
'principale' => 'oui',
'champs_editables' => array('nom', 'email', 'bio', 'nom_site', 'url_site', 'imessage', 'pgp'),
'champs_versionnes' => array('nom', 'bio', 'email', 'nom_site', 'url_site', 'login'),
'field' => array(
'id_auteur' => 'bigint(21) NOT NULL',
'nom' => "text DEFAULT '' NOT NULL",
'bio' => "text DEFAULT '' NOT NULL",
'email' => "tinytext DEFAULT '' NOT NULL",
'nom_site' => "tinytext DEFAULT '' NOT NULL",
'url_site' => "text DEFAULT '' NOT NULL",
'login' => 'VARCHAR(255) BINARY',
'pass' => "tinytext DEFAULT '' NOT NULL",
'low_sec' => "tinytext DEFAULT '' NOT NULL",
'statut' => "varchar(255) DEFAULT '0' NOT NULL",
'webmestre' => "varchar(3) DEFAULT 'non' NOT NULL",
'maj' => 'TIMESTAMP',
'pgp' => "TEXT DEFAULT '' NOT NULL",
'htpass' => "tinytext DEFAULT '' NOT NULL",
'en_ligne' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
'alea_actuel' => 'tinytext',
'alea_futur' => 'tinytext',
'prefs' => 'text',
'cookie_oubli' => 'tinytext',
'source' => "VARCHAR(10) DEFAULT 'spip' NOT NULL",
'lang' => "VARCHAR(10) DEFAULT '' NOT NULL"
),
'key' => array(
'PRIMARY KEY' => 'id_auteur',
'KEY login' => 'login',
'KEY statut' => 'statut',
'KEY en_ligne' => 'en_ligne',
),
'join' => array(
'id_auteur' => 'id_auteur',
'login' => 'login'
),
'rechercher_champs' => array(
'nom' => 5,
'bio' => 1,
'email' => 1,
'nom_site' => 1,
'url_site' => 1,
'login' => 1
),
// 2 conditions pour les auteurs : statut!=poubelle,
// et avoir des articles publies
'statut' => array(
array(
'champ' => 'statut',
'publie' => '!5poubelle',
'previsu' => '!5poubelle',
'exception' => 'statut'
),
array(
'champ' => array(
array('spip_auteurs_liens', 'id_auteur'),
array(
'spip_articles',
array('id_objet', 'id_article', 'objet', 'article')
),
'statut'
),
'publie' => 'publie',
'previsu' => '!',
'post_date' => 'date',
'exception' => array('statut', 'lien', 'tout')
),
),
'statut_images' => array(
'auteur-6forum-16.png',
'0minirezo' => 'auteur-0minirezo-16.png',
'1comite' => 'auteur-1comite-16.png',
'6forum' => 'auteur-6forum-16.png',
'5poubelle' => 'auteur-5poubelle-16.png',
'nouveau' => ''
),
'statut_titres' => array(
'titre_image_visiteur',
'0minirezo' => 'titre_image_administrateur',
'1comite' => 'titre_image_redacteur_02',
'6forum' => 'titre_image_visiteur',
'5poubelle' => 'titre_image_auteur_supprime',
),
'tables_jointures' => array(#'auteurs_liens' // declaration generique plus bas
),
),
'spip_rubriques' => array(
'page' => 'rubrique',
'url_voir' => 'rubrique',
'url_edit' => 'rubrique_edit',
'texte_retour' => 'icone_retour',
'texte_objets' => 'public:rubriques',
'texte_objet' => 'public:rubrique',
'texte_modifier' => 'icone_modifier_rubrique',
'texte_creer' => 'icone_creer_rubrique',
'texte_ajouter' => 'titre_ajouter_une_rubrique',
'texte_creer_associer' => 'creer_et_associer_une_rubrique',
'info_aucun_objet' => 'info_aucun_rubrique',
'info_1_objet' => 'info_1_rubrique',
'info_nb_objets' => 'info_nb_rubriques',
'texte_logo_objet' => 'logo_rubrique',
'texte_langue_objet' => 'titre_langue_rubrique',
'texte_definir_comme_traduction_objet' => 'texte_definir_comme_traduction_rubrique',
'titre' => 'titre, lang',
'date' => 'date',
'principale' => 'oui',
'introduction_longueur' => '600',
'champs_editables' => array('titre', 'texte', 'descriptif', 'extra'),
'champs_versionnes' => array('titre', 'descriptif', 'texte'),
'field' => array(
'id_rubrique' => 'bigint(21) NOT NULL',
'id_parent' => "bigint(21) DEFAULT '0' NOT NULL",
'titre' => "text DEFAULT '' NOT NULL",
'descriptif' => "text DEFAULT '' NOT NULL",
'texte' => "longtext DEFAULT '' NOT NULL",
'id_secteur' => "bigint(21) DEFAULT '0' NOT NULL",
'maj' => 'TIMESTAMP',
'statut' => "varchar(10) DEFAULT '0' NOT NULL",
'date' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
'lang' => "VARCHAR(10) DEFAULT '' NOT NULL",
'langue_choisie' => "VARCHAR(3) DEFAULT 'non'",
'statut_tmp' => "varchar(10) DEFAULT '0' NOT NULL",
'date_tmp' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
'profondeur' => "smallint(5) DEFAULT '0' NOT NULL"
),
'key' => array(
'PRIMARY KEY' => 'id_rubrique',
'KEY lang' => 'lang',
'KEY id_parent' => 'id_parent',
),
'rechercher_champs' => array(
'titre' => 8,
'descriptif' => 5,
'texte' => 1
),
'statut' => array(
array(
'champ' => 'statut',
'publie' => 'publie',
'previsu' => '!',
'exception' => array('statut', 'tout')
),
),
'tables_jointures' => array(#'id_auteur' => 'auteurs_liens' // declaration generique plus bas
),
),
// toutes les tables ont le droit a une jointure sur les auteurs
array('tables_jointures' => array('id_auteur' => 'auteurs_liens'))
);
// avant d'appeller les pipeline qui peuvent generer une reentrance a l'install
// initialiser la signature
$md5 = md5(serialize($infos_tables));
$GLOBALS['tables_principales'] = pipeline('declarer_tables_principales', $GLOBALS['tables_principales']);
$GLOBALS['tables_auxiliaires'] = pipeline('declarer_tables_auxiliaires', $GLOBALS['tables_auxiliaires']);
$infos_tables = pipeline('declarer_tables_objets_sql', $infos_tables);
// completer les informations manquantes ou implicites
$all = array();
foreach (array_keys($infos_tables) as $t) {
// les cles numeriques servent a declarer
// les proprietes applicables a tous les objets
// on les mets de cote
if (is_numeric($t)) {
$all = array_merge_recursive($all, $infos_tables[$t]);
unset($infos_tables[$t]);
} else {
$infos_tables[$t] = renseigner_table_objet_sql($t, $infos_tables[$t]);
}
}
// repercuter les proprietes generales communes a tous les objets
foreach (array_keys($infos_tables) as $t) {
foreach ($all as $i => $v) {
if (in_array($i, array('tables_jointures', 'champs_versionnes'))) {
$add = $all[$i];
// eviter les doublons de declaration de table jointure (ex des mots sur auteurs)
// pour les declarations generiques avec cles numeriques
if ($i == 'tables_jointures' and isset($infos_tables[$t][$i]) and count($infos_tables[$t][$i])) {
$doublons = array_intersect($infos_tables[$t][$i], $add);
foreach ($doublons as $d) {
if (is_numeric(array_search($d, $infos_tables[$t][$i]))
and is_numeric($k = array_search($d, $add))
) {
unset($add[$k]);
}
}
}
$infos_tables[$t][$i] = array_merge(isset($infos_tables[$t][$i]) ? $infos_tables[$t][$i] : array(), $add);
} else {
$infos_tables[$t][$i] = array_merge_recursive(
isset($infos_tables[$t][$i]) ? $infos_tables[$t][$i] : array(),
$all[$i]
);
}
}
}
// completer les tables principales et auxiliaires
// avec celles declarees uniquement dans declarer_table_objets_sql
// pour assurer la compat en transition
foreach ($infos_tables as $table => $infos) {
$principale_ou_auxiliaire = ($infos['principale'] ? 'tables_principales' : 'tables_auxiliaires');
// memoriser des champs eventuels declares par des plugins dans le pipeline tables_xxx
// qui a ete appelle avant
$mem = (isset($GLOBALS[$principale_ou_auxiliaire][$table]) ? $GLOBALS[$principale_ou_auxiliaire][$table] : array());
// l'ajouter au tableau
$GLOBALS[$principale_ou_auxiliaire][$table] = array();
if (isset($infos['field']) and isset($infos['key'])) {
foreach (array('field', 'key', 'join') as $k) {
if (isset($infos_tables[$table][$k])) {
$GLOBALS[$principale_ou_auxiliaire][$table][$k] = &$infos_tables[$table][$k];
}
}
} else {
// ici on ne renvoie que les declarations, donc RIEN
// pour avoir la vrai description en base, il faut passer par trouver_table
$GLOBALS[$principale_ou_auxiliaire][$table] = array();
}
if (count($mem)) {
foreach (array_keys($mem) as $k) {
if (isset($GLOBALS[$principale_ou_auxiliaire][$table][$k])) {
$GLOBALS[$principale_ou_auxiliaire][$table][$k] = array_merge(
$GLOBALS[$principale_ou_auxiliaire][$table][$k],
$mem[$k]
);
} else {
$GLOBALS[$principale_ou_auxiliaire][$table][$k] = $mem[$k];
}
}
}
}
// recuperer les interfaces (table_titre, table_date)
// on ne le fait que dans un second temps pour que table_objet soit fonctionnel
// dans le pipeline de declarer_tables_interfaces
include_spip('public/interfaces');
foreach (array_keys($infos_tables) as $t) {
$infos_tables[$t] = renseigner_table_objet_interfaces($t, $infos_tables[$t]);
}
$deja_la = false;
// signature
$md5 = md5(serialize($infos_tables));
}
if ($table_sql === '::md5') {
return $md5;
}
if ($table_sql and !isset($infos_tables[$table_sql])) {
#$desc = renseigner_table_objet_sql($table_sql,$desc);
$desc = renseigner_table_objet_interfaces($table_sql, $desc);
return $desc;
}
if ($table_sql) {
return isset($infos_tables[$table_sql]) ? $infos_tables[$table_sql] : array();
}
return $infos_tables;
}
/**
* Déclare les tables principales du Core
*
* Tables principales, hors objets éditoriaux.
*
* @param array $tables_principales
* Description des tables principales déjà déclarées
* @return void
**/
function base_serial(&$tables_principales) {
$spip_jobs = array(
'id_job' => 'bigint(21) NOT NULL',
'descriptif' => "text DEFAULT '' NOT NULL",
'fonction' => 'varchar(255) NOT NULL', //nom de la fonction
'args' => "longblob DEFAULT '' NOT NULL", // arguments
'md5args' => "char(32) NOT NULL default ''", // signature des arguments
'inclure' => 'varchar(255) NOT NULL', // fichier a inclure ou path/ pour charger_fonction
'priorite' => 'smallint(6) NOT NULL default 0',
'date' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL", // date au plus tot
'status' => 'tinyint NOT NULL default 1',
);
$spip_jobs_key = array(
'PRIMARY KEY' => 'id_job',
'KEY date' => 'date',
'KEY status' => 'status',
);
/// Attention: mes_fonctions peut avoir deja defini cette variable
/// il faut donc rajouter, mais pas reinitialiser
$tables_principales['spip_jobs'] = array('field' => &$spip_jobs, 'key' => &$spip_jobs_key);
}
/**
* Déclare les tables auxiliaires du Core
*
* @param array $tables_auxiliaires
* Description des tables auxiliaires déjà déclarées
* @return void
**/
function base_auxiliaires(&$tables_auxiliaires) {
$spip_resultats = array(
'recherche' => "char(16) DEFAULT '' NOT NULL",
'id' => 'INT UNSIGNED NOT NULL',
'points' => "INT UNSIGNED DEFAULT '0' NOT NULL",
'table_objet' => "varchar(30) DEFAULT '' NOT NULL",
'serveur' => "char(16) DEFAULT '' NOT NULL", // hash md5 partiel du serveur de base ('' pour le serveur principal)
'maj' => 'TIMESTAMP'
);
$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 ...
);
$spip_auteurs_liens = array(
'id_auteur' => "bigint(21) DEFAULT '0' NOT NULL",
'id_objet' => "bigint(21) DEFAULT '0' NOT NULL",
'objet' => "VARCHAR (25) DEFAULT '' NOT NULL",
'vu' => "VARCHAR(6) DEFAULT 'non' NOT NULL"
);
$spip_auteurs_liens_key = array(
'PRIMARY KEY' => 'id_auteur,id_objet,objet',
'KEY id_auteur' => 'id_auteur',
'KEY id_objet' => 'id_objet',
'KEY objet' => 'objet',
);
$spip_meta = array(
'nom' => 'VARCHAR (255) NOT NULL',
'valeur' => "text DEFAULT ''",
'impt' => "ENUM('non', 'oui') DEFAULT 'oui' NOT NULL",
'maj' => 'TIMESTAMP'
);
$spip_meta_key = array(
'PRIMARY KEY' => 'nom'
);
$spip_jobs_liens = array(
'id_job' => "bigint(21) DEFAULT '0' NOT NULL",
'id_objet' => "bigint(21) DEFAULT '0' NOT NULL",
'objet' => "VARCHAR (25) DEFAULT '' NOT NULL",
);
$spip_jobs_liens_key = array(
'PRIMARY KEY' => 'id_job,id_objet,objet',
'KEY id_job' => 'id_job'
);
$tables_auxiliaires['spip_auteurs_liens'] = array(
'field' => &$spip_auteurs_liens,
'key' => &$spip_auteurs_liens_key
);
$tables_auxiliaires['spip_meta'] = array(
'field' => &$spip_meta,
'key' => &$spip_meta_key
);
$tables_auxiliaires['spip_resultats'] = array(
'field' => &$spip_resultats,
'key' => &$spip_resultats_key
);
$tables_auxiliaires['spip_jobs_liens'] = array(
'field' => &$spip_jobs_liens,
'key' => &$spip_jobs_liens_key
);
}
/**
* Auto remplissage des informations non explicites
* sur un objet d'une table sql
*
* - table_objet
* - table_objet_surnoms
* - type
* - type_surnoms
* - url_voir
* - url_edit
* - icone_objet
*
* - texte_retour
* - texte_modifier
* - texte_creer
* - texte_creer_associer
* - texte_ajouter
* - texte_objets
* - texte_objet
*
* - info_aucun_objet
* - info_1_objet
* - info_nb_objets
*
* - texte_logo_objet
* - texte_langue_objet
* - texte_definir_comme_traduction_objet
*
* - principale
* - champs_contenu : utlisé pour générer l'affichage par défaut du contenu
* - editable
* - champs_editables : utilisé pour prendre en compte le post lors de l'édition
*
* - champs_versionnes
*
* L'objet doit définir de lui même ces champs pour gérer des statuts :
* - statut
* - statut_images
* - statut_titres
* - statut_textes_instituer
* - texte_changer_statut
* - aide_changer_statut
*
* - modeles : permet de declarer les modeles associes a cet objet
*
* Les infos non renseignées sont auto-déduites par conventions
* ou laissées vides
*
* @param string $table_sql
* @param array $infos
* @return array
*/
function renseigner_table_objet_sql($table_sql, &$infos) {
if (!isset($infos['type'])) {
// si on arrive de base/trouver_table, on a la cle primaire :
// s'en servir pour extrapoler le type
if (isset($infos['key']['PRIMARY KEY'])) {
$primary = $infos['key']['PRIMARY KEY'];
$primary = explode(',', $primary);
$primary = reset($primary);
$infos['type'] = preg_replace(',^spip_|^id_|s$,', '', $primary);
} else {
$infos['type'] = preg_replace(',^spip_|s$,', '', $table_sql);
}
}
if (!isset($infos['type_surnoms'])) {
$infos['type_surnoms'] = array();
}
if (!isset($infos['table_objet'])) {
$infos['table_objet'] = preg_replace(',^spip_,', '', $table_sql);
}
if (!isset($infos['table_objet_surnoms'])) {
$infos['table_objet_surnoms'] = array();
}
if (!isset($infos['principale'])) {
$infos['principale'] = (isset($GLOBALS['tables_principales'][$table_sql]) ? 'oui' : false);
}
// normaliser pour pouvoir tester en php $infos['principale']?
// et dans une boucle {principale=oui}
$infos['principale'] = (($infos['principale'] and $infos['principale'] != 'non') ? 'oui' : false);
// declarer et normaliser pour pouvoir tester en php $infos['editable']?
// et dans une boucle {editable=oui}
if (!isset($infos['editable'])) {
$infos['editable'] = 'oui';
}
$infos['editable'] = (($infos['editable'] and $infos['editable'] != 'non') ? 'oui' : false);
// les urls publiques sont par defaut page=type pour les tables principales, et rien pour les autres
// seules les exceptions sont donc a declarer
if (!isset($infos['page'])) {
$infos['page'] = ($infos['principale'] ? $infos['type'] : '');
}
if (!isset($infos['url_voir'])) {
$infos['url_voir'] = $infos['type'];
}
if (!isset($infos['url_edit'])) {
$infos['url_edit'] = $infos['url_voir'] . ($infos['editable'] ? '_edit' : '');
}
if (!isset($infos['icone_objet'])) {
$infos['icone_objet'] = $infos['type'];
}
// chaines de langue
// par defaut : objet:icone_xxx_objet
if (!isset($infos['texte_retour'])) {
$infos['texte_retour'] = 'icone_retour';
}
if (!isset($infos['texte_modifier'])) {
$infos['texte_modifier'] = $infos['type'] . ':' . 'icone_modifier_' . $infos['type'];
}
if (!isset($infos['texte_creer'])) {
$infos['texte_creer'] = $infos['type'] . ':' . 'icone_creer_' . $infos['type'];
}
if (!isset($infos['texte_creer_associer'])) {
$infos['texte_creer_associer'] = $infos['type'] . ':' . 'texte_creer_associer_' . $infos['type'];
}
if (!isset($infos['texte_ajouter'])) {
// Ajouter un X
$infos['texte_ajouter'] = $infos['type'] . ':' . 'texte_ajouter_' . $infos['type'];
}
if (!isset($infos['texte_objets'])) {
$infos['texte_objets'] = $infos['type'] . ':' . 'titre_' . $infos['table_objet'];
}
if (!isset($infos['texte_objet'])) {
$infos['texte_objet'] = $infos['type'] . ':' . 'titre_' . $infos['type'];
}
if (!isset($infos['texte_logo_objet'])) {
// objet:titre_logo_objet "Logo de ce X"
$infos['texte_logo_objet'] = $infos['type'] . ':' . 'titre_logo_' . $infos['type'];
}
if (!isset($infos['texte_langue_objet'])) {
// objet:texte_langue_objet "Langue de ce X"
$infos['texte_langue_objet'] = $infos['type'] . ':' . 'titre_langue_' . $infos['type'];
}
if (!isset($infos['texte_definir_comme_traduction_objet'])) {
// "Ce X est une traduction du X numéro :"
$infos['texte_definir_comme_traduction_objet'] = $infos['type'] . ':' . 'texte_definir_comme_traduction_' . $infos['type'];
}
// objet:info_aucun_objet
if (!isset($infos['info_aucun_objet'])) {
$infos['info_aucun_objet'] = $infos['type'] . ':' . 'info_aucun_' . $infos['type'];
}
// objet:info_1_objet
if (!isset($infos['info_1_objet'])) {
$infos['info_1_objet'] = $infos['type'] . ':' . 'info_1_' . $infos['type'];
}
// objet:info_nb_objets
if (!isset($infos['info_nb_objets'])) {
$infos['info_nb_objets'] = $infos['type'] . ':' . 'info_nb_' . $infos['table_objet'];
}
if (!isset($infos['champs_editables'])) {
$infos['champs_editables'] = array();
}
if (!isset($infos['champs_versionnes'])) {
$infos['champs_versionnes'] = array();
}
if (!isset($infos['rechercher_champs'])) {
$infos['rechercher_champs'] = array();
}
if (!isset($infos['rechercher_jointures'])) {
$infos['rechercher_jointures'] = array();
}
if (!isset($infos['modeles'])) {
$infos['modeles'] = array($infos['type']);
}
return $infos;
}
/**
* Renseigner les infos d'interface compilateur pour les tables objets
* complete la declaration precedente
*
* titre
* date
* statut
* tables_jointures
*
* @param $table_sql
* @param $infos
* @return array
*/
function renseigner_table_objet_interfaces($table_sql, &$infos) {
if (!isset($infos['titre'])) {
if (isset($infos['table_objet']) and isset($GLOBALS['table_titre'][$infos['table_objet']])) {
$infos['titre'] = $GLOBALS['table_titre'][$infos['table_objet']];
} else {
$infos['titre'] = ((isset($infos['field']['titre'])) ? 'titre,' : "'' as titre,");
$infos['titre'] .= ((isset($infos['field']['lang'])) ? 'lang' : "'' as lang");
}
}
if (!isset($infos['date'])) {
if (isset($infos['table_objet']) and isset($GLOBALS['table_date'][$infos['table_objet']])) {
$infos['date'] = $GLOBALS['table_date'][$infos['table_objet']];
} else {
$infos['date'] = ((isset($infos['field']['date'])) ? 'date' : '');
}
}
if (!isset($infos['statut'])) {
$infos['statut'] = isset($GLOBALS['table_statut'][$table_sql]) ? $GLOBALS['table_statut'][$table_sql] : '';
}
if (!isset($infos['tables_jointures'])) {
$infos['tables_jointures'] = array();
}
if (isset($GLOBALS['tables_jointures'][$table_sql])) {
$infos['tables_jointures'] = array_merge($infos['tables_jointures'], $GLOBALS['tables_jointures'][$table_sql]);
}
return $infos;
}
/**
* Retourne la liste des tables principales et leurs descriptions
*
* @api
* @return array
* Liste et descriptions des tables principales
**/
function lister_tables_principales() {
static $done = false;
if (!$done or !count($GLOBALS['tables_principales'])) {
lister_tables_objets_sql();
$done = true;
}
return $GLOBALS['tables_principales'];
}
/**
* Retourne la liste des tables auxiliaires et leurs descriptions
*
* @api
* @return array
* Liste et descriptions des tables auxiliaires
**/
function lister_tables_auxiliaires() {
static $done = false;
if (!$done or !count($GLOBALS['tables_auxiliaires'])) {
lister_tables_objets_sql();
$done = true;
}
return $GLOBALS['tables_auxiliaires'];
}
/**
* Recenser les surnoms de table_objet
*
* @return array
*/
function lister_tables_objets_surnoms() {
static $surnoms = null;
static $md5 = null;
if (!$surnoms
or $md5 != lister_tables_objets_sql('::md5')
) {
// passer dans un pipeline qui permet aux plugins de declarer leurs exceptions
// pour compatibilite, car il faut dorenavent utiliser
// declarer_table_objets_sql
$surnoms = pipeline(
'declarer_tables_objets_surnoms',
array(
# pour les modeles
# a enlever ?
'doc' => 'documents',
'img' => 'documents',
'emb' => 'documents',
)
);
$infos_tables = lister_tables_objets_sql();
foreach ($infos_tables as $t => $infos) {
// cas de base type=>table
// et preg_replace(',^spip_|^id_|s$,',table)=>table
if ($infos['table_objet']) { // securite, si la fonction est appelee trop tot, c'est vide
// optimisations pour table_objet
//$surnoms[$infos['type']] = $infos['table_objet'];
$surnoms[preg_replace(',^spip_|^id_|s$,', '', $infos['table_objet'])] = $infos['table_objet'];
$surnoms[preg_replace(',^spip_|^id_|s$,', '', $infos['type'])] = $infos['table_objet'];
if (is_array($infos['table_objet_surnoms']) and count($infos['table_objet_surnoms'])) {
foreach ($infos['table_objet_surnoms'] as $surnom) {
$surnoms[$surnom] = $infos['table_objet'];
}
}
}
}
$md5 = lister_tables_objets_sql('::md5');
}
return $surnoms;
}
/**
* Recenser les surnoms de table_objet
*
* @return array
*/
function lister_types_surnoms() {
static $surnoms = null;
static $md5 = null;
if (!$surnoms
or $md5 != lister_tables_objets_sql('::md5')
) {
// passer dans un pipeline qui permet aux plugins de declarer leurs exceptions
// pour compatibilite, car il faut dorenavent utiliser
// declarer_table_objets_sql
$surnoms = pipeline('declarer_type_surnoms', array('racine-site' => 'site'));
$infos_tables = lister_tables_objets_sql();
foreach ($infos_tables as $t => $infos) {
if ($infos['type']) { // securite, si la fonction est appelee trop tot, c'est vide
// optimisations pour objet_type
//$surnoms[$infos['type']] = $infos['type'];
$surnoms[preg_replace(',^spip_|^id_|s$,', '', $infos['table_objet'])] = $infos['type'];
$surnoms[preg_replace(',^spip_|^id_|s$,', '', $infos['type'])] = $infos['type'];
// surnoms declares
if (is_array($infos['type_surnoms']) and count($infos['type_surnoms'])) {
foreach ($infos['type_surnoms'] as $surnom) {
$surnoms[$surnom] = $infos['type'];
}
}
}
}
$md5 = lister_tables_objets_sql('::md5');
}
return $surnoms;
}
/**
* Retourne la liste des tables SQL qui concernent SPIP
*
* Cette liste n'est calculée qu'une fois par serveur pour l'ensemble du hit
*
* @param string $serveur
* Nom du fichier de connexion à la base de données
* @return array
* Couples (nom de la table SQL => même nom, sans 'spip_' devant)
**/
function lister_tables_spip($serveur = '') {
static $tables = array();
if (!isset($tables[$serveur])) {
$tables[$serveur] = array();
if (!function_exists('sql_alltable')) {
include_spip('base/abstract_sql');
}
$ts = sql_alltable(null, $serveur); // toutes les tables "spip_" (ou prefixe perso)
$connexion = $GLOBALS['connexions'][$serveur ? $serveur : 0];
$spip = $connexion['prefixe'] . '_';
foreach ($ts as $t) {
$t = substr($t, strlen($spip));
$tables[$serveur]["spip_$t"] = $t;
}
}
return $tables[$serveur];
}
/**
* Retourne la liste des tables SQL, Spip ou autres
*
* Cette liste n'est calculée qu'une fois par serveur pour l'ensemble du hit
*
* @param string $serveur
* Nom du fichier de connexion à la base de données
* @return array
* Couples (nom de la table SQL => même nom)
**/
function lister_toutes_tables($serveur){
static $tables = array();
if (!isset($tables[$serveur])){
$tables[$serveur] = array();
if (!function_exists("sql_alltable"))
include_spip("base/abstract_sql");
$ts = sql_alltable('%',$serveur); // toutes les tables
foreach ($ts as $t){
$tables[$serveur][$t] = $t;
}
}
return $tables[$serveur];
}
/**
* Retrouve le nom d'objet à partir de la table
*
* - spip_articles -> articles
* - id_article -> articles
* - article -> articles
*
* @api
* @param string $type
* Nom de la table SQL (le plus souvent)
* Tolère un nom de clé primaire.
* @param string $serveur
* Nom du connecteur
* @return string
* Nom de l'objet
**/
function table_objet($type, $serveur = '') {
$surnoms = lister_tables_objets_surnoms();
$type = preg_replace(',^spip_|^id_|s$,', '', $type);
if (!$type) {
return;
}
if (isset($surnoms[$type])) {
return $surnoms[$type];
}
if ($serveur !== false) {
$t = lister_tables_spip($serveur);
$trouver_table = charger_fonction('trouver_table', 'base');
$typetrim = rtrim($type, 's') . 's';
if ((isset($t[$typetrim]) or in_array($typetrim, $t))
and ($desc = $trouver_table(rtrim($type, 's') . 's', $serveur))
) {
return $desc['id_table'];
} elseif ((isset($t[$type]) or in_array($type, $t))
and ($desc = $trouver_table($type, $serveur))
) {
return $desc['id_table'];
}
spip_log('table_objet(' . $type . ') calculee sans verification');
#spip_log(debug_backtrace(),'db');
}
return rtrim($type, 's') . 's'; # cas historique ne devant plus servir, sauf si $serveur=false
}
/**
* Retrouve la table sql à partir de l'objet ou du type
*
* - articles -> spip_articles
* - article -> spip_articles
* - id_article -> spip_articles
*
* @api
* @param string $type
* Nom ou type de l'objet
* Tolère un nom de clé primaire.
* @param string $serveur
* Nom du connecteur
* @return string
* Nom de la table SQL
**/
function table_objet_sql($type, $serveur = '') {
$nom = table_objet($type, $serveur);
if (!isset($GLOBALS['table_des_tables']['articles'])) {
// eviter de multiples inclusions
include_spip('public/interfaces');
}
if (isset($GLOBALS['table_des_tables'][$nom])) {
$nom = $GLOBALS['table_des_tables'][$nom];
$nom = "spip_$nom";
} else {
$infos_tables = lister_tables_objets_sql();
if (isset($infos_tables["spip_$nom"])) {
$nom = "spip_$nom";
} elseif ($serveur !== false) {
$t = lister_tables_spip($serveur);
if (isset($t[$nom]) or in_array($nom, $t)) {
$trouver_table = charger_fonction('trouver_table', 'base');
if ($desc = $trouver_table($nom, $serveur)) {
return $desc['table_sql'];
}
}
}
}
return $nom;
}
/**
* Retrouve la clé primaire à partir du nom d'objet ou de table
*
* - articles -> id_article
* - article -> id_article
* - spip_articles -> id_article
*
* @api
* @param string $type
* Nom de la table SQL ou de l'objet
* @param string $serveur
* Nom du connecteur
* @return string
* Nom de la clé primaire
**/
function id_table_objet($type, $serveur = '') {
static $trouver_table = null;
$type = objet_type($type, $serveur);
if (!$type) {
return;
}
$t = table_objet($type);
if (!$trouver_table) {
$trouver_table = charger_fonction('trouver_table', 'base');
}
$ts = lister_tables_spip($serveur);
if (
in_array($t, $ts)
or in_array($t, lister_toutes_tables($serveur))
){
$desc = $trouver_table($t, $serveur);
if (isset($desc['key']['PRIMARY KEY'])) {
return $desc['key']['PRIMARY KEY'];
}
if (!$desc or isset($desc['field']["id_$type"])) {
return "id_$type";
}
// sinon renvoyer le premier champ de la table...
$keys = array_keys($desc['field']);
return array_shift($keys);
}
return "id_$type";
}
/**
* Retrouve le type d'objet à partir du nom d'objet ou de table
*
* - articles -> article
* - spip_articles -> article
* - id_article -> article
*
* @api
* @param string $table_objet
* Nom de l'objet ou de la table SQL
* @param string $serveur
* Nom du connecteur
* @return string
* Type de l'objet
**/
function objet_type($table_objet, $serveur = '') {
if (!$table_objet) {
return;
}
$surnoms = lister_types_surnoms();
// scenario de base
// le type est decline a partir du nom de la table en enlevant le prefixe eventuel
// et la marque du pluriel
// on accepte id_xx en entree aussi
$type = preg_replace(',^spip_|^id_|s$,', '', $table_objet);
if (isset($surnoms[$type])) {
return $surnoms[$type];
}
// securite : eliminer les caracteres non \w
$type = preg_replace(',[^\w-],', '', $type);
// si le type redonne bien la table c'est bon
// oui si table_objet ressemblait deja a un type
if ($type == $table_objet
or (table_objet($type, $serveur) == $table_objet)
or (table_objet_sql($type, $serveur) == $table_objet)
) {
return $type;
}
// si on ne veut pas chercher en base
if ($serveur === false) {
return $type;
}
// sinon on passe par la cle primaire id_xx pour trouver le type
// car le s a la fin est incertain
// notamment en cas de pluriel derogatoire
// id_jeu/spip_jeux id_journal/spip_journaux qui necessitent tout deux
// une declaration jeu => jeux, journal => journaux
// dans le pipeline declarer_tables_objets_surnoms
$trouver_table = charger_fonction('trouver_table', 'base');
$ts = lister_tables_spip($serveur);
$desc = false;
if (in_array($table_objet, $ts)) {
$desc = $trouver_table($table_objet);
}
if (!$desc and in_array($table_objet = table_objet($type, $serveur), $ts)) {
$desc = $trouver_table($table_objet, $serveur);
}
// si le type est declare : bingo !
if ($desc and isset($desc['type'])) {
return $desc['type'];
}
// on a fait ce qu'on a pu
return $type;
}
/**
* Determininer si un objet est publie ou non
*
* On se base pour cela sur sa declaration de statut
* pour des cas particuliers non declarables, on permet de fournir une fonction
* base_xxxx_test_si_publie qui sera appele par la fonction
*
* @param string $objet
* @param int $id_objet
* @param string $serveur
* @return bool
*/
function objet_test_si_publie($objet, $id_objet, $serveur = '') {
// voir si une fonction est definie pour faire le boulot
// elle a la priorite dans ce cas
if ($f = charger_fonction($objet . '_test_si_publie', 'base', true)) {
return $f($objet, $id_objet, $serveur);
}
// sinon on se fie a la declaration de l'objet si presente
$id_table = $table_objet = table_objet($objet);
$id_table_objet = id_table_objet($objet, $serveur);
$trouver_table = charger_fonction('trouver_table', 'base');
if ($desc = $trouver_table($table_objet, $serveur)
and isset($desc['statut'])
and $desc['statut']
) {
$boucle = new Boucle();
$boucle->show = $desc;
$boucle->nom = 'objet_test_si_publie';
$boucle->id_boucle = $id_table;
$boucle->id_table = $id_table;
$boucle->sql_serveur = $serveur;
$boucle->select[] = $id_table_objet;
$boucle->from[$table_objet] = table_objet_sql($objet, $serveur);
$boucle->where[] = $id_table . '.' . $id_table_objet . '=' . intval($id_objet);
include_spip('public/compiler');
include_spip('public/composer');
instituer_boucle($boucle, false, true);
$res = calculer_select(
$boucle->select,
$boucle->from,
$boucle->from_type,
$boucle->where,
$boucle->join,
$boucle->group,
$boucle->order,
$boucle->limit,
$boucle->having,
$table_objet,
$id_table,
$serveur
);
if (sql_fetch($res)) {
return true;
}
return false;
}
// si pas d'info statut ni de fonction : l'objet est publie
return true;
}
|