Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 867 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
866 raphael 1
<?php
2
/*
3
 * @author		Raphaël Droz <raphael@tela-botanica.org>
4
 * @copyright	Copyright (c) 2011, 2013 Tela Botanica (accueil@tela-botanica.org)
5
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
6
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
7
 *
8
 * Corrige les erreurs de saisie de nom à l'aide d'une recherche via un index sphinx
9
 * pour les observations ayant un nom saisi et dont l'un au moins de nom_ret[nn],
10
 * nt ou famille est NULL ou vide.
11
 *
12
 */
13
 
14
// time php -d memory_limit=1024M sphinx-maj-nom-ret.php 0 > sphinx-maj.log
15
// 23 secondes
16
 
876 raphael 17
// settings
18
define('USE_NVJFL', FALSE);
19
define('ESCAPE_ON_SPHINX_SYNERROR', TRUE);
20
 
21
define('TRY_FORCE_START_LINE', TRUE);
22
define('TRY_SPLIT',			   TRUE);
23
define('TRY_EXACT',			   TRUE);
24
define('TRY_REF',			   TRUE);
25
define('TRY_SPLIT_AND_AUTEUR', FALSE);
26
define('TRY_REMOVE_L',		   TRUE);
27
 
28
define('M_TRY_SPLIT',	0x01);
29
define('M_TRY_EXACT',	0x02);
30
define('M_TRY_REF',		0x04);
31
define('M_TRY_SPLIT_AND_AUTEUR',	0x08);
32
 
866 raphael 33
error_reporting(E_ALL);
876 raphael 34
$db = mysql_connect('localhost', 'root', '') or die('no mysql');
866 raphael 35
mysql_select_db('tb_cel', $db);
876 raphael 36
mysql_query("SET NAMES utf8", $db) or die('no sphinx');
866 raphael 37
$dbs = mysql_connect('127.0.0.1:9306', NULL, NULL, TRUE);
38
 
39
$req = <<<EOF
876 raphael 40
	SELECT id_observation, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, nt, famille, nom_referentiel
866 raphael 41
        FROM `cel_obs`
42
        WHERE nom_sel IS NOT NULL AND nom_sel != '' AND
43
		id_observation BETWEEN %d AND %d AND
44
		( nom_ret IS NULL or nom_ret = ''
45
		  OR nom_ret_nn IS NULL or nom_ret_nn = 0 or nom_ret_nn = ''
46
		  OR nt IS NULL or nt = 0 or nt = ''
47
		  OR famille IS NULL or famille = '' )
48
	LIMIT %d, %d
49
EOF;
50
 
51
array_shift($argv);
52
$start = array_shift($argv);
53
$max = array_shift($argv);
54
$chunk_size = array_shift($argv);
55
 
56
if(!$start) $start = 0;
57
// 1036314
58
if(!$max) $max = intval(mysql_fetch_assoc(mysql_query("SELECT MAX(id_observation) AS max FROM cel_obs", $db))['max']) + 1;
59
if(!$chunk_size) $chunk_size = 50000;
60
 
61
 
62
// escape sphinx
63
$from = array ( '\\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=', "'", "\x00", "\n", "\r", "\x1a" );
64
$to   = array ( '\\\\', '\\\(','\\\)','\\\|','\\\-','\\\!','\\\@','\\\~','\\\"', '\\\&', '\\\/', '\\\^', '\\\$', '\\\=', "\\'", "\\x00", "\\n", "\\r", "\\x1a" );
65
 
66
 
67
$stats = ['no_nom_sel' => ['count' => 0, 'data' => [] ],
68
		  'not found' => ['count' => 0, 'data' => [] ],
69
		  'too many' => ['count' => 0, 'data' => [] ],
70
		  'fixable' => ['count' => 0, 'data' => [] ],
71
		  'sauvages' => ['count' => 0, 'data' => [] ],
876 raphael 72
		  'sphinx errors' => ['count' => 0, 'data' => [] ],
73
		  'ref pb' => ['count' => 0, 'data' => [] ], ];
866 raphael 74
 
876 raphael 75
$sphinx_req = sprintf("SELECT * FROM i_bdtfx %s WHERE MATCH('%%s') LIMIT 5", USE_NVJFL ? ", i_nvjfl" : "");
866 raphael 76
 
77
for($current = 0; $current < intval($max/$chunk_size) + 1; $current++) {
78
	// printf("current = %d, chunk_size = %d, max = %d (rmax = %d) [real limit: %d]\n", $current, $chunk_size, $max, intval($max/$chunk_size) + 1, $current*$chunk_size);
79
	// printf(strtr($req, "\n", " ") . "\n", $start, $max, $current*$chunk_size, $chunk_size);
80
	$data = mysql_query(sprintf($req, $start, $max, $current*$chunk_size, $chunk_size), $db);
81
	if(!$data) { var_dump(mysql_error()); die('end'); }
82
	while($d = mysql_fetch_assoc($data)) {
876 raphael 83
		$n  = trim($d['nom_sel']);
84
		//d: fprintf(STDERR, "$n\n");
85
 
866 raphael 86
		if(!$n) {
87
			$stats['no_nom_sel']['count']++;
88
			// $stats['no_nom_sel']['data'][] = [$d['id_observation'], $n];*/
89
			continue;
90
		}
91
 
92
		if($n == 'Autre(s) espèce(s) (écrire le/les nom(s) dans les notes)' ||
93
		   $n == '-') {
94
			$stats['sauvages']['count']++;
95
			// $stats['sauvages']['data'][] = [$d['id_observation'], $n];
96
			continue;
97
		}
98
 
876 raphael 99
		$MASQUE = 0;
100
 
101
		if(TRY_REMOVE_L) {
102
			$n = str_replace(' L.','', $n);
103
		}
104
 
105
		$orig_n = $n;
106
 
867 raphael 107
	recherche:
876 raphael 108
		if(TRY_FORCE_START_LINE && !_has($MASQUE, M_TRY_EXACT)) {
109
			$n = '^' . $n;
110
		}
111
 
112
		$s = mysql_query(sprintf($sphinx_req, $n), $dbs);
113
 
114
 
115
		if(!$s && ESCAPE_ON_SPHINX_SYNERROR) {
116
			$s = mysql_query(sprintf($sphinx_req, str_replace($from,$to,$n)), $dbs);
117
		}
866 raphael 118
		if(!$s) {
119
			$stats['sphinx errors']['count']++;
876 raphael 120
			// $stats['sphinx errors']['data'][] = [$d['id_observation'], $orig_n];
866 raphael 121
			continue;
122
		}
123
 
124
		$c = mysql_num_rows($s);
876 raphael 125
		//d: fprintf(STDERR, "\t search [nb:%d] \"%s\" (msk:%d)\n", $c, $n, $MASQUE);
126
 
866 raphael 127
		if($c == 0) {
876 raphael 128
			if(TRY_SPLIT && !_has($MASQUE, M_TRY_SPLIT)) {
129
				require_once('lib-split-auteur.php');
130
				$MASQUE |= M_TRY_SPLIT;
131
				// $n = RechercheInfosTaxonBeta::supprimerAuteur($orig_n);
132
				// list($ret, $m) = RechercheInfosTaxonBeta::contientAuteur($orig_n);
133
				$ret = RechercheInfosTaxonBeta::supprimerAuteurBis($orig_n, $m);
134
				if($ret) {
135
					// printf("===================== SPLIT: contientAuteur \"%s\" [@%s @%s)\n", $orig_n, $ret, $m);
136
					$n = sprintf('%s @auteur %s', $ret, $m);
137
					goto recherche;
138
				}
139
			}
140
			if(TRY_SPLIT_AND_AUTEUR && !_has($MASQUE, M_TRY_SPLIT_AND_AUTEUR) && strpos($orig_n, ' ') !== FALSE) {
141
				require_once('lib-split-auteur.php');
142
				$MASQUE |= M_TRY_SPLIT_AND_AUTEUR;
143
				$ns = RechercheInfosTaxonBeta::supprimerAuteur($orig_n);
144
				if($ns) {
145
					$a = trim(substr($orig_n, strlen($n)));
146
					$n = sprintf("%s @auteur %s", $ns, $a);
147
					// echo "===================== SPLIT N/A: $n\n";
148
					goto recherche;
149
				}
150
			}
151
 
866 raphael 152
			$stats['not found']['count']++;
876 raphael 153
			// $stats['not found']['data'][] = [$d['id_observation'], $orig_n];
866 raphael 154
			continue;
155
		}
156
 
157
		if($c > 1) {
876 raphael 158
 
159
			if($c == 2) {
160
				if(mysql_fetch_array($s)['group_id'] !=
161
				   mysql_fetch_array($s)['group_id']) {
162
					// recherche donne seulement 2 résultats dans 2 référentiels
163
					// potentiellement fixable si l'on peut se référer à $d['nom_referentiel']
164
					$stats['ref pb']['count']++;
165
					// $stats['ref pb']['data'][] = [$d['id_observation'], $orig_n];
166
					continue;
167
				}
168
			}
169
 
170
			if(TRY_EXACT && !_has($MASQUE, M_TRY_EXACT)) {
171
				$MASQUE |= M_TRY_EXACT;
172
				$n = '"^' . trim($orig_n) . '$"';
173
				goto recherche;
174
			}
175
			if(TRY_REF && isset($d['nom_referentiel']) && !_has($MASQUE, M_TRY_REF)) {
176
				$MASQUE |= M_TRY_REF;
177
				$n = $orig_n . ' @group_id ' . $d['nom_referentiel'];
178
				goto recherche;
179
			}
180
 
866 raphael 181
			$stats['too many']['count']++;
876 raphael 182
			// $stats['too many']['data'][] = [$d['id_observation'], $orig_n];
866 raphael 183
			continue;
184
		}
185
 
876 raphael 186
 
187
	ok:
866 raphael 188
		$stats['fixable']['count']++;
876 raphael 189
		// $stats['fixable']['data'][] = [$d['id_observation'], $orig_n];
866 raphael 190
 
191
	}
192
}
193
 
876 raphael 194
function _has($v, $r) {
195
	return ($v & $r) == $r;
196
}
197
 
198
 
866 raphael 199
array_walk($stats, function(&$v) { unset($v['data']); });
200
print_r($stats);
201
printf("total traité: %d\n", array_sum(array_map(function($v) { return $v['count']; }, $stats)));