Ajustement de la config de sphinx à un nouveau modèle de déploiement dans lequel
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / scripts / sphinx.conf.py.in
1 #!/usr/bin/python
2 # coding: utf-8
3 #
4 # Ce script compose dynamiquement une configuration pour Sphinx. Il suffit
5 # que le fichier soit exécutable pour que Sphinx exécute le script plutôt
6 # que de le lire comme un fichier statique.
7 #
8 # Le manuel de sphinx se trouve au http://sphinxsearch.com/docs/manual-0.9.9.html
9
10 import glob
11 import os
12 import sys
13
14 sys.path[0:0] = [
15 "%(directory)s",
16 "%(directory)s/auf_savoirs_en_partage",
17 ]
18 os.environ['DJANGO_SETTINGS_MODULE'] = 'production'
19 for d in glob.glob("%(directory)s/eggs/*"):
20 sys.path[0:0] = [d,]
21 for d in glob.glob("%(directory)s/parts/*"):
22 sys.path[0:0] = [d,]
23
24 from django.conf import settings
25
26 SQL_HOST = settings.DATABASE_HOST
27 SQL_USER = settings.DATABASE_USER
28 SQL_PASS = settings.DATABASE_PASSWORD
29 SQL_DB = settings.DATABASE_NAME
30 DATA_DIR = '/var/lib/sphinxsearch/data'
31
32 def multiline(s):
33 """Place un marqueur de continuation avant chaque saut de ligne."""
34 return s.replace("\n", "\\\n")
35
36 def emit_source(name, sql_query, sql_query_info=None, sql_attr_multi=None, sql_attr_uint=None):
37 print '''
38 source %%(name)s
39 {
40 type = mysql
41 sql_host = %%(sql_host)s
42 sql_user = %%(sql_user)s
43 sql_pass = %%(sql_pass)s
44 sql_db = %%(sql_db)s
45 sql_query_pre = SET NAMES utf8
46 sql_query_pre = SET SESSION query_cache_type=OFF
47 sql_query = %%(sql_query)s
48 ''' %% dict(name=name, sql_host=SQL_HOST, sql_user=SQL_USER,
49 sql_pass=SQL_PASS, sql_db=SQL_DB, sql_query=multiline(sql_query))
50
51 if sql_query_info:
52 print ' sql_query_info = ' + sql_query_info
53
54 if sql_attr_multi:
55 for attr in sql_attr_multi:
56 print ' sql_attr_multi = uint %%s from field' %% attr
57
58 if sql_attr_uint:
59 for attr in sql_attr_uint:
60 print ' sql_attr_uint = ' + attr
61
62 print '}'
63
64 def emit_index(name):
65 print '''
66 index %%(name)s
67 {
68 morphology = libstemmer_fr
69 charset_type = utf-8
70 charset_table = 0..9, A..Z->a..z, _, a..z, \\
71 U+C0->a, U+C2->a, U+E0->a, U+E2->a, \\
72 U+C7->c, U+E7->c, \\
73 U+C8->e, U+C9->e, U+CA->e, U+E8->e, U+E9->e, U+EA->e, U+EB->e, \\
74 U+CE->i, U+EE->i, U+EF->i, \\
75 U+D4->o, U+F4->o, \\
76 U+F9->u, U+FB->u, U+FC->u
77 source = %%(name)s
78 path = %%(path)s
79 }''' %% dict(name=name, path=os.path.join(DATA_DIR, name))
80
81 emit_source('savoirsenpartage_ressources',
82 '''SELECT r.id AS id,
83 r.title AS title,
84 r.description AS description,
85 r.creator AS creator,
86 r.contributor AS contributor,
87 r.subject AS subject,
88 r.publisher AS publisher,
89 GROUP_CONCAT(DISTINCT d.nom_discipline) AS disciplines,
90 GROUP_CONCAT(DISTINCT d.id_discipline) AS discipline_ids,
91 GROUP_CONCAT(DISTINCT p.nom) AS pays,
92 GROUP_CONCAT(DISTINCT reg.nom) AS regions,
93 GROUP_CONCAT(DISTINCT reg.id) AS region_ids
94 FROM savoirs_record r
95 LEFT JOIN savoirs_record_disciplines rd ON rd.record_id = r.id
96 LEFT JOIN discipline d ON d.id_discipline = rd.discipline_id
97 LEFT JOIN savoirs_record_pays rp ON rp.record_id = r.id
98 LEFT JOIN ref_pays p ON p.id = rp.pays_id
99 LEFT JOIN savoirs_record_regions rr ON rr.record_id = r.id
100 LEFT JOIN ref_region reg ON reg.id = rr.region_id OR reg.id = p.region
101 LEFT JOIN savoirs_record_listsets rl ON rl.record_id = r.id
102 LEFT JOIN savoirs_listset l ON l.spec = rl.listset_id
103 WHERE r.validated AND (l.spec IS NULL OR l.validated)
104 GROUP BY r.id''',
105 sql_query_info='SELECT * from savoirs_record WHERE id=$id',
106 sql_attr_multi=['discipline_ids', 'region_ids']
107 )
108
109 emit_source('savoirsenpartage_actualites',
110 '''SELECT a.id_actualite AS id,
111 a.titre_actualite AS titre,
112 a.texte_actualite AS texte,
113 TO_DAYS(a.date_actualite) AS date,
114 GROUP_CONCAT(DISTINCT r.nom) AS regions,
115 GROUP_CONCAT(DISTINCT d.nom_discipline) AS disciplines
116 FROM actualite a
117 LEFT JOIN actualite_regions ar ON ar.actualite_id = a.id_actualite
118 LEFT JOIN ref_region r ON r.id = ar.region_id
119 LEFT JOIN actualite_disciplines ad ON ad.actualite_id = a.id_actualite
120 LEFT JOIN discipline d ON d.id_discipline = ad.discipline_id
121 WHERE a.visible_actualite
122 GROUP BY a.id_actualite''',
123 sql_query_info='SELECT * from actualite WHERE id_actualite=$id',
124 sql_attr_uint=['date']
125 )
126
127 emit_source('savoirsenpartage_evenements',
128 '''SELECT e.id AS id,
129 e.titre AS titre,
130 e.mots_cles AS mots_cles,
131 e.type AS type,
132 e.lieu AS lieu,
133 e.description AS description,
134 e.contact AS contact,
135 CONCAT_WS(',', d.nom_discipline, d2.nom_discipline) AS disciplines,
136 GROUP_CONCAT(DISTINCT r.nom) AS regions,
137 TO_DAYS(DATE(e.debut)) AS debut
138 FROM savoirs_evenement e
139 LEFT JOIN discipline d ON d.id_discipline = e.discipline_id
140 LEFT JOIN discipline d2 ON d2.id_discipline = e.discipline_secondaire_id
141 LEFT JOIN savoirs_evenement_regions er ON er.evenement_id = e.id
142 LEFT JOIN ref_region r ON r.id = er.region_id
143 WHERE e.approuve
144 GROUP BY e.id''',
145 sql_query_info='SELECT * from savoirs_evenement WHERE id=$id',
146 sql_attr_uint=['debut'])
147
148 emit_source('savoirsenpartage_chercheurs',
149 '''SELECT c.id AS id,
150 p.nom AS nom,
151 p.prenom AS prenom,
152 c.theme_recherche AS theme_recherche,
153 c.groupe_recherche AS groupe_recherche,
154 c.mots_cles AS mots_cles,
155 c.membre_association_francophone_details AS membre_association_francophone_details,
156 c.membre_reseau_institutionnel_details AS membre_reseau_institutionnel_details,
157 c.expert_oif_details AS expert_oif_details,
158 c.membre_instance_auf_details AS membre_instance_auf_details,
159 IFNULL(et.nom, etablissement_autre_nom) AS etablissement,
160 pays.nom AS pays,
161 pays.id AS pays_id,
162 r.nom AS region,
163 r.id AS region_id,
164 GROUP_CONCAT(DISTINCT d.nom_discipline) AS disciplines,
165 CONCAT_WS(pub1.titre, pub2.titre, pub3.titre, pub4.titre) AS publications,
166 t.titre AS these,
167 GROUP_CONCAT(DISTINCT g.nom) AS groupes,
168 GROUP_CONCAT(DISTINCT ex.nom) AS expertises,
169 GROUP_CONCAT(DISTINCT g.id) AS groupe_ids,
170 TO_DAYS(c.date_modification) AS date_modification,
171 CASE pays WHEN 'Nord' THEN 1
172 WHEN 'Sud' THEN 2
173 END AS nord_sud,
174 CASE statut WHEN 'enseignant' THEN 1
175 WHEN 'etudiant' THEN 2
176 WHEN 'independant' THEN 3
177 END AS statut,
178 (ex.id IS NULL) AS expert
179 FROM chercheurs_chercheur c
180 INNER JOIN chercheurs_personne p ON c.personne = p.id
181 LEFT JOIN ref_etablissement et ON et.id = c.etablissement
182 LEFT JOIN ref_pays pays ON pays.id = IFNULL(et.pays, c.etablissement_autre_pays)
183 LEFT JOIN ref_region r ON pays.region = r.id
184 LEFT JOIN discipline d ON d.id_discipline = c.discipline
185 LEFT JOIN chercheurs_publication pub1 ON pub1.id = c.publication1
186 LEFT JOIN chercheurs_publication pub2 ON pub2.id = c.publication2
187 LEFT JOIN chercheurs_publication pub3 ON pub3.id = c.publication3
188 LEFT JOIN chercheurs_publication pub4 ON pub4.id = c.publication4
189 LEFT JOIN chercheurs_publication t ON t.id = c.these
190 LEFT JOIN chercheurs_chercheurgroupe cg ON cg.chercheur = c.id
191 LEFT JOIN chercheurs_groupe g ON g.id = cg.groupe
192 LEFT JOIN chercheurs_expertise ex ON ex.chercheur_id = c.id
193 GROUP BY c.id''',
194 sql_query_info='SELECT * from chercheurs_chercheur WHERE id=$id',
195 sql_attr_multi=['groupe_ids'],
196 sql_attr_uint=['pays_id', 'region_id', 'nord_sud', 'date_modification', 'statut', 'expert'])
197
198 emit_source('savoirsenpartage_sites',
199 '''SELECT s.id AS id,
200 s.titre AS titre,
201 s.description AS description,
202 s.editeur AS editeur,
203 s.auteur AS auteur,
204 s.mots_cles AS mots_cles,
205 GROUP_CONCAT(DISTINCT d.nom_discipline) AS disciplines,
206 GROUP_CONCAT(DISTINCT p.nom) AS pays,
207 GROUP_CONCAT(DISTINCT p.id) AS pays_ids,
208 GROUP_CONCAT(DISTINCT r.nom) AS regions,
209 TO_DAYS(s.date_maj) AS date_maj
210 FROM sitotheque_site s
211 LEFT JOIN sitotheque_site_discipline sd ON sd.site_id = s.id
212 LEFT JOIN discipline d ON d.id_discipline = sd.discipline_id
213 LEFT JOIN ref_pays p ON p.id = s.pays
214 LEFT JOIN ref_region r ON r.id = p.region
215 GROUP BY s.id''',
216 'SELECT * FROM sitotheque_site WHERE id=$id',
217 sql_attr_multi=['pays_ids'],
218 sql_attr_uint=['date_maj']
219 )
220
221 emit_index('savoirsenpartage_actualites')
222 emit_index('savoirsenpartage_ressources')
223 emit_index('savoirsenpartage_evenements')
224 emit_index('savoirsenpartage_chercheurs')
225 emit_index('savoirsenpartage_sites')