forked from omzn/PMAN3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pman3.cgi
executable file
·5770 lines (5289 loc) · 154 KB
/
pman3.cgi
1
2
3
4
5
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# =================================================================================
# PMAN 3 - Paper MANagement system
#
# (c) 2002-2015 Osamu Mizuno, All right researved.
#
my $VERSION = "3.2.5 build 20150916";
#
# =================================================================================
BEGIN {
unshift(@INC, './lib');
}
use strict;
use utf8;
my $debug=0;
use DBI;
use CGI;
use CGI::Session;
use CGI::Cookie;
use HTML::Template;
use HTML::Scrubber;
use HTML::Entities;
use Encode;
use Digest::MD5 qw/md5_hex/;
use MIME::Types qw/by_suffix/;
use URI::Escape qw/uri_escape_utf8/;
use IO::String;
use BibTeX::Parser;
use BibTeX::Parser::Author;
#=====================================================
# Constants
#=====================================================
my $LIBDIR = "./lib";
my $TMPLDIR = "./tmpl";
my $TMPDIR = "./tmp";
my $DB = "./db/bibdat.db";
my $SESS_DB = "./db/sess.db";
my $CACHE_DB = "./db/cache.db";
my $OPTIONS_DB = "./db/config.db";
#=====================================================
# Options
#=====================================================
my $PASSWD;
my $titleOfSite;
my $maintainerName;
my $maintainerAddress;
my $texHeader;
my $texFooter;
my $use_cache = 0;
my $use_DBforSession = 0;
my $use_AutoJapaneseTags = 0;
my $use_RSS = 0;
my $use_XML = 0;
my $use_latexpdf = 0;
my $use_highcharts = 0;
my $use_mathjax = 0;
my $path_highcharts = 'lib/highcharts';
my $theme_highcharts = "gray.js";
my $latexcmd = "/usr/bin/platex -halt-on-error";
my $dvipdfcmd = "/usr/bin/dvipdfmx -V 4";
my $tmpl_name = "default";
my $title_list;
my $title_table;
my $title_latex ;
my $title_detail;
my $title_bbl ;
my $title_add ;
my $title_bib ;
my $title_graph ;
my $title_edit ;
my $title_category ;
my $title_config ;
my %opts = (
use_XML => $use_XML,
use_RSS => $use_RSS,
use_cache => $use_cache,
use_DBforSession => $use_DBforSession,
use_AutoJapaneseTags => $use_AutoJapaneseTags,
use_latexpdf => $use_latexpdf,
use_mathjax => $use_mathjax,
use_highcharts => $use_highcharts,
path_highcharts => $path_highcharts,
theme_highcharts => $theme_highcharts,
PASSWD => $PASSWD,
titleOfSite => $titleOfSite,
maintainerName => $maintainerName,
maintainerAddress => $maintainerAddress,
texHeader => $texHeader,
texFooter => $texFooter,
latexcmd => $latexcmd,
dvipdfcmd => $dvipdfcmd,
tmpl_name => $tmpl_name,
title_list => 'List of works',
title_table => 'Table of works',
title_latex => 'LaTeX list of works',
title_detail => 'Detail of a work',
title_bbl => 'BibTeX list of works',
);
&getOptionsDB;
#=====================================================
# Global Variables
#=====================================================
my $httpServerName = $ENV{'SERVER_NAME'};
my $scriptName = $ENV{'SCRIPT_NAME'};
my $query = "";
my $cgi = new CGI;
my $session;
my $login;
my %sbk ;
my $bib;
my %authors_hash;
my %ptype;
my @jname;
my @ptype_order ;
my @bb_order = ( 'title', 'title_e', 'author', 'author_e', 'editor',
'editor_e', 'key', 'journal', 'journal_e', 'booktitle',
'booktitle_e', 'series', 'volume', 'number', 'chapter',
'pages', 'edition', 'school', 'type', 'institution',
'organization', 'publisher', 'publisher_e', 'address', 'month',
'year', 'howpublished', 'acceptance', 'impactfactor', 'url',
'note', 'annote', 'abstract' );
my %mlist = ("0,en"=>"","0,ja"=>"",
"1,en"=>"January","1,ja"=>"1月","2,en"=>"February","2,ja"=>"2月",
"3,en"=>"March","3,ja"=>"3月","4,en"=>"April","4,ja"=>"4月",
"5,en"=>"May","5,ja"=>"5月","6,en"=>"June","6,ja"=>"6月",
"7,en"=>"July","7,ja"=>"7月","8,en"=>"August","8,ja"=>"8月",
"9,en"=>"September","9,ja"=>"9月","10,en"=>"October","10,ja"=>"10月",
"11,en"=>"November","11,ja"=>"11月","12,en"=>"December","12,ja"=>"12月");
#=====================================================
# lib/lang.*.plで設定する変数群 (our宣言)
#=====================================================
our %bt;
our %viewMenu;
our %topMenu;
our %msg;
#=====================================================
# Main
#=====================================================
use Time::HiRes qw/gettimeofday tv_interval/;
my $t0 = [Time::HiRes::gettimeofday];
my $dbh;
eval {
$dbh = DBI->connect("dbi:SQLite:dbname=$DB", undef, undef,
{AutoCommit => 0, RaiseError => 1 });
$dbh->{sqlite_unicode} = 1; # これがperl 5.8.5未満では動かない
};
&printError('Database not found. Run install.cgi first.') if ($@);
&manageSession;
&initialLaunch;
# キャッシュ読み込み処理の実装部
# LOGIN状態でない場合のみ.
# ここでgenerateURLの内容をキーとしてcacheDBを検索.
# 発見したらその内容をprintして終了.
if ($use_cache) {
my $page = &getCacheFromCDB;
if ($page) {
my $dt = Time::HiRes::tv_interval($t0);
$page =~ s/Time to show this page: [\d\.]+ seconds\./Time to show this page: $dt seconds\. (cached)/;
# if (utf8::is_utf8($page)) {
# print encode('utf-8', $page);
# } else {
print $page;
# }
exit 0;
}
}
$query = &makeQuery;
&getDataDB($query);
&getPtypeDB;
################################[TIME]
my $t1 = Time::HiRes::tv_interval($t0);
my $t2;
my ($tt1,$tt2,$tt3,$tt4);
################################[TIME]
&printScreen;
################################[TIME]
my $t3;
$t3 = Time::HiRes::tv_interval($t0);
################################[TIME]
&clearSessionParams;
################################[TIME]
#printf STDERR "[TIME] DB:%3.2f s, Format:%3.2f s, Write:%3.2f s",$t1, $t2-$t1, $t3-$t2 ;
#printf STDERR "[TIME FORMAT] %3.2f s, %3.2f s, %3.2f s, %3.2f s",$tt1,$tt2,$tt3,$tt4;
################################[TIME]
$dbh->disconnect;
exit(0);
#=====================================================
# 初期起動での誘導
#=====================================================
sub initialLaunch {
my $SQL;
my $info;
$SQL = "SELECT * FROM ptypes";
my $res = $dbh->selectrow_array($SQL);
if ($res == ()) {
unless ($login) {
$info = <<EOM;
<p>初期設定のために,ログインしてください.</p>
<p>Please login for your initial setup.</p>
<p class="login">
<form action="$scriptName" method="POST">
Password:
<input type="password" name="PASSWD" size="20" />
<input type="submit" value="Login" />
</form>
</p>
EOM
&printInfo($info);
}
if ($session->param('MODE') ne "category") {
$info = <<EOM;
<p>カテゴリが設定されていません.「<a href="$scriptName?MODE=category">カテゴリ設定</a>」へ移動してください.</p>
<p>No category is found. Please go to <a href="$scriptName?MODE=category">category setting</a>.</p>
EOM
&printInfo($info);
}
} else {
$SQL = "SELECT * FROM bib";
$res = $dbh->selectrow_array($SQL);
if ($res == () && $session->param('MODE') ne "add") {
$info = <<EOM;
<p>文献が1つも登録されていません.「<a href="$scriptName?MODE=add">文献追加</a>」から文献を登録してください.</p>
<p>No publication is registered. Please go to <a href="$scriptName?MODE=add">add publication</a>.</p>
EOM
&printInfo($info);
}
}
}
#=====================================================
# Session 管理
#=====================================================
sub manageSession {
if (!$use_DBforSession) {
# セッションのゴミ掃除
if(!opendir(DIR, "$TMPDIR")){
die;
}
# cgisess_の残骸で 3 hours (1/8 day) より古いものを探す
my @files = grep(/cgisess_/ && -f "$TMPDIR/$_" && ((-M "$TMPDIR/$_") > 1/8) , readdir(DIR));
closedir(DIR);
foreach(@files){
unlink("$TMPDIR/$_");
}
}
# CGI
$cgi->charset('utf-8');
# 短縮パラメータ
if ($cgi->param("D") ne "") {
$cgi->param("MODE","detail");
my $param = $cgi->param("D");
$param=~s/[\"\']//g;
if (utf8::is_utf8($param)) {
utf8::encode($param);
}
$cgi->param("ID",$param);
}
if ($cgi->param("A") ne "") {
$cgi->param("FROM","author");
$cgi->param("LOGIC","and");
my $param = $cgi->param("A");
$param=~s/[\"\']//g;
if (utf8::is_utf8($param)) {
utf8::encode($param);
}
$cgi->param("SEARCH",$param);
}
if ($cgi->param("T") ne "") {
$cgi->param("FROM","tag");
$cgi->param("LOGIC","or");
my $param = $cgi->param("T");
$param=~s/[\"\']//g;
if (utf8::is_utf8($param)) {
utf8::encode($param);
}
$cgi->param("SEARCH",$param);
}
# CGIで渡ってきた値をutf-8化
for my $g ($cgi->param) {
if ($g ne 'edit_upfile') { # upfileの破壊を防ぐ
my $param = $cgi->param($g);
#$param = &htmlScrub($param);
if (!utf8::is_utf8($param)) { # utf-8 flagが立ってないやつだけ.
my @v = map {Encode::decode('utf-8',$_)} $cgi->param($g);#$param;
foreach (@v) {
$_=~s/[\"\']//g;
}
$cgi->param($g,@v);
} else {
$param =~s/[\"\']//g;
$cgi->param($g,$param);
}
}
}
# この時点で,$cgiの中身はutf8 flag ON
my $sid = $cgi->param('SID') || $cgi->cookie('SID') || undef ;
if ($use_DBforSession) {
unless (-f $SESS_DB) {
eval {
my $sdbh = DBI->connect("dbi:SQLite:dbname=$SESS_DB", undef, undef,
{AutoCommit => 0, RaiseError => 1 });
my $SQL = <<EOM;
CREATE TABLE sessions (
id CHAR(32) NOT NULL PRIMARY KEY,
a_session TEXT NOT NULL);
EOM
$sdbh->do($SQL);
$sdbh->commit;
$sdbh->disconnect;
chmod(0666,$SESS_DB);
};
}
}
if ($sid) {
$session = new CGI::Session("driver:File", $sid, {Directory=>$TMPDIR})
if (!$use_DBforSession);
$session = new CGI::Session("driver:sqlite", $sid, {DataSource=>$SESS_DB})
if ($use_DBforSession);
# LOGIN = offであれば,sessionからPASSWD情報を削除.
if (defined($cgi->param('LOGIN')) && $cgi->param('LOGIN') eq "off") {
$cgi->delete('PASSWD');
$session->clear('PASSWD');
}
# cgiからPASSWDが渡ってきたら,MD5変換して再設定
if ($cgi->param('PASSWD')) {
$cgi->param('PASSWD',md5_hex($cgi->param('PASSWD')));
}
} else {
$session = new CGI::Session("driver:File", undef, {Directory=>$TMPDIR})
if (!$use_DBforSession);
$session = new CGI::Session("driver:sqlite", undef, {DataSource=>$SESS_DB})
if ($use_DBforSession);
}
$session->expire('+3h');
my $sp = $session->param_hashref();
# %sbk = %$sp;
foreach (keys(%$sp)) {
$sbk{$_} = $sp->{$_} ;
}
# ここで,cgi -> session を突っ込む.PASSWDも突っ込まれたはず.
$session->save_param($cgi); # OK?
# 言語設定
my $l = $session->param('LANG') || "ja";
require "$LIBDIR/lang.$l.pl";
$title_add = $msg{'Title_add'};
$title_bib = $msg{'Title_bib'};
$title_graph = $msg{'Title_graph'};
$title_edit = $msg{'Title_edit'};
$title_category = $msg{'Title_category'};
$title_config = $msg{'Title_config'};
# ログイン状態設定
if ($session->param('PASSWD') eq $PASSWD) {
$login = 1;
} else {
$login = 0;
}
# 画面表示のない処理へ
if (defined($session->param('MODE')) && $session->param('MODE') eq "edit2" ) {
®istEntry();
}
if (defined($session->param('MODE')) && $session->param('MODE') eq "add2" ) {
®istEntry();
}
if (defined($session->param('MODE')) && $session->param('MODE') eq "bib2" ) {
®istEntryByBib();
}
if (defined($session->param('MODE')) && $session->param('MODE') eq "delete" ) {
&deleteEntry();
}
if (defined($session->param('MODE')) && $session->param('MODE') eq "category2" ) {
&modifyCategory();
}
if (defined($session->param('MODE')) && $session->param('MODE') eq "config2" ) {
&doConfigSetting();
}
if (defined($session->param('MODE')) && $session->param('MODE') eq "filedelete" ) {
&deleteFile();
}
# ファイルダウンロード処理へ
if (defined($session->param('DOWNLOAD'))) {
my ($f,$m,$c,$a) = &downloadFileDB($session->param('DOWNLOAD'));
$session->clear('DOWNLOAD');
if ($f && $m && $c && ($a == 0 || $login == 1)) {
&printFile($f,$m,$c);
}
}
}
sub clearSessionParams {
if (defined($cgi->param('RSS')) || defined($cgi->param('XML')) || defined($cgi->param('SSI'))) {
$session->clear();
$session->flush();
foreach (keys(%sbk)) {
next if ($_=~/^_SESSION_/);
$session->param($_,$sbk{$_});
}
return;
}
my $mode = $session->param('MODE') ;
$session->clear('RSS');
$session->clear('XML');
$session->clear('STATIC');
$session->clear('SSI');
if ($mode eq "delete") {
$session->clear('ID');
$session->clear('MODE');
} elsif ($mode eq "add2" || $mode eq "edit2" || $mode eq "bib2") {
$session->clear('MODE');
} elsif ($mode eq "filedelete") {
$session->clear('MODE');
$session->clear('FID');
} elsif ($mode eq "config2") {
$session->clear('MODE');
$session->clear('tag');
$session->clear('cache');
my $sps = $session->param_hashref();
$session->clear([grep(/opt_/,keys(%$sps))]);
} elsif ($mode eq "category2") {
$session->clear('MODE');
$session->clear('op');
my $sps = $session->param_hashref();
$session->clear([grep(/cat_/,keys(%$sps))]);
} elsif ($mode eq "PDF") {
$session->param('MODE','latex');
}
}
#=====================================================
# CGIから検索条件取得 -> SQLのwhere句生成
#=====================================================
sub makeQuery {
my $mmode = $session->param('MENU') || "simple";
my $l = $session->param('LANG') || "ja";
$l = $dbh->quote($l);
if ($session->param('LOGIN') eq "on") {
return;
}
if ($session->param('MODE') eq "detail" || $session->param('MODE') eq "edit") {
my $id = $session->param('ID');
if ($id=~/^\d+$/) {
$id = $dbh->quote($id);
return ("WHERE ID=$id AND ptype=pt_type AND pt_lang=$l");
} else {
$session->clear("ID");
&printError('"ID" must be an integer.');
}
}
# pman2との互換
if (defined($cgi->param('FILTER'))) {
$session->param('SEARCH',&HTML::Entities::decode($cgi->param('FILTER')));
# $session->param('SEARCH',$session->param('FILTER'));
$session->clear('FILTER');
}
my @cond = ();
# 種別限定
my @pt = ();
if (ref($session->param('PTYPE')) eq 'ARRAY') {
@pt= @{$session->param('PTYPE')};
} else {
$pt[0] = $session->param('PTYPE') eq '' ? 'all': $session->param('PTYPE');
}
my @newpt;
for (my $i=0;$i<=$#pt;$i++) {
push(@newpt,$pt[$i]) if ($pt[$i] ne "all");
}
push(@cond," ptype IN ( ".join(",",@newpt)." ) ") if (@newpt != ());
# ソート順
my $order = "ORDER BY ";
my $od = $session->param('SORT') || "t_descend";
if ($od eq "ascend") {
$order .= "year asc,month asc,number asc";
} elsif ($od eq "descend") {
$order .= "year desc,month desc,number desc";
} elsif ($od eq "t_ascend") {
$order .= "pt_order,year asc,month asc,number asc";
} elsif ($od eq "t_descend") {
$order .= "pt_order,year desc,month desc,number desc";
} elsif ($od eq "y_t_ascend") {
$order .= "year asc,pt_order,month asc,number asc";
} elsif ($od eq "y_t_descend") {
$order .= "year desc,pt_order,month desc,number desc";
}
my @smenu = ();
if ($mmode eq "simple") {
$smenu[0] = "";
} elsif ($mmode eq "detail") {
$smenu[0] = "";
$smenu[1] = "1";
$smenu[2] = "2";
$smenu[3] = "3";
}
# 検索条件
for (my $j=0;$j<=$#smenu;$j++) {
my $s = $session->param("SEARCH$smenu[$j]") ;
next if ($s =~/^\s*$/);
$s =~s/^\s*//g; $s =~s/\s*$//g;
my @ss = split(/\s+/,$s);
my $lg = $session->param("LOGIC$smenu[$j]") || "and";
if (!($lg eq "and" || $lg eq "or")) {
$session->clear("LOGIC$smenu[$j]");
&printError('"Logic" must be and/or. What are you attempting?');
}
my $from = $session->param("FROM$smenu[$j]") || "author";
if ($from eq "author") { # 著者サーチ
for (my $i=0;$i<=$#ss;$i++) {
$ss[$i] = $dbh->quote("\%$ss[$i]\%");
my $pids = &getIdFromAuthorsDB($ss[$i]);
$ss[$i] = " id IN ( $pids ) ";
}
} elsif ($from eq "1stauthor") { # 筆頭著者サーチ
for (my $i=0;$i<=$#ss;$i++) {
$ss[$i] = $dbh->quote("\%$ss[$i]\%");
my $pids = &getIdFromAuthorsDB($ss[$i],0);
$ss[$i] = " id IN ( $pids ) ";
}
} elsif ($from eq "year") { # 出版年サーチ
for (my $i=0;$i<=$#ss;$i++) {
if ($ss[$i]=~/^\d+$/) { # 数字限定
$ss[$i] = $dbh->quote($ss[$i]);
$ss[$i] = " year=$ss[$i] ";
} else {
$ss[$i]="";
$session->clear("SEARCH$smenu[$j]");
&printError('"Year" must be an integer.');
}
}
} elsif ($from eq "title") { # タイトルサーチ
for (my $i=0;$i<=$#ss;$i++) {
$ss[$i] = $dbh->quote("\%$ss[$i]\%");
$ss[$i] = " ( title LIKE $ss[$i] OR title_e LIKE $ss[$i] ) ";
}
} elsif ($from eq "publish") { # 出版物サーチ
for (my $i=0;$i<=$#ss;$i++) {
$ss[$i] = $dbh->quote("\%$ss[$i]\%");
$ss[$i] = " ( journal LIKE $ss[$i] OR journal_e LIKE $ss[$i] OR booktitle LIKE $ss[$i] OR booktitle_e LIKE $ss[$i] ) ";
}
} elsif ($from eq "tag") { # タグサーチ
for (my $i=0;$i<=$#ss;$i++) {
$ss[$i] = $dbh->quote($ss[$i]);
my $pids = &getIdFromTagDB($ss[$i]);
$ss[$i] = " id IN ( $pids ) ";
}
} elsif ($from eq "all") { # 全サーチ
for (my $i=0;$i<=$#ss;$i++) {
my $w = $dbh->quote("\%$ss[$i]\%");
my $pidsTag = &getIdFromTagDB($dbh->quote($ss[$i]));
if ($pidsTag ne "") {
$pidsTag = "OR id IN ( ".$pidsTag." ) ";
}
my $pidsAuthor = &getIdFromAuthorsDB($w);
if ($pidsAuthor ne "") {
$pidsAuthor = "OR id IN ( ".$pidsAuthor." ) ";
}
$ss[$i] = " ( editor LIKE $w OR editor_e LIKE $w OR series LIKE $w OR volume LIKE $w OR number LIKE $w OR chapter LIKE $w OR pages LIKE $w OR edition LIKE $w OR school LIKE $w OR type LIKE $w OR institution LIKE $w OR organization LIKE $w OR publisher LIKE $w OR publisher_e LIKE $w OR address LIKE $w OR howpublished LIKE $w OR acceptance LIKE $w OR impactfactor LIKE $w OR url LIKE $w OR note LIKE $w OR annote LIKE $w OR abstract LIKE $w OR year LIKE $w OR month LIKE $w OR title LIKE $w OR title_e LIKE $w OR journal LIKE $w OR journal_e LIKE $w OR booktitle LIKE $w OR booktitle_e LIKE $w $pidsTag $pidsAuthor )";
}
} else {
return;
}
my @sss;
foreach (@ss) {
push(@sss,$_) if ($_ ne "") ;
}
push(@cond," ( ".join(" $lg ",@sss)." ) ") if (@sss);
}
# 既発表に限る (ログイン前)
push(@cond," ( year <= 9999 ) ") if ($login == 0);
# ptypes テーブルと統合
push(@cond," ptype=pt_type ");
push(@cond," pt_lang=$l ");
my $q = "";
$q = "WHERE ".join(" AND ",@cond) if (@cond) ;
$q .= " $order";
my $limit = $session->param('LIMIT');
if ($limit =~ /^\d+$/ && $limit > 0) {
$q .= " LIMIT '$limit'";
}
return $q;
}
#=====================================================
# DBからデータ取得
#=====================================================
sub getDataDB {
my $q = shift ;
if ($q eq "") {
return;
}
eval {
# 文献情報取得 -> $bib
my $SQL = "SELECT id,style,ptype,author,author_e,editor,editor_e,key,
title,title_e,journal,journal_e,booktitle,booktitle_e,
series,volume,number,chapter,
pages,edition,school,type,institution,organization,
publisher,publisher_e,
address,month,year,howpublished,acceptance,impactfactor,
note,annote,abstract,url
FROM bib,ptypes $q";
$bib = $dbh->selectall_arrayref($SQL,{Columns => {}});
# ジャーナル種類 -> @jname
$SQL = "SELECT DISTINCT journal FROM bib ;";
my $jnl = $dbh->selectall_arrayref($SQL,{Columns => {}});
foreach (@$jnl) {
push(@jname,$$_{'journal'});
}
$SQL = "SELECT paper_id,author_name,author_key FROM authors WHERE paper_id IN ( SELECT id FROM bib,ptypes $q ) ORDER BY paper_id,author_order";
my $a_ref = $dbh->selectall_arrayref($SQL,{Columns => {}});
foreach (@$a_ref) {
push(@{$authors_hash{$_->{'paper_id'}}->{'author_name'}},$_->{'author_name'});
push(@{$authors_hash{$_->{'paper_id'}}->{'author_key'}},$_->{'author_key'});
}
};
if ($@) {
$dbh->disconnect;
my $emsg = "Incomplete query.";
$emsg .= "<br /> $@ <br /> query: $q" if ($debug);
&printError($emsg);
}
return 0;
}
sub getPtypeDB {
my $SQL;
eval {
my $l = $session->param('LANG') || "ja";
$l = $dbh->quote($l);
# 業績種類 -> %ptype
$SQL = "SELECT pt_type,pt_desc FROM ptypes WHERE pt_lang=$l ORDER BY pt_type;";
my $pt = $dbh->selectall_arrayref($SQL,{Columns => {}});
foreach (@$pt) {
$ptype{$$_{'pt_type'}} = $$_{'pt_desc'};
}
$SQL = "SELECT pt_type FROM ptypes WHERE pt_lang=$l ORDER BY pt_order ;";
my $ptord = $dbh->selectcol_arrayref($SQL);
push(@ptype_order,@$ptord);
};
if ($@) {
$dbh->disconnect;
my $emsg = "Incomplete query.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
return 0;
}
# データ追加
sub insertDB {
my $sp = shift;
my $binfile = shift;
my %q;
my $SQL;
foreach my $p (grep(/edit_/,keys(%$sp))) {
#$$sp{$p}=~s/'/''/g; # 'のエスケープ
$p=~/^edit_(.+)$/;
$q{$1}=$$sp{$p};
}
# INSERT INTO bib
# VALUES ...
eval {
$SQL = "INSERT INTO bib VALUES(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
my $sth = $dbh->prepare($SQL);
$sth ->execute(
$q{'style'},$q{'ptype'},$q{'author'},$q{'editor'},$q{'key'},
$q{'title'},$q{'journal'},$q{'booktitle'},$q{'series'},$q{'volume'},
$q{'number'},$q{'chapter'},$q{'pages'},$q{'edition'},$q{'school'},
$q{'type'},$q{'institution'},$q{'organization'},$q{'publisher'},
$q{'address'},$q{'month'},$q{'year'},$q{'howpublished'},$q{'note'},
$q{'annote'},$q{'abstract'},$q{'title_e'},$q{'author_e'},$q{'editor_e'},
$q{'journal_e'},$q{'booktitle_e'},$q{'publisher_e'},$q{'acceptance'},
$q{'impactfactor'},$q{'url'}
);
$dbh->commit;
$SQL = "SELECT MAX(id) FROM bib;";
my $maxid = $dbh->selectrow_array($SQL);
$session->param('ID',$maxid);
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
}
# データ更新
sub updateDB {
my $sp = shift;
my @q;
my $SQL;
foreach my $p (grep(/edit_/,keys(%$sp))) {
# if ($$sp{$p} ne "") {
$$sp{$p}=$dbh->quote($$sp{$p});
$p=~/^edit_([\w_]+)$/;
push(@q,"$1=$$sp{$p}") if ($1 ne 'upfile' && $1 ne 'tags' && $1 ne 'bibentry');
# }
}
# UPDATE bib
# SET style = '...', ptype = '...',
# WHERE id = ???
my $SQL = "UPDATE bib SET ";
eval {
$SQL .= join(",",@q);
my $id = $dbh->quote($$sp{'ID'});
$SQL .= " WHERE id=$id;";
# &printError "Error: $@ $SQL";
my $sth = $dbh->do($SQL);
$dbh->commit;
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While inserting an entry.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
}
# データ削除
sub deleteDB {
my $id = shift;
my $idq = $dbh->quote($id);
my $SQL = "DELETE FROM bib WHERE id=$idq;";
$SQL .= "DELETE FROM files WHERE pid=$idq;";
$SQL .= "DELETE FROM tags WHERE paper_id=$idq;";
$SQL .= "DELETE FROM authors WHERE paper_id=$idq;";
eval {
my $sth = $dbh->do($SQL);
$dbh->commit;
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While deleting an entry.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
}
sub getTitleOnlyDB {
eval {
# 文献情報取得 -> $bib
my $SQL = "SELECT id,title,title_e FROM bib";
$bib = $dbh->selectall_arrayref($SQL,{Columns => {}});
};
if ($@) {
$dbh->disconnect;
my $emsg = "Incomplete query while getting titles.";
$emsg .= "<br /> $@ <br />" if ($debug);
&printError($emsg);
}
return 0;
}
sub insertFileDB {
my ($paper_id,$fname,$mimetype,$fh,$fa,$desc) = @_;
my $SQL = "INSERT INTO files VALUES(null,?,?,?,?,?,?)";
my $sth = $dbh->prepare($SQL);
eval {
$sth->execute($paper_id,$fname,$mimetype,$fh,$fa,$desc);
$dbh->commit;
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While inserting a file.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
}
sub changeAccessFileDB {
my ($fid,$fa) = @_;
my $SQL = sprintf("UPDATE files SET access=%s WHERE id=%s",$dbh->quote($fa),$dbh->quote($fid));
eval {
my $sth = $dbh->do($SQL);
$dbh->commit;
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While updating files db.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
}
sub changeDescFileDB {
my ($fid,$desc) = @_;
my $SQL = sprintf("UPDATE files SET file_desc=%s WHERE id=%s",$dbh->quote($desc),$dbh->quote($fid));
eval {
my $sth = $dbh->do($SQL);
$dbh->commit;
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While updating files db.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
}
sub getFileListDB {
my ($pid,$hash) = @_;
my $SQL = sprintf("SELECT id,filename,access,mimetype,file_desc FROM files WHERE pid=%s",$dbh->quote($pid));
eval {
my $f = $dbh->selectall_arrayref($SQL,{Columns => {}});
foreach (@$f) {
if ($_->{'access'}==0) {
$hash->{$_->{'id'}.",filename"} = $_->{'filename'};
$hash->{$_->{'id'}.",mimetype"} = $_->{'mimetype'};
$hash->{$_->{'id'}.",file_desc"} = $_->{'file_desc'};
$hash->{$_->{'id'}.",access"} = $_->{'access'};
} elsif ($_->{'access'}==1 && $login == 1) {
$hash->{$_->{'id'}.",filename"} = $_->{'filename'};
$hash->{$_->{'id'}.",mimetype"} = $_->{'mimetype'};
$hash->{$_->{'id'}.",file_desc"} = $_->{'file_desc'};
$hash->{$_->{'id'}.",access"} = $_->{'access'};
}
}
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While getting a file list.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
}
# file削除
sub deleteFileDB {
my $fid = shift;
my $SQL = sprintf("DELETE FROM files WHERE id=%s",$dbh->quote($fid));
eval {
my $sth = $dbh->do($SQL);
$dbh->commit;
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While deleting a file.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
}
sub getTagListDB {
my ($pid) = @_;
my @taglist;
my $SQL = sprintf("SELECT tag FROM tags WHERE paper_id=%s",$dbh->quote($pid));
eval {
my $f = $dbh->selectall_arrayref($SQL,{Columns => {}});
foreach (@$f) {
push(@taglist,$_->{'tag'});
}
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While getting a tag list.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
return join(" ",@taglist);
}
sub getIdFromTagDB {
my ($tag) = @_;
my @idlist;
# tag は quote済み
my $SQL = sprintf("SELECT paper_id FROM tags WHERE tag=%s",$tag);
eval {
my $f = $dbh->selectall_arrayref($SQL,{Columns => {}});
foreach (@$f) {
push(@idlist,$_->{'paper_id'});
}
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While getting an id list from tag.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
return join(",",@idlist);
}
sub getTop10TagDB {
my @tf;
my $SQL = "select tag,count(tag) from tags group by tag order by count(tag) desc;";
eval {
my $f = $dbh->selectall_arrayref($SQL,{Columns => {}});
foreach (@$f) {
push(@tf,$_->{'tag'});
push(@tf,$_->{'count(tag)'});
}
};
if ($@) {
$dbh->rollback; $dbh->disconnect;
my $emsg = "Incomplete query. While getting an top 10 list from tag.";
$emsg .= "<br /> $@ <br /> query: $SQL" if ($debug);
&printError($emsg);
}
return join(",",@tf);
}
# 与えられたid群に関連するタグを抽出
sub getMyTagDB {
my ($pids) = @_;
my @tf;
# $pids = join(",", map {$dbh->quote($_);} split(/,/,$pids));
my $SQL = "SELECT tag,count(tag) FROM tags WHERE paper_id IN ( $pids ) GROUP BY tag ORDER BY count(tag) desc;";
eval {
my $f = $dbh->selectall_arrayref($SQL,{Columns => {}});
foreach (@$f) {
push(@tf,$_->{'tag'});
push(@tf,$_->{'count(tag)'});