コード例 #1
0
ファイル: record.php プロジェクト: rebeccayshen/kitlist
 public function delete()
 {
     if ($this->id) {
         // delete history file first
         $uploads = wp_upload_dir();
         $file_name = $uploads['basedir'] . '/wpallimport/history/' . $this->id;
         @file_exists($file_name) and @is_file($file_name) and pmxi_remove_source($file_name, false);
     }
     return parent::delete();
 }
コード例 #2
0
ファイル: record.php プロジェクト: rebeccayshen/kitlist
 public function delete($db = true)
 {
     if ($this->id) {
         // delete history file first
         $uploads = wp_upload_dir();
         $file_name = $uploads['basedir'] . '/wpallimport/logs/' . $this->id . '.html';
         @file_exists($file_name) and @is_file($file_name) and pmxi_remove_source($file_name, true);
         $file_name = pmxi_secure_file($uploads['basedir'] . "/wpallimport/logs", 'logs', $this->id) . '/' . $this->id . '.html';
         @file_exists($file_name) and @is_file($file_name) and pmxi_remove_source($file_name, true);
     }
     return $db ? parent::delete() : true;
 }
コード例 #3
0
ファイル: import.php プロジェクト: rebeccayshen/kitlist
    /**
     * Import processing step (status console)
     */
    public function process($save_history = true)
    {
        $wp_uploads = wp_upload_dir();
        $import = $this->data['update_previous'];
        $history_log = new PMXI_History_Record();
        $input = new PMXI_Input();
        if (!empty(PMXI_Plugin::$session->history_id)) {
            $history_log->getById(PMXI_Plugin::$session->history_id);
        }
        $log_storage = (int) PMXI_Plugin::getInstance()->getOption('log_storage');
        if (!PMXI_Plugin::is_ajax()) {
            $import->set((empty(PMXI_Plugin::$session->source) ? array() : PMXI_Plugin::$session->source) + array('xpath' => PMXI_Plugin::$session->xpath, 'options' => PMXI_Plugin::$session->options, 'count' => PMXI_Plugin::$session->count, 'friendly_name' => PMXI_Plugin::$session->options['friendly_name'], 'feed_type' => PMXI_Plugin::$session->feed_type, 'parent_import_id' => $this->data['update_previous']->isEmpty() ? PMXI_Plugin::$session->parent_import_id : $this->data['update_previous']->parent_import_id, 'queue_chunk_number' => 0, 'triggered' => 0, 'processing' => 0, 'executing' => 1, 'iteration' => !empty($import->iteration) ? $import->iteration : 0))->save();
            if (PMXI_Plugin::$session->action != 'continue') {
                // store import info in database
                $import->set(array('imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0))->update();
            }
            // add history log
            $custom_type = get_post_type_object($import->options['custom_type']);
            // unlink previous logs
            $by = array();
            $by[] = array(array('import_id' => $import->id, 'type NOT LIKE' => 'trigger'), 'AND');
            $historyLogs = new PMXI_History_List();
            $historyLogs->setColumns('id', 'import_id', 'type', 'date')->getBy($by, 'id ASC');
            if ($historyLogs->count() and $historyLogs->count() >= $log_storage) {
                $logsToRemove = $historyLogs->count() - $log_storage;
                foreach ($historyLogs as $i => $file) {
                    $historyRecord = new PMXI_History_Record();
                    $historyRecord->getBy('id', $file['id']);
                    if (!$historyRecord->isEmpty()) {
                        $historyRecord->delete();
                    }
                    // unlink history file only
                    if ($i == $logsToRemove) {
                        break;
                    }
                }
            }
            $history_log->set(array('import_id' => $import->id, 'date' => date('Y-m-d H:i:s'), 'type' => PMXI_Plugin::$session->action != 'continue' ? 'manual' : 'continue', 'summary' => sprintf(__("%d %ss created %d updated %d deleted %d skipped", "pmxi_plugin"), $import->created, $custom_type->labels->singular_name, $import->updated, $import->deleted, $import->skipped)))->save();
            PMXI_Plugin::$session->set('history_id', $history_log->id);
            foreach (get_taxonomies() as $tax) {
                delete_transient("pmxi_{$tax}_terms");
            }
            do_action('pmxi_before_xml_import', $import->id);
            PMXI_Plugin::$session->set('update_previous', $import->id);
            if (empty($import->options['encoding'])) {
                $currentOptions = $import->options;
                $currentOptions['encoding'] = 'UTF-8';
                $import->set(array('options' => $currentOptions))->update();
            }
            // unlink previous files
            $history = new PMXI_File_List();
            $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $import->id), 'id DESC');
            if ($history->count()) {
                foreach ($history as $file) {
                    if (@file_exists($file['path']) and $file['path'] != PMXI_Plugin::$session->filePath) {
                        if (in_array($import->type, array('upload'))) {
                            pmxi_remove_source($file['path'], false);
                        } else {
                            pmxi_remove_source($file['path']);
                        }
                    }
                    $history_file = new PMXI_File_Record();
                    $history_file->getBy('id', $file['id']);
                    if (!$history_file->isEmpty()) {
                        $history_file->delete();
                    }
                }
            }
            if ($save_history) {
                $history_file = new PMXI_File_Record();
                $history_file->set(array('name' => $import->name, 'import_id' => $import->id, 'path' => PMXI_Plugin::$session->filePath, 'registered_on' => date('Y-m-d H:i:s')))->save();
            }
            /*
            	Split file up into 1000 record chunks.			
            	This option will decrease the amount of slowdown experienced at the end of large imports. 
            	The slowdown is partially caused by the need for WP All Import to read deeper and deeper into the file on each successive iteration. 
            	Splitting the file into pieces means that, for example, instead of having to read 19000 records into a 20000 record file when importing the last 1000 records, 
            	WP All Import will just split it into 20 chunks, and then read the last chunk from the beginning.
            */
            if ("ajax" == $import->options['import_processing'] and $import->count > PMXI_Plugin::getInstance()->getOption('large_feed_limit') and $import->options['chuncking']) {
                $chunk_files = array();
                if (!empty(PMXI_Plugin::$session->local_paths)) {
                    $records_count = 0;
                    $chunk_records_count = 0;
                    $feed = "<?xml version=\"1.0\" encoding=\"" . $import->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>";
                    foreach (PMXI_Plugin::$session->local_paths as $key => $path) {
                        $file = new PMXI_Chunk($path, array('element' => $import->root_element, 'encoding' => $import->options['encoding']));
                        // loop through the file until all lines are read
                        while ($xml = $file->read()) {
                            if (!empty($xml)) {
                                PMXI_Import_Record::preprocessXml($xml);
                                $chunk = "<?xml version=\"1.0\" encoding=\"" . $import->options['encoding'] . "\"?>" . "\n" . $xml;
                                $dom = new DOMDocument('1.0', $import->options['encoding']);
                                $old = libxml_use_internal_errors(true);
                                $dom->loadXML($chunk);
                                // FIX: libxml xpath doesn't handle default namespace properly, so remove it upon XML load
                                libxml_use_internal_errors($old);
                                $xpath = new DOMXPath($dom);
                                if ($elements = @$xpath->query($import->xpath) and $elements->length) {
                                    $records_count += $elements->length;
                                    $chunk_records_count += $elements->length;
                                    $feed .= $xml;
                                }
                            }
                            if ($chunk_records_count == PMXI_Plugin::getInstance()->getOption('large_feed_limit') or $records_count == $import->count) {
                                $feed .= "</pmxi_records>";
                                $chunk_file_path = pmxi_secure_file($wp_uploads['basedir'] . "/wpallimport/temp", 'temp') . "/pmxi_chunk_" . count($chunk_files) . "_" . basename($path);
                                file_put_contents($chunk_file_path, $feed);
                                $chunk_files[] = $chunk_file_path;
                                $chunk_records_count = 0;
                                $feed = "<?xml version=\"1.0\" encoding=\"" . $import->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>";
                            }
                        }
                    }
                    PMXI_Plugin::$session->set('local_paths', $chunk_files);
                }
            }
            PMXI_Plugin::$session->save_data();
            if ($log_storage) {
                $log_file = pmxi_secure_file($wp_uploads['basedir'] . "/wpallimport/logs", 'logs', $history_log->id) . '/' . $history_log->id . '.html';
                if (PMXI_Plugin::$session->action != 'continue' and file_exists($log_file)) {
                    pmxi_remove_source($log_file, false);
                }
            }
            $this->data['ajax_processing'] = "ajax" == $import->options['import_processing'] ? true : false;
            $this->render();
            wp_ob_end_flush_all();
            flush();
            @set_time_limit(0);
            $import_id = $input->get('id', 0);
            if ("ajax" == $import->options['import_processing'] and !$import_id) {
                PMXI_Plugin::$session->convertData($import->id);
                //die();
            }
        } elseif (empty($import->id)) {
            $import = new PMXI_Import_Record();
            $import_id = $input->get('id', PMXI_Plugin::$session->update_previous);
            $import->getById($import_id);
        }
        $ajax_processing = "ajax" == $import->options['import_processing'] ? true : false;
        if ($ajax_processing) {
            $logger = create_function('$m', 'echo "<div class=\\"progress-msg\\">$m</div>\\n"; flush();');
        } else {
            $logger = create_function('$m', 'echo "<div class=\\"progress-msg\\">$m</div>\\n"; if ( "" != strip_tags(pmxi_strip_tags_content($m))) { PMXI_Plugin::$session->log .= "<p>".strip_tags(pmxi_strip_tags_content($m))."</p>"; flush(); }');
        }
        PMXI_Plugin::$session->set('start_time', empty(PMXI_Plugin::$session->start_time) ? time() : PMXI_Plugin::$session->start_time);
        wp_cache_flush();
        if (PMXI_Plugin::is_ajax() or !$ajax_processing) {
            $iteration_start_time = time();
            if ($log_storage) {
                $log_file = pmxi_secure_file($wp_uploads['basedir'] . "/wpallimport/logs", 'logs', $history_log->id) . '/' . $history_log->id . '.html';
            }
            if ($ajax_processing) {
                // HTTP headers for no cache etc
                header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
                header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
                header("Cache-Control: no-store, no-cache, must-revalidate");
                header("Cache-Control: post-check=0, pre-check=0", false);
                header("Pragma: no-cache");
            }
            $loop = 0;
            $pointer = 0;
            $records = array();
            if ($import->options['is_import_specified']) {
                foreach (preg_split('% *, *%', $import->options['import_specified'], -1, PREG_SPLIT_NO_EMPTY) as $chank) {
                    if (preg_match('%^(\\d+)-(\\d+)$%', $chank, $mtch)) {
                        $records = array_merge($records, range(intval($mtch[1]), intval($mtch[2])));
                    } else {
                        $records = array_merge($records, array(intval($chank)));
                    }
                }
            }
            $records_to_import = empty($records) ? $import->count : $records[count($records) - 1];
            $failures = $input->get('failures', 0);
            // auto decrease records per iteration option
            if ($failures) {
                $options = $import->options;
                $options['records_per_request'] = ceil($options['records_per_request'] / 2) ? ceil($options['records_per_request'] / 2) : 1;
                $import->set(array('options' => $options))->update();
            }
            $records_per_request = (!$ajax_processing and $import->options['records_per_request'] < 50) ? 50 : $import->options['records_per_request'];
            if (!empty(PMXI_Plugin::$session->local_paths)) {
                $feed = "<?xml version=\"1.0\" encoding=\"" . $import->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>";
                foreach (PMXI_Plugin::$session->local_paths as $key => $path) {
                    $import_done = $import->imported + $import->skipped == $records_to_import ? true : false;
                    if ($import_done) {
                        if (strpos($path, "pmxi_chunk_") !== false and @file_exists($path)) {
                            pmxi_remove_source($path, false);
                        }
                        PMXI_Plugin::$session->set('local_paths', array());
                        PMXI_Plugin::$session->save_data();
                        break;
                    }
                    $file = new PMXI_Chunk($path, array('element' => $import->root_element, 'encoding' => $import->options['encoding'], 'pointer' => PMXI_Plugin::$session->pointer));
                    // loop through the file until all lines are read
                    while ($xml = $file->read() and empty($import->canceled)) {
                        if (!empty($xml)) {
                            PMXI_Import_Record::preprocessXml($xml);
                            $chunk = "<?xml version=\"1.0\" encoding=\"" . $import->options['encoding'] . "\"?>" . "\n" . $xml;
                            $dom = new DOMDocument('1.0', $import->options['encoding']);
                            $old = libxml_use_internal_errors(true);
                            $dom->loadXML($chunk);
                            // FIX: libxml xpath doesn't handle default namespace properly, so remove it upon XML load
                            libxml_use_internal_errors($old);
                            $xpath = new DOMXPath($dom);
                            $pointer++;
                            if ($this->data['elements'] = $elements = @$xpath->query($import->xpath) and $elements->length) {
                                /* Merge nested XML/CSV files */
                                /*$nested_files = json_decode($import->options['nested_files'], true);
                                		if ( ! empty($nested_files) and is_array($nested_files)){										
                                			$merger = new PMXI_Nested($dom, $nested_files, $xml, $import->xpath, $elements);
                                			$merger->merge();
                                			$xml = $merger->get_xml();
                                			unset($merger);
                                		}	*/
                                // continue action
                                if ($import->imported + $import->skipped >= PMXI_Plugin::$session->chunk_number + $elements->length - 1) {
                                    PMXI_Plugin::$session->set('chunk_number', PMXI_Plugin::$session->chunk_number + $elements->length);
                                    PMXI_Plugin::$session->save_data();
                                    continue;
                                }
                                if (!$loop and $ajax_processing) {
                                    ob_start();
                                }
                                $feed .= $xml;
                                $loop += $elements->length;
                                $processed_records = $import->imported + $import->skipped;
                                if ($loop == $records_per_request or $processed_records + $loop == $records_to_import or $processed_records == $records_to_import) {
                                    $feed .= "</pmxi_records>";
                                    $import->process($feed, $logger, PMXI_Plugin::$session->chunk_number, false, '/pmxi_records', $loop);
                                    unset($dom, $xpath);
                                    if (!$ajax_processing) {
                                        $feed = "<?xml version=\"1.0\" encoding=\"" . $import->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>";
                                        $loop = 0;
                                    } else {
                                        if (!$history_log->isEmpty()) {
                                            $custom_type = get_post_type_object($import->options['custom_type']);
                                            $history_log->set(array('time_run' => time() - strtotime($history_log->date), 'summary' => sprintf(__("%d %ss created %d updated %d deleted %d skipped", "pmxi_plugin"), $import->created, $custom_type->labels->singular_name, $import->updated, $import->deleted, $import->skipped)))->update();
                                        }
                                        unset($file);
                                        PMXI_Plugin::$session->set('pointer', PMXI_Plugin::$session->pointer + $pointer);
                                        PMXI_Plugin::$session->save_data();
                                        $log_data = ob_get_clean();
                                        if ($log_storage) {
                                            $log = @fopen($log_file, 'a+');
                                            @fwrite($log, $log_data);
                                            @fclose($log);
                                        }
                                        $iteration_execution_time = time() - $iteration_start_time;
                                        wp_send_json(array('created' => $import->created, 'updated' => $import->updated, 'percentage' => ceil($processed_records / $import->count * 100), 'warnings' => PMXI_Plugin::$session->warnings, 'errors' => PMXI_Plugin::$session->errors, 'log' => $log_data, 'done' => false, 'records_per_request' => $import->options['records_per_request']));
                                    }
                                }
                            }
                        }
                    }
                    // Move to the next file, set pointer to first element
                    if ($ajax_processing) {
                        if (strpos($path, "pmxi_chunk_") !== false and @file_exists($path)) {
                            @unlink($path);
                        }
                        PMXI_Plugin::$session->set('pointer', 1);
                        $pointer = 0;
                        $lp = PMXI_Plugin::$session->local_paths;
                        array_shift($lp);
                        PMXI_Plugin::$session->set('local_paths', $lp);
                        PMXI_Plugin::$session->save_data();
                    } else {
                        break;
                    }
                }
            }
        }
        if (PMXI_Plugin::is_ajax() and empty(PMXI_Plugin::$session->local_paths) or !$ajax_processing or !empty($import->canceled)) {
            do_action('pmxi_after_xml_import', $import->id);
            if ("ajax" != $import->options['import_processing'] and $log_storage) {
                $log_file = pmxi_secure_file($wp_uploads['basedir'] . "/wpallimport/logs", 'logs', $history_log->id) . '/' . $history_log->id . '.html';
                if (PMXI_Plugin::$session->action != 'continue') {
                    @file_put_contents($log_file, PMXI_Plugin::$session->log);
                } else {
                    $log = @fopen($log_file, 'a+');
                    @fwrite($log, PMXI_Plugin::$session->log);
                    @fclose($log);
                }
            }
            wp_cache_flush();
            foreach (get_taxonomies() as $tax) {
                delete_option("{$tax}_children");
                _get_term_hierarchy($tax);
            }
            $import->set(array('registered_on' => date('Y-m-d H:i:s'), 'executing' => 0))->update();
            // add history log
            $custom_type = get_post_type_object($import->options['custom_type']);
            $history_log->set(array('time_run' => time() - strtotime($history_log->date), 'summary' => sprintf(__("%d %ss created %d updated %d deleted %d skipped", "pmxi_plugin"), $import->created, $custom_type->labels->singular_name, $import->updated, $import->deleted, $import->skipped)))->update();
            // clear import session
            PMXI_Plugin::$session->clean_session($import->id);
            // clear session data (prevent from reimporting the same data on page refresh)
            // [indicate in header process is complete]
            $msg = !empty($import->canceled) ? addcslashes(__('Canceled', 'pmxi_plugin'), "\n\r") : addcslashes(__('Complete', 'pmxi_plugin'), "\n\r");
            if ($ajax_processing) {
                ob_start();
            }
            $import->options['is_import_specified'] and $logger and call_user_func($logger, 'Done');
            echo <<<COMPLETE
<script type="text/javascript">
//<![CDATA[
(function(\$){\t
\t\$('#status').html('{$msg}');\t
\twindow.onbeforeunload = false;
})(jQuery);
//]]>
</script>
COMPLETE;
            // [/indicate in header process is complete]
            if ($ajax_processing) {
                wp_send_json(array('created' => $import->created, 'updated' => $import->updated, 'percentage' => 100, 'warnings' => PMXI_Plugin::$session->warnings, 'errors' => PMXI_Plugin::$session->errors, 'log' => ob_get_clean(), 'done' => true, 'records_per_request' => $import->options['records_per_request']));
            }
        }
    }
コード例 #4
0
ファイル: wp_loaded.php プロジェクト: rebeccayshen/kitlist
function pmxi_wp_loaded()
{
    @ini_set("max_input_time", PMXI_Plugin::getInstance()->getOption('max_input_time'));
    @ini_set("max_execution_time", PMXI_Plugin::getInstance()->getOption('max_execution_time'));
    $table = PMXI_Plugin::getInstance()->getTablePrefix() . 'imports';
    global $wpdb;
    $imports = $wpdb->get_results("SELECT `id`, `name`, `path` FROM {$table} WHERE `path` IS NULL", ARRAY_A);
    if (!empty($imports)) {
        $importRecord = new PMXI_Import_Record();
        $importRecord->clear();
        foreach ($imports as $imp) {
            $importRecord->getById($imp['id']);
            if (!$importRecord->isEmpty()) {
                $importRecord->delete(true);
            }
            $importRecord->clear();
        }
    }
    /* Check if cron is manualy, then execute import */
    $cron_job_key = PMXI_Plugin::getInstance()->getOption('cron_job_key');
    if (!empty($cron_job_key) and !empty($_GET['import_id']) and !empty($_GET['import_key']) and $_GET['import_key'] == $cron_job_key and !empty($_GET['action']) and in_array($_GET['action'], array('processing', 'trigger', 'pipe'))) {
        $logger = create_function('$m', 'echo "<p>$m</p>\\n";');
        $import = new PMXI_Import_Record();
        $ids = explode(',', $_GET['import_id']);
        if (!empty($ids) and is_array($ids)) {
            foreach ($ids as $id) {
                if (empty($id)) {
                    continue;
                }
                $import->getById($id);
                if (!$import->isEmpty()) {
                    if (!in_array($import->type, array('url', 'ftp', 'file'))) {
                        $logger and call_user_func($logger, sprintf(__('Scheduling update is not working with "upload" import type. Import #%s.', 'pmxi_plugin'), $id));
                    }
                    switch ($_GET['action']) {
                        case 'trigger':
                            if ((int) $import->executing) {
                                $logger and call_user_func($logger, sprintf(__('Import #%s is currently in manually process. Request skipped.', 'pmxi_plugin'), $id));
                            } elseif (!$import->processing and !$import->triggered) {
                                $import->set(array('triggered' => 1, 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'queue_chunk_number' => 0, 'last_activity' => date('Y-m-d H:i:s')))->update();
                                $history_log = new PMXI_History_Record();
                                $history_log->set(array('import_id' => $import->id, 'date' => date('Y-m-d H:i:s'), 'type' => 'trigger', 'summary' => __("triggered by cron", "pmxi_plugin")))->save();
                                $logger and call_user_func($logger, sprintf(__('#%s Cron job triggered.', 'pmxi_plugin'), $id));
                            } elseif ($import->processing and !$import->triggered) {
                                $logger and call_user_func($logger, sprintf(__('Import #%s currently in process. Request skipped.', 'pmxi_plugin'), $id));
                            } elseif (!$import->processing and $import->triggered) {
                                $logger and call_user_func($logger, sprintf(__('Import #%s already triggered. Request skipped.', 'pmxi_plugin'), $id));
                            }
                            break;
                        case 'processing':
                            if ($import->processing == 1 and time() - strtotime($import->registered_on) > (PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') ? PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') : 120)) {
                                // it means processor crashed, so it will reset processing to false, and terminate. Then next run it will work normally.
                                $import->set(array('processing' => 0))->update();
                            }
                            // start execution imports that is in the cron process
                            if (!(int) $import->triggered) {
                                $logger and call_user_func($logger, sprintf(__('Import #%s is not triggered. Request skipped.', 'pmxi_plugin'), $id));
                            } elseif ((int) $import->executing) {
                                $logger and call_user_func($logger, sprintf(__('Import #%s is currently in manually process. Request skipped.', 'pmxi_plugin'), $id));
                            } elseif ((int) $import->triggered and !(int) $import->processing) {
                                $log_storage = (int) PMXI_Plugin::getInstance()->getOption('log_storage');
                                // unlink previous logs
                                $by = array();
                                $by[] = array(array('import_id' => $id, 'type NOT LIKE' => 'trigger'), 'AND');
                                $historyLogs = new PMXI_History_List();
                                $historyLogs->setColumns('id', 'import_id', 'type', 'date')->getBy($by, 'id ASC');
                                if ($historyLogs->count() and $historyLogs->count() >= $log_storage) {
                                    $logsToRemove = $historyLogs->count() - $log_storage;
                                    foreach ($historyLogs as $i => $file) {
                                        $historyRecord = new PMXI_History_Record();
                                        $historyRecord->getBy('id', $file['id']);
                                        if (!$historyRecord->isEmpty()) {
                                            $historyRecord->delete();
                                        }
                                        // unlink history file only
                                        if ($i == $logsToRemove) {
                                            break;
                                        }
                                    }
                                }
                                $history_log = new PMXI_History_Record();
                                $history_log->set(array('import_id' => $import->id, 'date' => date('Y-m-d H:i:s'), 'type' => 'processing', 'summary' => __("cron processing", "pmxi_plugin")))->save();
                                if ($log_storage) {
                                    $wp_uploads = wp_upload_dir();
                                    $log_file = pmxi_secure_file($wp_uploads['basedir'] . "/wpallimport/logs", 'logs', $history_log->id) . '/' . $history_log->id . '.html';
                                    if (@file_exists($log_file)) {
                                        pmxi_remove_source($log_file, false);
                                    }
                                }
                                ob_start();
                                $import->set(array('canceled' => 0, 'failed' => 0))->execute($logger, true, $history_log->id);
                                $log_data = ob_get_clean();
                                if ($log_storage) {
                                    $log = @fopen($log_file, 'a+');
                                    @fwrite($log, $log_data);
                                    @fclose($log);
                                }
                                if (!(int) $import->queue_chunk_number) {
                                    $logger and call_user_func($logger, sprintf(__('Import #%s complete', 'pmxi_plugin'), $import->id));
                                } else {
                                    $logger and call_user_func($logger, sprintf(__('Records Count %s', 'pmxi_plugin'), (int) $import->count));
                                    $logger and call_user_func($logger, sprintf(__('Records Processed %s', 'pmxi_plugin'), (int) $import->queue_chunk_number));
                                }
                            } else {
                                $logger and call_user_func($logger, sprintf(__('Import #%s already processing. Request skipped.', 'pmxi_plugin'), $id));
                            }
                            break;
                        case 'pipe':
                            $import->execute($logger);
                            break;
                    }
                }
            }
        }
    }
}
コード例 #5
0
ファイル: record.php プロジェクト: rebeccayshen/kitlist
 /**
  * Delete associated files
  * @return PMXI_Import_Record
  * @chainable
  */
 public function deleteFiles()
 {
     $fileList = new PMXI_File_List();
     foreach ($fileList->getBy('import_id', $this->id)->convertRecords() as $f) {
         if (@file_exists($f->path)) {
             pmxi_remove_source($f->path);
         }
         $f->delete();
     }
     return $this;
 }
コード例 #6
0
ファイル: upload.php プロジェクト: rebeccayshen/kitlist
 public function file()
 {
     $wp_uploads = wp_upload_dir();
     $uploads = $wp_uploads['basedir'] . '/wpallimport/files/';
     if (empty($this->file)) {
         $this->errors->add('form-validation', __('Please specify a file to import.', 'pmxi_plugin'));
     } elseif (preg_match('%\\W(zip)$%i', trim($this->file))) {
         if ($this->uploadsPath === false) {
             $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'pmxi_plugin'));
         }
         echo '<span style="display:none">';
         copy($uploads . $this->file, $this->uploadsPath . '/' . basename($this->file));
         echo '</span>';
         $zipfilePath = $this->uploadsPath . '/' . basename($this->file);
         include_once PMXI_Plugin::ROOT_DIR . '/libraries/pclzip.lib.php';
         $archive = new PclZip($zipfilePath);
         if (($v_result_list = $archive->extract(PCLZIP_OPT_PATH, $this->uploadsPath, PCLZIP_OPT_REPLACE_NEWER)) == 0) {
             $this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of  XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'pmxi_plugin'));
         } else {
             $filePath = '';
             if (!empty($v_result_list)) {
                 foreach ($v_result_list as $unzipped_file) {
                     if ($unzipped_file['status'] == 'ok' and preg_match('%\\W(xml|csv|txt|dat|psv|json)$%i', trim($unzipped_file['stored_filename']))) {
                         $filePath = $unzipped_file['filename'];
                         break;
                     }
                 }
             }
             if ($this->uploadsPath === false) {
                 $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'pmxi_plugin'));
             }
             if (empty($filePath)) {
                 $zip = zip_open(trim($zipfilePath));
                 if (is_resource($zip)) {
                     while ($zip_entry = zip_read($zip)) {
                         $filePath = zip_entry_name($zip_entry);
                         $fp = fopen($this->uploadsPath . "/" . $filePath, "w");
                         if (zip_entry_open($zip, $zip_entry, "r")) {
                             $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                             fwrite($fp, "{$buf}");
                             zip_entry_close($zip_entry);
                             fclose($fp);
                         }
                         break;
                     }
                     zip_close($zip);
                 } else {
                     $this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of  XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'pmxi_plugin'));
                 }
             }
             // Detect if file is very large
             $source = array('name' => basename($this->file), 'type' => 'file', 'path' => $uploads . $this->file);
             if (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($filePath))) {
                 // If CSV file found in archieve
                 if ($this->uploadsPath === false) {
                     $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'pmxi_plugin'));
                 }
                 include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                 $csv = new PMXI_CsvParser(array('filename' => $filePath, 'targetDir' => $this->uploadsPath));
                 // create chunks
                 //pmxi_remove_source($filePath, false);
                 $filePath = $csv->xml_path;
                 $this->is_csv = $csv->is_csv;
                 $this->root_element = 'node';
             } elseif (preg_match('%\\W(json)$%i', trim($filePath))) {
                 $json_str = file_get_contents($filePath);
                 $is_json = pmxi_isJson($json_str);
                 if (is_wp_error($is_json)) {
                     $this->errors->add('form-validation', $is_json->get_error_message(), 'pmxi_plugin');
                 } else {
                     $xml_data = pmxi_json_to_xml(json_decode($json_str, true));
                     if (empty($xml_data)) {
                         $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'pmxi_plugin'));
                     } else {
                         $jsontmpname = $this->uploadsPath . '/' . url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
                         file_put_contents($jsontmpname, $xml_data);
                         pmxi_remove_source($filePath);
                         $filePath = $jsontmpname;
                     }
                 }
             } elseif (preg_match('%\\W(sql)$%i', trim($filePath))) {
                 include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php';
                 $localSQLPath = $filePath;
                 $sql = new PMXI_SQLParser($localSQLPath, $this->uploadsPath);
                 $filePath = $sql->parse();
                 pmxi_remove_source($localSQLPath, false);
             }
         }
         if (file_exists($zipfilePath)) {
             pmxi_remove_source($zipfilePath, false);
         }
     } elseif (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($this->file))) {
         if ($this->uploadsPath === false) {
             $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'pmxi_plugin'));
         }
         // copy file in temporary folder
         // hide warning message
         echo '<span style="display:none">';
         copy($uploads . $this->file, $this->uploadsPath . '/' . basename($this->file));
         echo '</span>';
         $filePath = $this->uploadsPath . '/' . basename($this->file);
         $source = array('name' => basename($this->file), 'type' => 'file', 'path' => $uploads . $this->file);
         // Detect if file is very large
         include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
         $csv = new PMXI_CsvParser(array('filename' => $filePath, 'targetDir' => $this->uploadsPath));
         // create chunks
         //pmxi_remove_source($filePath, false);
         $filePath = $csv->xml_path;
         $this->is_csv = $csv->is_csv;
         $this->root_element = 'node';
     } elseif (preg_match('%\\W(json)$%i', trim($this->file))) {
         if ($this->uploadsPath === false) {
             $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'pmxi_plugin'));
         }
         // copy file in temporary folder
         // hide warning message
         echo '<span style="display:none">';
         copy($uploads . $this->file, $this->uploadsPath . '/' . basename($this->file));
         echo '</span>';
         $filePath = $this->uploadsPath . '/' . basename($this->file);
         $source = array('name' => basename($this->file), 'type' => 'file', 'path' => $uploads . $this->file);
         $json_str = file_get_contents($filePath);
         $is_json = pmxi_isJson($json_str);
         if (is_wp_error($is_json)) {
             $this->errors->add('form-validation', $is_json->get_error_message(), 'pmxi_plugin');
         } else {
             $xml_data = pmxi_json_to_xml(json_decode($json_str, true));
             if (empty($xml_data)) {
                 $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'pmxi_plugin'));
             } else {
                 $jsontmpname = $this->uploadsPath . '/' . url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
                 file_put_contents($jsontmpname, $xml_data);
                 pmxi_remove_source($filePath, false);
                 $filePath = $jsontmpname;
             }
         }
     } elseif (preg_match('%\\W(sql)$%i', trim($this->file))) {
         if ($this->uploadsPath === false) {
             $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'pmxi_plugin'));
         }
         // copy file in temporary folder
         // hide warning message
         echo '<span style="display:none">';
         copy($uploads . $this->file, $this->uploadsPath . '/' . basename($this->file));
         echo '</span>';
         $localSQLPath = $this->uploadsPath . '/' . basename($this->file);
         $source = array('name' => basename($this->file), 'type' => 'file', 'path' => $uploads . $this->file);
         include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php';
         $sql = new PMXI_SQLParser($localSQLPath, $this->uploadsPath);
         $filePath = $sql->parse();
         pmxi_remove_source($localSQLPath, false);
     } else {
         if ($this->uploadsPath === false) {
             $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'pmxi_plugin'));
         }
         // copy file in temporary folder
         // hide warning message
         echo '<span style="display:none">';
         copy($uploads . $this->file, $this->uploadsPath . '/' . basename($this->file));
         echo '</span>';
         $source = array('name' => basename($this->file), 'type' => 'file', 'path' => $uploads . $this->file);
         $filePath = $this->uploadsPath . '/' . basename($this->file);
         if (preg_match('%\\W(gz)$%i', basename($this->file))) {
             $fileInfo = pmxi_gzfile_get_contents($filePath, 0, $this->uploadsPath);
             if (!is_wp_error($fileInfo)) {
                 pmxi_remove_source($filePath, false);
                 $filePath = $fileInfo['localPath'];
             } else {
                 $this->errors->add('form-validation', $fileInfo->get_error_message());
             }
         }
         if (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($this->file)) or !empty($fileInfo) and $fileInfo['type'] == 'csv') {
             include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
             $csv = new PMXI_CsvParser(array('filename' => $filePath, 'targetDir' => $this->uploadsPath));
             // create chunks
             //pmxi_remove_source($filePath, false);
             $filePath = $csv->xml_path;
             $this->is_csv = $csv->is_csv;
             $this->root_element = 'node';
         }
     }
     if ($this->errors->get_error_codes()) {
         return $this->errors;
     }
     return array('filePath' => $filePath, 'source' => $source, 'root_element' => $this->root_element, 'is_csv' => $this->is_csv);
 }