Completed
Push — master ( 2d0edb...c1ab59 )
by cam
04:37
created
ecrire/inc/recherche_to_array.php 2 patches
Indentation   +233 added lines, -233 removed lines patch added patch discarded remove patch
@@ -12,243 +12,243 @@
 block discarded – undo
12 12
 
13 13
 
14 14
 if (!defined('_ECRIRE_INC_VERSION')) {
15
-	return;
15
+    return;
16 16
 }
17 17
 
18 18
 
19 19
 // methodes sql
20 20
 function inc_recherche_to_array_dist($recherche, $options = array()) {
21 21
 
22
-	// options par defaut
23
-	$options = array_merge(
24
-		array(
25
-			'score' => true,
26
-			'champs' => false,
27
-			'toutvoir' => false,
28
-			'matches' => false,
29
-			'jointures' => false
30
-		),
31
-		$options
32
-	);
33
-
34
-	include_spip('inc/rechercher');
35
-	include_spip('inc/autoriser');
36
-
37
-	$requete = array(
38
-		"SELECT" => array(),
39
-		"FROM" => array(),
40
-		"WHERE" => array(),
41
-		"GROUPBY" => array(),
42
-		"ORDERBY" => array(),
43
-		"LIMIT" => "",
44
-		"HAVING" => array()
45
-	);
46
-
47
-	$table = sinon($options['table'], 'article');
48
-	if ($options['champs']) {
49
-		$champs = $options['champs'];
50
-	} else {
51
-		$l = liste_des_champs();
52
-		$champs = $l['article'];
53
-	}
54
-	$serveur = $options['serveur'];
55
-
56
-	list($methode, $q, $preg) = expression_recherche($recherche, $options);
57
-
58
-	$jointures = $options['jointures']
59
-		? liste_des_jointures()
60
-		: array();
61
-
62
-	$_id_table = id_table_objet($table);
63
-
64
-	// c'est un pis-aller : ca a peu de chance de marcher, mais mieux quand meme que en conservant la ','
65
-	// (aka ca marche au moins dans certains cas comme avec spip_formulaires_reponses_champs)
66
-	if (strpos($_id_table, ",") !== false) {
67
-		$_id_table = explode(',', $_id_table);
68
-		$_id_table = reset($_id_table);
69
-	}
70
-
71
-	$requete['SELECT'][] = "t." . $_id_table;
72
-	$a = array();
73
-	// Recherche fulltext
74
-	foreach ($champs as $champ => $poids) {
75
-		if (is_array($champ)) {
76
-			spip_log("requetes imbriquees interdites");
77
-		} else {
78
-			if (strpos($champ, ".") === false) {
79
-				$champ = "t.$champ";
80
-			}
81
-			$requete['SELECT'][] = $champ;
82
-			$a[] = $champ . ' ' . $methode . ' ' . $q;
83
-		}
84
-	}
85
-	if ($a) {
86
-		$requete['WHERE'][] = join(" OR ", $a);
87
-	}
88
-	$requete['FROM'][] = table_objet_sql($table) . ' AS t';
89
-
90
-	$results = array();
91
-
92
-	$s = sql_select(
93
-		$requete['SELECT'], $requete['FROM'], $requete['WHERE'],
94
-		implode(" ", $requete['GROUPBY']),
95
-		$requete['ORDERBY'], $requete['LIMIT'],
96
-		$requete['HAVING'], $serveur
97
-	);
98
-
99
-	while ($t = sql_fetch($s, $serveur)
100
-		and (!isset($t['score']) or $t['score'] > 0)) {
101
-		$id = intval($t[$_id_table]);
102
-
103
-		if ($options['toutvoir']
104
-			or autoriser('voir', $table, $id)
105
-		) {
106
-			// indiquer les champs concernes
107
-			$champs_vus = array();
108
-			$score = 0;
109
-			$matches = array();
110
-
111
-			$vu = false;
112
-			foreach ($champs as $champ => $poids) {
113
-				$champ = explode('.', $champ);
114
-				$champ = end($champ);
115
-				// translitteration_rapide uniquement si on est deja en utf-8
116
-				$value = ($GLOBALS['meta']['charset'] == 'utf-8' ? translitteration_rapide($t[$champ]) : translitteration($t[$champ]));
117
-				if ($n =
118
-					($options['score'] || $options['matches'])
119
-						? preg_match_all($preg, $value, $regs, PREG_SET_ORDER)
120
-						: preg_match($preg, $value)
121
-				) {
122
-					$vu = true;
123
-
124
-					if ($options['champs']) {
125
-						$champs_vus[$champ] = $t[$champ];
126
-					}
127
-					if ($options['score']) {
128
-						// compter les points avec un peu de discernement : on pondere par la longueur du match compte en chars
129
-						$score += $poids * strlen(implode('',array_column($regs, 0)));
130
-					}
131
-
132
-					if ($options['matches']) {
133
-						$matches[$champ] = $regs;
134
-					}
135
-
136
-					if (!$options['champs']
137
-						and !$options['score']
138
-						and !$options['matches']
139
-					) {
140
-						break;
141
-					}
142
-				}
143
-			}
144
-
145
-			if ($vu) {
146
-				if (!isset($results)) {
147
-					$results = array();
148
-				}
149
-				$results[$id] = array();
150
-				if ($champs_vus) {
151
-					$results[$id]['champs'] = $champs_vus;
152
-				}
153
-				if ($score) {
154
-					$results[$id]['score'] = $score;
155
-				}
156
-				if ($matches) {
157
-					$results[$id]['matches'] = $matches;
158
-				}
159
-			}
160
-		}
161
-	}
162
-
163
-
164
-	// Gerer les donnees associees
165
-	// ici on est un peu naze : pas capables de reconstruire une jointure complexe
166
-	// on ne sait passer que par table de laison en 1 coup
167
-	if (isset($jointures[$table])
168
-		and $joints = recherche_en_base(
169
-			$recherche,
170
-			$jointures[$table],
171
-			array_merge($options, array('jointures' => false))
172
-		)
173
-	) {
174
-		include_spip('action/editer_liens');
175
-		$trouver_table = charger_fonction('trouver_table', 'base');
176
-		$cle_depart = id_table_objet($table);
177
-		$table_depart = table_objet($table, $serveur);
178
-		$desc_depart = $trouver_table($table_depart, $serveur);
179
-		$depart_associable = objet_associable($table);
180
-		foreach ($joints as $table_liee => $ids_trouves) {
181
-			// on peut definir une fonction de recherche jointe pour regler les cas particuliers
182
-			if (
183
-			!(
184
-				$rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}", "inc", true)
185
-				or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}", "inc", true)
186
-				or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie", "inc", true)
187
-			)
188
-			) {
189
-				$cle_arrivee = id_table_objet($table_liee);
190
-				$table_arrivee = table_objet($table_liee, $serveur);
191
-				$desc_arrivee = $trouver_table($table_arrivee, $serveur);
192
-				// cas simple : $cle_depart dans la table_liee
193
-				if (isset($desc_arrivee['field'][$cle_depart])) {
194
-					$s = sql_select("$cle_depart, $cle_arrivee", $desc_arrivee['table_sql'],
195
-						sql_in($cle_arrivee, array_keys($ids_trouves)), '', '', '', '', $serveur);
196
-				} // cas simple : $cle_arrivee dans la table
197
-				elseif (isset($desc_depart['field'][$cle_arrivee])) {
198
-					$s = sql_select("$cle_depart, $cle_arrivee", $desc_depart['table_sql'],
199
-						sql_in($cle_arrivee, array_keys($ids_trouves)), '', '', '', '', $serveur);
200
-				}
201
-				// sinon cherchons une table de liaison
202
-				// cas recherche principale article, objet lie document : passer par spip_documents_liens
203
-				elseif ($l = objet_associable($table_liee)) {
204
-					list($primary, $table_liens) = $l;
205
-					$s = sql_select("id_objet as $cle_depart, $primary as $cle_arrivee", $table_liens,
206
-						array("objet='$table'", sql_in($primary, array_keys($ids_trouves))), '', '', '', '', $serveur);
207
-				} // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens
208
-				elseif ($l = $depart_associable) {
209
-					list($primary, $table_liens) = $l;
210
-					$s = sql_select("$primary as $cle_depart, id_objet as $cle_arrivee", $table_liens,
211
-						array("objet='$table_liee'", sql_in('id_objet', array_keys($ids_trouves))), '', '', '', '', $serveur);
212
-				} // cas table de liaison generique spip_xxx_yyy
213
-				elseif ($t = $trouver_table($table_arrivee . "_" . $table_depart, $serveur)
214
-					or $t = $trouver_table($table_depart . "_" . $table_arrivee, $serveur)
215
-				) {
216
-					$s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)),
217
-						'', '', '', '', $serveur);
218
-				}
219
-			} else {
220
-				list($cle_depart, $cle_arrivee, $s) = $rechercher_joints($table, $table_liee, array_keys($ids_trouves),
221
-					$serveur);
222
-			}
223
-
224
-			while ($t = is_array($s) ? array_shift($s) : sql_fetch($s)) {
225
-				$id = $t[$cle_depart];
226
-				$joint = $ids_trouves[$t[$cle_arrivee]];
227
-				if (!isset($results)) {
228
-					$results = array();
229
-				}
230
-				if (!isset($results[$id])) {
231
-					$results[$id] = array();
232
-				}
233
-				if (isset($joint['score']) and $joint['score']) {
234
-					if (!isset($results[$id]['score'])) {
235
-						$results[$id]['score'] = 0;
236
-					}
237
-					$results[$id]['score'] += $joint['score'];
238
-				}
239
-				if (isset($joint['champs']) and $joint['champs']) {
240
-					foreach ($joint['champs'] as $c => $val) {
241
-						$results[$id]['champs'][$table_liee . '.' . $c] = $val;
242
-					}
243
-				}
244
-				if (isset($joint['matches']) and $joint['matches']) {
245
-					foreach ($joint['matches'] as $c => $val) {
246
-						$results[$id]['matches'][$table_liee . '.' . $c] = $val;
247
-					}
248
-				}
249
-			}
250
-		}
251
-	}
252
-
253
-	return $results;
22
+    // options par defaut
23
+    $options = array_merge(
24
+        array(
25
+            'score' => true,
26
+            'champs' => false,
27
+            'toutvoir' => false,
28
+            'matches' => false,
29
+            'jointures' => false
30
+        ),
31
+        $options
32
+    );
33
+
34
+    include_spip('inc/rechercher');
35
+    include_spip('inc/autoriser');
36
+
37
+    $requete = array(
38
+        "SELECT" => array(),
39
+        "FROM" => array(),
40
+        "WHERE" => array(),
41
+        "GROUPBY" => array(),
42
+        "ORDERBY" => array(),
43
+        "LIMIT" => "",
44
+        "HAVING" => array()
45
+    );
46
+
47
+    $table = sinon($options['table'], 'article');
48
+    if ($options['champs']) {
49
+        $champs = $options['champs'];
50
+    } else {
51
+        $l = liste_des_champs();
52
+        $champs = $l['article'];
53
+    }
54
+    $serveur = $options['serveur'];
55
+
56
+    list($methode, $q, $preg) = expression_recherche($recherche, $options);
57
+
58
+    $jointures = $options['jointures']
59
+        ? liste_des_jointures()
60
+        : array();
61
+
62
+    $_id_table = id_table_objet($table);
63
+
64
+    // c'est un pis-aller : ca a peu de chance de marcher, mais mieux quand meme que en conservant la ','
65
+    // (aka ca marche au moins dans certains cas comme avec spip_formulaires_reponses_champs)
66
+    if (strpos($_id_table, ",") !== false) {
67
+        $_id_table = explode(',', $_id_table);
68
+        $_id_table = reset($_id_table);
69
+    }
70
+
71
+    $requete['SELECT'][] = "t." . $_id_table;
72
+    $a = array();
73
+    // Recherche fulltext
74
+    foreach ($champs as $champ => $poids) {
75
+        if (is_array($champ)) {
76
+            spip_log("requetes imbriquees interdites");
77
+        } else {
78
+            if (strpos($champ, ".") === false) {
79
+                $champ = "t.$champ";
80
+            }
81
+            $requete['SELECT'][] = $champ;
82
+            $a[] = $champ . ' ' . $methode . ' ' . $q;
83
+        }
84
+    }
85
+    if ($a) {
86
+        $requete['WHERE'][] = join(" OR ", $a);
87
+    }
88
+    $requete['FROM'][] = table_objet_sql($table) . ' AS t';
89
+
90
+    $results = array();
91
+
92
+    $s = sql_select(
93
+        $requete['SELECT'], $requete['FROM'], $requete['WHERE'],
94
+        implode(" ", $requete['GROUPBY']),
95
+        $requete['ORDERBY'], $requete['LIMIT'],
96
+        $requete['HAVING'], $serveur
97
+    );
98
+
99
+    while ($t = sql_fetch($s, $serveur)
100
+        and (!isset($t['score']) or $t['score'] > 0)) {
101
+        $id = intval($t[$_id_table]);
102
+
103
+        if ($options['toutvoir']
104
+            or autoriser('voir', $table, $id)
105
+        ) {
106
+            // indiquer les champs concernes
107
+            $champs_vus = array();
108
+            $score = 0;
109
+            $matches = array();
110
+
111
+            $vu = false;
112
+            foreach ($champs as $champ => $poids) {
113
+                $champ = explode('.', $champ);
114
+                $champ = end($champ);
115
+                // translitteration_rapide uniquement si on est deja en utf-8
116
+                $value = ($GLOBALS['meta']['charset'] == 'utf-8' ? translitteration_rapide($t[$champ]) : translitteration($t[$champ]));
117
+                if ($n =
118
+                    ($options['score'] || $options['matches'])
119
+                        ? preg_match_all($preg, $value, $regs, PREG_SET_ORDER)
120
+                        : preg_match($preg, $value)
121
+                ) {
122
+                    $vu = true;
123
+
124
+                    if ($options['champs']) {
125
+                        $champs_vus[$champ] = $t[$champ];
126
+                    }
127
+                    if ($options['score']) {
128
+                        // compter les points avec un peu de discernement : on pondere par la longueur du match compte en chars
129
+                        $score += $poids * strlen(implode('',array_column($regs, 0)));
130
+                    }
131
+
132
+                    if ($options['matches']) {
133
+                        $matches[$champ] = $regs;
134
+                    }
135
+
136
+                    if (!$options['champs']
137
+                        and !$options['score']
138
+                        and !$options['matches']
139
+                    ) {
140
+                        break;
141
+                    }
142
+                }
143
+            }
144
+
145
+            if ($vu) {
146
+                if (!isset($results)) {
147
+                    $results = array();
148
+                }
149
+                $results[$id] = array();
150
+                if ($champs_vus) {
151
+                    $results[$id]['champs'] = $champs_vus;
152
+                }
153
+                if ($score) {
154
+                    $results[$id]['score'] = $score;
155
+                }
156
+                if ($matches) {
157
+                    $results[$id]['matches'] = $matches;
158
+                }
159
+            }
160
+        }
161
+    }
162
+
163
+
164
+    // Gerer les donnees associees
165
+    // ici on est un peu naze : pas capables de reconstruire une jointure complexe
166
+    // on ne sait passer que par table de laison en 1 coup
167
+    if (isset($jointures[$table])
168
+        and $joints = recherche_en_base(
169
+            $recherche,
170
+            $jointures[$table],
171
+            array_merge($options, array('jointures' => false))
172
+        )
173
+    ) {
174
+        include_spip('action/editer_liens');
175
+        $trouver_table = charger_fonction('trouver_table', 'base');
176
+        $cle_depart = id_table_objet($table);
177
+        $table_depart = table_objet($table, $serveur);
178
+        $desc_depart = $trouver_table($table_depart, $serveur);
179
+        $depart_associable = objet_associable($table);
180
+        foreach ($joints as $table_liee => $ids_trouves) {
181
+            // on peut definir une fonction de recherche jointe pour regler les cas particuliers
182
+            if (
183
+            !(
184
+                $rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}", "inc", true)
185
+                or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}", "inc", true)
186
+                or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie", "inc", true)
187
+            )
188
+            ) {
189
+                $cle_arrivee = id_table_objet($table_liee);
190
+                $table_arrivee = table_objet($table_liee, $serveur);
191
+                $desc_arrivee = $trouver_table($table_arrivee, $serveur);
192
+                // cas simple : $cle_depart dans la table_liee
193
+                if (isset($desc_arrivee['field'][$cle_depart])) {
194
+                    $s = sql_select("$cle_depart, $cle_arrivee", $desc_arrivee['table_sql'],
195
+                        sql_in($cle_arrivee, array_keys($ids_trouves)), '', '', '', '', $serveur);
196
+                } // cas simple : $cle_arrivee dans la table
197
+                elseif (isset($desc_depart['field'][$cle_arrivee])) {
198
+                    $s = sql_select("$cle_depart, $cle_arrivee", $desc_depart['table_sql'],
199
+                        sql_in($cle_arrivee, array_keys($ids_trouves)), '', '', '', '', $serveur);
200
+                }
201
+                // sinon cherchons une table de liaison
202
+                // cas recherche principale article, objet lie document : passer par spip_documents_liens
203
+                elseif ($l = objet_associable($table_liee)) {
204
+                    list($primary, $table_liens) = $l;
205
+                    $s = sql_select("id_objet as $cle_depart, $primary as $cle_arrivee", $table_liens,
206
+                        array("objet='$table'", sql_in($primary, array_keys($ids_trouves))), '', '', '', '', $serveur);
207
+                } // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens
208
+                elseif ($l = $depart_associable) {
209
+                    list($primary, $table_liens) = $l;
210
+                    $s = sql_select("$primary as $cle_depart, id_objet as $cle_arrivee", $table_liens,
211
+                        array("objet='$table_liee'", sql_in('id_objet', array_keys($ids_trouves))), '', '', '', '', $serveur);
212
+                } // cas table de liaison generique spip_xxx_yyy
213
+                elseif ($t = $trouver_table($table_arrivee . "_" . $table_depart, $serveur)
214
+                    or $t = $trouver_table($table_depart . "_" . $table_arrivee, $serveur)
215
+                ) {
216
+                    $s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)),
217
+                        '', '', '', '', $serveur);
218
+                }
219
+            } else {
220
+                list($cle_depart, $cle_arrivee, $s) = $rechercher_joints($table, $table_liee, array_keys($ids_trouves),
221
+                    $serveur);
222
+            }
223
+
224
+            while ($t = is_array($s) ? array_shift($s) : sql_fetch($s)) {
225
+                $id = $t[$cle_depart];
226
+                $joint = $ids_trouves[$t[$cle_arrivee]];
227
+                if (!isset($results)) {
228
+                    $results = array();
229
+                }
230
+                if (!isset($results[$id])) {
231
+                    $results[$id] = array();
232
+                }
233
+                if (isset($joint['score']) and $joint['score']) {
234
+                    if (!isset($results[$id]['score'])) {
235
+                        $results[$id]['score'] = 0;
236
+                    }
237
+                    $results[$id]['score'] += $joint['score'];
238
+                }
239
+                if (isset($joint['champs']) and $joint['champs']) {
240
+                    foreach ($joint['champs'] as $c => $val) {
241
+                        $results[$id]['champs'][$table_liee . '.' . $c] = $val;
242
+                    }
243
+                }
244
+                if (isset($joint['matches']) and $joint['matches']) {
245
+                    foreach ($joint['matches'] as $c => $val) {
246
+                        $results[$id]['matches'][$table_liee . '.' . $c] = $val;
247
+                    }
248
+                }
249
+            }
250
+        }
251
+    }
252
+
253
+    return $results;
254 254
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 		$_id_table = reset($_id_table);
69 69
 	}
70 70
 
71
-	$requete['SELECT'][] = "t." . $_id_table;
71
+	$requete['SELECT'][] = "t.".$_id_table;
72 72
 	$a = array();
73 73
 	// Recherche fulltext
74 74
 	foreach ($champs as $champ => $poids) {
@@ -79,13 +79,13 @@  discard block
 block discarded – undo
79 79
 				$champ = "t.$champ";
80 80
 			}
81 81
 			$requete['SELECT'][] = $champ;
82
-			$a[] = $champ . ' ' . $methode . ' ' . $q;
82
+			$a[] = $champ.' '.$methode.' '.$q;
83 83
 		}
84 84
 	}
85 85
 	if ($a) {
86 86
 		$requete['WHERE'][] = join(" OR ", $a);
87 87
 	}
88
-	$requete['FROM'][] = table_objet_sql($table) . ' AS t';
88
+	$requete['FROM'][] = table_objet_sql($table).' AS t';
89 89
 
90 90
 	$results = array();
91 91
 
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 					}
127 127
 					if ($options['score']) {
128 128
 						// compter les points avec un peu de discernement : on pondere par la longueur du match compte en chars
129
-						$score += $poids * strlen(implode('',array_column($regs, 0)));
129
+						$score += $poids * strlen(implode('', array_column($regs, 0)));
130 130
 					}
131 131
 
132 132
 					if ($options['matches']) {
@@ -210,8 +210,8 @@  discard block
 block discarded – undo
210 210
 					$s = sql_select("$primary as $cle_depart, id_objet as $cle_arrivee", $table_liens,
211 211
 						array("objet='$table_liee'", sql_in('id_objet', array_keys($ids_trouves))), '', '', '', '', $serveur);
212 212
 				} // cas table de liaison generique spip_xxx_yyy
213
-				elseif ($t = $trouver_table($table_arrivee . "_" . $table_depart, $serveur)
214
-					or $t = $trouver_table($table_depart . "_" . $table_arrivee, $serveur)
213
+				elseif ($t = $trouver_table($table_arrivee."_".$table_depart, $serveur)
214
+					or $t = $trouver_table($table_depart."_".$table_arrivee, $serveur)
215 215
 				) {
216 216
 					$s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)),
217 217
 						'', '', '', '', $serveur);
@@ -238,12 +238,12 @@  discard block
 block discarded – undo
238 238
 				}
239 239
 				if (isset($joint['champs']) and $joint['champs']) {
240 240
 					foreach ($joint['champs'] as $c => $val) {
241
-						$results[$id]['champs'][$table_liee . '.' . $c] = $val;
241
+						$results[$id]['champs'][$table_liee.'.'.$c] = $val;
242 242
 					}
243 243
 				}
244 244
 				if (isset($joint['matches']) and $joint['matches']) {
245 245
 					foreach ($joint['matches'] as $c => $val) {
246
-						$results[$id]['matches'][$table_liee . '.' . $c] = $val;
246
+						$results[$id]['matches'][$table_liee.'.'.$c] = $val;
247 247
 					}
248 248
 				}
249 249
 			}
Please login to merge, or discard this patch.