Subversion Repositories Sites.tela-botanica.org

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 david 1
<?php
2
 
3
//
4
// Ce fichier ne sera execute qu'une fois
5
if (defined("_ECRIRE_OPTIMISER")) return;
6
define("_ECRIRE_OPTIMISER", "1");
7
 
8
 
9
function optimiser_base() {
10
	spip_log ("optimisation de la base");
11
 
12
	$mydate = date("YmdHis", time() - 24 * 3600);
13
 
14
	//
15
	// Rubriques
16
	//
17
 
18
	$query = "SELECT id_rubrique FROM spip_rubriques";
19
	$result = spip_query($query);
20
	$rubriques = '0';
21
	while ($row = spip_fetch_array($result)) $rubriques .= ','.$row['id_rubrique'];
22
 
23
	if ($rubriques) {
24
		$query = "DELETE FROM spip_articles WHERE id_rubrique NOT IN ($rubriques) AND maj < $mydate";
25
		spip_query($query);
26
		$query = "DELETE FROM spip_breves WHERE id_rubrique NOT IN ($rubriques) AND maj < $mydate";
27
		spip_query($query);
28
		$query = "DELETE FROM spip_forum WHERE id_rubrique NOT IN (0,$rubriques)";
29
		spip_query($query);
30
		$query = "DELETE FROM spip_auteurs_rubriques WHERE id_rubrique NOT IN ($rubriques)";
31
		spip_query($query);
32
		$query = "DELETE FROM spip_documents_rubriques WHERE id_rubrique NOT IN ($rubriques)";
33
		spip_query($query);
34
		$query = "DELETE FROM spip_mots_rubriques WHERE id_rubrique NOT IN ($rubriques)";
35
		spip_query($query);
36
	}
37
 
38
 
39
	//
40
	// Articles
41
	//
42
 
43
	$query = "DELETE FROM spip_articles WHERE statut='poubelle' AND maj < $mydate";
44
	spip_query($query);
45
	$query = "SELECT id_article FROM spip_articles";
46
	$result = spip_query($query);
47
	$articles = '0';
48
	while ($row = spip_fetch_array($result)) $articles .= ','.$row['id_article'];
49
 
50
	if ($articles) {
51
		$query = "DELETE FROM spip_auteurs_articles WHERE id_article NOT IN ($articles)";
52
		spip_query($query);
53
		$query = "DELETE FROM spip_documents_articles WHERE id_article NOT IN ($articles)";
54
		spip_query($query);
55
		$query = "DELETE FROM spip_mots_articles WHERE id_article NOT IN ($articles)";
56
		spip_query($query);
57
		$query = "DELETE FROM spip_forum WHERE id_article NOT IN (0,$articles)";
58
		spip_query($query);
59
	}
60
 
61
 
62
	//
63
	// Breves
64
	//
65
 
66
	$query = "DELETE FROM spip_breves WHERE statut='refuse' AND maj < $mydate";
67
	spip_query($query);
68
	$breves = '0';
69
	$query = "SELECT id_breve FROM spip_breves";
70
	$result = spip_query($query);
71
	while ($row = spip_fetch_array($result)) $breves .= ','.$row['id_breve'];
72
 
73
	if ($breves) {
74
		$query = "DELETE FROM spip_documents_breves WHERE id_breve NOT IN ($breves)";
75
		spip_query($query);
76
		$query = "DELETE FROM spip_mots_breves WHERE id_breve NOT IN ($breves)";
77
		spip_query($query);
78
		$query = "DELETE FROM spip_forum WHERE id_breve NOT IN (0,$breves)";
79
		spip_query($query);
80
	}
81
 
82
 
83
	//
84
	// Sites
85
	//
86
 
87
	$query = "DELETE FROM spip_syndic WHERE maj < $mydate AND statut = 'refuse'";
88
	spip_query($query);
89
 
90
	$syndic = '0';
91
	$query = "SELECT id_syndic FROM spip_syndic";
92
	$result = spip_query($query);
93
	while ($row = spip_fetch_array($result)) $syndic .= ','.$row['id_syndic'];
94
 
95
	if ($syndic) {
96
		$query = "DELETE FROM spip_syndic_articles WHERE id_syndic NOT IN (0,$syndic)";
97
		spip_query($query);
98
		$query = "DELETE FROM spip_mots_syndic WHERE id_syndic NOT IN ($syndic)";
99
		spip_query($query);
100
		$query = "DELETE FROM spip_forum WHERE id_syndic NOT IN (0,$syndic)";
101
		spip_query($query);
102
	}
103
 
104
 
105
	//
106
	// Auteurs
107
	//
108
 
109
	$auteurs = '0';
110
	$query = "SELECT id_auteur FROM spip_auteurs";
111
	$result = spip_query($query);
112
	while ($row = spip_fetch_array($result)) $auteurs .= ','.$row['id_auteur'];
113
 
114
	if ($auteurs) {
115
		$query = "DELETE FROM spip_auteurs_articles WHERE id_auteur NOT IN ($auteurs)";
116
		spip_query($query);
117
		$query = "DELETE FROM spip_auteurs_messages WHERE id_auteur NOT IN ($auteurs)";
118
		spip_query($query);
119
		$query = "DELETE FROM spip_auteurs_rubriques WHERE id_auteur NOT IN ($auteurs)";
120
		spip_query($query);
121
	}
122
 
123
	$query = "SELECT id_auteur,nom,email FROM spip_auteurs WHERE statut='5poubelle' AND maj < $mydate";
124
	$result = spip_query($query);
125
	while ($row = spip_fetch_array($result)) {
126
		$id_auteur = $row['id_auteur'];
127
		$nom = $row['nom'];
128
		$email = $row['email'];
129
 
130
		$query2 = "SELECT * FROM spip_auteurs_articles WHERE id_auteur=$id_auteur";
131
		$result2 = spip_query($query2);
132
		if (!spip_num_rows($result2)) {
133
			$query3 = "DELETE FROM spip_auteurs WHERE id_auteur=$id_auteur";
134
			$result3 = spip_query($query3);
135
			spip_log ("suppression auteur $id_auteur ($nom, $email)");
136
		}
137
	}
138
 
139
 
140
	//
141
	// Messages prives
142
	//
143
 
144
	$query = "SELECT m.id_message FROM spip_messages AS m, spip_auteurs_messages AS lien ".
145
		"WHERE m.id_message = lien.id_message GROUP BY m.id_message";
146
	$result = spip_query($query);
147
	while ($row = spip_fetch_array($result)) $messages[] = $row['id_message'];
148
 
149
	$query = "SELECT id_message FROM spip_messages ".
150
		"WHERE type ='affich'";
151
	$result = spip_query($query);
152
	while ($row = spip_fetch_array($result)) $messages[] = $row['id_message'];
153
 
154
	if ($messages) {
155
		$messages = join(",", $messages);
156
 
157
		$query = "DELETE FROM spip_messages WHERE id_message NOT IN ($messages)";
158
		spip_query($query);
159
		$query = "DELETE FROM spip_forum WHERE id_message NOT IN (0,$messages)";
160
		spip_query($query);
161
	}
162
 
163
 
164
	//
165
	// Mots-cles
166
	//
167
 
168
	$query = "DELETE FROM spip_mots WHERE titre='' AND maj < $mydate";
169
	$result = spip_query($query);
170
 
171
	$mots = '0';
172
	$query = "SELECT id_mot FROM spip_mots";
173
	$result = spip_query($query);
174
	while ($row = spip_fetch_array($result)) $mots .= ','.$row['id_mot'];
175
 
176
	if ($mots) {
177
		$query = "DELETE FROM spip_mots_articles WHERE id_mot NOT IN ($mots)";
178
		spip_query($query);
179
		$query = "DELETE FROM spip_mots_breves WHERE id_mot NOT IN ($mots)";
180
		spip_query($query);
181
		$query = "DELETE FROM spip_mots_forum WHERE id_mot NOT IN ($mots)";
182
		spip_query($query);
183
		$query = "DELETE FROM spip_mots_rubriques WHERE id_mot NOT IN ($mots)";
184
		spip_query($query);
185
		$query = "DELETE FROM spip_mots_syndic WHERE id_mot NOT IN ($mots)";
186
		spip_query($query);
187
	}
188
 
189
 
190
	//
191
	// Documents
192
	//
193
 
194
/*
195
	$query = "SELECT id_document FROM spip_documents_articles";
196
	$result = spip_query($query);
197
	while ($row = spip_fetch_array($result)) $documents[] = $row['id_document'];
198
	$query = "SELECT id_document FROM spip_documents_rubriques";
199
	$result = spip_query($query);
200
	while ($row = spip_fetch_array($result)) $documents[] = $row['id_document'];
201
	$query = "SELECT id_document FROM spip_documents_breves";
202
	$result = spip_query($query);
203
	while ($row = spip_fetch_array($result)) $documents[] = $row['id_document'];
204
	$query = "SELECT DISTINCT id_vignette FROM spip_documents WHERE id_vignette != 0";
205
	$result = spip_query($query);
206
	while ($row = spip_fetch_array($result)) $documents[] = $row['id_vignette'];
207
 
208
	if ($documents) {
209
		$documents = join(",", $documents);
210
 
211
		$query = "SELECT id_document, fichier FROM spip_documents WHERE id_document NOT IN ($documents)";
212
		$result = spip_query($query);
213
		while ($row = spip_fetch_array($result)) {
214
			$documents_poubelle[] = $row['id_document'];
215
			$fichiers_poubelle[] = $row['fichier'];
216
		}
217
 
218
		if ($documents_poubelle) {
219
			$documents_poubelle = join(",", $documents_poubelle);
220
 
221
			$fichier = $GLOBALS['dir_ecrire'].'data/.poubelle';
222
			if (!$f = @fopen($fichier, 'a')) {
223
				spip_log("impossible d'ecrire dans $fichier !");
224
				@unlink($fichier);	// on essaie de forcer
225
				$f = @fopen($fichier, 'a');
226
			}
227
			spip_log("mise a la poubelle : ".join(", ", $fichiers_poubelle));
228
			$ok = fputs($f, join("\n", $fichiers_poubelle)."\n");
229
			fclose($f);
230
			if ($ok) {
231
				$query = "DELETE FROM spip_documents WHERE id_document IN ($documents_poubelle)";
232
				spip_query($query);
233
			}
234
		}
235
	}
236
*/
237
 
238
	//
239
	// Forums
240
	//
241
 
242
	$query = "DELETE FROM spip_forum WHERE statut='redac' AND maj < $mydate";
243
	spip_query($query);
244
 
245
	$forums = '0';
246
	$query = "SELECT id_forum FROM spip_forum";
247
	$result = spip_query($query);
248
	while ($row = spip_fetch_array($result)) $forums .= ','.$row[0];
249
 
250
	if ($forums) {
251
		$query = "DELETE FROM spip_forum WHERE id_parent NOT IN (0,$forums)";
252
		spip_query($query);
253
		$query = "DELETE FROM spip_mots_forum WHERE id_forum NOT IN ($forums)";
254
		spip_query($query);
255
	}
256
 
257
 
258
	//
259
	// Indexation
260
	//
261
 
262
	// les objets inutiles
263
	$types = array('article','auteur','breve','mot','rubrique','forum','signature','syndic');
264
	while (list(,$type) = each($types)) {
265
		$table_objet = 'spip_'.table_objet($type);
266
		$table_index = 'spip_index_'.table_objet($type);
267
 
268
		// limiter aux objets publies
269
		switch ($type) {
270
			case 'article':
271
			case 'breve':
272
			case 'rubrique':
273
			case 'syndic':
274
			case 'forum':
275
			case 'signature':
276
				$critere = "AND statut<>'publie'";
277
				break;
278
			case 'auteur':
279
				$critere = "AND statut NOT IN ('0minirezo', '1comite')";
280
				break;
281
			case 'mot':
282
			default:
283
				$critere = 'AND 1=0';	// ne jamais desindexer un mot
284
				break;
285
		}
286
 
287
		spip_query("UPDATE $table_objet SET idx='' WHERE idx<>'non' $critere");
288
 
289
		$suppr = '';
290
		$s = spip_query("SELECT id_$type FROM $table_objet WHERE idx='' $critere");
291
		while ($t = spip_fetch_array($s))
292
			$suppr .= ','.$t[0];
293
		$s = spip_query("SELECT id_$type FROM $table_objet WHERE idx='non'");
294
		while ($t = spip_fetch_array($s))
295
			$suppr .= ','.$t[0];
296
		if ($suppr)
297
			spip_query("DELETE FROM $table_index WHERE id_$type IN (0$suppr)");
298
	}
299
 
300
	// les objets supprimes
301
	$hash = array();
302
	$types = array('article','auteur','breve','mot','rubrique','forum','signature','syndic');
303
	while (list(,$type) = each($types)) {
304
		$table_objet = 'spip_'.table_objet($type);
305
		$table_index = 'spip_index_'.table_objet($type);
306
		$list_objets = table_objet($type);
307
		if ($liste = $$list_objets)
308
			spip_query("DELETE FROM $table_index WHERE id_$type NOT IN ($liste)");
309
		$s = spip_query("SELECT DISTINCT(hash) FROM $table_index");
310
		while ($t = spip_fetch_array($s))
311
			$hash[$t[0]] = $t[0];
312
	}
313
	if (count($hash)) {
314
		$s = spip_query("SELECT hash FROM spip_index_dico");
315
		$suppr = '';
316
		while ($t = spip_fetch_array($s))
317
			if (!$hash[$t[0]]) $suppr.=','.$t[0];
318
		if ($suppr)
319
			spip_query("DELETE FROM spip_index_dico WHERE hash IN (0$suppr)");
320
	}
321
 
322
 
323
	//
324
	// MySQL
325
	//
326
 
327
	$query = "SHOW TABLES LIKE '".$GLOBALS['table_prefix']."_%'";
328
	$result = spip_query($query);
329
	while ($row = spip_fetch_array($result)) $tables[] = $row[0];
330
 
331
	if ($tables) {
332
		$tables = join(",", $tables);
333
		$query = "OPTIMIZE TABLE ".$tables;
334
		spip_query($query);
335
	}
336
 
337
	spip_log("optimisation ok");
338
}
339
 
340
optimiser_base();
341
 
342
?>