_binary From f926b4662385cb7e210d7173c5f638cacef6bb97 Mon Sep 17 00:00:00 2001
From: pomishra13 <pooja192009@gmail.com>
Date: Fri, 8 May 2015 16:33:25 -0700
Subject: [PATCH] video updater patch

---
 bin/media_updater.php               | 76 +++++++++++++++++++++++++++++--------
 configs/config.php                  | 28 +++++++++++++-
 controllers/crawl_controller.php    |  7 +++-
 controllers/resource_controller.php |  9 +++--
 lib/mail_server.php                 | 47 +++++++++++++++++++++--
 lib/media_constants.php             | 56 +++++++++++++++++++++++++++
 6 files changed, 196 insertions(+), 27 deletions(-)
 create mode 100644 lib/media_constants.php

diff --git a/bin/media_updater.php b/bin/media_updater.php
index 3e7fd75..03a426e 100644
--- a/bin/media_updater.php
+++ b/bin/media_updater.php
@@ -60,6 +60,8 @@ define("MINIMUM_UPDATE_LOOP_TIME", 10);
 require_once BASE_DIR."/lib/crawl_daemon.php";
 /** To guess language based on page encoding */
 require_once BASE_DIR."/lib/locale_functions.php";
+/** Loads common constants for media operations*/
+require_once BASE_DIR."/lib/media_constants.php";
 /** Loads common constants for web crawling*/
 require_once BASE_DIR."/lib/crawl_constants.php";
 /** Used to reparse wiki pages after a media conversion */
@@ -107,7 +109,7 @@ if (function_exists('lcfirst') === false) {
  * @author Chris Pollett
  * @package seek_quarry\bin
  */
-class MediaUpdater implements CrawlConstants
+class MediaUpdater implements CrawlConstants,MediaConstants
 {
     /**
      * The last time feeds were checked for updates
@@ -127,16 +129,6 @@ class MediaUpdater implements CrawlConstants
      */
     var $video_convert_types = array("mov", "avi");
     /**
-     * Folder to store files that will be converted
-     * @var string
-     */
-    var $convert_folder = WORK_DIRECTORY . "/schedules/media_convert";
-    /**
-     * Folder to store files that after conversion
-     * @var string
-     */
-    var $converted_folder = WORK_DIRECTORY . "/schedules/media_converted";
-    /**
      * Sets up the field variables so that media updating can begin
      */
     function __construct()
@@ -175,6 +167,7 @@ class MediaUpdater implements CrawlConstants
             $this->getUpdateProperties();
             $this->newsUpdate();
             $this->videoUpdate();
+            $this->sendGroupNotificationEmailsInBatches();
             $sleep_time = max(0, ceil(
                 MINIMUM_UPDATE_LOOP_TIME - changeInMicrotime($start_time)));
             if ($sleep_time > 0) {
@@ -320,7 +313,7 @@ class MediaUpdater implements CrawlConstants
      */
     function splitVideos()
     {
-        $convert_folder = $this->convert_folder;
+        $convert_folder = WORK_DIRECTORY.self::CONVERT_FOLDER;
         if(!defined('FFMPEG') || !file_exists($convert_folder)) { return; }
         crawlLog("   Looking for video files to split...");
         $type_string = "{" . implode(",", $this->video_convert_types) . "}";
@@ -354,8 +347,8 @@ class MediaUpdater implements CrawlConstants
     function moveVideoFoldersToConvertedDirectory()
     {
         crawlLog("Moving video folders from media_convert to converted...");
-        $convert_folder = $this->convert_folder;
-        $converted_folder = $this->converted_folder;
+        $convert_folder = WORK_DIRECTORY.self::CONVERT_FOLDER;
+        $converted_folder = WORK_DIRECTORY.self::CONVERTED_FOLDER;
         if(!file_exists($converted_folder)) {
             mkdir($converted_folder);
         }
@@ -390,7 +383,7 @@ class MediaUpdater implements CrawlConstants
     function generateAssembleVideoFile()
     {
         crawlLog("Inside generateAssembleVideoFile function...");
-        $converted_folder = $this->converted_folder;
+        $converted_folder = WORK_DIRECTORY.self::CONVERTED_FOLDER;
         if(!file_exists($converted_folder)) {
             mkdir($converted_folder);
         }
@@ -445,7 +438,7 @@ class MediaUpdater implements CrawlConstants
     function concatenateVideos()
     {
         crawlLog("Concatenating videos...");
-        $converted_folder = $this->converted_folder;
+        $converted_folder = WORK_DIRECTORY.self::CONVERTED_FOLDER;
         if(!file_exists($converted_folder)) {
             mkdir($converted_folder);
         }
@@ -461,6 +454,13 @@ class MediaUpdater implements CrawlConstants
                 $file_name = trim($lines[3]);
                 if($this->mergeVideo($assemble_file, $file_name, $folder_name)){
                     $this->sourceModel->db->unlinkRecursive($video_path);
+                    $video_name=$folder_name."/".$file_name; 
+                    $extension = "." . UrlParser::getDocumentType($file_name,
+                         "");
+                    $thumb_file= substr($file_name, 0, -strlen($extension));
+                    $thumb_file_name=$thumb_file."png";
+                    $thumb_name=$folder_name."/".$thumb_file_name;
+                    $this->thumbFileFromVideo($video_name,$thumb_name);
                 }
             }
         }
@@ -499,6 +499,12 @@ class MediaUpdater implements CrawlConstants
     {
         crawlLog("Checking Name Server for video segments to convert..");
         $convert_folder = WORK_DIRECTORY."/schedules/media_convert";
+        $folders=scandir($convert_folder);
+        if (count($folders)>0){
+            foreach($folders as $folder){
+                $db->unlinkRecursive( $folder);
+            }
+        }
         $time = time();
         $session = md5($time . AUTH_KEY);
         $server = NAME_SERVER;
@@ -543,6 +549,44 @@ class MediaUpdater implements CrawlConstants
             crawlLog("No files on server to convert!");
         }
     }
+    /**
+     * Function to send emails to mailer batches created by 
+     * mail_server. This function would periodically be invoked and
+     * send emails reading data from the text files.
+     */
+    function sendGroupNotificationEmailsInBatches()
+    {
+        $mail_directory = WORK_DIRECTORY.self::MAIL_FOLDER;
+        if(!file_exists($mail_directory)){return;}
+        $files = glob($mail_directory."/*.{txt}",GLOB_BRACE);
+        $file_count=count($files);       
+        if($file_count==0){
+            return;
+        }
+        if ($current_machine == crawlHash(NAME_SERVER)) {
+            foreach($files as $file){
+                echo "for file :: $file";
+                $fp = fopen($file, "a+");
+                if (flock($fp, LOCK_EX | LOCK_NB)){
+                    crawlLog("...Lock for $file acquired! Sending emails!\n");
+                    $lines = file($file);
+                    foreach($lines as $line){
+                        $mail_fields=explode("-",$line);
+                        $to = $mail_fields[0];
+                        $subject = $mail_fields[1];
+                        $message = $mail_fields[2];
+                        $header = $mail_fields[3];
+                        $mail_sent=mail($to, $subject, $message, $header);
+                    }
+                    flock($fp, LOCK_UN);
+                    unlink($file);
+                    sleep(MAIL_SENDING_INTERVAL);   
+                }else{
+                    crawlLog("...Could not acquire the lock for $file!");
+                }
+            } 
+        }
+    }
 }
 /*
  * Instantiate and run the MediaUpdater program
diff --git a/configs/config.php b/configs/config.php
index fda788c..6ae3bb0 100755
--- a/configs/config.php
+++ b/configs/config.php
@@ -108,9 +108,9 @@ if (MAINTENANCE_MODE && $_SERVER["SERVER_ADDR"] != $_SERVER["REMOTE_ADDR"]) {
 }
 
 if (!defined('WORK_DIRECTORY')) {
-/*+++ The next block of code is machine edited, change at
+/*+++ The next block of code is machine edited, change at 
 your own risk, please use configure web page instead +++*/
-define('WORK_DIRECTORY', '');
+define('WORK_DIRECTORY', 'C:/xampp/htdocs/yioop_may_data');
 /*++++++*/
 // end machine edited code
 }
@@ -496,6 +496,15 @@ if(!defined("MAX_FILE_TIMESTAMP_LIMIT")) {
     define('MAX_FILE_TIMESTAMP_LIMIT', 600);
 }
 /**
+ * This mail timestamp limit allows mail server to create a new file 
+ * and write next mailer batch in the new file. Otherwise, new mailer 
+ * batch will be written in old file. For eg. new file will be created every 
+ * 5 minutes as per below value.
+ */
+if(!defined("MAX_MAIL_TIMESTAMP_LIMIT")) {
+    define('MAX_MAIL_TIMESTAMP_LIMIT', 300);
+}
+/**
  * @global array file extensions which can be handled by the search engine,
  * other extensions will be ignored. This array is populated in the individual
  * lib/processors page processors
@@ -736,6 +745,21 @@ define("UP_DOWN_VOTING_GROUP", 2);
  */
 define('STANDARD_GROUP_ITEM', 0);
 /**
+ *  set to true if Multiple news updaters are running
+ *  otherwise set to false if name server is running the news updater
+ */
+if(!defined("SEND_MAIL_IN_BATCHES")) {
+    define('SEND_MAIL_IN_BATCHES', false);
+}
+/**
+ *  This value will block the name server for some time 
+ *  before sending mails to next mailer batch. One can set this to 0 
+ *  if one does not want name server to stop.
+ */
+if(!defined("MAIL_SENDING_INTERVAL")) {
+    define('MAIL_SENDING_INTERVAL', 60);
+}
+/**
  *  Indicates the thread was created to go alongside the creation of a wiki
  *  page so that people can discuss the pages contents
  */
diff --git a/controllers/crawl_controller.php b/controllers/crawl_controller.php
index aa6a0ab..e5200c1 100644
--- a/controllers/crawl_controller.php
+++ b/controllers/crawl_controller.php
@@ -31,6 +31,8 @@
 if (!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
 /** Load base controller class if needed */
 require_once BASE_DIR."/controllers/controller.php";
+/** Loads common constants for media operations*/
+require_once BASE_DIR."/lib/media_constants.php";
 /** Loads common constants for web crawling*/
 require_once BASE_DIR."/lib/crawl_constants.php";
 /** Need getHost to partition urls to different queue_servers*/
@@ -47,7 +49,8 @@ require_once BASE_DIR."/lib/url_parser.php";
  * @author Chris Pollett
  * @package seek_quarry\controller
  */
-class CrawlController extends Controller implements CrawlConstants
+class CrawlController extends Controller implements CrawlConstants,
+    MediaConstants
 {
     /**
      * These are the activities supported by this controller
@@ -389,7 +392,7 @@ class CrawlController extends Controller implements CrawlConstants
     {
         if (!isset($_REQUEST["arg"])) { return; }
         $current_machine = $this->clean(webdecode($_REQUEST["arg"]), "string");
-        $convert_folder = WORK_DIRECTORY. "/schedules/media_convert";
+        $convert_folder = WORK_DIRECTORY.self::CONVERT_FOLDER;
         $current_time = time();
         foreach (glob($convert_folder . "/*") as $folder) {
             foreach (glob($folder."/*.{mov,avi}", GLOB_BRACE) as $file) {
diff --git a/controllers/resource_controller.php b/controllers/resource_controller.php
index c461237..3894c3e 100644
--- a/controllers/resource_controller.php
+++ b/controllers/resource_controller.php
@@ -31,6 +31,8 @@
 if (!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
 /** Load base controller class if needed */
 require_once BASE_DIR."/controllers/controller.php";
+/** Loads common constants for media operations*/
+require_once BASE_DIR."/lib/media_constants.php";
 /** Loads common constants for web crawling*/
 require_once BASE_DIR."/lib/crawl_constants.php";
 /** Loads url_parser to clean resource name*/
@@ -41,7 +43,8 @@ require_once BASE_DIR."/lib/url_parser.php";
  * @author Chris Pollett
  * @package seek_quarry\controller
  */
-class ResourceController extends Controller implements CrawlConstants
+class ResourceController extends Controller implements CrawlConstants,
+    MediaConstants
 {
     /**
      * These are the activities supported by this controller
@@ -323,8 +326,8 @@ class ResourceController extends Controller implements CrawlConstants
             echo "Missing parameters in upload message";
             return;
         }
-        $convert_folder = WORK_DIRECTORY."/schedules/media_convert";
-        $converted_folder = WORK_DIRECTORY."/schedules/media_converted";
+        $convert_folder = WORK_DIRECTORY.self::CONVERT_FOLDER;
+        $converted_folder = WORK_DIRECTORY.self::CONVERTED_FOLDER;
         $data = $_REQUEST['data'];
         $folder_name = webdecode($_REQUEST['folder_name']);
         $file_name = webdecode($_REQUEST['file_name']);
diff --git a/lib/mail_server.php b/lib/mail_server.php
index 954133d..11a6258 100644
--- a/lib/mail_server.php
+++ b/lib/mail_server.php
@@ -36,6 +36,8 @@ require_once BASE_DIR.'/lib/analytics_manager.php';
  * Timing functions
  */
 require_once BASE_DIR."/lib/utility.php";
+/** Loads common constants for media operations*/
+require_once BASE_DIR."/lib/media_constants.php";
 /**
  * A small class for communicating with an SMTP server. Used to avoid
  * configuration issues that might be needed with PHP's built-in mail()
@@ -51,7 +53,7 @@ require_once BASE_DIR."/lib/utility.php";
  * @author Chris Pollett
  * @package seek_quarry\library
  */
-class MailServer
+class MailServer implements MediaConstants
 {
     /**
      * Email address of default mail sender
@@ -252,9 +254,46 @@ class MailServer
             $from = $this->sender_email;
         }
         $eol = self::EOL;
-        if (USE_MAIL_PHP) {
-            $header = "From: " . $from . $eol;
-            mail($to, $subject, $message, $header);
+        if (SEND_MAIL_IN_BATCHES) {
+            $mail_directory = WORK_DIRECTORY.self::MAIL_FOLDER;
+            $files = glob($mail_directory."/*.{txt}",GLOB_BRACE);
+            $file_count=count($files);           
+            $current_count=0;
+            $current_time=time();
+            $diff=0;
+            if($file_count>0){
+                $file=end($files);
+                $file_name = str_replace($mail_directory."/","",$file);
+                        $new_file_name = substr($file_name, 0, -4);
+                $diff=$current_time-$new_file_name;
+            } 
+            $header = "From: " . $from . eol;
+            $mail_details=$to."-".$subject."-".$message."-".$header;
+            if($diff > MAX_MAIL_TIMESTAMP_LIMIT || $file_count==0)
+            {                
+                 $file_time=time();
+                 $fp = fopen($mail_directory."/".$file_time.".txt", "a+");
+                 if (flock($fp, LOCK_EX | LOCK_NB)) {
+                    crawlLog("....Lock for $file acquired! Sending emails!\n");
+                    fwrite($fp, $mail_details);
+                    fwrite($fp, PHP_EOL);
+                    flock($fp, LOCK_UN);
+                 } else{
+                    crawlLog("Could not acquire the lock! for $file!\n");
+                }   
+            }else{               
+                crawlLog("...Creating a new file for next mailer batch!\n");
+                $fp = fopen($mail_directory."/".$new_file_name.".txt", "a+");
+                 if (flock($fp, LOCK_EX | LOCK_NB)) {
+                    crawlLog("....Lock acquired! Sending emails now!\n");
+                    fwrite($fp, $mail_details);
+                    fwrite($fp, PHP_EOL);
+                    flock($fp, LOCK_UN);
+                 }else{
+                    crawlLog("Could not acquire the lock! for $file!\n");
+                }  
+            
+            } 
             return;
         }
         $this->messages = "";
diff --git a/lib/media_constants.php b/lib/media_constants.php
new file mode 100644
index 0000000..895da54
--- /dev/null
+++ b/lib/media_constants.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * SeekQuarry/Yioop --
+ * Open Source Pure PHP Search Engine, Crawler, and Indexer
+ *
+ * Copyright (C) 2009 - 2015  Chris Pollett chris@pollett.org
+ *
+ * LICENSE:
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * END LICENSE
+ *
+ * @author Chris Pollett chris@pollett.org
+ * @license http://www.gnu.org/licenses/ GPL3
+ * @link http://www.seekquarry.com/
+ * @copyright 2009 - 2015
+ * @filesource
+ */
+if (!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
+/**
+ * Shared constants and enums used by components that are involved in the
+ * media related operations
+ *
+ * @author Chris Pollett
+ * @package seek_quarry\library
+ */
+interface MediaConstants
+{
+     /**
+     * Used to define folder used for 
+     * placing video files to be converted. 
+     */
+    const CONVERT_FOLDER = "/schedules/media_convert";
+     /**
+     * Used to define folder used for 
+     * placing video files after conversion. 
+     */
+    const CONVERTED_FOLDER = "/schedules/media_converted";
+     /**
+     * Used to place text files(mailer lists) for sending
+     * in batches.
+     */
+    const MAIL_FOLDER = "/schedules/mail";
+}
\ No newline at end of file
-- 
1.9.4.msysgit.1

