[Affelio-cvs 116] CVS update: affelio/apps/album

Back to archive index

Yoshihisa Fukuhara higef****@users*****
2005年 6月 22日 (水) 14:54:55 JST


Index: affelio/apps/album/Album.pm
diff -u affelio/apps/album/Album.pm:1.6 affelio/apps/album/Album.pm:1.7
--- affelio/apps/album/Album.pm:1.6	Wed Jun 22 13:14:51 2005
+++ affelio/apps/album/Album.pm	Wed Jun 22 14:54:54 2005
@@ -15,11 +15,6 @@
 use HTML::Template;
 use Config::Tiny;
 
-our $databasedir = "./data";
-our $idfile = "./data/id";
-our $album_table = "albums";
-our $album_comment_table = "album_comments";
-our $album_image_table = "album_images";
 our $max_entries = 3000;
 
 ##############################################
@@ -33,28 +28,21 @@
 
 	my $this = {};
 	$this->{afap}  = $afap;
-	$this->{uname} = $afap->{af}->{site__username};
 	$this->{tmpfile}= $afap->get_userdata_dir()."/.sqltmp";
+	$this->{album_tb}= "album_$afap->{install_name}_entries";
+	$this->{image_tb}= "album_$afap->{install_name}_images";
+	$this->{comment_tb}= "album_$afap->{install_name}_comments";
 	$this->{dbh} = undef;
-	
-        my $DBConfig = Config::Tiny->new();
-	$DBConfig = Config::Tiny->read("$this->{afap}->{af}->{site__user_dir}/db.cfg");
-	my $db_type = $DBConfig->{db}->{type};
-	
         # 初期化
+	
 	unless(-f $this->{tmpfile}) {
 	    open(TMP,"> $this->{tmpfile}");
 	    close(TMP); 	    
 	    $this->{dbh} = $afap->get_userdata_dbh();
 		# 日記テーブル
 	    my $query;
-	    if ($db_type eq "sqlite"){
-		$query = "id INTEGER PRIMARY KEY,";
-	    }elsif ($db_type eq "mysql"){
-		$query = "id INTEGER AUTO_INCREMENT INPRIMARY KEY,";
-	    }
-	    $query.=
-			"title		TEXT,
+	    $query="id INTEGER".get_query_primarykey($this)."
+			title		TEXT,
 			contents	TEXT,
 			timestamp	INTEGER,
 			update_time	INTEGER,
@@ -63,15 +51,11 @@
 			pswd		TEXT,
 			ord		INTEGER";
 			
-	    $this->{dbh}->do("CREATE TABLE $album_table ($query)");
+	    $this->{dbh}->do("CREATE TABLE $this->{album_tb} ($query)");
 
 		# コメントテーブル
-	    if ($db_type eq "sqlite"){
-		$query = "pkey INTEGER PRIMARY KEY,";
-	    }elsif ($db_type eq "mysql"){
-		$query = "pkey INTEGER AUTO_INCREMENT INPRIMARY KEY,";
-	    }
-	    $query.="
+
+	    $query="pkey INTEGER".get_query_primarykey($this)."
 			id		INTEGER,
 			user		TEXT,
 			afid		TEXT,
@@ -79,15 +63,12 @@
 			pswd		TEXT,
 			timestamp	INTEGER";
 		
-	    $this->{dbh}->do("CREATE TABLE $album_comment_table ($query)");
+	    $this->{dbh}->do("CREATE TABLE $this->{comment_tb} ($query)");
+
 
 		# 画像テーブル
-	    if ($db_type eq "sqlite"){
-		$query = "pkey INTEGER PRIMARY KEY,";
-	    }elsif ($db_type eq "mysql"){
-		$query = "pkey INTEGER AUTO_INCREMENT INPRIMARY KEY,";
-	    }
-	    $query.="
+#
+	    $query="pkey INTEGER".get_query_primarykey($this)."
 			id		INTEGER,
 			image		TEXT,
 			title		TEXT,
@@ -97,7 +78,7 @@
 			pswd		TEXT,
 			timestamp	INTEGER";
 
-	    $this->{dbh}->do("CREATE TABLE $album_image_table ($query)");
+	    $this->{dbh}->do("CREATE TABLE $this->{image_tb} ($query)");
 
 		$this->{dbh}->disconnect;
 	}
@@ -123,7 +104,7 @@
 
 
 ##############################################
-# addEntry
+# addAlbum
 # 日記に新しいエントリを追加します。
 ##############################################
 
@@ -133,17 +114,17 @@
 
 #	my ($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
 #	$year += 1900; $mon += 1;
-  	my $id = $this->getColumn("SELECT MAX(id) FROM $album_table");
+  	my $id = $this->getColumn("SELECT MAX(id) FROM $this->{album_tb}");
 	$title = $this->validate($title);
 	$user = $this->validate($user);
 	$contents = $this->validate_entry($id, $title, $contents);
 
 	# 二重投稿を防ぐ
-#	my @same = $this->getall("SELECT id FROM $album_table WHERE title = '$title' AND contents = '$contents'");
+#	my @same = $this->getall("SELECT id FROM $this->{album_tb} WHERE title = '$title' AND contents = '$contents'");
 #	if($#same >= 0) { return; }
 
-	$this->{dbh}->do("INSERT INTO $album_table (title, contents, timestamp, update_time, user, afid, pswd, ord) VALUES ('$title', '$contents', $time, $time, '$user', '$afid', '', '')");
-  	$id = $this->getColumn("SELECT MAX(id) FROM $album_table");
+	$this->{dbh}->do("INSERT INTO $this->{album_tb} (title, contents, timestamp, update_time, user, afid, pswd, ord) VALUES ('$title', '$contents', $time, $time, '$user', '$afid', '', '')");
+  	$id = $this->getColumn("SELECT MAX(id) FROM $this->{album_tb}");
 
         my $data_dir=$this->{afap}->get_userdata_dir()."/";
   	$data_dir.= $id;
@@ -172,13 +153,13 @@
 	$user = $this->validate($user);
 
 	# 二重投稿を防ぐ
-#	my @same = $this->getall("SELECT id FROM $album_image_table WHERE title = '$title' AND comment = '$comment'");
+#	my @same = $this->getall("SELECT id FROM $this->{image_tb} WHERE title = '$title' AND comment = '$comment'");
 #	if($#same >= 0) { return; }
-	my @same = $this->getall("SELECT id FROM $album_image_table WHERE id = $id AND image = '$image'");
+	my @same = $this->getall("SELECT id FROM $this->{image_tb} WHERE id = $id AND image = '$image'");
 	if($#same > 0) {
-	$this->{dbh}->do("UPDATE $album_image_table SET title = '$title', comment='$comment', user='$user', afid='$afid', time=$time WHERE id = $id AND image='$image'");
+	$this->{dbh}->do("UPDATE $this->{image_tb} SET title = '$title', comment='$comment', user='$user', afid='$afid', time=$time WHERE id = $id AND image='$image'");
         }else{
-	$this->{dbh}->do("INSERT INTO $album_image_table (id, image, title, user, afid, comment, pswd, timestamp) VALUES ($id, '$image', '$title', '$user', '$afid', '$comment', '', $time)");
+	$this->{dbh}->do("INSERT INTO $this->{image_tb} (id, image, title, user, afid, comment, pswd, timestamp) VALUES ($id, '$image', '$title', '$user', '$afid', '$comment', '', $time)");
       	}
 }
 
@@ -194,7 +175,7 @@
 	$title = $this->validate($title);
 	$contents = $this->validate($contents);
   	my $time = time;
-	$this->{dbh}->do("UPDATE $album_table SET title = '$title', contents = '$contents', update_time=$time WHERE id = $id");
+	$this->{dbh}->do("UPDATE $this->{album_tb} SET title = '$title', contents = '$contents', update_time=$time WHERE id = $id");
 }
 
 ##############################################
@@ -207,7 +188,7 @@
 	$title = $this->validate($title);
 	$comment = $this->validate($comment);
   	my $time = time;
-	$this->{dbh}->do("UPDATE $album_image_table SET title = '$title', comment = '$comment' WHERE id = $id AND image='$image'");
+	$this->{dbh}->do("UPDATE $this->{image_tb} SET title = '$title', comment = '$comment' WHERE id = $id AND image='$image'");
 }
 
 ##############################################
@@ -218,7 +199,7 @@
 sub updateTimestamp {
 	my ($this, $id) = @_;
   	my $time = time;
-	$this->{dbh}->do("UPDATE $album_table SET update_time=$time WHERE id = $id");
+	$this->{dbh}->do("UPDATE $this->{album_tb} SET update_time=$time WHERE id = $id");
 }
 
 
@@ -229,10 +210,10 @@
 
 sub removeAlbum {
 	my ($this, $id) = @_;
-        my @ret = $this->getall("SELECT * FROM $album_image_table WHERE id = $id");
-	$this->{dbh}->do("DELETE FROM $album_table WHERE id = $id");
-	$this->{dbh}->do("DELETE FROM $album_comment_table WHERE id = $id");
-	$this->{dbh}->do("DELETE FROM $album_image_table WHERE id = $id");
+        my @ret = $this->getall("SELECT * FROM $this->{image_tb} WHERE id = $id");
+	$this->{dbh}->do("DELETE FROM $this->{album_tb} WHERE id = $id");
+	$this->{dbh}->do("DELETE FROM $this->{comment_tb} WHERE id = $id");
+	$this->{dbh}->do("DELETE FROM $this->{image_tb} WHERE id = $id");
         my $data_dir=$this->{afap}->get_userdata_dir()."/".$id."/";
        	my $thumb_dir=$data_dir."thumbnail/";
   	foreach(@ret){
@@ -258,8 +239,8 @@
        	my $thumb_dir=$data_dir."thumbnail/";
   	my @ret;
   	foreach(@pkey){
-		@ret = $this->getall("SELECT * FROM $album_image_table WHERE id = $id AND pkey=$_");
-		$this->{dbh}->do("DELETE FROM $album_image_table WHERE id = $id AND pkey=$_");
+		@ret = $this->getall("SELECT * FROM $this->{image_tb} WHERE id = $id AND pkey=$_");
+		$this->{dbh}->do("DELETE FROM $this->{image_tb} WHERE id = $id AND pkey=$_");
           	
   		unlink($data_dir.$ret[0]->{image});
 	  	unlink($thumb_dir.$ret[0]->{image});
@@ -274,7 +255,7 @@
 sub removeComment {
 	my ($this, $id, @pkey) = @_;
   	foreach(@pkey){
-		$this->{dbh}->do("DELETE FROM $album_comment_table WHERE id = $id AND pkey=$_");
+		$this->{dbh}->do("DELETE FROM $this->{comment_tb} WHERE id = $id AND pkey=$_");
       	}
 }
 
@@ -286,7 +267,7 @@
 
 sub getEntry {
 	my ($this, $id) = @_;
-	my @ret = $this->getall("SELECT * FROM $album_table WHERE id = $id");
+	my @ret = $this->getall("SELECT * FROM $this->{album_tb} WHERE id = $id");
 	return $ret[0];
 }
 
@@ -297,17 +278,17 @@
 
 sub getImage {
 	my ($this, $id, $pkey) = @_;
-	my @ret = $this->getall("SELECT * FROM $album_image_table WHERE id = $id AND pkey=$pkey");
+	my @ret = $this->getall("SELECT * FROM $this->{image_tb} WHERE id = $id AND pkey=$pkey");
 	return $ret[0];
 }
 sub getAllImage {
 	my ($this, $id) = @_;
-	return $this->getall("SELECT * FROM $album_image_table WHERE id = $id");
+	return $this->getall("SELECT * FROM $this->{image_tb} WHERE id = $id");
 }
 
 sub checkImagefile {
 	my ($this, $id, $image) = @_;
-	my @ret = $this->getall("SELECT * FROM $album_image_table WHERE id = $id AND image='$image'");
+	my @ret = $this->getall("SELECT * FROM $this->{image_tb} WHERE id = $id AND image='$image'");
 	return $ret[0];
 }
 
@@ -319,7 +300,7 @@
 sub getNewestEntries {
 	my ($this, $num) = @_;
 	unless ($num) { $num = 5; }
-	return $this->getall("SELECT * FROM $album_table ORDER BY update_time DESC LIMIT $num");
+	return $this->getall("SELECT * FROM $this->{album_tb} ORDER BY update_time DESC LIMIT $num");
 }
 
 ##############################################
@@ -329,7 +310,7 @@
 
 sub getNewestAlbumId {
   	my ($this) = @_;
-  	my @ret = $this->getall("SELECT MAX(id) as id FROM $album_table");
+  	my @ret = $this->getall("SELECT MAX(id) as id FROM $this->{album_tb}");
   	return $ret[0];
 }
 
@@ -340,7 +321,7 @@
 
 sub getAllEntries {
 	my ($this) = @_;
-	return $this->getall("SELECT * FROM $album_table ORDER BY update_time DESC");
+	return $this->getall("SELECT * FROM $this->{album_tb} ORDER BY update_time DESC");
 }
 
 
@@ -356,10 +337,10 @@
 #	$comment = $this->validate($comment);
 	
 	# 二重投稿を防ぐ
-	my @same = $this->getall("SELECT id FROM $album_comment_table WHERE user = '$user' AND comment = '$comment'");
+	my @same = $this->getall("SELECT id FROM $this->{comment_tb} WHERE user = '$user' AND comment = '$comment'");
 	if($#same >= 0) { return; }
 	
-	$this->{dbh}->do("INSERT INTO $album_comment_table (id, user, afid, comment, pswd, timestamp) VALUES ($id, '$user', '$afid', '$comment', '', $time)");
+	$this->{dbh}->do("INSERT INTO $this->{comment_tb} (id, user, afid, comment, pswd, timestamp) VALUES ($id, '$user', '$afid', '$comment', '', $time)");
 }
 
 
@@ -370,7 +351,7 @@
 
 sub getComments {
 	my ($this, $id) = @_;
-	return $this->getall("SELECT * FROM $album_comment_table WHERE id = $id ORDER BY timestamp");
+	return $this->getall("SELECT * FROM $this->{comment_tb} WHERE id = $id ORDER BY timestamp");
 }
 
 ##############################################
@@ -380,7 +361,7 @@
 
 sub getCommentsNo {
 	my ($this, $id) = @_;
-	return $this->getColumn("SELECT COUNT(*) FROM $album_comment_table WHERE id = $id");
+	return $this->getColumn("SELECT COUNT(*) FROM $this->{comment_tb} WHERE id = $id");
 }
 
 sub getColumn {
@@ -509,7 +490,7 @@
 sub getURLDescription {
 	my ($this, $id) = @_;
 	
-	my ($entry) = $this->getall("SELECT * FROM $album_table WHERE id = $id");
+	my ($entry) = $this->getall("SELECT * FROM $this->{album_tb} WHERE id = $id");
 	my $tmpl = new HTML::Template(filename => "./templates/tpingrdf.tmpl");
 	my ($sec, $min, $hour, $mday, $mon, $year) = localtime($entry->{timestamp});
 	$year += 1900; $mon += 1;
@@ -527,21 +508,21 @@
 }
 
 # 日記に振るユニークなIDを取得する
-sub getID {
-	local (*F);
-	my $count = 0;
-
-	open(F, "+< $idfile");
-	flock(F, 2);
-	$count = <F>;
-	$count++;
-	seek(F, 0, 0);
-	print F $count;
-	flock(F, 8);
-	close(F);
+#sub getID {
+#	local (*F);
+#	my $count = 0;
+
+#	open(F, "+< $idfile");
+#	flock(F, 2);
+#	$count = <F>;
+#	$count++;
+#	seek(F, 0, 0);
+#	print F $count;
+#	flock(F, 8);
+#	close(F);
 
-	return $count;
-}
+#	return $count;
+#}
 
 # トラックバックPINGを自動的に飛ばす
 sub validate_entry {
@@ -651,4 +632,19 @@
 	}
 }
 
+sub get_query_primarykey {
+    my ($this) = @_;
+    my $DBConfig = Config::Tiny->new();
+    $DBConfig = Config::Tiny->read("$this->{afap}->{af}->{site__user_dir}/db.cfg");
+    my $db_type = $DBConfig->{db}->{type};
+    my $query;
+
+    if ($db_type eq "sqlite"){
+	$query = " PRIMARY KEY,";
+    }elsif ($db_type eq "mysql"){
+	$query = " AUTO_INCREMENT PRIMARY KEY,";
+    }
+    return $query;
+}
+
 1;


Affelio-cvs メーリングリストの案内
Back to archive index