| 1 |
|
|---|
| 2 |
package Classifier::Bayes; |
|---|
| 3 |
|
|---|
| 4 |
use POPFile::Module; |
|---|
| 5 |
@ISA = ("POPFile::Module"); |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
use strict; |
|---|
| 34 |
use warnings; |
|---|
| 35 |
use locale; |
|---|
| 36 |
use Classifier::MailParse; |
|---|
| 37 |
use IO::Handle; |
|---|
| 38 |
use DBI; |
|---|
| 39 |
use Digest::MD5 qw( md5_hex ); |
|---|
| 40 |
use MIME::Base64; |
|---|
| 41 |
use File::Copy; |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
use Sys::Hostname; |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
my $eol = "\015\012"; |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
my $ksc5601_sym = '(?:[\xA1-\xAC][\xA1-\xFE])'; |
|---|
| 55 |
my $ksc5601_han = '(?:[\xB0-\xC8][\xA1-\xFE])'; |
|---|
| 56 |
my $ksc5601_hanja = '(?:[\xCA-\xFD][\xA1-\xFE])'; |
|---|
| 57 |
my $ksc5601 = "(?:$ksc5601_sym|$ksc5601_han|$ksc5601_hanja)"; |
|---|
| 58 |
|
|---|
| 59 |
my $eksc = "(?:$ksc5601|[\x81-\xC6][\x41-\xFE])"; |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
sub new |
|---|
| 67 |
{ |
|---|
| 68 |
my $type = shift; |
|---|
| 69 |
my $self = POPFile::Module->new(); |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
$self->{wordscores__} = 0; |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
$self->{wmformat__} = ''; |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
$self->{hostname__} = ''; |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
$self->{db__} = {}; |
|---|
| 86 |
|
|---|
| 87 |
$self->{history__} = 0; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
$self->{db_get_buckets__} = 0; |
|---|
| 93 |
$self->{db_get_wordid__} = 0; |
|---|
| 94 |
$self->{db_get_word_count__} = 0; |
|---|
| 95 |
$self->{db_put_word_count__} = 0; |
|---|
| 96 |
$self->{db_get_bucket_unique_counts__} = 0; |
|---|
| 97 |
$self->{db_get_unique_word_count__} = 0; |
|---|
| 98 |
$self->{db_get_bucket_word_counts__} = 0; |
|---|
| 99 |
$self->{db_get_full_total__} = 0; |
|---|
| 100 |
$self->{db_get_bucket_parameter__} = 0; |
|---|
| 101 |
$self->{db_set_bucket_parameter__} = 0; |
|---|
| 102 |
$self->{db_get_bucket_parameter_default__} = 0; |
|---|
| 103 |
$self->{db_get_buckets_with_magnets__} = 0; |
|---|
| 104 |
$self->{db_delete_zero_words__} = 0; |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
$self->{db_bucketid__} = {}; |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
$self->{db_parameterid__} = {}; |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
$self->{db_parameters__} = {}; |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
$self->{parser__} = new Classifier::MailParse; |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
$self->{possible_colors__} = [ 'red', 'green', 'blue', 'brown', |
|---|
| 129 |
'orange', 'purple', 'magenta', 'gray', |
|---|
| 130 |
'plum', 'silver', 'pink', 'lightgreen', |
|---|
| 131 |
'lightblue', 'lightcyan', 'lightcoral', 'lightsalmon', |
|---|
| 132 |
'lightgrey', 'darkorange', 'darkcyan', 'feldspar', |
|---|
| 133 |
'black' ]; |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
$self->{bucket_start__} = {}; |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
$self->{not_likely__} = {}; |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
$self->{corpus_version__} = 1; |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
$self->{unclassified__} = log(100); |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
$self->{magnet_used__} = 0; |
|---|
| 155 |
$self->{magnet_detail__} = 0; |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
$self->{api_sessions__} = {}; |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
$self->{db_is_sqlite__} = 0; |
|---|
| 168 |
$self->{db_name__} = ''; |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
bless $self, $type; |
|---|
| 173 |
|
|---|
| 174 |
$self->name( 'bayes' ); |
|---|
| 175 |
|
|---|
| 176 |
return $self; |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
sub forked |
|---|
| 188 |
{ |
|---|
| 189 |
my ( $self ) = @_; |
|---|
| 190 |
|
|---|
| 191 |
$self->db_connect__(); |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
sub childexit |
|---|
| 203 |
{ |
|---|
| 204 |
my ( $self ) = @_; |
|---|
| 205 |
|
|---|
| 206 |
$self->db_disconnect__(); |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
sub initialize |
|---|
| 217 |
{ |
|---|
| 218 |
my ( $self ) = @_; |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
$self->config_( 'database', 'popfile.db' ); |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
$self->config_( 'dbconnect', 'dbi:SQLite:dbname=$dbname' ); |
|---|
| 236 |
$self->config_( 'dbuser', '' ); $self->config_( 'dbauth', '' ); |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
$self->config_( 'bad_sqlite_version', '4.0.0' ); |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
$self->config_( 'unclassified_weight', 100 ); |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
$self->config_( 'corpus', 'corpus' ); |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
$self->config_( 'subject_mod_left', '[' ); |
|---|
| 260 |
$self->config_( 'subject_mod_right', ']' ); |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
$self->{hostname__} = hostname; |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
$self->config_( 'hostname', $self->{hostname__} ); |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
$self->config_( 'xpl_angle', 0 ); |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
$self->config_( 'localhostname', '' ); |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
$self->config_( 'sqlite_tweaks', 0xFFFFFFFF ); |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
$self->config_( 'nihongo_parser', 'kakasi' ); |
|---|
| 293 |
|
|---|
| 294 |
$self->mq_register_( 'COMIT', $self ); |
|---|
| 295 |
$self->mq_register_( 'RELSE', $self ); |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
$self->mq_register_( 'TICKD', $self ); |
|---|
| 302 |
|
|---|
| 303 |
return 1; |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
sub deliver |
|---|
| 316 |
{ |
|---|
| 317 |
my ( $self, $type, @message ) = @_; |
|---|
| 318 |
|
|---|
| 319 |
if ( $type eq 'COMIT' ) { |
|---|
| 320 |
$self->classified( $message[0], $message[2] ); |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
if ( $type eq 'RELSE' ) { |
|---|
| 324 |
$self->release_session_key_private__( $message[0] ); |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
if ( $type eq 'TICKD' ) { |
|---|
| 328 |
$self->backup_database__(); |
|---|
| 329 |
} |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
sub start |
|---|
| 340 |
{ |
|---|
| 341 |
my ( $self ) = @_; |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
my $lang = $self->module_config_( 'html', 'language' ) || ''; |
|---|
| 351 |
|
|---|
| 352 |
if ( $lang =~ /^(Nihongo$|Korean$|Chinese)/ ) { |
|---|
| 353 |
use POSIX qw( locale_h ); |
|---|
| 354 |
setlocale( LC_COLLATE, 'C' ); |
|---|
| 355 |
} |
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
$self->{parser__}->{lang__} = $lang; |
|---|
| 360 |
$self->{unclassified__} = log( $self->config_( 'unclassified_weight' ) ); |
|---|
| 361 |
|
|---|
| 362 |
if ( !$self->db_connect__() ) { |
|---|
| 363 |
return 0; |
|---|
| 364 |
} |
|---|
| 365 |
|
|---|
| 366 |
if ( $lang eq 'Nihongo' ) { |
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
my $nihongo_parser = $self->config_( 'nihongo_parser' ); |
|---|
| 370 |
|
|---|
| 371 |
$nihongo_parser = $self->{parser__}->setup_nihongo_parser( $nihongo_parser ); |
|---|
| 372 |
|
|---|
| 373 |
$self->log_( 2, "Use Nihongo (Japanese) parser : $nihongo_parser" ); |
|---|
| 374 |
$self->config_( 'nihongo_parser', $nihongo_parser ); |
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
if ( ( $nihongo_parser eq 'kakasi' ) && ( $^O eq 'MSWin32' ) && |
|---|
| 381 |
( ( ( $self->module_config_( 'pop3', 'enabled' ) ) && |
|---|
| 382 |
( $self->module_config_( 'pop3', 'force_fork' ) ) ) || |
|---|
| 383 |
( ( $self->module_config_( 'nntp', 'enabled' ) ) && |
|---|
| 384 |
( $self->module_config_( 'nntp', 'force_fork' ) ) ) || |
|---|
| 385 |
( ( $self->module_config_( 'smtp', 'enabled' ) ) && |
|---|
| 386 |
( $self->module_config_( 'smtp', 'force_fork' ) ) ) ) ) { |
|---|
| 387 |
$self->{parser__}->{need_kakasi_mutex__} = 1; |
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
require POPFile::Mutex; |
|---|
| 391 |
$self->{parser__}->{kakasi_mutex__} = new POPFile::Mutex( 'mailparse_kakasi' ); |
|---|
| 392 |
$self->log_( 2, "Create mutex for Kakasi." ); |
|---|
| 393 |
} |
|---|
| 394 |
} |
|---|
| 395 |
|
|---|
| 396 |
$self->upgrade_predatabase_data__(); |
|---|
| 397 |
|
|---|
| 398 |
return 1; |
|---|
| 399 |
} |
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
sub stop |
|---|
| 409 |
{ |
|---|
| 410 |
my ( $self ) = @_; |
|---|
| 411 |
|
|---|
| 412 |
$self->db_disconnect__(); |
|---|
| 413 |
delete $self->{parser__}; |
|---|
| 414 |
} |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
sub classified |
|---|
| 426 |
{ |
|---|
| 427 |
my ( $self, $session, $class ) = @_; |
|---|
| 428 |
|
|---|
| 429 |
$self->set_bucket_parameter( $session, $class, 'count', |
|---|
| 430 |
$self->get_bucket_parameter( $session, $class, 'count' ) + 1 ); |
|---|
| 431 |
} |
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
sub backup_database__ |
|---|
| 442 |
{ |
|---|
| 443 |
my ( $self ) = @_; |
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
if ( ( $self->config_( 'sqlite_tweaks' ) & 2 ) && |
|---|
| 449 |
$self->{db_is_sqlite__} ) { |
|---|
| 450 |
if ( !copy( $self->{db_name__}, $self->{db_name__} . ".backup" ) ) { |
|---|
| 451 |
$self->log_( 0, "Failed to backup database ".$self->{db_name__} ); |
|---|
| 452 |
} |
|---|
| 453 |
} |
|---|
| 454 |
} |
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
sub tweak_sqlite |
|---|
| 468 |
{ |
|---|
| 469 |
my ( $self, $tweak, $state, $db ) = @_; |
|---|
| 470 |
|
|---|
| 471 |
if ( $self->{db_is_sqlite__} && |
|---|
| 472 |
( $self->config_( 'sqlite_tweaks' ) & $tweak ) ) { |
|---|
| 473 |
|
|---|
| 474 |
$self->log_( 1, "Performing tweak $tweak to $state" ); |
|---|
| 475 |
|
|---|
| 476 |
if ( $tweak == 1 ) { |
|---|
| 477 |
my $sync = $state?'off':'normal'; |
|---|
| 478 |
$db->do( "pragma synchronous=$sync;" ); |
|---|
| 479 |
} |
|---|
| 480 |
} |
|---|
| 481 |
} |
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
sub reclassified |
|---|
| 499 |
{ |
|---|
| 500 |
my ( $self, $session, $bucket, $newbucket, $undo ) = @_; |
|---|
| 501 |
|
|---|
| 502 |
$self->log_( 0, "Reclassification from $bucket to $newbucket" ); |
|---|
| 503 |
|
|---|
| 504 |
my $c = $undo?-1:1; |
|---|
| 505 |
|
|---|
| 506 |
if ( $bucket ne $newbucket ) { |
|---|
| 507 |
my $count = $self->get_bucket_parameter( |
|---|
| 508 |
$session, $newbucket, 'count' ); |
|---|
| 509 |
my $newcount = $count + $c; |
|---|
| 510 |
$newcount = 0 if ( $newcount < 0 ); |
|---|
| 511 |
$self->set_bucket_parameter( |
|---|
| 512 |
$session, $newbucket, 'count', $newcount ); |
|---|
| 513 |
|
|---|
| 514 |
$count = $self->get_bucket_parameter( |
|---|
| 515 |
$session, $bucket, 'count' ); |
|---|
| 516 |
$newcount = $count - $c; |
|---|
| 517 |
$newcount = 0 if ( $newcount < 0 ); |
|---|
| 518 |
$self->set_bucket_parameter( |
|---|
| 519 |
$session, $bucket, 'count', $newcount ); |
|---|
| 520 |
|
|---|
| 521 |
my $fncount = $self->get_bucket_parameter( |
|---|
| 522 |
$session, $newbucket, 'fncount' ); |
|---|
| 523 |
my $newfncount = $fncount + $c; |
|---|
| 524 |
$newfncount = 0 if ( $newfncount < 0 ); |
|---|
| 525 |
$self->set_bucket_parameter( |
|---|
| 526 |
$session, $newbucket, 'fncount', $newfncount ); |
|---|
| 527 |
|
|---|
| 528 |
my $fpcount = $self->get_bucket_parameter( |
|---|
| 529 |
$session, $bucket, 'fpcount' ); |
|---|
| 530 |
my $newfpcount = $fpcount + $c; |
|---|
| 531 |
$newfpcount = 0 if ( $newfpcount < 0 ); |
|---|
| 532 |
$self->set_bucket_parameter( |
|---|
| 533 |
$session, $bucket, 'fpcount', $newfpcount ); |
|---|
| 534 |
} |
|---|
| 535 |
} |
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
sub get_color |
|---|
| 548 |
{ |
|---|
| 549 |
my ( $self, $session, $word ) = @_; |
|---|
| 550 |
|
|---|
| 551 |
my $max = -10000; |
|---|
| 552 |
my $color = 'black'; |
|---|
| 553 |
|
|---|
| 554 |
for my $bucket ($self->get_buckets( $session )) { |
|---|
| 555 |
my $prob = $self->get_value_( $session, $bucket, $word ); |
|---|
| 556 |
|
|---|
| 557 |
if ( $prob != 0 ) { |
|---|
| 558 |
if ( $prob > $max ) { |
|---|
| 559 |
$max = $prob; |
|---|
| 560 |
$color = $self->get_bucket_parameter( $session, $bucket, |
|---|
| 561 |
'color' ); |
|---|
| 562 |
} |
|---|
| 563 |
} |
|---|
| 564 |
} |
|---|
| 565 |
|
|---|
| 566 |
return $color; |
|---|
| 567 |
} |
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
sub get_not_likely_ |
|---|
| 577 |
{ |
|---|
| 578 |
my ( $self, $session ) = @_; |
|---|
| 579 |
|
|---|
| 580 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 581 |
return undef if ( !defined( $userid ) ); |
|---|
| 582 |
|
|---|
| 583 |
return $self->{not_likely__}{$userid}; |
|---|
| 584 |
} |
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
sub get_value_ |
|---|
| 596 |
{ |
|---|
| 597 |
my ( $self, $session, $bucket, $word ) = @_; |
|---|
| 598 |
|
|---|
| 599 |
my $value = $self->db_get_word_count__( $session, $bucket, $word ); |
|---|
| 600 |
|
|---|
| 601 |
if ( defined( $value ) && ( $value > 0 ) ) { |
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
|
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
return log( $value / |
|---|
| 610 |
$self->get_bucket_word_count( $session, $bucket ) ); |
|---|
| 611 |
} else { |
|---|
| 612 |
return 0; |
|---|
| 613 |
} |
|---|
| 614 |
} |
|---|
| 615 |
|
|---|
| 616 |
sub get_base_value_ |
|---|
| 617 |
{ |
|---|
| 618 |
my ( $self, $session, $bucket, $word ) = @_; |
|---|
| 619 |
|
|---|
| 620 |
my $value = $self->db_get_word_count__( $session, $bucket, $word ); |
|---|
| 621 |
|
|---|
| 622 |
if ( defined( $value ) ) { |
|---|
| 623 |
return $value; |
|---|
| 624 |
} else { |
|---|
| 625 |
return 0; |
|---|
| 626 |
} |
|---|
| 627 |
} |
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
sub set_value_ |
|---|
| 638 |
{ |
|---|
| 639 |
my ( $self, $session, $bucket, $word, $value ) = @_; |
|---|
| 640 |
|
|---|
| 641 |
if ( $self->db_put_word_count__( $session, $bucket, |
|---|
| 642 |
$word, $value ) == 1 ) { |
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 648 |
my $bucketid = $self->{db_bucketid__}{$userid}{$bucket}{id}; |
|---|
| 649 |
$self->validate_sql_prepare_and_execute( $self->{db_delete_zero_words__}, $bucketid ); |
|---|
| 650 |
|
|---|
| 651 |
return 1; |
|---|
| 652 |
} else { |
|---|
| 653 |
return 0; |
|---|
| 654 |
} |
|---|
| 655 |
} |
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
sub get_sort_value_ |
|---|
| 665 |
{ |
|---|
| 666 |
my ( $self, $session, $bucket, $word ) = @_; |
|---|
| 667 |
|
|---|
| 668 |
my $v = $self->get_value_( $session, $bucket, $word ); |
|---|
| 669 |
|
|---|
| 670 |
if ( $v == 0 ) { |
|---|
| 671 |
|
|---|
| 672 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 673 |
return undef if ( !defined( $userid ) ); |
|---|
| 674 |
|
|---|
| 675 |
return $self->{not_likely__}{$userid}; |
|---|
| 676 |
} else { |
|---|
| 677 |
return $v; |
|---|
| 678 |
} |
|---|
| 679 |
} |
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
sub update_constants__ |
|---|
| 689 |
{ |
|---|
| 690 |
my ( $self, $session ) = @_; |
|---|
| 691 |
|
|---|
| 692 |
my $wc = $self->get_word_count( $session ); |
|---|
| 693 |
|
|---|
| 694 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 695 |
return undef if ( !defined( $userid ) ); |
|---|
| 696 |
|
|---|
| 697 |
if ( defined( $wc ) && $wc > 0 ) { |
|---|
| 698 |
$self->{not_likely__}{$userid} = -log( 10 * $wc ); |
|---|
| 699 |
|
|---|
| 700 |
foreach my $bucket ($self->get_buckets( $session )) { |
|---|
| 701 |
my $total = $self->get_bucket_word_count( $session, $bucket ); |
|---|
| 702 |
|
|---|
| 703 |
if ( $total != 0 ) { |
|---|
| 704 |
$self->{bucket_start__}{$userid}{$bucket} = log( $total / |
|---|
| 705 |
$wc ); |
|---|
| 706 |
} else { |
|---|
| 707 |
$self->{bucket_start__}{$userid}{$bucket} = 0; |
|---|
| 708 |
} |
|---|
| 709 |
} |
|---|
| 710 |
} else { |
|---|
| 711 |
$self->{not_likely__}{$userid} = 0; |
|---|
| 712 |
} |
|---|
| 713 |
} |
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
sub db_connect__ |
|---|
| 723 |
{ |
|---|
| 724 |
my ( $self ) = @_; |
|---|
| 725 |
|
|---|
| 726 |
|
|---|
| 727 |
|
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
my $dbname; |
|---|
| 737 |
my $dbconnect = $self->config_( 'dbconnect' ); |
|---|
| 738 |
my $dbpresent; |
|---|
| 739 |
my $sqlite = ( $dbconnect =~ /sqlite/i ); |
|---|
| 740 |
my $mysql = ( $dbconnect =~ /mysql/i ); |
|---|
| 741 |
my %connection_options = (); |
|---|
| 742 |
|
|---|
| 743 |
if ( $sqlite ) { |
|---|
| 744 |
$dbname = $self->get_user_path_( $self->config_( 'database' ) ); |
|---|
| 745 |
$dbpresent = ( -e $dbname ) || 0; |
|---|
| 746 |
} else { |
|---|
| 747 |
$dbname = $self->config_( 'database' ); |
|---|
| 748 |
$dbpresent = 1; |
|---|
| 749 |
|
|---|
| 750 |
if ( $mysql ) { |
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
$connection_options{mysql_auto_reconnect} = 1; |
|---|
| 755 |
} |
|---|
| 756 |
} |
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
$self->{db_is_sqlite__} = $sqlite; |
|---|
| 764 |
$self->{db_name__} = $dbname; |
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
$dbconnect =~ s/\$dbname/$dbname/g; |
|---|
| 771 |
|
|---|
| 772 |
$self->log_( 0, "Attempting to connect to $dbconnect ($dbpresent)" ); |
|---|
| 773 |
|
|---|
| 774 |
my $need_convert = 0; |
|---|
| 775 |
my $old_dbh; |
|---|
| 776 |
|
|---|
| 777 |
if ( $sqlite && $dbpresent ) { |
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
open DBFILE, $dbname; |
|---|
| 782 |
my $buffer; |
|---|
| 783 |
my $readed = sysread( DBFILE, $buffer, 47 ); |
|---|
| 784 |
close DBFILE; |
|---|
| 785 |
|
|---|
| 786 |
if ( $buffer eq '** This file contains an SQLite 2.1 database **' ) { |
|---|
| 787 |
$self->log_( 0, 'SQLite 2 database found. Try to upgrade' ); |
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
my $ver = -1; |
|---|
| 792 |
eval { |
|---|
| 793 |
require DBD::SQLite; |
|---|
| 794 |
$ver = $DBD::SQLite::VERSION; |
|---|
| 795 |
}; |
|---|
| 796 |
|
|---|
| 797 |
if ( $ver >= 1.00 ) { |
|---|
| 798 |
$self->log_( 0, "DBD::SQLite $ver found" ); |
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
my $old_dbname = $dbname . '-sqlite2'; |
|---|
| 803 |
unlink $old_dbname; |
|---|
| 804 |
rename $dbname, $old_dbname; |
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
my $old_dbconnect = $self->config_( 'dbconnect' ); |
|---|
| 809 |
$old_dbconnect =~ s/SQLite:/SQLite2:/; |
|---|
| 810 |
$old_dbconnect =~ s/\$dbname/$old_dbname/g; |
|---|
| 811 |
|
|---|
| 812 |
$old_dbh = DBI->connect( $old_dbconnect, |
|---|
| 813 |
$self->config_( 'dbuser' ), |
|---|
| 814 |
$self->config_( 'dbauth' ) ); |
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 |
|
|---|
| 818 |
$dbconnect = $self->config_( 'dbconnect' ); |
|---|
| 819 |
$dbconnect =~ s/SQLite2:/SQLite:/; |
|---|
| 820 |
$self->config_( 'dbconnect', $dbconnect ); |
|---|
| 821 |
$dbconnect =~ s/\$dbname/$dbname/g; |
|---|
| 822 |
|
|---|
| 823 |
$need_convert = 1; |
|---|
| 824 |
} |
|---|
| 825 |
} else { |
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 |
|
|---|
| 829 |
$dbconnect = $self->config_( 'dbconnect' ); |
|---|
| 830 |
$dbconnect =~ s/SQLite2:/SQLite:/; |
|---|
| 831 |
$self->config_( 'dbconnect', $dbconnect ); |
|---|
| 832 |
$dbconnect =~ s/\$dbname/$dbname/g; |
|---|
| 833 |
} |
|---|
| 834 |
} |
|---|
| 835 |
|
|---|
| 836 |
$self->{db__} = DBI->connect( $dbconnect, |
|---|
| 837 |
$self->config_( 'dbuser' ), |
|---|
| 838 |
$self->config_( 'dbauth' ), |
|---|
| 839 |
\%connection_options ); |
|---|
| 840 |
|
|---|
| 841 |
if ( !defined( $self->{db__} ) ) { |
|---|
| 842 |
$self->log_( 0, "Failed to connect to database and got error $DBI::errstr" ); |
|---|
| 843 |
return 0; |
|---|
| 844 |
} |
|---|
| 845 |
|
|---|
| 846 |
if ( $sqlite ) { |
|---|
| 847 |
$self->log_( 0, "Using SQLite library version " . $self->{db__}{sqlite_version} ); |
|---|
| 848 |
|
|---|
| 849 |
if ( $need_convert ) { |
|---|
| 850 |
$self->log_( 0, 'Convert SQLite2 database to SQLite3 database' ); |
|---|
| 851 |
|
|---|
| 852 |
$self->db_upgrade__( $old_dbh ); |
|---|
| 853 |
$old_dbh->disconnect; |
|---|
| 854 |
|
|---|
| 855 |
$self->log_( 0, 'Database convert completed' ); |
|---|
| 856 |
} |
|---|
| 857 |
|
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
if ( $self->{parser__}->{lang__} eq 'Nihongo' ) { |
|---|
| 861 |
$self->{db__}->do( 'pragma case_sensitive_like=1;' ); |
|---|
| 862 |
} |
|---|
| 863 |
} |
|---|
| 864 |
|
|---|
| 865 |
if ( !$dbpresent ) { |
|---|
| 866 |
if ( !$self->insert_schema__( $sqlite ) ) { |
|---|
| 867 |
return 0; |
|---|
| 868 |
} |
|---|
| 869 |
} |
|---|
| 870 |
|
|---|
| 871 |
|
|---|
| 872 |
|
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
|
|---|
| 876 |
|
|---|
| 877 |
open SCHEMA, '<' . $self->get_root_path_( 'Classifier/popfile.sql' ); |
|---|
| 878 |
<SCHEMA> =~ /-- POPFILE SCHEMA (\d+)/; |
|---|
| 879 |
my $version = $1; |
|---|
| 880 |
close SCHEMA; |
|---|
| 881 |
|
|---|
| 882 |
my $need_upgrade = 1; |
|---|
| 883 |
|
|---|
| 884 |
|
|---|
| 885 |
|
|---|
| 886 |
|
|---|
| 887 |
|
|---|
| 888 |
|
|---|
| 889 |
my $sqlquotechar = $self->{db__}->get_info(29) || ''; |
|---|
| 890 |
my @tables = map { s/$sqlquotechar//g; $_ } ($self->{db__}->tables()); |
|---|
| 891 |
|
|---|
| 892 |
foreach my $table (@tables) { |
|---|
| 893 |
if ( $table =~ /\.?popfile$/ ) { |
|---|
| 894 |
my @row = $self->{db__}->selectrow_array( |
|---|
| 895 |
'select version from popfile;' ); |
|---|
| 896 |
|
|---|
| 897 |
if ( $#row == 0 ) { |
|---|
| 898 |
$need_upgrade = ( $row[0] != $version ); |
|---|
| 899 |
} |
|---|
| 900 |
} |
|---|
| 901 |
} |
|---|
| 902 |
|
|---|
| 903 |
if ( $need_upgrade ) { |
|---|
| 904 |
|
|---|
| 905 |
print "\n\nDatabase schema is outdated, performing automatic upgrade\n"; |
|---|
| 906 |
|
|---|
| 907 |
|
|---|
| 908 |
|
|---|
| 909 |
$self->db_upgrade__(); |
|---|
| 910 |
|
|---|
| 911 |
print "\nDatabase upgrade complete\n\n"; |
|---|
| 912 |
} |
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 |
|
|---|
| 916 |
|
|---|
| 917 |
|
|---|
| 918 |
|
|---|
| 919 |
|
|---|
| 920 |
|
|---|
| 921 |
|
|---|
| 922 |
$self->{db_get_buckets__} = $self->{db__}->prepare( |
|---|
| 923 |
'select name, id, pseudo from buckets |
|---|
| 924 |
where buckets.userid = ?;' ); |
|---|
| 925 |
|
|---|
| 926 |
$self->{db_get_wordid__} = $self->{db__}->prepare( |
|---|
| 927 |
'select id from words |
|---|
| 928 |
where words.word = ? limit 1;' ); |
|---|
| 929 |
|
|---|
| 930 |
$self->{db_get_userid__} = $self->{db__}->prepare( |
|---|
| 931 |
'select id from users where name = ? |
|---|
| 932 |
and password = ? limit 1;' ); |
|---|
| 933 |
|
|---|
| 934 |
$self->{db_get_word_count__} = $self->{db__}->prepare( |
|---|
| 935 |
'select matrix.times from matrix |
|---|
| 936 |
where matrix.bucketid = ? and |
|---|
| 937 |
matrix.wordid = ? limit 1;' ); |
|---|
| 938 |
|
|---|
| 939 |
$self->{db_put_word_count__} = $self->{db__}->prepare( |
|---|
| 940 |
'replace into matrix ( bucketid, wordid, times ) values ( ?, ?, ? );' ); |
|---|
| 941 |
|
|---|
| 942 |
$self->{db_get_bucket_unique_counts__} = $self->{db__}->prepare( |
|---|
| 943 |
'select count(matrix.wordid), buckets.name from matrix, buckets |
|---|
| 944 |
where buckets.userid = ? |
|---|
| 945 |
and matrix.bucketid = buckets.id |
|---|
| 946 |
group by buckets.name;' ); |
|---|
| 947 |
|
|---|
| 948 |
$self->{db_get_bucket_word_counts__} = $self->{db__}->prepare( |
|---|
| 949 |
'select sum(matrix.times), buckets.name from matrix, buckets |
|---|
| 950 |
where matrix.bucketid = buckets.id |
|---|
| 951 |
and buckets.userid = ? |
|---|
| 952 |
group by buckets.name;' ); |
|---|
| 953 |
|
|---|
| 954 |
$self->{db_get_unique_word_count__} = $self->{db__}->prepare( |
|---|
| 955 |
'select count(matrix.wordid) from matrix, buckets |
|---|
| 956 |
where matrix.bucketid = buckets.id and |
|---|
| 957 |
buckets.userid = ?;' ); |
|---|
| 958 |
|
|---|
| 959 |
$self->{db_get_full_total__} = $self->{db__}->prepare( |
|---|
| 960 |
'select sum(matrix.times) from matrix, buckets |
|---|
| 961 |
where buckets.userid = ? and |
|---|
| 962 |
matrix.bucketid = buckets.id;' ); |
|---|
| 963 |
|
|---|
| 964 |
$self->{db_get_bucket_parameter__} = $self->{db__}->prepare( |
|---|
| 965 |
'select bucket_params.val from bucket_params |
|---|
| 966 |
where bucket_params.bucketid = ? and |
|---|
| 967 |
bucket_params.btid = ?;' ); |
|---|
| 968 |
|
|---|
| 969 |
$self->{db_set_bucket_parameter__} = $self->{db__}->prepare( |
|---|
| 970 |
'replace into bucket_params ( bucketid, btid, val ) values ( ?, ?, ? );' ); |
|---|
| 971 |
|
|---|
| 972 |
$self->{db_get_bucket_parameter_default__} = $self->{db__}->prepare( |
|---|
| 973 |
'select bucket_template.def from bucket_template |
|---|
| 974 |
where bucket_template.id = ?;' ); |
|---|
| 975 |
|
|---|
| 976 |
$self->{db_get_buckets_with_magnets__} = $self->{db__}->prepare( |
|---|
| 977 |
'select buckets.name from buckets, magnets |
|---|
| 978 |
where buckets.userid = ? and |
|---|
| 979 |
magnets.id != 0 and |
|---|
| 980 |
magnets.bucketid = buckets.id group by buckets.name order by buckets.name;' ); |
|---|
| 981 |
|
|---|
| 982 |
$self->{db_delete_zero_words__} = $self->{db__}->prepare( |
|---|
| 983 |
'delete from matrix |
|---|
| 984 |
where matrix.times = 0 |
|---|
| 985 |
and matrix.bucketid = ?;' ); |
|---|
| 986 |
|
|---|
| 987 |
|
|---|
| 988 |
|
|---|
| 989 |
my $h = $self->validate_sql_prepare_and_execute( "select name, id from bucket_template;" ); |
|---|
| 990 |
while ( my $row = $h->fetchrow_arrayref ) { |
|---|
| 991 |
$self->{db_parameterid__}{$row->[0]} = $row->[1]; |
|---|
| 992 |
} |
|---|
| 993 |
$h->finish; |
|---|
| 994 |
|
|---|
| 995 |
return 1; |
|---|
| 996 |
} |
|---|
| 997 |
|
|---|
| 998 |
|
|---|
| 999 |
|
|---|
| 1000 |
|
|---|
| 1001 |
|
|---|
| 1002 |
|
|---|
| 1003 |
|
|---|
| 1004 |
|
|---|
| 1005 |
|
|---|
| 1006 |
|
|---|
| 1007 |
sub insert_schema__ |
|---|
| 1008 |
{ |
|---|
| 1009 |
my ( $self, $sqlite ) = @_; |
|---|
| 1010 |
|
|---|
| 1011 |
if ( -e $self->get_root_path_( 'Classifier/popfile.sql' ) ) { |
|---|
| 1012 |
my $schema = ''; |
|---|
| 1013 |
|
|---|
| 1014 |
$self->log_( 0, "Creating database schema" ); |
|---|
| 1015 |
|
|---|
| 1016 |
open SCHEMA, '<' . $self->get_root_path_( 'Classifier/popfile.sql' ); |
|---|
| 1017 |
while ( <SCHEMA> ) { |
|---|
| 1018 |
next if ( /^--/ ); |
|---|
| 1019 |
next if ( !/[a-z;]/ ); |
|---|
| 1020 |
s/--.*$//; |
|---|
| 1021 |
|
|---|
| 1022 |
|
|---|
| 1023 |
|
|---|
| 1024 |
|
|---|
| 1025 |
if ( $sqlite && ( /^alter/i ) ) { |
|---|
| 1026 |
next; |
|---|
| 1027 |
} |
|---|
| 1028 |
|
|---|
| 1029 |
$schema .= $_; |
|---|
| 1030 |
|
|---|
| 1031 |
if ( ( /end;/ ) || ( /\);/ ) || ( /^alter/i ) ) { |
|---|
| 1032 |
$self->{db__}->do( $schema ); |
|---|
| 1033 |
$schema = ''; |
|---|
| 1034 |
} |
|---|
| 1035 |
} |
|---|
| 1036 |
close SCHEMA; |
|---|
| 1037 |
return 1; |
|---|
| 1038 |
} else { |
|---|
| 1039 |
$self->log_( 0, "Can't find the database schema" ); |
|---|
| 1040 |
return 0; |
|---|
| 1041 |
} |
|---|
| 1042 |
} |
|---|
| 1043 |
|
|---|
| 1044 |
|
|---|
| 1045 |
|
|---|
| 1046 |
|
|---|
| 1047 |
|
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 |
|
|---|
| 1051 |
|
|---|
| 1052 |
|
|---|
| 1053 |
|
|---|
| 1054 |
sub db_upgrade__ |
|---|
| 1055 |
{ |
|---|
| 1056 |
my ( $self, $db_from ) = @_; |
|---|
| 1057 |
|
|---|
| 1058 |
my $drop_table; |
|---|
| 1059 |
|
|---|
| 1060 |
if ( !defined( $db_from ) ) { |
|---|
| 1061 |
|
|---|
| 1062 |
|
|---|
| 1063 |
$drop_table = 1; |
|---|
| 1064 |
$db_from = $self->{db__}; |
|---|
| 1065 |
} |
|---|
| 1066 |
|
|---|
| 1067 |
my $from_sqlite = ( $db_from->{Driver}->{Name} =~ /SQLite/ ); |
|---|
| 1068 |
my $to_sqlite = ( $self->{db__}->{Driver}->{Name} =~ /SQLite/ ); |
|---|
| 1069 |
|
|---|
| 1070 |
my $sqlquotechar = $db_from->get_info(29) || ''; |
|---|
| 1071 |
my @tables = map { s/$sqlquotechar//g; $_ } ($db_from->tables()); |
|---|
| 1072 |
|
|---|
| 1073 |
|
|---|
| 1074 |
|
|---|
| 1075 |
|
|---|
| 1076 |
|
|---|
| 1077 |
|
|---|
| 1078 |
my $i = 0; |
|---|
| 1079 |
my $ins_file = $self->get_user_path_( 'insert.sql' ); |
|---|
| 1080 |
open INSERT, '>' . $ins_file; |
|---|
| 1081 |
|
|---|
| 1082 |
foreach my $table (@tables) { |
|---|
| 1083 |
next if ( $table =~ /\.?popfile$/ ); |
|---|
| 1084 |
if ( $from_sqlite && ( $table =~ /^sqlite_/ ) ) { |
|---|
| 1085 |
next; |
|---|
| 1086 |
} |
|---|
| 1087 |
if ( $i > 99 ) { |
|---|
| 1088 |
print "\n"; |
|---|
| 1089 |
} |
|---|
| 1090 |
print " Saving table $table\n "; |
|---|
| 1091 |
|
|---|
| 1092 |
my $t = $db_from->prepare( "select * from $table;" ); |
|---|
| 1093 |
$t->execute; |
|---|
| 1094 |
$i = 0; |
|---|
| 1095 |
while ( 1 ) { |
|---|
| 1096 |
if ( ( ++$i % 100 ) == 0 ) { |
|---|
| 1097 |
print "[$i]"; |
|---|
| 1098 |
flush STDOUT; |
|---|
| 1099 |
} |
|---|
| 1100 |
if ( ( $i % 1000 ) == 0 ) { |
|---|
| 1101 |
print "\n"; |
|---|
| 1102 |
flush STDOUT; |
|---|
| 1103 |
} |
|---|
| 1104 |
my $rows = $t->fetchrow_arrayref; |
|---|
| 1105 |
|
|---|
| 1106 |
last if ( !defined( $rows ) ); |
|---|
| 1107 |
|
|---|
| 1108 |
if ( $to_sqlite ) { |
|---|
| 1109 |
print INSERT "INSERT OR IGNORE INTO $table ("; |
|---|
| 1110 |
} else { |
|---|
| 1111 |
print INSERT "INSERT INTO $table ("; |
|---|
| 1112 |
} |
|---|
| 1113 |
for my $i (0..$t->{NUM_OF_FIELDS}-1) { |
|---|
| 1114 |
if ( $i != 0 ) { |
|---|
| 1115 |
print INSERT ','; |
|---|
| 1116 |
} |
|---|
| 1117 |
print INSERT $t->{NAME}->[$i]; |
|---|
| 1118 |
} |
|---|
| 1119 |
print INSERT ') VALUES ('; |
|---|
| 1120 |
for my $i (0..$t->{NUM_OF_FIELDS}-1) { |
|---|
| 1121 |
if ( $i != 0 ) { |
|---|
| 1122 |
print INSERT ','; |
|---|
| 1123 |
} |
|---|
| 1124 |
my $val = $rows->[$i]; |
|---|
| 1125 |
if ( $t->{TYPE}->[$i] !~ /^int/i ) { |
|---|
| 1126 |
$val = '' if ( !defined( $val ) ); |
|---|
| 1127 |
$val = $self->db_quote( $val ); |
|---|
| 1128 |
} else { |
|---|
| 1129 |
$val = 'NULL' if ( !defined( $val ) ); |
|---|
| 1130 |
} |
|---|
| 1131 |
print INSERT $val; |
|---|
| 1132 |
} |
|---|
| 1133 |
print INSERT ");\n"; |
|---|
| 1134 |
} |
|---|
| 1135 |
$t->finish; |
|---|
| 1136 |
} |
|---|
| 1137 |
|
|---|
| 1138 |
close INSERT; |
|---|
| 1139 |
|
|---|
| 1140 |
if ( $i > 99 ) { |
|---|
| 1141 |
print "\n"; |
|---|
| 1142 |
} |
|---|
| 1143 |
|
|---|
| 1144 |
if ( $drop_table ) { |
|---|
| 1145 |
foreach my $table (@tables) { |
|---|
| 1146 |
if ( $from_sqlite && ( $table =~ /^sqlite_/ ) ) { |
|---|
| 1147 |
next; |
|---|
| 1148 |
} |
|---|
| 1149 |
print " Dropping old table $table\n"; |
|---|
| 1150 |
$self->{db__}->do( "DROP TABLE $table;" ); |
|---|
| 1151 |
} |
|---|
| 1152 |
} |
|---|
| 1153 |
|
|---|
| 1154 |
print " Inserting new database schema\n"; |
|---|
| 1155 |
if ( !$self->insert_schema__( $to_sqlite ) ) { |
|---|
| 1156 |
return 0; |
|---|
| 1157 |
} |
|---|
| 1158 |
|
|---|
| 1159 |
print " Restoring old data\n "; |
|---|
| 1160 |
|
|---|
| 1161 |
$self->{db__}->begin_work; |
|---|
| 1162 |
open INSERT, '<' . $ins_file; |
|---|
| 1163 |
$i = 0; |
|---|
| 1164 |
while ( <INSERT> ) { |
|---|
| 1165 |
if ( ( ++$i % 100 ) == 0 ) { |
|---|
| 1166 |
print "[$i]"; |
|---|
| 1167 |
flush STDOUT; |
|---|
| 1168 |
} |
|---|
| 1169 |
if ( ( $i % 1000 ) == 0 ) { |
|---|
| 1170 |
print "\n"; |
|---|
| 1171 |
flush STDOUT; |
|---|
| 1172 |
} |
|---|
| 1173 |
s/[\r\n]//g; |
|---|
| 1174 |
$self->{db__}->do( $_ ); |
|---|
| 1175 |
} |
|---|
| 1176 |
close INSERT; |
|---|
| 1177 |
$self->{db__}->commit; |
|---|
| 1178 |
|
|---|
| 1179 |
unlink $ins_file; |
|---|
| 1180 |
} |
|---|
| 1181 |
|
|---|
| 1182 |
|
|---|
| 1183 |
|
|---|
| 1184 |
|
|---|
| 1185 |
|
|---|
| 1186 |
|
|---|
| 1187 |
|
|---|
| 1188 |
|
|---|
| 1189 |
sub db_disconnect__ |
|---|
| 1190 |
{ |
|---|
| 1191 |
my ( $self ) = @_; |
|---|
| 1192 |
|
|---|
| 1193 |
$self->{db_get_buckets__}->finish; |
|---|
| 1194 |
$self->{db_get_wordid__}->finish; |
|---|
| 1195 |
$self->{db_get_userid__}->finish; |
|---|
| 1196 |
$self->{db_get_word_count__}->finish; |
|---|
| 1197 |
$self->{db_put_word_count__}->finish; |
|---|
| 1198 |
$self->{db_get_bucket_unique_counts__}->finish; |
|---|
| 1199 |
$self->{db_get_bucket_word_counts__}->finish; |
|---|
| 1200 |
$self->{db_get_unique_word_count__}->finish; |
|---|
| 1201 |
$self->{db_get_full_total__}->finish; |
|---|
| 1202 |
$self->{db_get_bucket_parameter__}->finish; |
|---|
| 1203 |
$self->{db_set_bucket_parameter__}->finish; |
|---|
| 1204 |
$self->{db_get_bucket_parameter_default__}->finish; |
|---|
| 1205 |
$self->{db_get_buckets_with_magnets__}->finish; |
|---|
| 1206 |
$self->{db_delete_zero_words__}->finish; |
|---|
| 1207 |
|
|---|
| 1208 |
|
|---|
| 1209 |
|
|---|
| 1210 |
undef $self->{db_get_buckets__}; |
|---|
| 1211 |
undef $self->{db_get_wordid__}; |
|---|
| 1212 |
undef $self->{db_get_userid__}; |
|---|
| 1213 |
undef $self->{db_get_word_count__}; |
|---|
| 1214 |
undef $self->{db_put_word_count__}; |
|---|
| 1215 |
undef $self->{db_get_bucket_unique_counts__}; |
|---|
| 1216 |
undef $self->{db_get_bucket_word_counts__}; |
|---|
| 1217 |
undef $self->{db_get_unique_word_count__}; |
|---|
| 1218 |
undef $self->{db_get_full_total__}; |
|---|
| 1219 |
undef $self->{db_get_bucket_parameter__}; |
|---|
| 1220 |
undef $self->{db_set_bucket_parameter__}; |
|---|
| 1221 |
undef $self->{db_get_bucket_parameter_default__}; |
|---|
| 1222 |
undef $self->{db_get_buckets_with_magnets__}; |
|---|
| 1223 |
undef $self->{db_delete_zero_words__}; |
|---|
| 1224 |
|
|---|
| 1225 |
if ( defined( $self->{db__} ) ) { |
|---|
| 1226 |
$self->{db__}->disconnect; |
|---|
| 1227 |
undef $self->{db__}; |
|---|
| 1228 |
} |
|---|
| 1229 |
} |
|---|
| 1230 |
|
|---|
| 1231 |
|
|---|
| 1232 |
|
|---|
| 1233 |
|
|---|
| 1234 |
|
|---|
| 1235 |
|
|---|
| 1236 |
|
|---|
| 1237 |
|
|---|
| 1238 |
|
|---|
| 1239 |
|
|---|
| 1240 |
sub db_update_cache__ |
|---|
| 1241 |
{ |
|---|
| 1242 |
my ( $self, $session ) = @_; |
|---|
| 1243 |
|
|---|
| 1244 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 1245 |
return undef if ( !defined( $userid ) ); |
|---|
| 1246 |
|
|---|
| 1247 |
delete $self->{db_bucketid__}{$userid}; |
|---|
| 1248 |
|
|---|
| 1249 |
$self->validate_sql_prepare_and_execute( $self->{db_get_buckets__}, $userid ); |
|---|
| 1250 |
while ( my $row = $self->{db_get_buckets__}->fetchrow_arrayref ) { |
|---|
| 1251 |
$self->{db_bucketid__}{$userid}{$row->[0]}{id} = $row->[1]; |
|---|
| 1252 |
$self->{db_bucketid__}{$userid}{$row->[0]}{pseudo} = $row->[2]; |
|---|
| 1253 |
$self->{db_bucketcount__}{$userid}{$row->[0]} = 0; |
|---|
| 1254 |
} |
|---|
| 1255 |
|
|---|
| 1256 |
$self->validate_sql_prepare_and_execute( $self->{db_get_bucket_word_counts__}, $userid ); |
|---|
| 1257 |
|
|---|
| 1258 |
for my $b (sort keys %{$self->{db_bucketid__}{$userid}}) { |
|---|
| 1259 |
$self->{db_bucketcount__}{$userid}{$b} = 0; |
|---|
| 1260 |
$self->{db_bucketunique__}{$userid}{$b} = 0; |
|---|
| 1261 |
} |
|---|
| 1262 |
|
|---|
| 1263 |
while ( my $row = $self->{db_get_bucket_word_counts__}->fetchrow_arrayref ) { |
|---|
| 1264 |
$self->{db_bucketcount__}{$userid}{$row->[1]} = $row->[0]; |
|---|
| 1265 |
} |
|---|
| 1266 |
|
|---|
| 1267 |
$self->validate_sql_prepare_and_execute( $self->{db_get_bucket_unique_counts__}, $userid ); |
|---|
| 1268 |
|
|---|
| 1269 |
while ( my $row = $self->{db_get_bucket_unique_counts__}->fetchrow_arrayref ) { |
|---|
| 1270 |
$self->{db_bucketunique__}{$userid}{$row->[1]} = $row->[0]; |
|---|
| 1271 |
} |
|---|
| 1272 |
|
|---|
| 1273 |
$self->update_constants__( $session ); |
|---|
| 1274 |
} |
|---|
| 1275 |
|
|---|
| 1276 |
|
|---|
| 1277 |
|
|---|
| 1278 |
|
|---|
| 1279 |
|
|---|
| 1280 |
|
|---|
| 1281 |
|
|---|
| 1282 |
|
|---|
| 1283 |
|
|---|
| 1284 |
|
|---|
| 1285 |
|
|---|
| 1286 |
|
|---|
| 1287 |
|
|---|
| 1288 |
sub db_get_word_count__ |
|---|
| 1289 |
{ |
|---|
| 1290 |
my ( $self, $session, $bucket, $word ) = @_; |
|---|
| 1291 |
|
|---|
| 1292 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 1293 |
return undef if ( !defined( $userid ) ); |
|---|
| 1294 |
|
|---|
| 1295 |
$self->validate_sql_prepare_and_execute( $self->{db_get_wordid__}, $word ); |
|---|
| 1296 |
my $result = $self->{db_get_wordid__}->fetchrow_arrayref; |
|---|
| 1297 |
if ( !defined( $result ) ) { |
|---|
| 1298 |
return undef; |
|---|
| 1299 |
} |
|---|
| 1300 |
|
|---|
| 1301 |
my $wordid = $result->[0]; |
|---|
| 1302 |
|
|---|
| 1303 |
$self->validate_sql_prepare_and_execute( $self->{db_get_word_count__}, $self->{db_bucketid__}{$userid}{$bucket}{id}, $wordid ); |
|---|
| 1304 |
$result = $self->{db_get_word_count__}->fetchrow_arrayref; |
|---|
| 1305 |
if ( defined( $result ) ) { |
|---|
| 1306 |
return $result->[0]; |
|---|
| 1307 |
} else { |
|---|
| 1308 |
return undef; |
|---|
| 1309 |
} |
|---|
| 1310 |
} |
|---|
| 1311 |
|
|---|
| 1312 |
|
|---|
| 1313 |
|
|---|
| 1314 |
|
|---|
| 1315 |
|
|---|
| 1316 |
|
|---|
| 1317 |
|
|---|
| 1318 |
|
|---|
| 1319 |
|
|---|
| 1320 |
|
|---|
| 1321 |
|
|---|
| 1322 |
|
|---|
| 1323 |
|
|---|
| 1324 |
|
|---|
| 1325 |
sub db_put_word_count__ |
|---|
| 1326 |
{ |
|---|
| 1327 |
my ( $self, $session, $bucket, $word, $count ) = @_; |
|---|
| 1328 |
|
|---|
| 1329 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 1330 |
return undef if ( !defined( $userid ) ); |
|---|
| 1331 |
|
|---|
| 1332 |
|
|---|
| 1333 |
|
|---|
| 1334 |
|
|---|
| 1335 |
|
|---|
| 1336 |
$word = $self->db_quote($word); |
|---|
| 1337 |
|
|---|
| 1338 |
my $result = $self->{db__}->selectrow_arrayref( |
|---|
| 1339 |
"select words.id from words where words.word = $word limit 1;"); |
|---|
| 1340 |
|
|---|
| 1341 |
if ( !defined( $result ) ) { |
|---|
| 1342 |
$self->{db__}->do( "insert into words ( word ) values ( $word );" ); |
|---|
| 1343 |
$result = $self->{db__}->selectrow_arrayref( |
|---|
| 1344 |
"select words.id from words where words.word = $word limit 1;"); |
|---|
| 1345 |
} |
|---|
| 1346 |
|
|---|
| 1347 |
my $wordid = $result->[0]; |
|---|
| 1348 |
my $bucketid = $self->{db_bucketid__}{$userid}{$bucket}{id}; |
|---|
| 1349 |
|
|---|
| 1350 |
$self->validate_sql_prepare_and_execute( $self->{db_put_word_count__}, $bucketid, $wordid, $count ); |
|---|
| 1351 |
|
|---|
| 1352 |
return 1; |
|---|
| 1353 |
} |
|---|
| 1354 |
|
|---|
| 1355 |
|
|---|
| 1356 |
|
|---|
| 1357 |
|
|---|
| 1358 |
|
|---|
| 1359 |
|
|---|
| 1360 |
|
|---|
| 1361 |
|
|---|
| 1362 |
|
|---|
| 1363 |
sub upgrade_predatabase_data__ |
|---|
| 1364 |
{ |
|---|
| 1365 |
my ( $self ) = @_; |
|---|
| 1366 |
my $c = 0; |
|---|
| 1367 |
|
|---|
| 1368 |
|
|---|
| 1369 |
|
|---|
| 1370 |
|
|---|
| 1371 |
|
|---|
| 1372 |
my $session = $self->get_session_key( 'admin', '' ); |
|---|
| 1373 |
|
|---|
| 1374 |
if ( !defined( $session ) ) { |
|---|
| 1375 |
$self->log_( 0, "Tried to get the session key for user admin and failed; cannot upgrade old data" ); |
|---|
| 1376 |
return; |
|---|
| 1377 |
} |
|---|
| 1378 |
|
|---|
| 1379 |
my @buckets = glob $self->get_user_path_( $self->config_( 'corpus' ) . '/*' ); |
|---|
| 1380 |
|
|---|
| 1381 |
foreach my $bucket (@buckets) { |
|---|
| 1382 |
|
|---|
| 1383 |
|
|---|
| 1384 |
|
|---|
| 1385 |
next unless ( -d $bucket ); |
|---|
| 1386 |
next unless ( ( -e "$bucket/table" ) || ( -e "$bucket/table.db" ) ); |
|---|
| 1387 |
|
|---|
| 1388 |
return 0 if ( !$self->upgrade_bucket__( $session, $bucket ) ); |
|---|
| 1389 |
|
|---|
| 1390 |
my $color = ''; |
|---|
| 1391 |
|
|---|
| 1392 |
|
|---|
| 1393 |
if ( open COLOR, '<' . "$bucket/color" ) { |
|---|
| 1394 |
$color = <COLOR>; |
|---|
| 1395 |
|
|---|
| 1396 |
|
|---|
| 1397 |
|
|---|
| 1398 |
|
|---|
| 1399 |
|
|---|
| 1400 |
|
|---|
| 1401 |
|
|---|
| 1402 |
|
|---|
| 1403 |
if ( !defined( $color ) ) { |
|---|
| 1404 |
$color = ''; |
|---|
| 1405 |
} else { |
|---|
| 1406 |
$color =~ s/[\r\n]//g; |
|---|
| 1407 |
} |
|---|
| 1408 |
close COLOR; |
|---|
| 1409 |
unlink "$bucket/color"; |
|---|
| 1410 |
} |
|---|
| 1411 |
|
|---|
| 1412 |
$bucket =~ /([[:alpha:]0-9-_]+)$/; |
|---|
| 1413 |
$bucket = $1; |
|---|
| 1414 |
|
|---|
| 1415 |
$self->set_bucket_color( $session, $bucket, ($color eq '')?$self->{possible_colors__}[$c]:$color ); |
|---|
| 1416 |
|
|---|
| 1417 |
$c = ($c+1) % ($#{$self->{possible_colors__}}+1); |
|---|
| 1418 |
} |
|---|
| 1419 |
|
|---|
| 1420 |
$self->release_session_key( $session ); |
|---|
| 1421 |
|
|---|
| 1422 |
return 1; |
|---|
| 1423 |
} |
|---|
| 1424 |
|
|---|
| 1425 |
|
|---|
| 1426 |
|
|---|
| 1427 |
|
|---|
| 1428 |
|
|---|
| 1429 |
|
|---|
| 1430 |
|
|---|
| 1431 |
|
|---|
| 1432 |
|
|---|
| 1433 |
|
|---|
| 1434 |
|
|---|
| 1435 |
sub upgrade_bucket__ |
|---|
| 1436 |
{ |
|---|
| 1437 |
my ( $self, $session, $bucket ) = @_; |
|---|
| 1438 |
|
|---|
| 1439 |
$bucket =~ /([[:alpha:]0-9-_]+)$/; |
|---|
| 1440 |
$bucket = $1; |
|---|
| 1441 |
|
|---|
| 1442 |
$self->create_bucket( $session, $bucket ); |
|---|
| 1443 |
|
|---|
| 1444 |
if ( open PARAMS, '<' . $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/params" ) ) { |
|---|
| 1445 |
while ( <PARAMS> ) { |
|---|
| 1446 |
s/[\r\n]//g; |
|---|
| 1447 |
if ( /^([[:lower:]]+) ([^\r\n\t ]+)$/ ) { |
|---|
| 1448 |
$self->set_bucket_parameter( $session, $bucket, $1, $2 ); |
|---|
| 1449 |
} |
|---|
| 1450 |
} |
|---|
| 1451 |
close PARAMS; |
|---|
| 1452 |
unlink $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/params" ); |
|---|
| 1453 |
} |
|---|
| 1454 |
|
|---|
| 1455 |
|
|---|
| 1456 |
|
|---|
| 1457 |
|
|---|
| 1458 |
|
|---|
| 1459 |
|
|---|
| 1460 |
foreach my $gl ( 'subject', 'xtc', 'xpl' ) { |
|---|
| 1461 |
$self->log_( 1, "Checking deprecated parameter GLOBAL_$gl for $bucket\n" ); |
|---|
| 1462 |
my $val = $self->{configuration__}->deprecated_parameter( "GLOBAL_$gl" ); |
|---|
| 1463 |
if ( defined( $val ) && ( $val == 0 ) ) { |
|---|
| 1464 |
$self->log_( 1, "GLOBAL_$gl is 0 for $bucket, overriding $gl\n" ); |
|---|
| 1465 |
$self->set_bucket_parameter( $session, $bucket, $gl, 0 ); |
|---|
| 1466 |
} |
|---|
| 1467 |
} |
|---|
| 1468 |
|
|---|
| 1469 |
|
|---|
| 1470 |
if ( open MAGNETS, '<' . $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/magnets" ) ) { |
|---|
| 1471 |
while ( <MAGNETS> ) { |
|---|
| 1472 |
s/[\r\n]//g; |
|---|
| 1473 |
|
|---|
| 1474 |
|
|---|
| 1475 |
|
|---|
| 1476 |
|
|---|
| 1477 |
|
|---|
| 1478 |
|
|---|
| 1479 |
if ( /^([^ ]+) (.+)$/ ) { |
|---|
| 1480 |
my $type = $1; |
|---|
| 1481 |
my $value = $2; |
|---|
| 1482 |
|
|---|
| 1483 |
|
|---|
| 1484 |
|
|---|
| 1485 |
|
|---|
| 1486 |
|
|---|
| 1487 |
|
|---|
| 1488 |
$value =~ s/^[ \t]+//g; |
|---|
| 1489 |
$value =~ s/[ \t]+$//g; |
|---|
| 1490 |
|
|---|
| 1491 |
$value =~ s/\\(\?|\*|\||\(|\)|\[|\]|\{|\}|\^|\$|\.)/$1/g; |
|---|
| 1492 |
$self->create_magnet( $session, $bucket, $type, $value ); |
|---|
| 1493 |
} else { |
|---|
| 1494 |
|
|---|
| 1495 |
|
|---|
| 1496 |
|
|---|
| 1497 |
|
|---|
| 1498 |
|
|---|
| 1499 |
if ( /^(.+)$/ ) { |
|---|
| 1500 |
my $value = $1; |
|---|
| 1501 |
$value =~ s/\\(\?|\*|\||\(|\)|\[|\]|\{|\}|\^|\$|\.)/$1/g; |
|---|
| 1502 |
$self->create_magnet( $session, $bucket, 'from', $value ); |
|---|
| 1503 |
} |
|---|
| 1504 |
} |
|---|
| 1505 |
} |
|---|
| 1506 |
close MAGNETS; |
|---|
| 1507 |
unlink $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/magnets" ); |
|---|
| 1508 |
} |
|---|
| 1509 |
|
|---|
| 1510 |
|
|---|
| 1511 |
|
|---|
| 1512 |
|
|---|
| 1513 |
|
|---|
| 1514 |
if ( -e $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/table" ) ) { |
|---|
| 1515 |
$self->log_( 0, "Performing automatic upgrade of $bucket corpus from flat file to DBI" ); |
|---|
| 1516 |
|
|---|
| 1517 |
$self->{db__}->begin_work; |
|---|
| 1518 |
|
|---|
| 1519 |
if ( open WORDS, '<' . $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/table" ) ) { |
|---|
| 1520 |
|
|---|
| 1521 |
my $wc = 1; |
|---|
| 1522 |
|
|---|
| 1523 |
my $first = <WORDS>; |
|---|
| 1524 |
if ( defined( $first ) && ( $first =~ s/^__CORPUS__ __VERSION__ (\d+)// ) ) { |
|---|
| 1525 |
if ( $1 != $self->{corpus_version__} ) { |
|---|
| 1526 |
print STDERR "Incompatible corpus version in $bucket\n"; |
|---|
| 1527 |
close WORDS; |
|---|
| 1528 |
$self->{db__}->rollback; |
|---|
| 1529 |
return 0; |
|---|
| 1530 |
} else { |
|---|
| 1531 |
$self->log_( 0, "Upgrading bucket $bucket..." ); |
|---|
| 1532 |
|
|---|
| 1533 |
while ( <WORDS> ) { |
|---|
| 1534 |
if ( $wc % 100 == 0 ) { |
|---|
| 1535 |
$self->log_( 0, "$wc" ); |
|---|
| 1536 |
} |
|---|
| 1537 |
$wc += 1; |
|---|
| 1538 |
s/[\r\n]//g; |
|---|
| 1539 |
|
|---|
| 1540 |
if ( /^([^\s]+) (\d+)$/ ) { |
|---|
| 1541 |
if ( $2 != 0 ) { |
|---|
| 1542 |
$self->db_put_word_count__( $session, $bucket, $1, $2 ); |
|---|
| 1543 |
} |
|---|
| 1544 |
} else { |
|---|
| 1545 |
$self->log_( 0, "Found entry in corpus for $bucket that looks wrong: \"$_\" (ignoring)" ); |
|---|
| 1546 |
} |
|---|
| 1547 |
} |
|---|
| 1548 |
} |
|---|
| 1549 |
|
|---|
| 1550 |
if ( $wc > 1 ) { |
|---|
| 1551 |
$wc -= 1; |
|---|
| 1552 |
$self->log_( 0, "(completed $wc words)" ); |
|---|
| 1553 |
} |
|---|
| 1554 |
close WORDS; |
|---|
| 1555 |
} else { |
|---|
| 1556 |
close WORDS; |
|---|
| 1557 |
$self->{db__}->rollback; |
|---|
| 1558 |
unlink $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/table" ); |
|---|
| 1559 |
return 0; |
|---|
| 1560 |
} |
|---|
| 1561 |
|
|---|
| 1562 |
$self->{db__}->commit; |
|---|
| 1563 |
unlink $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/table" ); |
|---|
| 1564 |
} |
|---|
| 1565 |
} |
|---|
| 1566 |
|
|---|
| 1567 |
|
|---|
| 1568 |
|
|---|
| 1569 |
my $bdb_file = $self->get_user_path_( $self->config_( 'corpus' ) . "/$bucket/table.db" ); |
|---|
| 1570 |
|
|---|
| 1571 |
if ( -e $bdb_file ) { |
|---|
| 1572 |
$self->log_( 0, "Performing automatic upgrade of $bucket corpus from BerkeleyDB to DBI" ); |
|---|
| 1573 |
|
|---|
| 1574 |
require BerkeleyDB; |
|---|
| 1575 |
|
|---|
| 1576 |
my %h; |
|---|
| 1577 |
tie %h, "BerkeleyDB::Hash", -Filename => $bdb_file; |
|---|
| 1578 |
|
|---|
| 1579 |
$self->log_( 0, "Upgrading bucket $bucket..." ); |
|---|
| 1580 |
$self->{db__}->begin_work; |
|---|
| 1581 |
|
|---|
| 1582 |
my $wc = 1; |
|---|
| 1583 |
|
|---|
| 1584 |
for my $word (keys %h) { |
|---|
| 1585 |
if ( $wc % 100 == 0 ) { |
|---|
| 1586 |
$self->log_( 0, "$wc" ); |
|---|
| 1587 |
} |
|---|
| 1588 |
|
|---|
| 1589 |
next if ( $word =~ /__POPFILE__(LOG__TOTAL|TOTAL|UNIQUE)__/ ); |
|---|
| 1590 |
|
|---|
| 1591 |
$wc += 1; |
|---|
| 1592 |
if ( $h{$word} != 0 ) { |
|---|
| 1593 |
$self->db_put_word_count__( $session, $bucket, $word, $h{$word} ); |
|---|
| 1594 |
} |
|---|
| 1595 |
} |
|---|
| 1596 |
|
|---|
| 1597 |
$wc -= 1; |
|---|
| 1598 |
$self->log_( 0, "(completed $wc words)" ); |
|---|
| 1599 |
$self->{db__}->commit; |
|---|
| 1600 |
untie %h; |
|---|
| 1601 |
unlink $bdb_file; |
|---|
| 1602 |
} |
|---|
| 1603 |
|
|---|
| 1604 |
return 1; |
|---|
| 1605 |
} |
|---|
| 1606 |
|
|---|
| 1607 |
|
|---|
| 1608 |
|
|---|
| 1609 |
|
|---|
| 1610 |
|
|---|
| 1611 |
|
|---|
| 1612 |
|
|---|
| 1613 |
|
|---|
| 1614 |
|
|---|
| 1615 |
|
|---|
| 1616 |
|
|---|
| 1617 |
|
|---|
| 1618 |
|
|---|
| 1619 |
|
|---|
| 1620 |
sub magnet_match_helper__ |
|---|
| 1621 |
{ |
|---|
| 1622 |
my ( $self, $session, $match, $bucket, $type ) = @_; |
|---|
| 1623 |
|
|---|
| 1624 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 1625 |
return undef if ( !defined( $userid ) ); |
|---|
| 1626 |
|
|---|
| 1627 |
$match = lc($match); |
|---|
| 1628 |
|
|---|
| 1629 |
my @magnets; |
|---|
| 1630 |
|
|---|
| 1631 |
my $bucketid = $self->{db_bucketid__}{$userid}{$bucket}{id}; |
|---|
| 1632 |
my $h = $self->validate_sql_prepare_and_execute( |
|---|
| 1633 |
"select magnets.val, magnets.id from magnets, users, buckets, magnet_types |
|---|
| 1634 |
where buckets.id = $bucketid and |
|---|
| 1635 |
magnets.id != 0 and |
|---|
| 1636 |
users.id = buckets.userid and |
|---|
| 1637 |
magnets.bucketid = buckets.id and |
|---|
| 1638 |
magnet_types.mtype = '$type' and |
|---|
| 1639 |
magnets.mtid = magnet_types.id order by magnets.val;" ); |
|---|
| 1640 |
while ( my $row = $h->fetchrow_arrayref ) { |
|---|
| 1641 |
push @magnets, [$row->[0], $row->[1]]; |
|---|
| 1642 |
} |
|---|
| 1643 |
$h->finish; |
|---|
| 1644 |
|
|---|
| 1645 |
foreach my $m (@magnets) { |
|---|
| 1646 |
my ( $magnet, $id ) = @{$m}; |
|---|
| 1647 |
$magnet = lc($magnet); |
|---|
| 1648 |
|
|---|
| 1649 |
for my $i (0..(length($match)-length($magnet))) { |
|---|
| 1650 |
if ( substr( $match, $i, length($magnet)) eq $magnet ) { |
|---|
| 1651 |
$self->{magnet_used__} = 1; |
|---|
| 1652 |
$self->{magnet_detail__} = $id; |
|---|
| 1653 |
|
|---|
| 1654 |
return 1; |
|---|
| 1655 |
} |
|---|
| 1656 |
} |
|---|
| 1657 |
} |
|---|
| 1658 |
|
|---|
| 1659 |
return 0; |
|---|
| 1660 |
} |
|---|
| 1661 |
|
|---|
| 1662 |
|
|---|
| 1663 |
|
|---|
| 1664 |
|
|---|
| 1665 |
|
|---|
| 1666 |
|
|---|
| 1667 |
|
|---|
| 1668 |
|
|---|
| 1669 |
|
|---|
| 1670 |
|
|---|
| 1671 |
|
|---|
| 1672 |
|
|---|
| 1673 |
|
|---|
| 1674 |
|
|---|
| 1675 |
sub magnet_match__ |
|---|
| 1676 |
{ |
|---|
| 1677 |
my ( $self, $session, $match, $bucket, $type ) = @_; |
|---|
| 1678 |
|
|---|
| 1679 |
return $self->magnet_match_helper__( $session, $match, $bucket, $type ); |
|---|
| 1680 |
} |
|---|
| 1681 |
|
|---|
| 1682 |
|
|---|
| 1683 |
|
|---|
| 1684 |
|
|---|
| 1685 |
|
|---|
| 1686 |
|
|---|
| 1687 |
|
|---|
| 1688 |
|
|---|
| 1689 |
|
|---|
| 1690 |
|
|---|
| 1691 |
|
|---|
| 1692 |
|
|---|
| 1693 |
|
|---|
| 1694 |
sub write_line__ |
|---|
| 1695 |
{ |
|---|
| 1696 |
my ( $self, $file, $line, $class ) = @_; |
|---|
| 1697 |
|
|---|
| 1698 |
print $file $line if defined( $file ); |
|---|
| 1699 |
|
|---|
| 1700 |
if ( $class eq '' ) { |
|---|
| 1701 |
$self->{parser__}->parse_line( $line ); |
|---|
| 1702 |
} |
|---|
| 1703 |
} |
|---|
| 1704 |
|
|---|
| 1705 |
|
|---|
| 1706 |
|
|---|
| 1707 |
|
|---|
| 1708 |
|
|---|
| 1709 |
|
|---|
| 1710 |
|
|---|
| 1711 |
|
|---|
| 1712 |
|
|---|
| 1713 |
|
|---|
| 1714 |
|
|---|
| 1715 |
|
|---|
| 1716 |
|
|---|
| 1717 |
|
|---|
| 1718 |
sub add_words_to_bucket__ |
|---|
| 1719 |
{ |
|---|
| 1720 |
my ( $self, $session, $bucket, $subtract ) = @_; |
|---|
| 1721 |
|
|---|
| 1722 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 1723 |
return undef if ( !defined( $userid ) ); |
|---|
| 1724 |
|
|---|
| 1725 |
|
|---|
| 1726 |
|
|---|
| 1727 |
|
|---|
| 1728 |
my $words; |
|---|
| 1729 |
$words = join( ',', map( $self->{db__}->quote( $_ ), (sort keys %{$self->{parser__}{words__}}) ) ); |
|---|
| 1730 |
$self->{get_wordids__} = $self->validate_sql_prepare_and_execute( |
|---|
| 1731 |
"select id, word |
|---|
| 1732 |
from words |
|---|
| 1733 |
where word in ( $words );" ); |
|---|
| 1734 |
|
|---|
| 1735 |
my @id_list; |
|---|
| 1736 |
my %wordmap; |
|---|
| 1737 |
|
|---|
| 1738 |
while ( my $row = $self->{get_wordids__}->fetchrow_arrayref ) { |
|---|
| 1739 |
push @id_list, ($row->[0]); |
|---|
| 1740 |
$wordmap{$row->[1]} = $row->[0]; |
|---|
| 1741 |
} |
|---|
| 1742 |
|
|---|
| 1743 |
$self->{get_wordids__}->finish; |
|---|
| 1744 |
undef $self->{get_wordids__}; |
|---|
| 1745 |
|
|---|
| 1746 |
my $ids = join( ',', @id_list ); |
|---|
| 1747 |
|
|---|
| 1748 |
$self->{db_getwords__} = $self->validate_sql_prepare_and_execute( |
|---|
| 1749 |
"select matrix.times, matrix.wordid |
|---|
| 1750 |
from matrix |
|---|
| 1751 |
where matrix.wordid in ( $ids ) |
|---|
| 1752 |
and matrix.bucketid = $self->{db_bucketid__}{$userid}{$bucket}{id};" ); |
|---|
| 1753 |
|
|---|
| 1754 |
my %counts; |
|---|
| 1755 |
|
|---|
| 1756 |
while ( my $row = $self->{db_getwords__}->fetchrow_arrayref ) { |
|---|
| 1757 |
$counts{$row->[1]} = $row->[0]; |
|---|
| 1758 |
} |
|---|
| 1759 |
|
|---|
| 1760 |
$self->{db_getwords__}->finish; |
|---|
| 1761 |
undef $self->{db_getwords__}; |
|---|
| 1762 |
|
|---|
| 1763 |
$self->{db__}->begin_work; |
|---|
| 1764 |
foreach my $word (keys %{$self->{parser__}->{words__}}) { |
|---|
| 1765 |
|
|---|
| 1766 |
|
|---|
| 1767 |
|
|---|
| 1768 |
|
|---|
| 1769 |
|
|---|
| 1770 |
|
|---|
| 1771 |
|
|---|
| 1772 |
if ( defined( $wordmap{$word} ) && defined( $counts{$wordmap{$word}} ) ) { |
|---|
| 1773 |
$self->validate_sql_prepare_and_execute( $self->{db_put_word_count__}, $self->{db_bucketid__}{$userid}{$bucket}{id}, |
|---|
| 1774 |
$wordmap{$word}, $counts{$wordmap{$word}} + $subtract * $self->{parser__}->{words__}{$word} ); |
|---|
| 1775 |
} else { |
|---|
| 1776 |
|
|---|
| 1777 |
|
|---|
| 1778 |
|
|---|
| 1779 |
|
|---|
| 1780 |
|
|---|
| 1781 |
if ( $subtract == 1 ) { |
|---|
| 1782 |
$self->db_put_word_count__( $session, $bucket, $word, $self->{parser__}->{words__}{$word} ); |
|---|
| 1783 |
} |
|---|
| 1784 |
} |
|---|
| 1785 |
} |
|---|
| 1786 |
|
|---|
| 1787 |
|
|---|
| 1788 |
|
|---|
| 1789 |
|
|---|
| 1790 |
|
|---|
| 1791 |
if ( $subtract == -1 ) { |
|---|
| 1792 |
$self->validate_sql_prepare_and_execute( $self->{db_delete_zero_words__}, $self->{db_bucketid__}{$userid}{$bucket}{id} ); |
|---|
| 1793 |
} |
|---|
| 1794 |
|
|---|
| 1795 |
$self->{db__}->commit; |
|---|
| 1796 |
} |
|---|
| 1797 |
|
|---|
| 1798 |
|
|---|
| 1799 |
|
|---|
| 1800 |
|
|---|
| 1801 |
|
|---|
| 1802 |
|
|---|
| 1803 |
|
|---|
| 1804 |
|
|---|
| 1805 |
|
|---|
| 1806 |
|
|---|
| 1807 |
|
|---|
| 1808 |
|
|---|
| 1809 |
|
|---|
| 1810 |
|
|---|
| 1811 |
|
|---|
| 1812 |
|
|---|
| 1813 |
|
|---|
| 1814 |
|
|---|
| 1815 |
|
|---|
| 1816 |
|
|---|
| 1817 |
|
|---|
| 1818 |
sub echo_to_dot_ |
|---|
| 1819 |
{ |
|---|
| 1820 |
my ( $self, $mail, $client, $file, $before ) = @_; |
|---|
| 1821 |
|
|---|
| 1822 |
my $hit_dot = 0; |
|---|
| 1823 |
|
|---|
| 1824 |
my $isopen = open FILE, "$file" if ( defined( $file ) ); |
|---|
| 1825 |
binmode FILE if ($isopen); |
|---|
| 1826 |
|
|---|
| 1827 |
while ( my $line = $self->slurp_( $mail ) ) { |
|---|
| 1828 |
|
|---|
| 1829 |
|
|---|
| 1830 |
|
|---|
| 1831 |
last if ( $self->{alive_} == 0 ); |
|---|
| 1832 |
|
|---|
| 1833 |
|
|---|
| 1834 |
|
|---|
| 1835 |
|
|---|
| 1836 |
|
|---|
| 1837 |
|
|---|
| 1838 |
if ( $line =~ /^\.(\r\n|\r|\n)$/ ) { |
|---|
| 1839 |
$hit_dot = 1; |
|---|
| 1840 |
|
|---|
| 1841 |
if ( defined( $before ) && ( $before ne '' ) ) { |
|---|
| 1842 |
print $client $before if ( defined( $client ) ); |
|---|
| 1843 |
print FILE $before if ( defined( $isopen ) ); |
|---|
| 1844 |
} |
|---|
| 1845 |
|
|---|
| 1846 |
|
|---|
| 1847 |
|
|---|
| 1848 |
|
|---|
| 1849 |
|
|---|
| 1850 |
print $client $line if ( defined( $client ) ); |
|---|
| 1851 |
last; |
|---|
| 1852 |
} |
|---|
| 1853 |
|
|---|
| 1854 |
print $client $line if ( defined( $client ) ); |
|---|
| 1855 |
print FILE $line if ( defined( $isopen ) ); |
|---|
| 1856 |
|
|---|
| 1857 |
} |
|---|
| 1858 |
|
|---|
| 1859 |
close FILE if ( $isopen ); |
|---|
| 1860 |
|
|---|
| 1861 |
return $hit_dot; |
|---|
| 1862 |
} |
|---|
| 1863 |
|
|---|
| 1864 |
|
|---|
| 1865 |
|
|---|
| 1866 |
|
|---|
| 1867 |
|
|---|
| 1868 |
|
|---|
| 1869 |
|
|---|
| 1870 |
|
|---|
| 1871 |
|
|---|
| 1872 |
|
|---|
| 1873 |
|
|---|
| 1874 |
sub substr_euc__ |
|---|
| 1875 |
{ |
|---|
| 1876 |
my ( $str, $pos, $len ) = @_; |
|---|
| 1877 |
my $result_str; |
|---|
| 1878 |
my $char; |
|---|
| 1879 |
my $count = 0; |
|---|
| 1880 |
if ( !$pos ) { |
|---|
| 1881 |
$pos = 0; |
|---|
| 1882 |
} |
|---|
| 1883 |
if ( !$len ) { |
|---|
| 1884 |
$len = length( $str ); |
|---|
| 1885 |
} |
|---|
| 1886 |
|
|---|
| 1887 |
for ( $pos = 0; $count < $len; $pos++ ) { |
|---|
| 1888 |
$char = substr( $str, $pos, 1 ); |
|---|
| 1889 |
if ( $char =~ /[\x80-\xff]/ ) { |
|---|
| 1890 |
$char = substr( $str, $pos++, 2 ); |
|---|
| 1891 |
} |
|---|
| 1892 |
$result_str .= $char; |
|---|
| 1893 |
$count++; |
|---|
| 1894 |
} |
|---|
| 1895 |
|
|---|
| 1896 |
return $result_str; |
|---|
| 1897 |
} |
|---|
| 1898 |
|
|---|
| 1899 |
|
|---|
| 1900 |
|
|---|
| 1901 |
|
|---|
| 1902 |
|
|---|
| 1903 |
|
|---|
| 1904 |
|
|---|
| 1905 |
|
|---|
| 1906 |
|
|---|
| 1907 |
sub generate_unique_session_key__ |
|---|
| 1908 |
{ |
|---|
| 1909 |
my ( $self ) = @_; |
|---|
| 1910 |
|
|---|
| 1911 |
my @chars = ( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
|---|
| 1912 |
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'U', 'V', 'W', 'X', 'Y', |
|---|
| 1913 |
'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A' ); |
|---|
| 1914 |
|
|---|
| 1915 |
my $session; |
|---|
| 1916 |
|
|---|
| 1917 |
do { |
|---|
| 1918 |
$session = ''; |
|---|
| 1919 |
my $length = int( 16 + rand(4) ); |
|---|
| 1920 |
|
|---|
| 1921 |
for my $i (0 .. $length) { |
|---|
| 1922 |
my $random = $chars[int( rand(36) )]; |
|---|
| 1923 |
|
|---|
| 1924 |
|
|---|
| 1925 |
|
|---|
| 1926 |
if ( rand(1) < rand(1) ) { |
|---|
| 1927 |
$random = lc($random); |
|---|
| 1928 |
} |
|---|
| 1929 |
|
|---|
| 1930 |
$session .= $random; |
|---|
| 1931 |
} |
|---|
| 1932 |
} while ( defined( $self->{api_sessions__}{$session} ) ); |
|---|
| 1933 |
|
|---|
| 1934 |
return $session; |
|---|
| 1935 |
} |
|---|
| 1936 |
|
|---|
| 1937 |
|
|---|
| 1938 |
|
|---|
| 1939 |
|
|---|
| 1940 |
|
|---|
| 1941 |
|
|---|
| 1942 |
|
|---|
| 1943 |
|
|---|
| 1944 |
|
|---|
| 1945 |
|
|---|
| 1946 |
|
|---|
| 1947 |
|
|---|
| 1948 |
|
|---|
| 1949 |
|
|---|
| 1950 |
sub release_session_key_private__ |
|---|
| 1951 |
{ |
|---|
| 1952 |
my ( $self, $session ) = @_; |
|---|
| 1953 |
|
|---|
| 1954 |
if ( defined( $self->{api_sessions__}{$session} ) ) { |
|---|
| 1955 |
$self->log_( 1, "release_session_key releasing key $session for user $self->{api_sessions__}{$session}" ); |
|---|
| 1956 |
delete $self->{api_sessions__}{$session}; |
|---|
| 1957 |
} |
|---|
| 1958 |
} |
|---|
| 1959 |
|
|---|
| 1960 |
|
|---|
| 1961 |
|
|---|
| 1962 |
|
|---|
| 1963 |
|
|---|
| 1964 |
|
|---|
| 1965 |
|
|---|
| 1966 |
|
|---|
| 1967 |
|
|---|
| 1968 |
|
|---|
| 1969 |
|
|---|
| 1970 |
|
|---|
| 1971 |
sub valid_session_key__ |
|---|
| 1972 |
{ |
|---|
| 1973 |
my ( $self, $session ) = @_; |
|---|
| 1974 |
|
|---|
| 1975 |
|
|---|
| 1976 |
|
|---|
| 1977 |
|
|---|
| 1978 |
|
|---|
| 1979 |
return undef if ( caller ne 'Classifier::Bayes' ); |
|---|
| 1980 |
|
|---|
| 1981 |
|
|---|
| 1982 |
|
|---|
| 1983 |
|
|---|
| 1984 |
|
|---|
| 1985 |
|
|---|
| 1986 |
|
|---|
| 1987 |
|
|---|
| 1988 |
|
|---|
| 1989 |
if ( !defined( $self->{api_sessions__}{$session} ) ) { |
|---|
| 1990 |
my ( $package, $filename, $line, $subroutine ) = caller; |
|---|
| 1991 |
$self->log_( 0, "Invalid session key $session provided in $package @ $line" ); |
|---|
| 1992 |
select( undef, undef, undef, 1 ); |
|---|
| 1993 |
} |
|---|
| 1994 |
|
|---|
| 1995 |
return $self->{api_sessions__}{$session}; |
|---|
| 1996 |
} |
|---|
| 1997 |
|
|---|
| 1998 |
|
|---|
| 1999 |
|
|---|
| 2000 |
|
|---|
| 2001 |
|
|---|
| 2002 |
|
|---|
| 2003 |
|
|---|
| 2004 |
|
|---|
| 2005 |
|
|---|
| 2006 |
|
|---|
| 2007 |
|
|---|
| 2008 |
|
|---|
| 2009 |
|
|---|
| 2010 |
|
|---|
| 2011 |
|
|---|
| 2012 |
|
|---|
| 2013 |
|
|---|
| 2014 |
|
|---|
| 2015 |
|
|---|
| 2016 |
|
|---|
| 2017 |
|
|---|
| 2018 |
|
|---|
| 2019 |
|
|---|
| 2020 |
|
|---|
| 2021 |
|
|---|
| 2022 |
|
|---|
| 2023 |
|
|---|
| 2024 |
|
|---|
| 2025 |
|
|---|
| 2026 |
|
|---|
| 2027 |
|
|---|
| 2028 |
sub get_session_key |
|---|
| 2029 |
{ |
|---|
| 2030 |
my ( $self, $user, $pwd ) = @_; |
|---|
| 2031 |
|
|---|
| 2032 |
|
|---|
| 2033 |
|
|---|
| 2034 |
|
|---|
| 2035 |
|
|---|
| 2036 |
my $hash = md5_hex( $user . '__popfile__' . $pwd ); |
|---|
| 2037 |
|
|---|
| 2038 |
$self->validate_sql_prepare_and_execute( $self->{db_get_userid__}, $user, $hash ); |
|---|
| 2039 |
my $result = $self->{db_get_userid__}->fetchrow_arrayref; |
|---|
| 2040 |
if ( !defined( $result ) ) { |
|---|
| 2041 |
|
|---|
| 2042 |
|
|---|
| 2043 |
|
|---|
| 2044 |
|
|---|
| 2045 |
|
|---|
| 2046 |
$self->log_( 0, "Attempt to login with incorrect credentials for user $user" ); |
|---|
| 2047 |
select( undef, undef, undef, 1 ); |
|---|
| 2048 |
return undef; |
|---|
| 2049 |
} |
|---|
| 2050 |
|
|---|
| 2051 |
my $session = $self->generate_unique_session_key__(); |
|---|
| 2052 |
|
|---|
| 2053 |
$self->{api_sessions__}{$session} = $result->[0]; |
|---|
| 2054 |
|
|---|
| 2055 |
$self->db_update_cache__( $session ); |
|---|
| 2056 |
|
|---|
| 2057 |
$self->log_( 1, "get_session_key returning key $session for user $self->{api_sessions__}{$session}" ); |
|---|
| 2058 |
|
|---|
| 2059 |
return $session; |
|---|
| 2060 |
} |
|---|
| 2061 |
|
|---|
| 2062 |
|
|---|
| 2063 |
|
|---|
| 2064 |
|
|---|
| 2065 |
|
|---|
| 2066 |
|
|---|
| 2067 |
|
|---|
| 2068 |
|
|---|
| 2069 |
|
|---|
| 2070 |
|
|---|
| 2071 |
sub release_session_key |
|---|
| 2072 |
{ |
|---|
| 2073 |
my ( $self, $session ) = @_; |
|---|
| 2074 |
|
|---|
| 2075 |
$self->mq_post_( "RELSE", $session ); |
|---|
| 2076 |
} |
|---|
| 2077 |
|
|---|
| 2078 |
|
|---|
| 2079 |
|
|---|
| 2080 |
|
|---|
| 2081 |
|
|---|
| 2082 |
|
|---|
| 2083 |
|
|---|
| 2084 |
|
|---|
| 2085 |
|
|---|
| 2086 |
|
|---|
| 2087 |
|
|---|
| 2088 |
|
|---|
| 2089 |
|
|---|
| 2090 |
|
|---|
| 2091 |
|
|---|
| 2092 |
|
|---|
| 2093 |
|
|---|
| 2094 |
|
|---|
| 2095 |
sub get_top_bucket__ |
|---|
| 2096 |
{ |
|---|
| 2097 |
my ( $self, $userid, $id, $matrix, $buckets ) = @_; |
|---|
| 2098 |
|
|---|
| 2099 |
my $best_probability = 0; |
|---|
| 2100 |
my $top_bucket = 'unclassified'; |
|---|
| 2101 |
|
|---|
| 2102 |
for my $bucket (@$buckets) { |
|---|
| 2103 |
my $probability = 0; |
|---|
| 2104 |
if ( defined($$matrix{$id}{$bucket}) && ( $$matrix{$id}{$bucket} > 0 ) ) { |
|---|
| 2105 |
$probability = $$matrix{$id}{$bucket} / $self->{db_bucketcount__}{$userid}{$bucket}; |
|---|
| 2106 |
} |
|---|
| 2107 |
|
|---|
| 2108 |
if ( $probability > $best_probability ) { |
|---|
| 2109 |
$best_probability = $probability; |
|---|
| 2110 |
$top_bucket = $bucket; |
|---|
| 2111 |
} |
|---|
| 2112 |
} |
|---|
| 2113 |
|
|---|
| 2114 |
return $top_bucket; |
|---|
| 2115 |
} |
|---|
| 2116 |
|
|---|
| 2117 |
|
|---|
| 2118 |
|
|---|
| 2119 |
|
|---|
| 2120 |
|
|---|
| 2121 |
|
|---|
| 2122 |
|
|---|
| 2123 |
|
|---|
| 2124 |
|
|---|
| 2125 |
|
|---|
| 2126 |
|
|---|
| 2127 |
|
|---|
| 2128 |
|
|---|
| 2129 |
|
|---|
| 2130 |
|
|---|
| 2131 |
|
|---|
| 2132 |
|
|---|
| 2133 |
|
|---|
| 2134 |
|
|---|
| 2135 |
sub classify |
|---|
| 2136 |
{ |
|---|
| 2137 |
my ( $self, $session, $file, $templ, $matrix, $idmap ) = @_; |
|---|
| 2138 |
my $msg_total = 0; |
|---|
| 2139 |
|
|---|
| 2140 |
my $userid = $self->valid_session_key__( $session ); |
|---|
| 2141 |
return undef if ( !defined( $userid ) ); |
|---|
| 2142 |
|
|---|
| 2143 |
$self->{unclassified__} = log( $self->config_( 'unclassified_weight' ) ); |
|---|
| 2144 |
|
|---|
| 2145 |
$self->{magnet_used__} = 0; |
|---|
| 2146 |
$self->{magnet_detail__} = 0; |
|---|
| 2147 |
|
|---|
| 2148 |
if ( defined( $file ) ) { |
|---|
| 2149 |
$self->{parser__}->parse_file( $file, |
|---|
| 2150 |
$self->global_config_( 'message_cutoff' ) ); |
|---|
| 2151 |
} |
|---|
| 2152 |
|
|---|
| 2153 |
|
|---|
| 2154 |
|
|---|
| 2155 |
my @buckets = $self->get_buckets( $session ); |
|---|
| 2156 |
|
|---|
| 2157 |
|
|---|
| 2158 |
|
|---|
| 2159 |
|
|---|
| 2160 |
return "unclassified" if ( $#buckets == -1 ); |
|---|
| 2161 |
|
|---|
| 2162 |
|
|---|
| 2163 |
|
|---|
| 2164 |
for my $bucket ($self->get_buckets_with_magnets( $session )) { |
|---|
| 2165 |
for my $type ($self->get_magnet_types_in_bucket( $session, $bucket )) { |
|---|
| 2166 |
if ( $self->magnet_match__( $session, $self->{parser__}->get_header($type), $bucket, $type ) ) { |
|---|
| 2167 |
return $bucket; |
|---|
| 2168 |
} |
|---|
| 2169 |
} |
|---|
| 2170 |
} |
|---|
| 2171 |
|
|---|
| 2172 |
|
|---|
| 2173 |
|
|---|
| 2174 |
|
|---|
| 2175 |
|
|---|
| 2176 |
|
|---|
| 2177 |
my %score; |
|---|
| 2178 |
my %matchcount; |
|---|
| 2179 |
|
|---|
| 2180 |
|
|---|
| 2181 |
|
|---|
| 2182 |
|
|---|
| 2183 |
my @ok_buckets; |
|---|
| 2184 |
|
|---|
| 2185 |
for my $bucket (@buckets) { |
|---|
| 2186 |
if ( $self->{bucket_start__}{$userid}{$bucket} != 0 ) { |
|---|
| 2187 |
$score{$bucket} = $self->{bucket_start__}{$userid}{$bucket}; |
|---|
| 2188 |
$matchcount{$bucket} = 0; |
|---|
| 2189 |
push @ok_buckets, ( $bucket ); |
|---|
| 2190 |
} |
|---|
| 2191 |
} |
|---|
| 2192 |
|
|---|
| 2193 |
@buckets = @ok_buckets; |
|---|
| 2194 |
|
|---|
| 2195 |
|
|---|
| 2196 |
|
|---|
| 2197 |
|
|---|
| 2198 |
return "unclassified" if ( $#buckets < 1 ); |
|---|
| 2199 |
|
|---|
| 2200 |
|
|---|
| 2201 |
|
|---|
| 2202 |
|
|---|
| 2203 |
|
|---|
| 2204 |
my $word_count = 0; |
|---|
| 2205 |
|
|---|
| 2206 |
|
|---|
| 2207 |
|
|---|
| 2208 |
|
|---|
| 2209 |
|
|---|
| 2210 |
|
|---|
| 2211 |
|
|---|
| 2212 |
|
|---|
| 2213 |
my $correction = 0; |
|---|
| 2214 |
|
|---|
| 2215 |
|
|---|
| 2216 |
|
|---|
| 2217 |
|
|---|
| 2218 |
|
|---|
| 2219 |
|
|---|
| 2220 |
|
|---|
| 2221 |
|
|---|
| 2222 |
|
|---|
| 2223 |
|
|---|
| 2224 |
|
|---|
| 2225 |
|
|---|
| 2226 |
|
|---|
| 2227 |
|
|---|
| 2228 |
|
|---|
| 2229 |
|
|---|
| 2230 |
|
|---|
| 2231 |
|
|---|
| 2232 |
|
|---|
| 2233 |
|
|---|
| 2234 |
|
|---|
| 2235 |
|
|---|
| 2236 |
|
|---|
| 2237 |
|
|---|
| 2238 |
|
|---|
| 2239 |
|
|---|
| 2240 |
|
|---|
| 2241 |
|
|---|
| 2242 |
|
|---|
| 2243 |
|
|---|
| 2244 |
my $words; |
|---|
| 2245 |
$words = join( ',', map( $self->{db__}->quote( $_ ), (sort keys %{$self->{parser__}{words__}}) ) ); |
|---|
| 2246 |
$self->{get_wordids__} = $self->validate_sql_prepare_and_execute( |
|---|
| 2247 |
"select id, word |
|---|
| 2248 |
from words |
|---|
| 2249 |
where word in ( $words ) |
|---|
| 2250 |
order by id;" ); |
|---|
| 2251 |
|
|---|
| 2252 |
my @id_list; |
|---|
| 2253 |
my %temp_idmap; |
|---|
| 2254 |
|
|---|
| 2255 |
if ( !defined( $idmap ) ) { |
|---|
| 2256 |
$idmap = \%temp_idmap; |
|---|
| 2257 |
} |
|---|
| 2258 |
|
|---|
| 2259 |
while ( my $row = $self->{get_wordids__}->fetchrow_arrayref ) { |
|---|
| 2260 |
push @id_list, ($row->[0]); |
|---|
| 2261 |
$$idmap{$row->[0]} = $row->[1]; |
|---|
| 2262 |
} |
|---|
| 2263 |
|
|---|
| 2264 |
$self->{get_wordids__}->finish; |
|---|
| 2265 |
undef $self->{get_wordids__}; |
|---|
| 2266 |
|
|---|
| 2267 |
my $ids = join( ',', @id_list ); |
|---|
| 2268 |
|
|---|
| 2269 |
$self->{db_classify__} = $self->validate_sql_prepare_and_execute( |
|---|
| 2270 |
"select matrix.times, matrix.wordid, buckets.name |
|---|
| 2271 |
from matrix, buckets |
|---|
| 2272 |
where matrix.wordid in ( $ids ) |
|---|
| 2273 |
and matrix.bucketid = buckets.id |
|---|
| 2274 |
and buckets.userid = $userid;" ); |
|---|
| 2275 |
|
|---|
| 2276 |
|
|---|
| 2277 |
|
|---|
| 2278 |
|
|---|
| 2279 |
my %temp_matrix; |
|---|
| 2280 |
|
|---|
| 2281 |
if ( !defined( $matrix ) ) { |
|---|
| 2282 |
$matrix = \%temp_matrix; |
|---|
| 2283 |
} |
|---|
| 2284 |
|
|---|
| 2285 |
while ( my $row = $self->{db_classify__}->fetchrow_arrayref ) { |
|---|
| 2286 |
$$matrix{$row->[1]}{$row->[2]} = $row->[0]; |
|---|
| 2287 |
} |
|---|
| 2288 |
|
|---|
| 2289 |
$self->{db_classify__}->finish; |
|---|
| 2290 |
undef $self->{db_classify__}; |
|---|
| 2291 |
|
|---|
| 2292 |
foreach my $id (@id_list) { |
|---|
| 2293 |
$word_count += 2; |
|---|
| 2294 |
my $wmax = -10000; |
|---|
| 2295 |
|
|---|
| 2296 |
foreach my $bucket (@buckets) { |
|---|
| 2297 |
my $probability = 0; |
|---|
| 2298 |
|
|---|
| 2299 |
if ( defined($$matrix{$id}{$bucket}) && ( $$matrix{$id}{$bucket} > 0 ) ) { |
|---|
| 2300 |
$probability = log( $$matrix{$id}{$bucket} / $self->{db_bucketcount__}{$userid}{$bucket} ); |
|---|
| 2301 |
} |
|---|
| 2302 |
|
|---|
| 2303 |
$matchcount{$bucket} += $self->{parser__}{words__}{$$idmap{$id}} if ($probability != 0); |
|---|
| 2304 |
$probability = $self->{not_likely__}{$userid} if ( $probability == 0 ); |
|---|
| 2305 |
$wmax = $probability if ( $wmax < $probability ); |
|---|
| 2306 |
$score{$bucket} += ( $probability * $self->{parser__}{words__}{$$idmap{$id}} ); |
|---|
| 2307 |
} |
|---|
| 2308 |
|
|---|
| 2309 |
if ($wmax > $self->{not_likely__}{$userid}) { |
|---|
| 2310 |
$correction += $self->{not_likely__}{$userid} * $self->{parser__}{words__}{$$idmap{$id}}; |
|---|
| 2311 |
} else { |
|---|
| 2312 |
$correction += $wmax * $self->{parser__}{words__}{$$idmap{$id}}; |
|---|
| 2313 |
} |
|---|
| 2314 |
} |
|---|
| 2315 |
|
|---|
| 2316 |
|
|---|
| 2317 |
|
|---|
| 2318 |
|
|---|
| 2319 |
my @ranking = sort {$score{$b} <=> $score{$a}} keys %score; |
|---|
| 2320 |
|
|---|
| 2321 |
my %raw_score; |
|---|
| 2322 |
my $base_score = $score{$ranking[0]}; |
|---|
| 2323 |
my $total = 0; |
|---|
| 2324 |
|
|---|
| 2325 |
|
|---|
| 2326 |
|
|---|
| 2327 |
|
|---|
| 2328 |
|
|---|
| 2329 |
my $class = 'unclassified'; |
|---|
| 2330 |
|
|---|
| 2331 |
if ( @buckets > 1 && $score{$ranking[0]} > ( $score{$ranking[1]} + $self->{unclassified__} ) ) { |
|---|
| 2332 |
$class = $ranking[0]; |
|---|
| 2333 |
} |
|---|
| 2334 |
|
|---|
| 2335 |
|
|---|
| 2336 |
|
|---|
| 2337 |
|
|---|
| 2338 |
|
|---|
| 2339 |
|
|---|
| 2340 |
my $ln2p_54 = -54 * log(2); |
|---|
| 2341 |
|
|---|
| 2342 |
foreach my $b (@ranking) { |
|---|
| 2343 |
$raw_score{$b} = $score{$b}; |
|---|
| 2344 |
$score{$b} -= $base_score; |
|---|
| 2345 |
|
|---|
| 2346 |
$total += exp($score{$b}) if ($score{$b} > $ln2p_54 ); |
|---|
| 2347 |
} |
|---|
| 2348 |
|
|---|
| 2349 |
if ($self->{wordscores__} && defined($templ) ) { |
|---|
| 2350 |
my %qm = %{$self->{parser__}->quickmagnets()}; |
|---|
| 2351 |
my $mlen = scalar(keys %{$self->{parser__}->quickmagnets()}); |
|---|
| 2352 |
|
|---|
| 2353 |
if ( $mlen > 0 ) { |
|---|
| 2354 |
$templ->param( 'View_QuickMagnets_If' => 1 ); |
|---|
| 2355 |
$templ->param( 'View_QuickMagnets_Count' => ($mlen + 1) ); |
|---|
| 2356 |
my @buckets = $self->get_buckets( $session ); |
|---|
| 2357 |
my $i = 0; |
|---|
| 2358 |
my %types = $self->get_magnet_types( $session ); |
|---|
| 2359 |
|
|---|
| 2360 |
my @bucket_data; |
|---|
| 2361 |
foreach my $bucket (@buckets) { |
|---|
| 2362 |
my %row_data; |
|---|
| 2363 |
$row_data{View_QuickMagnets_Bucket} = $bucket; |
|---|
| 2364 |
$row_data{View_QuickMagnets_Bucket_Color} = $self->get_bucket_color( $session, $bucket ); |
|---|
| 2365 |
push ( @bucket_data, \%row_data ); |
|---|
| 2366 |
} |
|---|
| 2367 |
|
|---|
| 2368 |
my @qm_data; |
|---|
| 2369 |
foreach my $type (sort keys %types) { |
|---|