Completed
Push — master ( 16d018...f09bd9 )
by cam
06:05 queued 01:44
created
ecrire/action/editer_liens.php 2 patches
Indentation   +520 added lines, -520 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
  */
30 30
 
31 31
 if (!defined('_ECRIRE_INC_VERSION')) {
32
-	return;
32
+    return;
33 33
 }
34 34
 
35 35
 // charger la gestion les rôles sur les objets
@@ -47,21 +47,21 @@  discard block
 block discarded – undo
47 47
  *     - array(clé primaire, nom de la table de lien) si associable
48 48
  */
49 49
 function objet_associable($objet) {
50
-	$trouver_table = charger_fonction('trouver_table', 'base');
51
-	$table_sql = table_objet_sql($objet);
50
+    $trouver_table = charger_fonction('trouver_table', 'base');
51
+    $table_sql = table_objet_sql($objet);
52 52
 
53
-	$l = "";
54
-	if ($primary = id_table_objet($objet)
55
-		and $trouver_table($l = $table_sql . "_liens")
56
-		and !preg_match(',[^\w],', $primary)
57
-		and !preg_match(',[^\w],', $l)
58
-	) {
59
-		return array($primary, $l);
60
-	}
53
+    $l = "";
54
+    if ($primary = id_table_objet($objet)
55
+        and $trouver_table($l = $table_sql . "_liens")
56
+        and !preg_match(',[^\w],', $primary)
57
+        and !preg_match(',[^\w],', $l)
58
+    ) {
59
+        return array($primary, $l);
60
+    }
61 61
 
62
-	spip_log("Objet $objet non associable : ne dispose pas d'une cle primaire $primary OU d'une table liens $l");
62
+    spip_log("Objet $objet non associable : ne dispose pas d'une cle primaire $primary OU d'une table liens $l");
63 63
 
64
-	return false;
64
+    return false;
65 65
 }
66 66
 
67 67
 /**
@@ -87,13 +87,13 @@  discard block
 block discarded – undo
87 87
  * @return bool|int
88 88
  */
89 89
 function objet_associer($objets_source, $objets_lies, $qualif = null) {
90
-	$modifs = objet_traiter_liaisons('lien_insert', $objets_source, $objets_lies, $qualif);
90
+    $modifs = objet_traiter_liaisons('lien_insert', $objets_source, $objets_lies, $qualif);
91 91
 
92
-	if ($qualif) {
93
-		objet_qualifier_liens($objets_source, $objets_lies, $qualif);
94
-	}
92
+    if ($qualif) {
93
+        objet_qualifier_liens($objets_source, $objets_lies, $qualif);
94
+    }
95 95
 
96
-	return $modifs; // pas d'erreur
96
+    return $modifs; // pas d'erreur
97 97
 }
98 98
 
99 99
 
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
  * @return bool|int
128 128
  */
129 129
 function objet_dissocier($objets_source, $objets_lies, $cond = null) {
130
-	return objet_traiter_liaisons('lien_delete', $objets_source, $objets_lies, $cond);
130
+    return objet_traiter_liaisons('lien_delete', $objets_source, $objets_lies, $cond);
131 131
 }
132 132
 
133 133
 
@@ -152,7 +152,7 @@  discard block
 block discarded – undo
152 152
  * @return bool|int
153 153
  */
154 154
 function objet_qualifier_liens($objets_source, $objets_lies, $qualif) {
155
-	return objet_traiter_liaisons('lien_set', $objets_source, $objets_lies, $qualif);
155
+    return objet_traiter_liaisons('lien_set', $objets_source, $objets_lies, $qualif);
156 156
 }
157 157
 
158 158
 
@@ -187,7 +187,7 @@  discard block
 block discarded – undo
187 187
  *     Liste des trouvailles
188 188
  */
189 189
 function objet_trouver_liens($objets_source, $objets_lies, $cond = null) {
190
-	return objet_traiter_liaisons('lien_find', $objets_source, $objets_lies, $cond);
190
+    return objet_traiter_liaisons('lien_find', $objets_source, $objets_lies, $cond);
191 191
 }
192 192
 
193 193
 
@@ -211,8 +211,8 @@  discard block
 block discarded – undo
211 211
  * @return int
212 212
  */
213 213
 function objet_optimiser_liens($objets_source, $objets_lies) {
214
-	spip_log("objet_optimiser_liens : ".json_encode($objets_source) . ', ' . json_encode($objets_lies), 'genie'._LOG_DEBUG);
215
-	return objet_traiter_liaisons('lien_optimise', $objets_source, $objets_lies);
214
+    spip_log("objet_optimiser_liens : ".json_encode($objets_source) . ', ' . json_encode($objets_lies), 'genie'._LOG_DEBUG);
215
+    return objet_traiter_liaisons('lien_optimise', $objets_source, $objets_lies);
216 216
 }
217 217
 
218 218
 
@@ -232,37 +232,37 @@  discard block
 block discarded – undo
232 232
  *     Nombre de liens copiés
233 233
  */
234 234
 function objet_dupliquer_liens($objet, $id_source, $id_cible, $types = null, $exclure_types = null) {
235
-	include_spip('base/objets');
236
-	$tables = lister_tables_objets_sql();
237
-	$n = 0;
238
-	foreach ($tables as $table_sql => $infos) {
239
-		if (
240
-			(is_null($types) or in_array($infos['type'], $types))
241
-			and (is_null($exclure_types) or !in_array($infos['type'], $exclure_types))
242
-		) {
243
-			if (objet_associable($infos['type'])) {
244
-				$liens = (($infos['type'] == $objet) ?
245
-					objet_trouver_liens(array($objet => $id_source), '*')
246
-					:
247
-					objet_trouver_liens(array($infos['type'] => '*'), array($objet => $id_source)));
248
-				foreach ($liens as $lien) {
249
-					$n++;
250
-					if ($infos['type'] == $objet) {
251
-						if (
252
-							(is_null($types) or in_array($lien['objet'], $types))
253
-							and (is_null($exclure_types) or !in_array($lien['objet'], $exclure_types))
254
-						) {
255
-							objet_associer(array($objet => $id_cible), array($lien['objet'] => $lien[$lien['objet']]), $lien);
256
-						}
257
-					} else {
258
-						objet_associer(array($infos['type'] => $lien[$infos['type']]), array($objet => $id_cible), $lien);
259
-					}
260
-				}
261
-			}
262
-		}
263
-	}
264
-
265
-	return $n;
235
+    include_spip('base/objets');
236
+    $tables = lister_tables_objets_sql();
237
+    $n = 0;
238
+    foreach ($tables as $table_sql => $infos) {
239
+        if (
240
+            (is_null($types) or in_array($infos['type'], $types))
241
+            and (is_null($exclure_types) or !in_array($infos['type'], $exclure_types))
242
+        ) {
243
+            if (objet_associable($infos['type'])) {
244
+                $liens = (($infos['type'] == $objet) ?
245
+                    objet_trouver_liens(array($objet => $id_source), '*')
246
+                    :
247
+                    objet_trouver_liens(array($infos['type'] => '*'), array($objet => $id_source)));
248
+                foreach ($liens as $lien) {
249
+                    $n++;
250
+                    if ($infos['type'] == $objet) {
251
+                        if (
252
+                            (is_null($types) or in_array($lien['objet'], $types))
253
+                            and (is_null($exclure_types) or !in_array($lien['objet'], $exclure_types))
254
+                        ) {
255
+                            objet_associer(array($objet => $id_cible), array($lien['objet'] => $lien[$lien['objet']]), $lien);
256
+                        }
257
+                    } else {
258
+                        objet_associer(array($infos['type'] => $lien[$infos['type']]), array($objet => $id_cible), $lien);
259
+                    }
260
+                }
261
+            }
262
+        }
263
+    }
264
+
265
+    return $n;
266 266
 }
267 267
 
268 268
 /**
@@ -304,38 +304,38 @@  discard block
 block discarded – undo
304 304
  * @return bool|int|array
305 305
  */
306 306
 function objet_traiter_liaisons($operation, $objets_source, $objets_lies, $set = null) {
307
-	// accepter une syntaxe minimale pour supprimer tous les liens
308
-	if ($objets_lies == '*') {
309
-		$objets_lies = array('*' => '*');
310
-	}
311
-	$modifs = 0; // compter le nombre de modifications
312
-	$echec = null;
313
-	foreach ($objets_source as $objet => $ids) {
314
-		if ($a = objet_associable($objet)) {
315
-			list($primary, $l) = $a;
316
-			if (!is_array($ids)) {
317
-				$ids = array($ids);
318
-			} elseif (reset($ids) == "NOT") {
319
-				// si on demande un array('NOT',...) => recuperer la liste d'ids correspondants
320
-				$where = lien_where($primary, $ids, '*', '*');
321
-				$ids = sql_allfetsel($primary, $l, $where);
322
-				$ids = array_map('reset', $ids);
323
-			}
324
-			foreach ($ids as $id) {
325
-				$res = $operation($objet, $primary, $l, $id, $objets_lies, $set);
326
-				if ($res === false) {
327
-					spip_log("objet_traiter_liaisons [Echec] : $operation sur $objet/$primary/$l/$id", _LOG_ERREUR);
328
-					$echec = true;
329
-				} else {
330
-					$modifs = ($modifs ? (is_array($res) ? array_merge($modifs, $res) : $modifs + $res) : $res);
331
-				}
332
-			}
333
-		} else {
334
-			$echec = true;
335
-		}
336
-	}
337
-
338
-	return ($echec ? false : $modifs); // pas d'erreur
307
+    // accepter une syntaxe minimale pour supprimer tous les liens
308
+    if ($objets_lies == '*') {
309
+        $objets_lies = array('*' => '*');
310
+    }
311
+    $modifs = 0; // compter le nombre de modifications
312
+    $echec = null;
313
+    foreach ($objets_source as $objet => $ids) {
314
+        if ($a = objet_associable($objet)) {
315
+            list($primary, $l) = $a;
316
+            if (!is_array($ids)) {
317
+                $ids = array($ids);
318
+            } elseif (reset($ids) == "NOT") {
319
+                // si on demande un array('NOT',...) => recuperer la liste d'ids correspondants
320
+                $where = lien_where($primary, $ids, '*', '*');
321
+                $ids = sql_allfetsel($primary, $l, $where);
322
+                $ids = array_map('reset', $ids);
323
+            }
324
+            foreach ($ids as $id) {
325
+                $res = $operation($objet, $primary, $l, $id, $objets_lies, $set);
326
+                if ($res === false) {
327
+                    spip_log("objet_traiter_liaisons [Echec] : $operation sur $objet/$primary/$l/$id", _LOG_ERREUR);
328
+                    $echec = true;
329
+                } else {
330
+                    $modifs = ($modifs ? (is_array($res) ? array_merge($modifs, $res) : $modifs + $res) : $res);
331
+                }
332
+            }
333
+        } else {
334
+            $echec = true;
335
+        }
336
+    }
337
+
338
+    return ($echec ? false : $modifs); // pas d'erreur
339 339
 }
340 340
 
341 341
 
@@ -364,106 +364,106 @@  discard block
 block discarded – undo
364 364
  *     Nombre d'insertions faites, false si échec.
365 365
  */
366 366
 function lien_insert($objet_source, $primary, $table_lien, $id, $objets, $qualif) {
367
-	$ins = 0;
368
-	$echec = null;
369
-	if (is_null($qualif)) {
370
-		$qualif = array();
371
-	}
372
-
373
-	foreach ($objets as $objet => $id_objets) {
374
-		if (!is_array($id_objets)) {
375
-			$id_objets = array($id_objets);
376
-		}
377
-
378
-		// role, colonne, where par défaut
379
-		list($role, $colonne_role, $cond) =
380
-			roles_trouver_dans_qualif($objet_source, $objet, $qualif);
381
-
382
-		foreach ($id_objets as $id_objet) {
383
-			$objet = (($objet == '*') ? $objet : objet_type($objet)); # securite
384
-
385
-			$insertions = array(
386
-				'id_objet' => $id_objet,
387
-				'objet' => $objet,
388
-				$primary => $id
389
-			);
390
-			// rôle en plus s'il est défini
391
-			if ($role) {
392
-				$insertions += array(
393
-					$colonne_role => $role
394
-				);
395
-			}
396
-
397
-			if (lien_triables($table_lien)) {
398
-				$where = lien_where($primary, $id, $objet, $id_objet);
399
-				// si il y a deja un lien pour ce couple (avec un autre role?) on reprend le meme rang si non nul
400
-				if (!$rang = intval(sql_getfetsel('rang_lien', $table_lien, $where))) {
401
-					$where = lien_where($primary, '*', $objet, $id_objet);
402
-					$rang = intval(sql_getfetsel('max(rang_lien)', $table_lien, $where));
403
-					// si aucun lien n'a de rang, on en introduit pas, on garde zero
404
-					if ($rang>0) {
405
-						$rang = intval($rang) + 1;
406
-					}
407
-				}
408
-				$insertions['rang_lien'] = $rang;
409
-			}
410
-
411
-			$args = array(
412
-				'table_lien' => $table_lien,
413
-				'objet_source' => $objet_source,
414
-				'id_objet_source' => $id,
415
-				'objet' => $objet,
416
-				'id_objet' => $id_objet,
417
-				'role' => $role,
418
-				'colonne_role' => $colonne_role,
419
-				'action' => 'insert',
420
-			);
421
-
422
-			// Envoyer aux plugins
423
-			$insertions = pipeline('pre_edition_lien',
424
-				array(
425
-					'args' => $args,
426
-					'data' => $insertions
427
-				)
428
-			);
429
-			$args['id_objet'] = $insertions['id_objet'];
430
-
431
-			$where = lien_where($primary, $id, $objet, $id_objet, $cond);
432
-
433
-			if (($id_objet = intval($insertions['id_objet']) or in_array($objet, ['site', 'rubrique']))
434
-				and !sql_getfetsel($primary, $table_lien, $where)
435
-			) {
436
-
437
-				if (lien_triables($table_lien) and isset($insertions['rang_lien']) and intval($insertions['rang_lien'])) {
438
-					// on decale les liens de rang_lien>=la valeur inseree pour faire la place
439
-					$w = lien_where($primary, '*', $objet, $id_objet, array('rang_lien>='.intval($insertions['rang_lien']),"$primary!=".intval($id)));
440
-					sql_update($table_lien, array('rang_lien'=>'rang_lien+1'), $w);
441
-				}
442
-
443
-				$e = sql_insertq($table_lien, $insertions);
444
-				if ($e !== false) {
445
-					$ins++;
446
-					lien_propage_date_modif($objet, $id_objet);
447
-					lien_propage_date_modif($objet_source, $id);
448
-					// Envoyer aux plugins
449
-					pipeline('post_edition_lien',
450
-						array(
451
-							'args' => $args,
452
-							'data' => $insertions
453
-						)
454
-					);
455
-				} else {
456
-					$echec = true;
457
-				}
458
-			}
459
-		}
460
-	}
461
-	// si on a fait des insertions, on reordonne les liens concernes
462
-	if ($ins>0) {
463
-		lien_ordonner($objet_source, $primary, $table_lien, $id, $objets);
464
-	}
465
-
466
-	return ($echec ? false : $ins);
367
+    $ins = 0;
368
+    $echec = null;
369
+    if (is_null($qualif)) {
370
+        $qualif = array();
371
+    }
372
+
373
+    foreach ($objets as $objet => $id_objets) {
374
+        if (!is_array($id_objets)) {
375
+            $id_objets = array($id_objets);
376
+        }
377
+
378
+        // role, colonne, where par défaut
379
+        list($role, $colonne_role, $cond) =
380
+            roles_trouver_dans_qualif($objet_source, $objet, $qualif);
381
+
382
+        foreach ($id_objets as $id_objet) {
383
+            $objet = (($objet == '*') ? $objet : objet_type($objet)); # securite
384
+
385
+            $insertions = array(
386
+                'id_objet' => $id_objet,
387
+                'objet' => $objet,
388
+                $primary => $id
389
+            );
390
+            // rôle en plus s'il est défini
391
+            if ($role) {
392
+                $insertions += array(
393
+                    $colonne_role => $role
394
+                );
395
+            }
396
+
397
+            if (lien_triables($table_lien)) {
398
+                $where = lien_where($primary, $id, $objet, $id_objet);
399
+                // si il y a deja un lien pour ce couple (avec un autre role?) on reprend le meme rang si non nul
400
+                if (!$rang = intval(sql_getfetsel('rang_lien', $table_lien, $where))) {
401
+                    $where = lien_where($primary, '*', $objet, $id_objet);
402
+                    $rang = intval(sql_getfetsel('max(rang_lien)', $table_lien, $where));
403
+                    // si aucun lien n'a de rang, on en introduit pas, on garde zero
404
+                    if ($rang>0) {
405
+                        $rang = intval($rang) + 1;
406
+                    }
407
+                }
408
+                $insertions['rang_lien'] = $rang;
409
+            }
410
+
411
+            $args = array(
412
+                'table_lien' => $table_lien,
413
+                'objet_source' => $objet_source,
414
+                'id_objet_source' => $id,
415
+                'objet' => $objet,
416
+                'id_objet' => $id_objet,
417
+                'role' => $role,
418
+                'colonne_role' => $colonne_role,
419
+                'action' => 'insert',
420
+            );
421
+
422
+            // Envoyer aux plugins
423
+            $insertions = pipeline('pre_edition_lien',
424
+                array(
425
+                    'args' => $args,
426
+                    'data' => $insertions
427
+                )
428
+            );
429
+            $args['id_objet'] = $insertions['id_objet'];
430
+
431
+            $where = lien_where($primary, $id, $objet, $id_objet, $cond);
432
+
433
+            if (($id_objet = intval($insertions['id_objet']) or in_array($objet, ['site', 'rubrique']))
434
+                and !sql_getfetsel($primary, $table_lien, $where)
435
+            ) {
436
+
437
+                if (lien_triables($table_lien) and isset($insertions['rang_lien']) and intval($insertions['rang_lien'])) {
438
+                    // on decale les liens de rang_lien>=la valeur inseree pour faire la place
439
+                    $w = lien_where($primary, '*', $objet, $id_objet, array('rang_lien>='.intval($insertions['rang_lien']),"$primary!=".intval($id)));
440
+                    sql_update($table_lien, array('rang_lien'=>'rang_lien+1'), $w);
441
+                }
442
+
443
+                $e = sql_insertq($table_lien, $insertions);
444
+                if ($e !== false) {
445
+                    $ins++;
446
+                    lien_propage_date_modif($objet, $id_objet);
447
+                    lien_propage_date_modif($objet_source, $id);
448
+                    // Envoyer aux plugins
449
+                    pipeline('post_edition_lien',
450
+                        array(
451
+                            'args' => $args,
452
+                            'data' => $insertions
453
+                        )
454
+                    );
455
+                } else {
456
+                    $echec = true;
457
+                }
458
+            }
459
+        }
460
+    }
461
+    // si on a fait des insertions, on reordonne les liens concernes
462
+    if ($ins>0) {
463
+        lien_ordonner($objet_source, $primary, $table_lien, $id, $objets);
464
+    }
465
+
466
+    return ($echec ? false : $ins);
467 467
 }
468 468
 
469 469
 
@@ -476,32 +476,32 @@  discard block
 block discarded – undo
476 476
  * @param array|string $objets
477 477
  */
478 478
 function lien_ordonner($objet_source, $primary, $table_lien, $id, $objets) {
479
-	if (!lien_triables($table_lien)) {
480
-		return;
481
-	}
482
-
483
-	foreach ($objets as $objet => $id_objets) {
484
-		if (!is_array($id_objets)) {
485
-			$id_objets = array($id_objets);
486
-		}
487
-
488
-		foreach ($id_objets as $id_objet) {
489
-			$objet = (($objet == '*') ? $objet : objet_type($objet)); # securite
490
-
491
-			$where = lien_where($primary, '*', $objet, $id_objet);
492
-			$liens = sql_allfetsel("$primary, id_objet, objet, rang_lien", $table_lien, $where, $primary,"rang_lien");
493
-
494
-			$rangs = array_column($liens, 'rang_lien');
495
-			if (count($rangs) and (max($rangs)>0 or min($rangs)<0)) {
496
-				$rang = 1;
497
-				foreach ($liens as $lien) {
498
-					$where = lien_where($primary, $lien[$primary], $objet, $id_objet, array('rang_lien!='.intval($rang)));
499
-					sql_updateq($table_lien, array('rang_lien' => $rang), $where);
500
-					$rang++;
501
-				}
502
-			}
503
-		}
504
-	}
479
+    if (!lien_triables($table_lien)) {
480
+        return;
481
+    }
482
+
483
+    foreach ($objets as $objet => $id_objets) {
484
+        if (!is_array($id_objets)) {
485
+            $id_objets = array($id_objets);
486
+        }
487
+
488
+        foreach ($id_objets as $id_objet) {
489
+            $objet = (($objet == '*') ? $objet : objet_type($objet)); # securite
490
+
491
+            $where = lien_where($primary, '*', $objet, $id_objet);
492
+            $liens = sql_allfetsel("$primary, id_objet, objet, rang_lien", $table_lien, $where, $primary,"rang_lien");
493
+
494
+            $rangs = array_column($liens, 'rang_lien');
495
+            if (count($rangs) and (max($rangs)>0 or min($rangs)<0)) {
496
+                $rang = 1;
497
+                foreach ($liens as $lien) {
498
+                    $where = lien_where($primary, $lien[$primary], $objet, $id_objet, array('rang_lien!='.intval($rang)));
499
+                    sql_updateq($table_lien, array('rang_lien' => $rang), $where);
500
+                    $rang++;
501
+                }
502
+            }
503
+        }
504
+    }
505 505
 }
506 506
 
507 507
 
@@ -512,18 +512,18 @@  discard block
 block discarded – undo
512 512
  * @return mixed
513 513
  */
514 514
 function lien_triables($table_lien) {
515
-	static $triables = array();
516
-	if (!isset($triables[$table_lien])) {
517
-		$trouver_table = charger_fonction('trouver_table', 'base');
518
-		$desc = $trouver_table($table_lien);
519
-		if ($desc and isset($desc['field']['rang_lien'])) {
520
-			$triables[$table_lien] = true;
521
-		}
522
-		else {
523
-			$triables[$table_lien] = false;
524
-		}
525
-	}
526
-	return $triables[$table_lien];
515
+    static $triables = array();
516
+    if (!isset($triables[$table_lien])) {
517
+        $trouver_table = charger_fonction('trouver_table', 'base');
518
+        $desc = $trouver_table($table_lien);
519
+        if ($desc and isset($desc['field']['rang_lien'])) {
520
+            $triables[$table_lien] = true;
521
+        }
522
+        else {
523
+            $triables[$table_lien] = false;
524
+        }
525
+    }
526
+    return $triables[$table_lien];
527 527
 }
528 528
 
529 529
 
@@ -539,45 +539,45 @@  discard block
 block discarded – undo
539 539
  * @return array                        Liste des conditions
540 540
  */
541 541
 function lien_where($primary, $id_source, $objet, $id_objet, $cond = array()) {
542
-	if ((!is_array($id_source) and !strlen($id_source))
543
-		or !strlen($objet)
544
-		or (!is_array($id_objet) and !strlen($id_objet))
545
-	) {
546
-		return array("0=1");
547
-	} // securite
548
-
549
-	$not = "";
550
-	if (is_array($id_source) and reset($id_source) == "NOT") {
551
-		$not = array_shift($id_source);
552
-		$id_source = reset($id_source);
553
-	}
554
-
555
-	$where = $cond;
556
-
557
-	if ($id_source !== '*') {
558
-		$where[] = (is_array($id_source) ? sql_in(addslashes($primary), array_map('intval', $id_source),
559
-			$not) : addslashes($primary) . ($not ? "<>" : "=") . intval($id_source));
560
-	} elseif ($not) {
561
-		$where[] = "0=1";
562
-	} // idiot mais quand meme
563
-
564
-	$not = "";
565
-	if (is_array($id_objet) and reset($id_objet) == "NOT") {
566
-		$not = array_shift($id_objet);
567
-		$id_objet = reset($id_objet);
568
-	}
569
-
570
-	if ($objet !== '*') {
571
-		$where[] = "objet=" . sql_quote($objet);
572
-	}
573
-	if ($id_objet !== '*') {
574
-		$where[] = (is_array($id_objet) ? sql_in('id_objet', array_map('intval', $id_objet),
575
-			$not) : "id_objet" . ($not ? "<>" : "=") . intval($id_objet));
576
-	} elseif ($not) {
577
-		$where[] = "0=1";
578
-	} // idiot mais quand meme
579
-
580
-	return $where;
542
+    if ((!is_array($id_source) and !strlen($id_source))
543
+        or !strlen($objet)
544
+        or (!is_array($id_objet) and !strlen($id_objet))
545
+    ) {
546
+        return array("0=1");
547
+    } // securite
548
+
549
+    $not = "";
550
+    if (is_array($id_source) and reset($id_source) == "NOT") {
551
+        $not = array_shift($id_source);
552
+        $id_source = reset($id_source);
553
+    }
554
+
555
+    $where = $cond;
556
+
557
+    if ($id_source !== '*') {
558
+        $where[] = (is_array($id_source) ? sql_in(addslashes($primary), array_map('intval', $id_source),
559
+            $not) : addslashes($primary) . ($not ? "<>" : "=") . intval($id_source));
560
+    } elseif ($not) {
561
+        $where[] = "0=1";
562
+    } // idiot mais quand meme
563
+
564
+    $not = "";
565
+    if (is_array($id_objet) and reset($id_objet) == "NOT") {
566
+        $not = array_shift($id_objet);
567
+        $id_objet = reset($id_objet);
568
+    }
569
+
570
+    if ($objet !== '*') {
571
+        $where[] = "objet=" . sql_quote($objet);
572
+    }
573
+    if ($id_objet !== '*') {
574
+        $where[] = (is_array($id_objet) ? sql_in('id_objet', array_map('intval', $id_objet),
575
+            $not) : "id_objet" . ($not ? "<>" : "=") . intval($id_objet));
576
+    } elseif ($not) {
577
+        $where[] = "0=1";
578
+    } // idiot mais quand meme
579
+
580
+    return $where;
581 581
 }
582 582
 
583 583
 /**
@@ -608,89 +608,89 @@  discard block
 block discarded – undo
608 608
  */
609 609
 function lien_delete($objet_source, $primary, $table_lien, $id, $objets, $cond = null) {
610 610
 
611
-	$retire = array();
612
-	$dels = 0;
613
-	$echec = false;
614
-	if (is_null($cond)) {
615
-		$cond = array();
616
-	}
617
-
618
-	foreach ($objets as $objet => $id_objets) {
619
-		$objet = ($objet == '*') ? $objet : objet_type($objet); # securite
620
-		if (!is_array($id_objets) or reset($id_objets) == "NOT") {
621
-			$id_objets = array($id_objets);
622
-		}
623
-		foreach ($id_objets as $id_objet) {
624
-			list($cond, $colonne_role, $role) = roles_creer_condition_role($objet_source, $objet, $cond);
625
-			// id_objet peut valoir '*'
626
-			$where = lien_where($primary, $id, $objet, $id_objet, $cond);
627
-
628
-			// lire les liens existants pour propager la date de modif
629
-			$select = "$primary,id_objet,objet";
630
-			if ($colonne_role) {
631
-				$select .= ",$colonne_role";
632
-			}
633
-			$liens = sql_allfetsel($select, $table_lien, $where);
634
-
635
-			// iterer sur les liens pour permettre aux plugins de gerer
636
-			foreach ($liens as $l) {
637
-
638
-				$args = array(
639
-					'table_lien' => $table_lien,
640
-					'objet_source' => $objet_source,
641
-					'id_objet_source' => $l[$primary],
642
-					'objet' => $l['objet'],
643
-					'id_objet' => $l['id_objet'],
644
-					'colonne_role' => $colonne_role,
645
-					'role' => ($colonne_role ? $l[$colonne_role] : ''),
646
-					'action' => 'delete',
647
-				);
648
-
649
-				// Envoyer aux plugins
650
-				$l = pipeline('pre_edition_lien',
651
-					array(
652
-						'args' => $args,
653
-						'data' => $l
654
-					)
655
-				);
656
-				$args['id_objet'] = $id_o = $l['id_objet'];
657
-
658
-				if ($id_o = intval($l['id_objet']) or in_array($l['objet'], ['site', 'rubrique'])) {
659
-					$where = lien_where($primary, $l[$primary], $l['objet'], $id_o, $cond);
660
-					$e = sql_delete($table_lien, $where);
661
-					if ($e !== false) {
662
-						$dels += $e;
663
-						lien_propage_date_modif($l['objet'], $id_o);
664
-						lien_propage_date_modif($objet_source, $l[$primary]);
665
-					} else {
666
-						$echec = true;
667
-					}
668
-					$retire[] = array(
669
-						'source' => array($objet_source => $l[$primary]),
670
-						'lien' => array($l['objet'] => $id_o),
671
-						'type' => $l['objet'],
672
-						'role' => ($colonne_role ? $l[$colonne_role] : ''),
673
-						'id' => $id_o
674
-					);
675
-					// Envoyer aux plugins
676
-					pipeline('post_edition_lien',
677
-						array(
678
-							'args' => $args,
679
-							'data' => $l
680
-						)
681
-					);
682
-				}
683
-			}
684
-		}
685
-	}
686
-	// si on a supprime des liens, on reordonne les liens concernes
687
-	if ($dels) {
688
-		lien_ordonner($objet_source, $primary, $table_lien, $id, $objets);
689
-	}
690
-
691
-	pipeline('trig_supprimer_objets_lies', $retire);
692
-
693
-	return ($echec ? false : $dels);
611
+    $retire = array();
612
+    $dels = 0;
613
+    $echec = false;
614
+    if (is_null($cond)) {
615
+        $cond = array();
616
+    }
617
+
618
+    foreach ($objets as $objet => $id_objets) {
619
+        $objet = ($objet == '*') ? $objet : objet_type($objet); # securite
620
+        if (!is_array($id_objets) or reset($id_objets) == "NOT") {
621
+            $id_objets = array($id_objets);
622
+        }
623
+        foreach ($id_objets as $id_objet) {
624
+            list($cond, $colonne_role, $role) = roles_creer_condition_role($objet_source, $objet, $cond);
625
+            // id_objet peut valoir '*'
626
+            $where = lien_where($primary, $id, $objet, $id_objet, $cond);
627
+
628
+            // lire les liens existants pour propager la date de modif
629
+            $select = "$primary,id_objet,objet";
630
+            if ($colonne_role) {
631
+                $select .= ",$colonne_role";
632
+            }
633
+            $liens = sql_allfetsel($select, $table_lien, $where);
634
+
635
+            // iterer sur les liens pour permettre aux plugins de gerer
636
+            foreach ($liens as $l) {
637
+
638
+                $args = array(
639
+                    'table_lien' => $table_lien,
640
+                    'objet_source' => $objet_source,
641
+                    'id_objet_source' => $l[$primary],
642
+                    'objet' => $l['objet'],
643
+                    'id_objet' => $l['id_objet'],
644
+                    'colonne_role' => $colonne_role,
645
+                    'role' => ($colonne_role ? $l[$colonne_role] : ''),
646
+                    'action' => 'delete',
647
+                );
648
+
649
+                // Envoyer aux plugins
650
+                $l = pipeline('pre_edition_lien',
651
+                    array(
652
+                        'args' => $args,
653
+                        'data' => $l
654
+                    )
655
+                );
656
+                $args['id_objet'] = $id_o = $l['id_objet'];
657
+
658
+                if ($id_o = intval($l['id_objet']) or in_array($l['objet'], ['site', 'rubrique'])) {
659
+                    $where = lien_where($primary, $l[$primary], $l['objet'], $id_o, $cond);
660
+                    $e = sql_delete($table_lien, $where);
661
+                    if ($e !== false) {
662
+                        $dels += $e;
663
+                        lien_propage_date_modif($l['objet'], $id_o);
664
+                        lien_propage_date_modif($objet_source, $l[$primary]);
665
+                    } else {
666
+                        $echec = true;
667
+                    }
668
+                    $retire[] = array(
669
+                        'source' => array($objet_source => $l[$primary]),
670
+                        'lien' => array($l['objet'] => $id_o),
671
+                        'type' => $l['objet'],
672
+                        'role' => ($colonne_role ? $l[$colonne_role] : ''),
673
+                        'id' => $id_o
674
+                    );
675
+                    // Envoyer aux plugins
676
+                    pipeline('post_edition_lien',
677
+                        array(
678
+                            'args' => $args,
679
+                            'data' => $l
680
+                        )
681
+                    );
682
+                }
683
+            }
684
+        }
685
+    }
686
+    // si on a supprime des liens, on reordonne les liens concernes
687
+    if ($dels) {
688
+        lien_ordonner($objet_source, $primary, $table_lien, $id, $objets);
689
+    }
690
+
691
+    pipeline('trig_supprimer_objets_lies', $retire);
692
+
693
+    return ($echec ? false : $dels);
694 694
 }
695 695
 
696 696
 
@@ -713,61 +713,61 @@  discard block
 block discarded – undo
713 713
  * @return bool|int
714 714
  */
715 715
 function lien_optimise($objet_source, $primary, $table_lien, $id, $objets) {
716
-	include_spip('genie/optimiser');
717
-	$echec = false;
718
-	$dels = 0;
719
-	foreach ($objets as $objet => $id_objets) {
720
-		$objet = ($objet == '*') ? $objet : objet_type($objet); # securite
721
-		if (!is_array($id_objets) or reset($id_objets) == "NOT") {
722
-			$id_objets = array($id_objets);
723
-		}
724
-		foreach ($id_objets as $id_objet) {
725
-			$where = lien_where($primary, $id, $objet, $id_objet);
726
-			# les liens vers un objet inexistant
727
-			$r = sql_select("DISTINCT objet", $table_lien, $where);
728
-			while ($t = sql_fetch($r)) {
729
-				$type = $t['objet'];
730
-				$spip_table_objet = table_objet_sql($type);
731
-				$id_table_objet = id_table_objet($type);
732
-				$res = sql_select("L.$primary AS id,L.id_objet",
733
-					// la condition de jointure inclue L.objet='xxx' pour ne joindre que les bonnes lignes
734
-					// du coups toutes les lignes avec un autre objet ont un id_xxx=NULL puisque LEFT JOIN
735
-					// il faut les eliminier en repetant la condition dans le where L.objet='xxx'
736
-					"$table_lien AS L
716
+    include_spip('genie/optimiser');
717
+    $echec = false;
718
+    $dels = 0;
719
+    foreach ($objets as $objet => $id_objets) {
720
+        $objet = ($objet == '*') ? $objet : objet_type($objet); # securite
721
+        if (!is_array($id_objets) or reset($id_objets) == "NOT") {
722
+            $id_objets = array($id_objets);
723
+        }
724
+        foreach ($id_objets as $id_objet) {
725
+            $where = lien_where($primary, $id, $objet, $id_objet);
726
+            # les liens vers un objet inexistant
727
+            $r = sql_select("DISTINCT objet", $table_lien, $where);
728
+            while ($t = sql_fetch($r)) {
729
+                $type = $t['objet'];
730
+                $spip_table_objet = table_objet_sql($type);
731
+                $id_table_objet = id_table_objet($type);
732
+                $res = sql_select("L.$primary AS id,L.id_objet",
733
+                    // la condition de jointure inclue L.objet='xxx' pour ne joindre que les bonnes lignes
734
+                    // du coups toutes les lignes avec un autre objet ont un id_xxx=NULL puisque LEFT JOIN
735
+                    // il faut les eliminier en repetant la condition dans le where L.objet='xxx'
736
+                    "$table_lien AS L
737 737
 									LEFT JOIN $spip_table_objet AS O
738 738
 										ON (O.$id_table_objet=L.id_objet AND L.objet=" . sql_quote($type) . ")",
739
-					"L.objet=" . sql_quote($type) . " AND O.$id_table_objet IS NULL");
740
-				// sur une cle primaire composee, pas d'autres solutions que de virer un a un
741
-				while ($row = sql_fetch($res)) {
742
-					if ($primary === 'id_document' and in_array($type, ['site', 'rubrique']) and !intval($row['id_objet'])) {
743
-						continue; // gaffe, c'est le logo du site ou des rubriques!
744
-					}
745
-					$e = sql_delete($table_lien,
746
-						array("$primary=" . $row['id'], "id_objet=" . $row['id_objet'], "objet=" . sql_quote($type)));
747
-					if ($e != false) {
748
-						$dels += $e;
749
-						spip_log(
750
-							"lien_optimise: Entree " . $row['id'] . "/" . $row['id_objet'] . "/$type supprimee dans la table $table_lien",
751
-							'genie'._LOG_INFO_IMPORTANTE
752
-						);
753
-					}
754
-				}
755
-			}
756
-
757
-			# les liens depuis un objet inexistant
758
-			$table_source = table_objet_sql($objet_source);
759
-			// filtrer selon $id, $objet, $id_objet eventuellement fournis
760
-			// (en general '*' pour chaque)
761
-			$where = lien_where("L.$primary", $id, $objet, $id_objet);
762
-			$where[] = "O.$primary IS NULL";
763
-			$res = sql_select("L.$primary AS id",
764
-				"$table_lien AS L LEFT JOIN $table_source AS O ON L.$primary=O.$primary",
765
-				$where);
766
-			$dels += optimiser_sansref($table_lien, $primary, $res);
767
-		}
768
-	}
769
-
770
-	return ($echec ? false : $dels);
739
+                    "L.objet=" . sql_quote($type) . " AND O.$id_table_objet IS NULL");
740
+                // sur une cle primaire composee, pas d'autres solutions que de virer un a un
741
+                while ($row = sql_fetch($res)) {
742
+                    if ($primary === 'id_document' and in_array($type, ['site', 'rubrique']) and !intval($row['id_objet'])) {
743
+                        continue; // gaffe, c'est le logo du site ou des rubriques!
744
+                    }
745
+                    $e = sql_delete($table_lien,
746
+                        array("$primary=" . $row['id'], "id_objet=" . $row['id_objet'], "objet=" . sql_quote($type)));
747
+                    if ($e != false) {
748
+                        $dels += $e;
749
+                        spip_log(
750
+                            "lien_optimise: Entree " . $row['id'] . "/" . $row['id_objet'] . "/$type supprimee dans la table $table_lien",
751
+                            'genie'._LOG_INFO_IMPORTANTE
752
+                        );
753
+                    }
754
+                }
755
+            }
756
+
757
+            # les liens depuis un objet inexistant
758
+            $table_source = table_objet_sql($objet_source);
759
+            // filtrer selon $id, $objet, $id_objet eventuellement fournis
760
+            // (en general '*' pour chaque)
761
+            $where = lien_where("L.$primary", $id, $objet, $id_objet);
762
+            $where[] = "O.$primary IS NULL";
763
+            $res = sql_select("L.$primary AS id",
764
+                "$table_lien AS L LEFT JOIN $table_source AS O ON L.$primary=O.$primary",
765
+                $where);
766
+            $dels += optimiser_sansref($table_lien, $primary, $res);
767
+        }
768
+    }
769
+
770
+    return ($echec ? false : $dels);
771 771
 }
772 772
 
773 773
 
@@ -799,89 +799,89 @@  discard block
 block discarded – undo
799 799
  *     Nombre de modifications faites, false si échec.
800 800
  */
801 801
 function lien_set($objet_source, $primary, $table_lien, $id, $objets, $qualif) {
802
-	$echec = null;
803
-	$ok = 0;
804
-	$reordonner = false;
805
-	if (!$qualif) {
806
-		return false;
807
-	}
808
-	// nettoyer qualif qui peut venir directement d'un objet_trouver_lien :
809
-	unset($qualif[$primary]);
810
-	unset($qualif[$objet_source]);
811
-	if (isset($qualif['objet'])) {
812
-		unset($qualif[$qualif['objet']]);
813
-	}
814
-	unset($qualif['objet']);
815
-	unset($qualif['id_objet']);
816
-	foreach ($objets as $objet => $id_objets) {
817
-
818
-		// role, colonne, where par défaut
819
-		list($role, $colonne_role, $cond) =
820
-			roles_trouver_dans_qualif($objet_source, $objet, $qualif);
821
-
822
-		$objet = ($objet == '*') ? $objet : objet_type($objet); # securite
823
-		if (!is_array($id_objets) or reset($id_objets) == "NOT") {
824
-			$id_objets = array($id_objets);
825
-		}
826
-		foreach ($id_objets as $id_objet) {
827
-
828
-			$args = array(
829
-				'table_lien' => $table_lien,
830
-				'objet_source' => $objet_source,
831
-				'id_objet_source' => $id,
832
-				'objet' => $objet,
833
-				'id_objet' => $id_objet,
834
-				'role' => $role,
835
-				'colonne_role' => $colonne_role,
836
-				'action' => 'modifier',
837
-			);
838
-
839
-			// Envoyer aux plugins
840
-			$qualif = pipeline('pre_edition_lien',
841
-				array(
842
-					'args' => $args,
843
-					'data' => $qualif,
844
-				)
845
-			);
846
-			$args['id_objet'] = $id_objet;
847
-
848
-			if (lien_triables($table_lien) and isset($qualif['rang_lien'])) {
849
-				if (intval($qualif['rang_lien'])) {
850
-					// on decale les liens de rang_lien>=la valeur inseree pour faire la place
851
-					$w = lien_where($primary, '*', $objet, $id_objet, array('rang_lien>='.intval($qualif['rang_lien']),"$primary!=".intval($id)));
852
-					sql_update($table_lien, array('rang_lien'=>'rang_lien+1'), $w);
853
-				}
854
-				// tous les liens de même rôle recoivent le rang indiqué aussi
855
-				if (roles_colonne($objet_source, $objet)) {
856
-					$w = lien_where($primary, $id, $objet, $id_objet);
857
-					sql_updateq($table_lien, array('rang_lien' => intval($qualif['rang_lien'])), $w);
858
-				}
859
-				$reordonner = true;
860
-			}
861
-
862
-			$where = lien_where($primary, $id, $objet, $id_objet, $cond);
863
-			$e = sql_updateq($table_lien, $qualif, $where);
864
-
865
-			if ($e === false) {
866
-				$echec = true;
867
-			} else {
868
-				// Envoyer aux plugins
869
-				pipeline('post_edition_lien',
870
-					array(
871
-						'args' => $args,
872
-						'data' => $qualif
873
-					)
874
-				);
875
-				$ok++;
876
-			}
877
-		}
878
-	}
879
-	// si on a fait des modif de rang, on reordonne les liens concernes
880
-	if ($reordonner) {
881
-		lien_ordonner($objet_source, $primary, $table_lien, $id, $objets);
882
-	}
883
-
884
-	return ($echec ? false : $ok);
802
+    $echec = null;
803
+    $ok = 0;
804
+    $reordonner = false;
805
+    if (!$qualif) {
806
+        return false;
807
+    }
808
+    // nettoyer qualif qui peut venir directement d'un objet_trouver_lien :
809
+    unset($qualif[$primary]);
810
+    unset($qualif[$objet_source]);
811
+    if (isset($qualif['objet'])) {
812
+        unset($qualif[$qualif['objet']]);
813
+    }
814
+    unset($qualif['objet']);
815
+    unset($qualif['id_objet']);
816
+    foreach ($objets as $objet => $id_objets) {
817
+
818
+        // role, colonne, where par défaut
819
+        list($role, $colonne_role, $cond) =
820
+            roles_trouver_dans_qualif($objet_source, $objet, $qualif);
821
+
822
+        $objet = ($objet == '*') ? $objet : objet_type($objet); # securite
823
+        if (!is_array($id_objets) or reset($id_objets) == "NOT") {
824
+            $id_objets = array($id_objets);
825
+        }
826
+        foreach ($id_objets as $id_objet) {
827
+
828
+            $args = array(
829
+                'table_lien' => $table_lien,
830
+                'objet_source' => $objet_source,
831
+                'id_objet_source' => $id,
832
+                'objet' => $objet,
833
+                'id_objet' => $id_objet,
834
+                'role' => $role,
835
+                'colonne_role' => $colonne_role,
836
+                'action' => 'modifier',
837
+            );
838
+
839
+            // Envoyer aux plugins
840
+            $qualif = pipeline('pre_edition_lien',
841
+                array(
842
+                    'args' => $args,
843
+                    'data' => $qualif,
844
+                )
845
+            );
846
+            $args['id_objet'] = $id_objet;
847
+
848
+            if (lien_triables($table_lien) and isset($qualif['rang_lien'])) {
849
+                if (intval($qualif['rang_lien'])) {
850
+                    // on decale les liens de rang_lien>=la valeur inseree pour faire la place
851
+                    $w = lien_where($primary, '*', $objet, $id_objet, array('rang_lien>='.intval($qualif['rang_lien']),"$primary!=".intval($id)));
852
+                    sql_update($table_lien, array('rang_lien'=>'rang_lien+1'), $w);
853
+                }
854
+                // tous les liens de même rôle recoivent le rang indiqué aussi
855
+                if (roles_colonne($objet_source, $objet)) {
856
+                    $w = lien_where($primary, $id, $objet, $id_objet);
857
+                    sql_updateq($table_lien, array('rang_lien' => intval($qualif['rang_lien'])), $w);
858
+                }
859
+                $reordonner = true;
860
+            }
861
+
862
+            $where = lien_where($primary, $id, $objet, $id_objet, $cond);
863
+            $e = sql_updateq($table_lien, $qualif, $where);
864
+
865
+            if ($e === false) {
866
+                $echec = true;
867
+            } else {
868
+                // Envoyer aux plugins
869
+                pipeline('post_edition_lien',
870
+                    array(
871
+                        'args' => $args,
872
+                        'data' => $qualif
873
+                    )
874
+                );
875
+                $ok++;
876
+            }
877
+        }
878
+    }
879
+    // si on a fait des modif de rang, on reordonne les liens concernes
880
+    if ($reordonner) {
881
+        lien_ordonner($objet_source, $primary, $table_lien, $id, $objets);
882
+    }
883
+
884
+    return ($echec ? false : $ok);
885 885
 }
886 886
 
887 887
 /**
@@ -911,23 +911,23 @@  discard block
 block discarded – undo
911 911
  * @return array
912 912
  */
913 913
 function lien_find($objet_source, $primary, $table_lien, $id, $objets, $cond = null) {
914
-	$trouve = array();
915
-	foreach ($objets as $objet => $id_objets) {
916
-		$objet = ($objet == '*') ? $objet : objet_type($objet); # securite
917
-		// gerer les roles s'il y en a dans $cond
918
-		list($cond) = roles_creer_condition_role($objet_source, $objet, $cond, true);
919
-		// lien_where prend en charge les $id_objets sous forme int ou array
920
-		$where = lien_where($primary, $id, $objet, $id_objets, $cond);
921
-		$liens = sql_allfetsel('*', $table_lien, $where);
922
-		// ajouter les entrees objet_source et objet cible par convenance
923
-		foreach ($liens as $l) {
924
-			$l[$objet_source] = $l[$primary];
925
-			$l[$l['objet']] = $l['id_objet'];
926
-			$trouve[] = $l;
927
-		}
928
-	}
929
-
930
-	return $trouve;
914
+    $trouve = array();
915
+    foreach ($objets as $objet => $id_objets) {
916
+        $objet = ($objet == '*') ? $objet : objet_type($objet); # securite
917
+        // gerer les roles s'il y en a dans $cond
918
+        list($cond) = roles_creer_condition_role($objet_source, $objet, $cond, true);
919
+        // lien_where prend en charge les $id_objets sous forme int ou array
920
+        $where = lien_where($primary, $id, $objet, $id_objets, $cond);
921
+        $liens = sql_allfetsel('*', $table_lien, $where);
922
+        // ajouter les entrees objet_source et objet cible par convenance
923
+        foreach ($liens as $l) {
924
+            $l[$objet_source] = $l[$primary];
925
+            $l[$l['objet']] = $l['id_objet'];
926
+            $trouve[] = $l;
927
+        }
928
+    }
929
+
930
+    return $trouve;
931 931
 }
932 932
 
933 933
 /**
@@ -938,25 +938,25 @@  discard block
 block discarded – undo
938 938
  * @param array|int $ids
939 939
  */
940 940
 function lien_propage_date_modif($objet, $ids) {
941
-	static $done = array();
942
-	$hash = md5($objet . serialize($ids));
943
-
944
-	// sql_updateq, peut être un rien lent.
945
-	// On évite de l'appeler 2 fois sur les mêmes choses
946
-	if (isset($done[$hash])) {
947
-		return;
948
-	}
949
-
950
-	$trouver_table = charger_fonction('trouver_table', 'base');
951
-
952
-	$table = table_objet_sql($objet);
953
-	if ($desc = $trouver_table($table)
954
-		and isset($desc['field']['date_modif'])
955
-	) {
956
-		$primary = id_table_objet($objet);
957
-		$where = (is_array($ids) ? sql_in($primary, array_map('intval', $ids)) : "$primary=" . intval($ids));
958
-		sql_updateq($table, array('date_modif' => date('Y-m-d H:i:s')), $where);
959
-	}
960
-
961
-	$done[$hash] = true;
941
+    static $done = array();
942
+    $hash = md5($objet . serialize($ids));
943
+
944
+    // sql_updateq, peut être un rien lent.
945
+    // On évite de l'appeler 2 fois sur les mêmes choses
946
+    if (isset($done[$hash])) {
947
+        return;
948
+    }
949
+
950
+    $trouver_table = charger_fonction('trouver_table', 'base');
951
+
952
+    $table = table_objet_sql($objet);
953
+    if ($desc = $trouver_table($table)
954
+        and isset($desc['field']['date_modif'])
955
+    ) {
956
+        $primary = id_table_objet($objet);
957
+        $where = (is_array($ids) ? sql_in($primary, array_map('intval', $ids)) : "$primary=" . intval($ids));
958
+        sql_updateq($table, array('date_modif' => date('Y-m-d H:i:s')), $where);
959
+    }
960
+
961
+    $done[$hash] = true;
962 962
 }
Please login to merge, or discard this patch.
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
 
53 53
 	$l = "";
54 54
 	if ($primary = id_table_objet($objet)
55
-		and $trouver_table($l = $table_sql . "_liens")
55
+		and $trouver_table($l = $table_sql."_liens")
56 56
 		and !preg_match(',[^\w],', $primary)
57 57
 		and !preg_match(',[^\w],', $l)
58 58
 	) {
@@ -211,7 +211,7 @@  discard block
 block discarded – undo
211 211
  * @return int
212 212
  */
213 213
 function objet_optimiser_liens($objets_source, $objets_lies) {
214
-	spip_log("objet_optimiser_liens : ".json_encode($objets_source) . ', ' . json_encode($objets_lies), 'genie'._LOG_DEBUG);
214
+	spip_log("objet_optimiser_liens : ".json_encode($objets_source).', '.json_encode($objets_lies), 'genie'._LOG_DEBUG);
215 215
 	return objet_traiter_liaisons('lien_optimise', $objets_source, $objets_lies);
216 216
 }
217 217
 
@@ -401,7 +401,7 @@  discard block
 block discarded – undo
401 401
 					$where = lien_where($primary, '*', $objet, $id_objet);
402 402
 					$rang = intval(sql_getfetsel('max(rang_lien)', $table_lien, $where));
403 403
 					// si aucun lien n'a de rang, on en introduit pas, on garde zero
404
-					if ($rang>0) {
404
+					if ($rang > 0) {
405 405
 						$rang = intval($rang) + 1;
406 406
 					}
407 407
 				}
@@ -436,7 +436,7 @@  discard block
 block discarded – undo
436 436
 
437 437
 				if (lien_triables($table_lien) and isset($insertions['rang_lien']) and intval($insertions['rang_lien'])) {
438 438
 					// on decale les liens de rang_lien>=la valeur inseree pour faire la place
439
-					$w = lien_where($primary, '*', $objet, $id_objet, array('rang_lien>='.intval($insertions['rang_lien']),"$primary!=".intval($id)));
439
+					$w = lien_where($primary, '*', $objet, $id_objet, array('rang_lien>='.intval($insertions['rang_lien']), "$primary!=".intval($id)));
440 440
 					sql_update($table_lien, array('rang_lien'=>'rang_lien+1'), $w);
441 441
 				}
442 442
 
@@ -459,7 +459,7 @@  discard block
 block discarded – undo
459 459
 		}
460 460
 	}
461 461
 	// si on a fait des insertions, on reordonne les liens concernes
462
-	if ($ins>0) {
462
+	if ($ins > 0) {
463 463
 		lien_ordonner($objet_source, $primary, $table_lien, $id, $objets);
464 464
 	}
465 465
 
@@ -489,10 +489,10 @@  discard block
 block discarded – undo
489 489
 			$objet = (($objet == '*') ? $objet : objet_type($objet)); # securite
490 490
 
491 491
 			$where = lien_where($primary, '*', $objet, $id_objet);
492
-			$liens = sql_allfetsel("$primary, id_objet, objet, rang_lien", $table_lien, $where, $primary,"rang_lien");
492
+			$liens = sql_allfetsel("$primary, id_objet, objet, rang_lien", $table_lien, $where, $primary, "rang_lien");
493 493
 
494 494
 			$rangs = array_column($liens, 'rang_lien');
495
-			if (count($rangs) and (max($rangs)>0 or min($rangs)<0)) {
495
+			if (count($rangs) and (max($rangs) > 0 or min($rangs) < 0)) {
496 496
 				$rang = 1;
497 497
 				foreach ($liens as $lien) {
498 498
 					$where = lien_where($primary, $lien[$primary], $objet, $id_objet, array('rang_lien!='.intval($rang)));
@@ -556,7 +556,7 @@  discard block
 block discarded – undo
556 556
 
557 557
 	if ($id_source !== '*') {
558 558
 		$where[] = (is_array($id_source) ? sql_in(addslashes($primary), array_map('intval', $id_source),
559
-			$not) : addslashes($primary) . ($not ? "<>" : "=") . intval($id_source));
559
+			$not) : addslashes($primary).($not ? "<>" : "=").intval($id_source));
560 560
 	} elseif ($not) {
561 561
 		$where[] = "0=1";
562 562
 	} // idiot mais quand meme
@@ -568,11 +568,11 @@  discard block
 block discarded – undo
568 568
 	}
569 569
 
570 570
 	if ($objet !== '*') {
571
-		$where[] = "objet=" . sql_quote($objet);
571
+		$where[] = "objet=".sql_quote($objet);
572 572
 	}
573 573
 	if ($id_objet !== '*') {
574 574
 		$where[] = (is_array($id_objet) ? sql_in('id_objet', array_map('intval', $id_objet),
575
-			$not) : "id_objet" . ($not ? "<>" : "=") . intval($id_objet));
575
+			$not) : "id_objet".($not ? "<>" : "=").intval($id_objet));
576 576
 	} elseif ($not) {
577 577
 		$where[] = "0=1";
578 578
 	} // idiot mais quand meme
@@ -735,19 +735,19 @@  discard block
 block discarded – undo
735 735
 					// il faut les eliminier en repetant la condition dans le where L.objet='xxx'
736 736
 					"$table_lien AS L
737 737
 									LEFT JOIN $spip_table_objet AS O
738
-										ON (O.$id_table_objet=L.id_objet AND L.objet=" . sql_quote($type) . ")",
739
-					"L.objet=" . sql_quote($type) . " AND O.$id_table_objet IS NULL");
738
+										ON (O.$id_table_objet=L.id_objet AND L.objet=".sql_quote($type).")",
739
+					"L.objet=".sql_quote($type)." AND O.$id_table_objet IS NULL");
740 740
 				// sur une cle primaire composee, pas d'autres solutions que de virer un a un
741 741
 				while ($row = sql_fetch($res)) {
742 742
 					if ($primary === 'id_document' and in_array($type, ['site', 'rubrique']) and !intval($row['id_objet'])) {
743 743
 						continue; // gaffe, c'est le logo du site ou des rubriques!
744 744
 					}
745 745
 					$e = sql_delete($table_lien,
746
-						array("$primary=" . $row['id'], "id_objet=" . $row['id_objet'], "objet=" . sql_quote($type)));
746
+						array("$primary=".$row['id'], "id_objet=".$row['id_objet'], "objet=".sql_quote($type)));
747 747
 					if ($e != false) {
748 748
 						$dels += $e;
749 749
 						spip_log(
750
-							"lien_optimise: Entree " . $row['id'] . "/" . $row['id_objet'] . "/$type supprimee dans la table $table_lien",
750
+							"lien_optimise: Entree ".$row['id']."/".$row['id_objet']."/$type supprimee dans la table $table_lien",
751 751
 							'genie'._LOG_INFO_IMPORTANTE
752 752
 						);
753 753
 					}
@@ -848,7 +848,7 @@  discard block
 block discarded – undo
848 848
 			if (lien_triables($table_lien) and isset($qualif['rang_lien'])) {
849 849
 				if (intval($qualif['rang_lien'])) {
850 850
 					// on decale les liens de rang_lien>=la valeur inseree pour faire la place
851
-					$w = lien_where($primary, '*', $objet, $id_objet, array('rang_lien>='.intval($qualif['rang_lien']),"$primary!=".intval($id)));
851
+					$w = lien_where($primary, '*', $objet, $id_objet, array('rang_lien>='.intval($qualif['rang_lien']), "$primary!=".intval($id)));
852 852
 					sql_update($table_lien, array('rang_lien'=>'rang_lien+1'), $w);
853 853
 				}
854 854
 				// tous les liens de même rôle recoivent le rang indiqué aussi
@@ -939,7 +939,7 @@  discard block
 block discarded – undo
939 939
  */
940 940
 function lien_propage_date_modif($objet, $ids) {
941 941
 	static $done = array();
942
-	$hash = md5($objet . serialize($ids));
942
+	$hash = md5($objet.serialize($ids));
943 943
 
944 944
 	// sql_updateq, peut être un rien lent.
945 945
 	// On évite de l'appeler 2 fois sur les mêmes choses
@@ -954,7 +954,7 @@  discard block
 block discarded – undo
954 954
 		and isset($desc['field']['date_modif'])
955 955
 	) {
956 956
 		$primary = id_table_objet($objet);
957
-		$where = (is_array($ids) ? sql_in($primary, array_map('intval', $ids)) : "$primary=" . intval($ids));
957
+		$where = (is_array($ids) ? sql_in($primary, array_map('intval', $ids)) : "$primary=".intval($ids));
958 958
 		sql_updateq($table, array('date_modif' => date('Y-m-d H:i:s')), $where);
959 959
 	}
960 960
 
Please login to merge, or discard this patch.
ecrire/genie/optimiser.php 2 patches
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
  **/
18 18
 
19 19
 if (!defined('_ECRIRE_INC_VERSION')) {
20
-	return;
20
+    return;
21 21
 }
22 22
 
23 23
 include_spip('base/abstract_sql');
@@ -35,14 +35,14 @@  discard block
 block discarded – undo
35 35
  **/
36 36
 function genie_optimiser_dist($t) {
37 37
 
38
-	optimiser_base_une_table();
39
-	optimiser_base();
38
+    optimiser_base_une_table();
39
+    optimiser_base();
40 40
 
41
-	// la date souhaitee pour le tour suivant = apres-demain a 4h du mat ;
42
-	// sachant qu'on a un delai de 48h, on renvoie aujourd'hui a 4h du mat
43
-	// avec une periode de flou entre 2h et 6h pour ne pas saturer un hebergeur
44
-	// qui aurait beaucoup de sites SPIP
45
-	return -(mktime(2, 0, 0) + rand(0, 3600 * 4));
41
+    // la date souhaitee pour le tour suivant = apres-demain a 4h du mat ;
42
+    // sachant qu'on a un delai de 48h, on renvoie aujourd'hui a 4h du mat
43
+    // avec une periode de flou entre 2h et 6h pour ne pas saturer un hebergeur
44
+    // qui aurait beaucoup de sites SPIP
45
+    return -(mktime(2, 0, 0) + rand(0, 3600 * 4));
46 46
 }
47 47
 
48 48
 /**
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
  * @return void
58 58
  **/
59 59
 function optimiser_base($attente = 86400) {
60
-	optimiser_base_disparus($attente);
60
+    optimiser_base_disparus($attente);
61 61
 }
62 62
 
63 63
 
@@ -74,28 +74,28 @@  discard block
 block discarded – undo
74 74
  **/
75 75
 function optimiser_base_une_table() {
76 76
 
77
-	$tables = array();
78
-	$result = sql_showbase();
79
-
80
-	// on n'optimise qu'une seule table a chaque fois,
81
-	// pour ne pas vautrer le systeme
82
-	// lire http://dev.mysql.com/doc/refman/5.0/fr/optimize-table.html
83
-	while ($row = sql_fetch($result)) {
84
-		$tables[] = array_shift($row);
85
-	}
86
-
87
-	spip_log("optimiser_base_une_table ".json_encode($tables), 'genie'._LOG_DEBUG);
88
-	if ($tables) {
89
-		$table_op = intval(lire_config('optimiser_table', 0) + 1) % sizeof($tables);
90
-		ecrire_config('optimiser_table', $table_op);
91
-		$q = $tables[$table_op];
92
-		spip_log("optimiser_base_une_table : debut d'optimisation de la table $q", 'genie'._LOG_DEBUG);
93
-		if (sql_optimize($q)) {
94
-			spip_log("optimiser_base_une_table : fin d'optimisation de la table $q", 'genie'._LOG_DEBUG);
95
-		} else {
96
-			spip_log("optimiser_base_une_table : Pas d'optimiseur necessaire", 'genie'._LOG_DEBUG);
97
-		}
98
-	}
77
+    $tables = array();
78
+    $result = sql_showbase();
79
+
80
+    // on n'optimise qu'une seule table a chaque fois,
81
+    // pour ne pas vautrer le systeme
82
+    // lire http://dev.mysql.com/doc/refman/5.0/fr/optimize-table.html
83
+    while ($row = sql_fetch($result)) {
84
+        $tables[] = array_shift($row);
85
+    }
86
+
87
+    spip_log("optimiser_base_une_table ".json_encode($tables), 'genie'._LOG_DEBUG);
88
+    if ($tables) {
89
+        $table_op = intval(lire_config('optimiser_table', 0) + 1) % sizeof($tables);
90
+        ecrire_config('optimiser_table', $table_op);
91
+        $q = $tables[$table_op];
92
+        spip_log("optimiser_base_une_table : debut d'optimisation de la table $q", 'genie'._LOG_DEBUG);
93
+        if (sql_optimize($q)) {
94
+            spip_log("optimiser_base_une_table : fin d'optimisation de la table $q", 'genie'._LOG_DEBUG);
95
+        } else {
96
+            spip_log("optimiser_base_une_table : Pas d'optimiseur necessaire", 'genie'._LOG_DEBUG);
97
+        }
98
+    }
99 99
 }
100 100
 
101 101
 
@@ -121,18 +121,18 @@  discard block
 block discarded – undo
121 121
  *     Nombre de suppressions
122 122
  **/
123 123
 function optimiser_sansref($table, $id, $sel, $and = '') {
124
-	$in = array();
125
-	while ($row = sql_fetch($sel)) {
126
-		$in[$row['id']] = true;
127
-	}
128
-	sql_free($sel);
129
-
130
-	if ($in) {
131
-		sql_delete($table, sql_in($id, array_keys($in)) . ($and ? " AND $and" : ''));
132
-		spip_log("optimiser_sansref: Numeros des entrees $id supprimees dans la table $table: " . implode(', ', array_keys($in)), 'genie'._LOG_DEBUG);
133
-	}
134
-
135
-	return count($in);
124
+    $in = array();
125
+    while ($row = sql_fetch($sel)) {
126
+        $in[$row['id']] = true;
127
+    }
128
+    sql_free($sel);
129
+
130
+    if ($in) {
131
+        sql_delete($table, sql_in($id, array_keys($in)) . ($and ? " AND $and" : ''));
132
+        spip_log("optimiser_sansref: Numeros des entrees $id supprimees dans la table $table: " . implode(', ', array_keys($in)), 'genie'._LOG_DEBUG);
133
+    }
134
+
135
+    return count($in);
136 136
 }
137 137
 
138 138
 
@@ -154,73 +154,73 @@  discard block
 block discarded – undo
154 154
  **/
155 155
 function optimiser_base_disparus($attente = 86400) {
156 156
 
157
-	# format = 20060610110141, si on veut forcer une optimisation tout de suite
158
-	$mydate = sql_quote(date("Y-m-d H:i:s", time() - $attente));
157
+    # format = 20060610110141, si on veut forcer une optimisation tout de suite
158
+    $mydate = sql_quote(date("Y-m-d H:i:s", time() - $attente));
159 159
 
160
-	$n = 0;
160
+    $n = 0;
161 161
 
162
-	//
163
-	// Rubriques 
164
-	//
162
+    //
163
+    // Rubriques 
164
+    //
165 165
 
166
-	# les articles qui sont dans une id_rubrique inexistante
167
-	# attention on controle id_rubrique>0 pour ne pas tuer les articles
168
-	# specialement affectes a une rubrique non-existante (plugin,
169
-	# cf. https://core.spip.net/issues/1549 )
170
-	$res = sql_select("A.id_article AS id",
171
-		"spip_articles AS A
166
+    # les articles qui sont dans une id_rubrique inexistante
167
+    # attention on controle id_rubrique>0 pour ne pas tuer les articles
168
+    # specialement affectes a une rubrique non-existante (plugin,
169
+    # cf. https://core.spip.net/issues/1549 )
170
+    $res = sql_select("A.id_article AS id",
171
+        "spip_articles AS A
172 172
 		        LEFT JOIN spip_rubriques AS R
173 173
 		          ON A.id_rubrique=R.id_rubrique",
174
-		"A.id_rubrique > 0
174
+        "A.id_rubrique > 0
175 175
 			 AND R.id_rubrique IS NULL
176 176
 		         AND A.maj < $mydate");
177 177
 
178
-	$n += optimiser_sansref('spip_articles', 'id_article', $res);
178
+    $n += optimiser_sansref('spip_articles', 'id_article', $res);
179 179
 
180
-	// les articles a la poubelle
181
-	sql_delete("spip_articles", "statut='poubelle' AND maj < $mydate");
180
+    // les articles a la poubelle
181
+    sql_delete("spip_articles", "statut='poubelle' AND maj < $mydate");
182 182
 
183
-	//
184
-	// Auteurs
185
-	//
183
+    //
184
+    // Auteurs
185
+    //
186 186
 
187
-	include_spip('action/editer_liens');
188
-	// optimiser les liens de tous les auteurs vers des objets effaces
189
-	// et depuis des auteurs effaces
190
-	$n += objet_optimiser_liens(array('auteur' => '*'), '*');
187
+    include_spip('action/editer_liens');
188
+    // optimiser les liens de tous les auteurs vers des objets effaces
189
+    // et depuis des auteurs effaces
190
+    $n += objet_optimiser_liens(array('auteur' => '*'), '*');
191 191
 
192
-	# effacer les auteurs poubelle qui ne sont lies a rien
193
-	$res = sql_select("A.id_auteur AS id",
194
-		"spip_auteurs AS A
192
+    # effacer les auteurs poubelle qui ne sont lies a rien
193
+    $res = sql_select("A.id_auteur AS id",
194
+        "spip_auteurs AS A
195 195
 		      	LEFT JOIN spip_auteurs_liens AS L
196 196
 		          ON L.id_auteur=A.id_auteur",
197
-		"L.id_auteur IS NULL
197
+        "L.id_auteur IS NULL
198 198
 		       	AND A.statut='5poubelle' AND A.maj < $mydate");
199 199
 
200
-	$n += optimiser_sansref('spip_auteurs', 'id_auteur', $res);
201
-
202
-	# supprimer les auteurs 'nouveau' qui n'ont jamais donne suite
203
-	# au mail de confirmation (45 jours pour repondre, ca devrait suffire)
204
-	sql_delete("spip_auteurs", "statut='nouveau' AND maj < " . sql_quote(date('Y-m-d', time() - 45 * 24 * 3600)));
205
-
206
-	/**
207
-	 * Permet aux plugins de compléter l'optimisation suite aux éléments disparus
208
-	 *
209
-	 * L'index 'data' est un entier indiquant le nombre d'optimisations
210
-	 * qui ont été réalisées (par exemple le nombre de suppressions faites)
211
-	 * et qui doit être incrémenté par les fonctions
212
-	 * utilisant ce pipeline si elles suppriment des éléments.
213
-	 *
214
-	 * @pipeline_appel optimiser_base_disparus
215
-	 */
216
-	$n = pipeline('optimiser_base_disparus', array(
217
-		'args' => array(
218
-			'attente' => $attente,
219
-			'date' => $mydate
220
-		),
221
-		'data' => $n
222
-	));
223
-
224
-
225
-	spip_log("optimiser_base_disparus : {$n} lien(s) mort(s)", 'genie'._LOG_DEBUG);
200
+    $n += optimiser_sansref('spip_auteurs', 'id_auteur', $res);
201
+
202
+    # supprimer les auteurs 'nouveau' qui n'ont jamais donne suite
203
+    # au mail de confirmation (45 jours pour repondre, ca devrait suffire)
204
+    sql_delete("spip_auteurs", "statut='nouveau' AND maj < " . sql_quote(date('Y-m-d', time() - 45 * 24 * 3600)));
205
+
206
+    /**
207
+     * Permet aux plugins de compléter l'optimisation suite aux éléments disparus
208
+     *
209
+     * L'index 'data' est un entier indiquant le nombre d'optimisations
210
+     * qui ont été réalisées (par exemple le nombre de suppressions faites)
211
+     * et qui doit être incrémenté par les fonctions
212
+     * utilisant ce pipeline si elles suppriment des éléments.
213
+     *
214
+     * @pipeline_appel optimiser_base_disparus
215
+     */
216
+    $n = pipeline('optimiser_base_disparus', array(
217
+        'args' => array(
218
+            'attente' => $attente,
219
+            'date' => $mydate
220
+        ),
221
+        'data' => $n
222
+    ));
223
+
224
+
225
+    spip_log("optimiser_base_disparus : {$n} lien(s) mort(s)", 'genie'._LOG_DEBUG);
226 226
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -128,8 +128,8 @@  discard block
 block discarded – undo
128 128
 	sql_free($sel);
129 129
 
130 130
 	if ($in) {
131
-		sql_delete($table, sql_in($id, array_keys($in)) . ($and ? " AND $and" : ''));
132
-		spip_log("optimiser_sansref: Numeros des entrees $id supprimees dans la table $table: " . implode(', ', array_keys($in)), 'genie'._LOG_DEBUG);
131
+		sql_delete($table, sql_in($id, array_keys($in)).($and ? " AND $and" : ''));
132
+		spip_log("optimiser_sansref: Numeros des entrees $id supprimees dans la table $table: ".implode(', ', array_keys($in)), 'genie'._LOG_DEBUG);
133 133
 	}
134 134
 
135 135
 	return count($in);
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
 
202 202
 	# supprimer les auteurs 'nouveau' qui n'ont jamais donne suite
203 203
 	# au mail de confirmation (45 jours pour repondre, ca devrait suffire)
204
-	sql_delete("spip_auteurs", "statut='nouveau' AND maj < " . sql_quote(date('Y-m-d', time() - 45 * 24 * 3600)));
204
+	sql_delete("spip_auteurs", "statut='nouveau' AND maj < ".sql_quote(date('Y-m-d', time() - 45 * 24 * 3600)));
205 205
 
206 206
 	/**
207 207
 	 * Permet aux plugins de compléter l'optimisation suite aux éléments disparus
Please login to merge, or discard this patch.