[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / base / medias.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
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 \***************************************************************************/
12
13 /**
14 * Déclarations relatives à la base de données
15 *
16 * @package SPIP\Medias\Pipelines
17 **/
18
19 if (!defined('_ECRIRE_INC_VERSION')) {
20 return;
21 }
22
23 /**
24 * Interfaces des tables documents pour le compilateur
25 *
26 * @param array $interfaces
27 * @return array
28 */
29 function medias_declarer_tables_interfaces($interfaces) {
30 $interfaces['table_des_tables']['documents'] = 'documents';
31 $interfaces['table_des_tables']['types_documents'] = 'types_documents';
32
33 $interfaces['exceptions_des_tables']['documents']['type_document'] = array('types_documents', 'titre');
34 $interfaces['exceptions_des_tables']['documents']['extension_document'] = array('types_documents', 'extension');
35 $interfaces['exceptions_des_tables']['documents']['mime_type'] = array('types_documents', 'mime_type');
36 $interfaces['exceptions_des_tables']['documents']['media_document'] = array('types_documents', 'media');
37
38 $interfaces['exceptions_des_jointures']['spip_documents']['id_forum'] = array('spip_documents_liens', 'id_forum');
39 $interfaces['exceptions_des_jointures']['spip_documents']['vu'] = array('spip_documents_liens', 'vu');
40 $interfaces['table_date']['types_documents'] = 'date';
41
42 $interfaces['table_des_traitements']['FICHIER'][] = 'get_spip_doc(%s)';
43
44 return $interfaces;
45 }
46
47
48 /**
49 * Table principale spip_documents et spip_types_documents
50 *
51 * @param array $tables_principales
52 * @return array
53 */
54 function medias_declarer_tables_principales($tables_principales) {
55
56 $spip_types_documents = array(
57 'extension' => "varchar(10) DEFAULT '' NOT NULL",
58 'titre' => "text DEFAULT '' NOT NULL",
59 'descriptif' => "text DEFAULT '' NOT NULL",
60 'mime_type' => "varchar(100) DEFAULT '' NOT NULL",
61 'inclus' => "ENUM('non', 'image', 'embed') DEFAULT 'non' NOT NULL",
62 'upload' => "ENUM('oui', 'non') DEFAULT 'oui' NOT NULL",
63 'media_defaut' => "varchar(10) DEFAULT 'file' NOT NULL",
64 'maj' => 'TIMESTAMP'
65 );
66
67 $spip_types_documents_key = array(
68 'PRIMARY KEY' => 'extension',
69 'KEY inclus' => 'inclus'
70 );
71
72 $tables_principales['spip_types_documents'] =
73 array('field' => &$spip_types_documents, 'key' => &$spip_types_documents_key);
74
75 return $tables_principales;
76 }
77
78 /**
79 * Table des liens documents-objets spip_documents_liens
80 *
81 * @param array $tables_auxiliaires
82 * @return array
83 */
84 function medias_declarer_tables_auxiliaires($tables_auxiliaires) {
85
86 $spip_documents_liens = array(
87 'id_document' => "bigint(21) DEFAULT '0' NOT NULL",
88 'id_objet' => "bigint(21) DEFAULT '0' NOT NULL",
89 'objet' => "VARCHAR (25) DEFAULT '' NOT NULL",
90 'vu' => "ENUM('non', 'oui') DEFAULT 'non' NOT NULL",
91 'rang_lien' => "int(4) DEFAULT '0' NOT NULL"
92 );
93
94 $spip_documents_liens_key = array(
95 'PRIMARY KEY' => 'id_document,id_objet,objet',
96 'KEY id_document' => 'id_document',
97 'KEY id_objet' => 'id_objet',
98 'KEY objet' => 'objet',
99 );
100
101 $tables_auxiliaires['spip_documents_liens'] = array(
102 'field' => &$spip_documents_liens,
103 'key' => &$spip_documents_liens_key
104 );
105
106 return $tables_auxiliaires;
107 }
108
109 /**
110 * Declarer le surnom des breves
111 *
112 * @param array $surnoms
113 * @return array
114 */
115 function medias_declarer_tables_objets_surnoms($surnoms) {
116 $surnoms['type_document'] = 'types_documents'; # hum
117 #$surnoms['extension'] = "types_documents"; # hum
118 #$surnoms['type'] = "types_documents"; # a ajouter pour id_table_objet('type')=='extension' ?
119 return $surnoms;
120 }
121
122 function medias_declarer_tables_objets_sql($tables) {
123 $tables['spip_articles']['champs_versionnes'][] = 'jointure_documents';
124 $tables['spip_documents'] = array(
125 'table_objet_surnoms' => array('doc', 'img', 'emb'),
126 'type_surnoms' => array(),
127 'url_voir' => 'document_edit',
128 'url_edit' => 'document_edit',
129 'page' => '',
130 'texte_retour' => 'icone_retour',
131 'texte_objets' => 'medias:objet_documents',
132 'texte_objet' => 'medias:objet_document',
133 'texte_modifier' => 'medias:info_modifier_document',
134 'info_aucun_objet' => 'medias:aucun_document',
135 'info_1_objet' => 'medias:un_document',
136 'info_nb_objets' => 'medias:des_documents',
137 'titre' => "CASE WHEN length(titre)>0 THEN titre ELSE fichier END as titre, '' AS lang",
138 'date' => 'date',
139 'principale' => 'oui',
140 'field' => array(
141 'id_document' => 'bigint(21) NOT NULL',
142 'id_vignette' => "bigint(21) DEFAULT '0' NOT NULL",
143 'extension' => "VARCHAR(10) DEFAULT '' NOT NULL",
144 'titre' => "text DEFAULT '' NOT NULL",
145 'date' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
146 'descriptif' => "text DEFAULT '' NOT NULL",
147 'fichier' => "text NOT NULL DEFAULT ''",
148 'taille' => 'bigint',
149 'largeur' => 'integer',
150 'hauteur' => 'integer',
151 'duree' => 'integer',
152 'media' => "varchar(10) DEFAULT 'file' NOT NULL",
153 'mode' => "varchar(10) DEFAULT 'document' NOT NULL",
154 'distant' => "VARCHAR(3) DEFAULT 'non'",
155 'statut' => "varchar(10) DEFAULT '0' NOT NULL",
156 'credits' => "text DEFAULT '' NOT NULL",
157 'date_publication' => "datetime DEFAULT '0000-00-00 00:00:00' NOT NULL",
158 'brise' => 'tinyint DEFAULT 0',
159 'maj' => 'TIMESTAMP'
160 ),
161 'key' => array(
162 'PRIMARY KEY' => 'id_document',
163 'KEY id_vignette' => 'id_vignette',
164 'KEY mode' => 'mode',
165 'KEY extension' => 'extension'
166 ),
167 'join' => array(
168 'id_document' => 'id_document',
169 'extension' => 'extension'
170 ),
171 'statut' => array(
172 array(
173 'champ' => 'statut',
174 'publie' => 'publie',
175 'previsu' => 'publie,prop,prepa',
176 'post_date' => 'date_publication',
177 'exception' => array('statut', 'tout')
178 )
179 ),
180 'tables_jointures' => array('types_documents'),
181 'rechercher_champs' => array(
182 'titre' => 3,
183 'descriptif' => 1,
184 'fichier' => 1,
185 'credits' => 1,
186 ),
187 'champs_editables' => array(
188 'titre',
189 'descriptif',
190 'date',
191 'taille',
192 'largeur',
193 'hauteur',
194 'duree',
195 'mode',
196 'credits',
197 'fichier',
198 'distant',
199 'extension',
200 'id_vignette',
201 'media'
202 ),
203 'champs_versionnes' => array(
204 'id_vignette',
205 'titre',
206 'descriptif',
207 'taille',
208 'largeur',
209 'hauteur',
210 'duree',
211 'mode',
212 'credits',
213 'fichier',
214 'distant'
215 ),
216 'modeles' => array('document', 'doc', 'img', 'emb', 'image', 'video', 'text', 'audio', 'application'),
217 );
218
219 // jointures sur les forum pour tous les objets
220 $tables[]['tables_jointures'][] = 'documents_liens';
221
222 // recherche jointe sur les documents pour les articles et rubriques
223 $tables['spip_articles']['rechercher_jointures']['document'] = array('titre' => 2, 'descriptif' => 1);
224 $tables['spip_rubriques']['rechercher_jointures']['document'] = array('titre' => 2, 'descriptif' => 1);
225
226 return $tables;
227 }
228
229 /**
230 * Creer la table des types de document
231 *
232 * @param string $serveur
233 * @param string $champ_media
234 * @return void
235 */
236 function creer_base_types_doc($serveur = '', $champ_media = 'media_defaut') {
237 global $tables_images, $tables_sequences, $tables_documents, $tables_mime;
238 include_spip('base/typedoc');
239 include_spip('base/abstract_sql');
240
241 // charger en memoire tous les types deja definis pour limiter les requettes
242 $rows = sql_allfetsel('mime_type,titre,inclus,extension,' . $champ_media . ',upload,descriptif', 'spip_types_documents', '', '', '', '', '', $serveur);
243 $deja = array();
244 foreach ($rows as $k => $row) {
245 $deja[$row['extension']] = &$rows[$k];
246 }
247
248 $insertions = array();
249 $updates = array();
250
251 foreach ($tables_mime as $extension => $type_mime) {
252 if (isset($tables_images[$extension])) {
253 $titre = $tables_images[$extension];
254 $inclus = 'image';
255 } else {
256 if (isset($tables_sequences[$extension])) {
257 $titre = $tables_sequences[$extension];
258 $inclus = 'embed';
259 } else {
260 $inclus = 'non';
261 if (isset($tables_documents[$extension])) {
262 $titre = $tables_documents[$extension];
263 } else {
264 $titre = '';
265 }
266 }
267 }
268
269 // type de media
270 $media = 'file';
271 if (preg_match(',^image/,', $type_mime) or in_array($type_mime, array('application/illustrator'))) {
272 $media = 'image';
273 } elseif (preg_match(',^audio/,', $type_mime)) {
274 $media = 'audio';
275 } elseif (preg_match(',^video/,', $type_mime) or in_array(
276 $type_mime,
277 array('application/ogg', 'application/x-shockwave-flash', 'application/mp4')
278 )) {
279 $media = 'video';
280 }
281
282 $set = array(
283 'mime_type' => $type_mime,
284 'titre' => $titre,
285 'inclus' => $inclus,
286 'extension' => $extension,
287 $champ_media => $media,
288 'upload' => 'oui',
289 'descriptif' => '',
290 );
291 if (!isset($deja[$extension])) {
292 $insertions[] = $set;
293 } elseif (array_diff($deja[$extension], $set)) {
294 $updates[$extension] = $set;
295 }
296 }
297
298 if (count($updates)) {
299 foreach ($updates as $extension => $set) {
300 sql_updateq('spip_types_documents', $set, 'extension=' . sql_quote($extension));
301 }
302 }
303
304 if ($insertions) {
305 sql_insertq_multi('spip_types_documents', $insertions, '', $serveur);
306 }
307 }
308
309
310 /**
311 * Optimiser la base de données en supprimant les liens orphelins
312 *
313 * @param array $flux
314 * @return array
315 */
316 function medias_optimiser_base_disparus($flux) {
317
318 include_spip('action/editer_liens');
319 // optimiser les liens morts :
320 // entre documents vers des objets effaces
321 // depuis des documents effaces
322 $flux['data'] += objet_optimiser_liens(array('document' => '*'), '*');
323
324 // on ne nettoie volontairement pas automatiquement les documents orphelins
325
326 return $flux;
327 }