diff interface/edit_section_db.php @ 2:da5309d54083

add missing books to sections as dummy entries for further modification. (1) Alter "Auto_increment" to 5190896 for sections table in database. (2) Finds missing books (using mysql OR _select_distinct() in add_missing_books_to_sections.php (3) Put the missing_books.csv in intermediate_results. and execute: add_missing_books_to_sections.php
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Fri, 23 Jan 2015 19:00:57 +0100
parents ef6d0c6a13d7
children f196939ccc03
line wrap: on
line diff
--- a/interface/edit_section_db.php	Wed Jan 21 10:49:44 2015 +0100
+++ b/interface/edit_section_db.php	Fri Jan 23 19:00:57 2015 +0100
@@ -64,15 +64,20 @@
 	return $pageArray;
 }
 function getSectionArray($bookId,$count,$versionInfo){
+		
         $str="";
-        if($versionInfo['version']!=0){
-		$table="sections_revisions";
-		$condition="AND deleted=0 ";
-		$condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 ";
-	}else{
-		$table="sections";
-		$condition="";
-	}
+       	if($versionInfo['version']!=0){
+			$table="sections_revisions";
+			$condition="AND deleted=0 ";
+			$condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 ";
+		}else{
+			$table="sections";
+			$condition="";
+		}
+
+		echo "select from ".$table."\n";
+
+
         $query="SELECT * 
                 FROM ".$table."
                 WHERE books_id=".$bookId." ".$condition."
@@ -81,6 +86,7 @@
                 ORDER BY id ASC";
                 /*ORDER BY start_page ASC, level ASC, id ASC,end_page DESC";*/
         $result=mysql_query($query);
+
         $i=0;
 	$pageArray=array();
         while($row=mysql_fetch_assoc($result)){
@@ -155,10 +161,110 @@
 	}
 }
 
+
+function _select_distinct(){
+	/*
+	$query="SELECT DISTINCT (books_id) FROM sections";
+	
+	$result=mysql_query($query);
+	if(!$result){
+		echo json_encode("Failed during inserting section records.");
+		return;
+	}else{
+		echo json_encode("Succeeded.");
+	}
+
+	while($row=mysql_fetch_assoc($result)){
+		// echo $row['books_id'], ", ";		
+	}
+
+	echo "section num: ".mysql_num_rows($result);
+	echo '\n';
+	*/
+
+
+	//$query="SELECT id FROM books WHERE id NOT IN (SELECT DISTINCT (books_id) FROM sections)"; 
+	$query="SELECT books.id, books.name FROM books LEFT JOIN sections ON books.id=sections.books_id WHERE sections.books_id IS NULL";
+	
+	$result=mysql_query($query);
+	if(!$result){
+		echo json_encode("Failed during inserting section records.");
+		return;
+	}
+
+	while($row=mysql_fetch_assoc($result)){
+		//echo $row['books_id'], ", ";
+		//echo $row.", ";		
+	}
+	echo "missing books num: ".mysql_num_rows($result);
+	echo '\n';
+
+	
+	
+	
+}
+
+
+
+function add_missing_books_to_sections(){
+	// find missing books
+	// Run the following line/mysql to find out what books are missing in sections table
+	// _select_distinct();
+	// Or load missing_books_id.csv
+	if (($missing_books = fopen("intermediate_results/missing_books.csv", "r")) !== FALSE) {
+	    while (($data = fgetcsv($missing_books, ",")) !== FALSE) {
+
+	    	$bookId = $data[0];
+	    	$name = $data[1];
+
+			_add_book_to_sections($name, $bookId);
+	    	
+	    }
+	    fclose($missing_books);	
+	}
+
+}
+
+
+function _add_book_to_sections($name, $bookId){
+
+	/* 
+	// get Auto_increment 
+	$query = mysql_query("SHOW TABLE STATUS WHERE name='sections'");
+	if (mysql_num_rows($query)) {
+		$result = mysql_fetch_assoc($query);
+		echo $result['Auto_increment'];
+	} else {//error
+		//error control here
+		echo "error in getting Auto_increment";
+	}
+	*/
+
+	// id should be autoincremated 
+	$query="INSERT INTO sections (name,books_id,section_after) VALUES ";
+
+	$str="(";
+	$str.="'".$name."','";
+	$str.=$bookId."',";
+	$str.="''";
+	$str.=")";
+	$query.=$str;	
+	
+	// echo $name.", ".$bookId."\n";
+
+	$result=mysql_query($query);
+	if(!$result){
+		echo json_encode("Failed during inserting section records.");
+		return;
+	}
+	// echo mysql_insert_id();
+	
+}
+
+
 if(isset($_POST['command']) && $_POST['command']!=''){
 	$command=$_POST['command'];
 	if($command=="write"){		
-		
 		$bookId=$_POST['bookId'];
 		$sectionArray=$_POST['sectionArray'];
 		$version=$_POST['version'];