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