/**
 * main functionality function
 * @param  [string] $filename 
 * @return [array]
 */
function whatsapp_reader($filename)
{
    $error_flag = false;
    $errors = [];
    $chat = [];
    $names_array = [];
    $chat_file_path = 'conversations/' . $filename;
    if (!file_exists($chat_file_path)) {
        add_error_message($errors, $error_flag, 'File 404<br>File is like a unicorn to our servers, file was not uploaded properly');
    } else {
        $file_handle = fopen($chat_file_path, "r");
        if (!$file_handle) {
            add_error_message($errors, $error_flag, 'Oh Snap!<br>Some technical glitch, it\'ll be resolved soon!');
        } else {
            $index = 0;
            $first_message = true;
            while (($line = fgets($file_handle)) !== false) {
                $line = explode('-', $line);
                $timestamp = $line[0];
                $timestamp = returntimestamp($timestamp);
                if (!$timestamp) {
                    if ($first_message) {
                        add_error_message($errors, $error_flag, 'It wasn\'t a valid text file or we were not able to convert it!');
                        $first_message = false;
                        break;
                    }
                    $line = implode('-', $line);
                    $last_element_index = sizeof($chat) - 1;
                    $chat[$last_element_index]['line'] .= "\n" . $line;
                } else {
                    if ($first_message) {
                        $first_message = false;
                    }
                    unset($line[0]);
                    $line = implode('-', $line);
                    $line = explode(':', trim($line));
                    $name = trim($line[0]);
                    unset($line[0]);
                    $line = implode(':', $line);
                    $final_chat_string = trim($line);
                    $user_index = get_user_index($names_array, $name);
                    if (strtolower($final_chat_string) == MEDIA_STRING) {
                        $final_string_to_be_printed = null;
                    } else {
                        $final_string_to_be_printed = htmlspecialchars($final_chat_string);
                    }
                    $temp_element = ['index' => $user_index, 'line' => $final_string_to_be_printed, 'time' => $timestamp];
                    array_push($chat, $temp_element);
                }
            }
            // close file handle
            fclose($file_handle);
            // delete file
            // yes, i respect privary
            unlink($chat_file_path);
        }
    }
    $final_response = array('success' => !$error_flag);
    if ($error_flag) {
        $final_response['errors'] = $errors;
    } else {
        $final_response['chat'] = $chat;
        $final_response['users'] = $names_array;
    }
    return $final_response;
}
示例#2
0
/**
 * 保存用户索引
 * 
 * @return void
 * @author seatle <*****@*****.**> 
 * @created time :2015-08-02 12:30
 */
function save_user_index($worker = null)
{
    // 先给一条记录上锁, 采用队列之后就不需要了,这个多进程下还是有问题
    $progress_id = posix_getpid();
    $time = time();
    // 会和下面的更新采集时间发送死锁,因为Order By 会扫描整张表,虽然desc出来的rows为1,也不知道为什么
    //$sql = "Update `user` Set `index_progress_id`='{$progress_id}' Order By `index_uptime` Asc Limit 1";
    // 效率太低
    //$sql = "Update `user` Set `index_progress_id`='15895' Where `index_uptime` = (Select Min(`index_uptime`) From (Select tmp.* From user tmp) a limit 1);";
    // 语法错误
    //$sql = "Update `user` Set `index_progress_id`='{$progress_id}' Where `index_uptime` = (Select Min(`index_uptime`) From `user`)";
    //db::query($sql);
    //$sql = "Select `username`, `depth` From `user` Where `index_progress_id`='{$progress_id}' Order By `index_uptime` Asc Limit 1";
    //$row = db::get_one($sql);
    //if (!empty($row['username']))
    $username = get_user_queue('index');
    if (!empty($username)) {
        $username = addslashes($username);
        // 先把用户深度拿出来,下面要增加1给新用户
        $sql = "Select `depth` From `user` Where `username`='{$username}'";
        $row = db::get_one($sql);
        $depth = $row['depth'];
        // 更新采集时间, 让队列每次都取到不同的用户
        $sql = "Update `user` Set `index_uptime`='{$time}',`index_progress_id`='{$progress_id}' Where `username`='{$username}'";
        db::query($sql);
        $worker->log("采集用户列表 --- " . $username . " --- 开始");
        // $user_rows = get_user_index($username);
        // $user_type followees 、followers
        // 获取关注了
        $followees_user = get_user_index($username, 'followees', $worker);
        $worker->log("采集用户列表 --- " . $username . " --- 关注了 --- 成功");
        // 获取关注者
        $followers_user = get_user_index($username, 'followers', $worker);
        $worker->log("采集用户列表 --- " . $username . " --- 关注者 --- 成功");
        // 合并 关注了 和 关注者
        $user_rows = array_merge($followers_user, $followees_user);
        if (!empty($user_rows)) {
            $worker->log("采集用户列表 --- " . $username . " --- 成功");
            foreach ($user_rows as $user_row) {
                // 子用户
                $c_username = addslashes($user_row['username']);
                $sql = "Select Count(*) As count From `user` Where `username`='{$c_username}'";
                $row = db::get_one($sql);
                // 如果用户不存在
                if (!$row['count']) {
                    $user_row['depth'] = $depth + 1;
                    $user_row['parent_username'] = $username;
                    $user_row['addtime'] = $user_row['index_uptime'] = $user_row['info_uptime'] = time();
                    if (db::insert('user', $user_row)) {
                        $worker->log("入库用户 --- " . $c_username . " --- 成功");
                    } else {
                        $worker->log("入库用户 --- " . $c_username . " --- 失败");
                    }
                }
            }
        } else {
            $worker->log("采集用户列表 --- " . $username . " --- 失败");
        }
    } else {
        $worker->log("采集用户 ---  队列不存在");
    }
}