Beispiel #1
0
 /**
  * @see PMXI_Model_Record::insert()
  */
 public function insert()
 {
     $file_contents = NULL;
     if ($this->offsetExists('contents')) {
         $file_contents = $this['contents'];
         unset($this->contents);
     }
     parent::insert();
     $uploads = wp_upload_dir();
     if (isset($this->id) and !is_null($file_contents)) {
         file_put_contents($uploads['basedir'] . '/wpallimport/history/' . $this->id, $file_contents);
     }
     $list = new PMXI_File_List();
     $list->sweepHistory();
     return $this;
 }
Beispiel #2
0
 /**
  * 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)) {
             wp_all_import_remove_source($f->path);
         }
         $f->delete();
     }
     return $this;
 }
Beispiel #3
0
 /**
  * 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) {
         $f->delete();
     }
     return $this;
 }
Beispiel #4
0
 /**
  * Re-run import
  */
 public function update()
 {
     $id = $this->input->get('id');
     PMXI_Plugin::$session->clean_session($id);
     $action_type = false;
     $this->data['import'] = $item = new PMXI_Import_Record();
     if (!$id or $item->getById($id)->isEmpty()) {
         wp_redirect($this->baseUrl);
         die;
     }
     $this->data['isWizard'] = false;
     $default = PMXI_Plugin::get_default_import_options();
     $DefaultOptions = $item->options + $default;
     foreach (PMXI_Admin_Addons::get_active_addons() as $class) {
         if (class_exists($class)) {
             $DefaultOptions += call_user_func(array($class, "get_default_import_options"));
         }
     }
     $this->data['post'] =& $DefaultOptions;
     $this->data['source'] = array('path' => $item->path, 'root_element' => $item->root_element);
     $this->data['xpath'] = $item->xpath;
     $this->data['count'] = $item->count;
     $history = new PMXI_File_List();
     $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $item->id), 'id DESC');
     if ($history->count()) {
         foreach ($history as $file) {
             if (@file_exists($file['path'])) {
                 $this->data['locfilePath'] = $file['path'];
                 break;
             }
         }
     }
     $chunks = 0;
     if ($this->input->post('is_confirmed') and check_admin_referer('confirm', '_wpnonce_confirm')) {
         $continue = $this->input->post('is_continue', 'no');
         // mark action type ad continue
         if ($continue == 'yes') {
             $action_type = 'continue';
         }
         $filePath = '';
         // upload new file in case when import is not continue
         if (empty(PMXI_Plugin::$session->chunk_number)) {
             if ($item->type == 'upload') {
                 // retrieve already uploaded file
                 $uploader = new PMXI_Upload(trim($item->path), $this->errors, rtrim(str_replace(basename($item->path), '', $item->path), '/'));
                 $upload_result = $uploader->upload();
                 if ($upload_result instanceof WP_Error) {
                     $this->errors = $upload_result;
                 } else {
                     $filePath = $upload_result['filePath'];
                 }
             }
             if (empty($item->options['encoding'])) {
                 $currentOptions = $item->options;
                 $currentOptions['encoding'] = 'UTF-8';
                 $item->set(array('options' => $currentOptions))->update();
             }
             @set_time_limit(0);
             $local_paths = !empty($local_paths) ? $local_paths : array($filePath);
             foreach ($local_paths as $key => $path) {
                 if (!empty($action_type) and $action_type == 'continue') {
                     $chunks = $item->count;
                 } else {
                     $file = new PMXI_Chunk($path, array('element' => $item->root_element, 'encoding' => $item->options['encoding']));
                     while ($xml = $file->read()) {
                         if (!empty($xml)) {
                             PMXI_Import_Record::preprocessXml($xml);
                             $xml = "<?xml version=\"1.0\" encoding=\"" . $item->options['encoding'] . "\"?>" . "\n" . $xml;
                             $dom = new DOMDocument('1.0', !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8');
                             $old = libxml_use_internal_errors(true);
                             $dom->loadXML($xml);
                             // 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($item->xpath) and !empty($elements) and !empty($elements->length)) {
                                 $chunks += $elements->length;
                             }
                             unset($dom, $xpath, $elements);
                         }
                     }
                     unset($file);
                 }
                 !$key and $filePath = $path;
             }
             if (empty($chunks)) {
                 $this->errors->add('form-validation', __('No matching elements found for Root element and XPath expression specified', 'wp_all_import_plugin'));
             }
         }
         if ($chunks) {
             // xml is valid
             if (!PMXI_Plugin::is_ajax() and empty(PMXI_Plugin::$session->chunk_number)) {
                 // compose data to look like result of wizard steps
                 $sesson_data = array('filePath' => $filePath, 'source' => array('name' => $item->name, 'type' => $item->type, 'path' => $item->path, 'root_element' => $item->root_element), 'feed_type' => $item->feed_type, 'update_previous' => $item->id, 'parent_import_id' => $item->parent_import_id, 'xpath' => $item->xpath, 'options' => $item->options, 'encoding' => !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8', 'is_csv' => !empty($item->options['delimiter']) ? $item->options['delimiter'] : PMXI_Plugin::$is_csv, 'csv_path' => PMXI_Plugin::$csv_path, 'chunk_number' => 1, 'log' => '', 'warnings' => 0, 'errors' => 0, 'start_time' => 0, 'pointer' => 1, 'count' => isset($chunks) ? $chunks : 0, 'local_paths' => !empty($local_paths) ? $local_paths : array(), 'action' => (!empty($action_type) and $action_type == 'continue') ? 'continue' : 'update', 'nonce' => wp_create_nonce('import'));
                 foreach ($sesson_data as $key => $value) {
                     PMXI_Plugin::$session->set($key, $value);
                 }
                 PMXI_Plugin::$session->save_data();
             }
             $item->set(array('canceled' => 0, 'failed' => 0))->update();
             // deligate operation to other controller
             $controller = new PMXI_Admin_Import();
             $controller->data['update_previous'] = $item;
             $controller->process();
             return;
         }
     }
     $this->render('admin/import/confirm');
 }
Beispiel #5
0
    public function __fix_db_schema()
    {
        $uploads = wp_upload_dir();
        if (!is_dir($uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY) or !is_writable($uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY)) {
            die(sprintf(__('Uploads folder %s must be writable', 'wp_all_import_plugin'), $uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY));
        }
        if (!is_dir($uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY) or !is_writable($uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY)) {
            die(sprintf(__('Uploads folder %s must be writable', 'wp_all_import_plugin'), $uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY));
        }
        global $wpdb;
        // do not execute ALTER TABLE queries if sql user doesn't have ALTER privileges
        $grands = $wpdb->get_results("SELECT * FROM information_schema.user_privileges WHERE grantee LIKE \"'" . DB_USER . "'%\" AND PRIVILEGE_TYPE = 'ALTER' AND IS_GRANTABLE = 'YES';");
        $table = $table = $this->getTablePrefix() . 'files';
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        // For every field in the table
        foreach ($tablefields as $tablefield) {
            if ('contents' == $tablefield->Field) {
                $list = new PMXI_File_List();
                for ($i = 1; $list->getBy(NULL, 'id', $i, 1)->count(); $i++) {
                    foreach ($list->convertRecords() as $file) {
                        $file->save();
                        // resave file for file to be stored in uploads folder
                    }
                }
                if (!empty($grands)) {
                    $wpdb->query("ALTER TABLE {$table} DROP " . $tablefield->Field);
                }
                break;
            }
        }
        $table = $this->getTablePrefix() . 'imports';
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        $fields_to_alter = array('parent_import_id', 'iteration', 'deleted', 'executing', 'canceled', 'canceled_on', 'failed', 'failed_on', 'settings_update_on', 'last_activity');
        // Check if field exists
        foreach ($tablefields as $tablefield) {
            if (in_array($tablefield->Field, $fields_to_alter)) {
                $fields_to_alter = array_diff($fields_to_alter, array($tablefield->Field));
            }
        }
        if (!empty($fields_to_alter)) {
            if (empty($grands)) {
                return false;
            }
            foreach ($fields_to_alter as $field) {
                switch ($field) {
                    case 'parent_import_id':
                        $wpdb->query("ALTER TABLE {$table} ADD `parent_import_id` BIGINT(20) NOT NULL DEFAULT 0;");
                        break;
                    case 'iteration':
                        $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;");
                        break;
                    case 'deleted':
                        $wpdb->query("ALTER TABLE {$table} ADD `deleted` BIGINT(20) NOT NULL DEFAULT 0;");
                        break;
                    case 'executing':
                        $wpdb->query("ALTER TABLE {$table} ADD `executing` BOOL NOT NULL DEFAULT 0;");
                        break;
                    case 'canceled':
                        $wpdb->query("ALTER TABLE {$table} ADD `canceled` BOOL NOT NULL DEFAULT 0;");
                        break;
                    case 'canceled_on':
                        $wpdb->query("ALTER TABLE {$table} ADD `canceled_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
                        break;
                    case 'failed':
                        $wpdb->query("ALTER TABLE {$table} ADD `failed` BOOL NOT NULL DEFAULT 0;");
                        break;
                    case 'failed_on':
                        $wpdb->query("ALTER TABLE {$table} ADD `failed_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
                        break;
                    case 'settings_update_on':
                        $wpdb->query("ALTER TABLE {$table} ADD `settings_update_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
                        break;
                    case 'last_activity':
                        $wpdb->query("ALTER TABLE {$table} ADD `last_activity` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
                        break;
                    default:
                        # code...
                        break;
                }
            }
        }
        $table = $this->getTablePrefix() . 'posts';
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        $iteration = false;
        // Check if field exists
        foreach ($tablefields as $tablefield) {
            if ('iteration' == $tablefield->Field) {
                $iteration = true;
            }
        }
        if (!$iteration) {
            if (empty($grands)) {
                ?>
				<div class="error"><p>
					<?php 
                printf(__('<b>%s Plugin</b>: Current sql user %s doesn\'t have ALTER privileges', 'pmwi_plugin'), self::getInstance()->getName(), DB_USER);
                ?>
				</p></div>
				<?php 
                return false;
            }
            $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;");
        }
        if (!empty($wpdb->charset)) {
            $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
        }
        if (!empty($wpdb->collate)) {
            $charset_collate .= " COLLATE {$wpdb->collate}";
        }
        $table_prefix = $this->getTablePrefix();
        $wpdb->query("CREATE TABLE IF NOT EXISTS {$table_prefix}history (\n\t\t\tid BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n\t\t\timport_id BIGINT(20) UNSIGNED NOT NULL,\n\t\t\ttype ENUM('manual','processing','trigger','continue','') NOT NULL DEFAULT '',\t\n\t\t\ttime_run TEXT,\t\n\t\t\tdate DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\t\t\n\t\t\tsummary TEXT,\n\t\t\tPRIMARY KEY  (id)\n\t\t) {$charset_collate};");
        return true;
    }
Beispiel #6
0
    /**
     * 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) {
                    $history_file_path = wp_all_import_get_absolute_path($file['path']);
                    if (@file_exists($history_file_path) and $history_file_path != PMXI_Plugin::$session->filePath) {
                        if (in_array($import->type, array('upload'))) {
                            wp_all_import_remove_source($history_file_path, false);
                        } else {
                            wp_all_import_remove_source($history_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' => wp_all_import_get_relative_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 = wp_all_import_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::TEMP_DIRECTORY) . DIRECTORY_SEPARATOR . "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 = wp_all_import_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::LOGS_DIRECTORY, $history_log->id) . DIRECTORY_SEPARATOR . $history_log->id . '.html';
                if (PMXI_Plugin::$session->action != 'continue') {
                    if (file_exists($log_file)) {
                        wp_all_import_remove_source($log_file, false);
                    }
                    //@file_put_contents($log_file, sprintf(__('<p>Source path `%s`</p>', 'wp_all_import_plugin'), $import->path));
                }
            }
            $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 (PMXI_Plugin::is_ajax() and $ajax_processing and !check_ajax_referer('wp_all_import_secure', 'security', false)) {
            exit(__('Security check', 'wp_all_import_plugin'));
        }
        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(wp_all_import_strip_tags_content($m))) { PMXI_Plugin::$session->log .= "<p>".strip_tags(wp_all_import_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 = wp_all_import_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::LOGS_DIRECTORY, $history_log->id) . DIRECTORY_SEPARATOR . $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)) {
                            wp_all_import_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, 'filter' => true));
                    // loop through the file until all lines are read
                    while ($xml = $file->read() and empty($import->canceled)) {
                        if (!empty($xml)) {
                            if (!$import->options['chuncking']) {
                                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('imported' => $import->imported, '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)) {
            if ("ajax" != $import->options['import_processing'] and $log_storage) {
                $log_file = wp_all_import_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::LOGS_DIRECTORY, $history_log->id) . DIRECTORY_SEPARATOR . $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', 'wp_all_import_plugin'), "\n\r") : addcslashes(__('Complete', 'wp_all_import_plugin'), "\n\r");
            if ($ajax_processing) {
                ob_start();
            }
            do_action('pmxi_after_xml_import', $import->id);
            $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('imported' => $import->imported, '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']));
            }
        }
    }
Beispiel #7
0
    public function __fix_db_schema()
    {
        $uploads = wp_upload_dir();
        if (!is_dir($uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY) or !is_writable($uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY)) {
            die(sprintf(__('Uploads folder %s must be writable', 'wp_all_import_plugin'), $uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY));
        }
        if (!is_dir($uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY) or !is_writable($uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY)) {
            die(sprintf(__('Uploads folder %s must be writable', 'wp_all_import_plugin'), $uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY));
        }
        // create/update required database tables
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        require self::ROOT_DIR . '/schema.php';
        global $wpdb;
        if (function_exists('is_multisite') && is_multisite()) {
            // check if it is a network activation - if so, run the activation function for each blog id
            if (isset($_GET['networkwide']) && $_GET['networkwide'] == 1) {
                $old_blog = $wpdb->blogid;
                // Get all blog ids
                $blogids = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs}");
                foreach ($blogids as $blog_id) {
                    switch_to_blog($blog_id);
                    require self::ROOT_DIR . '/schema.php';
                    dbDelta($plugin_queries);
                    // sync data between plugin tables and wordpress (mostly for the case when plugin is reactivated)
                    $post = new PMXI_Post_Record();
                    $wpdb->query('DELETE FROM ' . $post->getTable() . ' WHERE post_id NOT IN (SELECT ID FROM ' . $wpdb->posts . ')');
                }
                switch_to_blog($old_blog);
                return;
            }
        }
        dbDelta($plugin_queries);
        // do not execute ALTER TABLE queries if sql user doesn't have ALTER privileges
        $grands = $wpdb->get_results("SELECT * FROM information_schema.user_privileges WHERE grantee LIKE \"'" . DB_USER . "'%\" AND PRIVILEGE_TYPE = 'ALTER' AND IS_GRANTABLE = 'YES';");
        $table = $table = $this->getTablePrefix() . 'files';
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        // For every field in the table
        foreach ($tablefields as $tablefield) {
            if ('contents' == $tablefield->Field) {
                $list = new PMXI_File_List();
                for ($i = 1; $list->getBy(NULL, 'id', $i, 1)->count(); $i++) {
                    foreach ($list->convertRecords() as $file) {
                        $file->save();
                        // resave file for file to be stored in uploads folder
                    }
                }
                if (!empty($grands)) {
                    $wpdb->query("ALTER TABLE {$table} DROP " . $tablefield->Field);
                }
                break;
            }
        }
        $table = $this->getTablePrefix() . 'imports';
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        $fields_to_alter = array('parent_import_id', 'iteration', 'deleted', 'executing', 'canceled', 'canceled_on', 'failed', 'failed_on', 'settings_update_on', 'last_activity');
        // Check if field exists
        foreach ($tablefields as $tablefield) {
            if (in_array($tablefield->Field, $fields_to_alter)) {
                $fields_to_alter = array_diff($fields_to_alter, array($tablefield->Field));
            }
        }
        if (!empty($fields_to_alter)) {
            if (empty($grands)) {
                return false;
            }
            foreach ($fields_to_alter as $field) {
                switch ($field) {
                    case 'parent_import_id':
                        $wpdb->query("ALTER TABLE {$table} ADD `parent_import_id` BIGINT(20) NOT NULL DEFAULT 0;");
                        break;
                    case 'iteration':
                        $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;");
                        break;
                    case 'deleted':
                        $wpdb->query("ALTER TABLE {$table} ADD `deleted` BIGINT(20) NOT NULL DEFAULT 0;");
                        break;
                    case 'executing':
                        $wpdb->query("ALTER TABLE {$table} ADD `executing` BOOL NOT NULL DEFAULT 0;");
                        break;
                    case 'canceled':
                        $wpdb->query("ALTER TABLE {$table} ADD `canceled` BOOL NOT NULL DEFAULT 0;");
                        break;
                    case 'canceled_on':
                        $wpdb->query("ALTER TABLE {$table} ADD `canceled_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
                        break;
                    case 'failed':
                        $wpdb->query("ALTER TABLE {$table} ADD `failed` BOOL NOT NULL DEFAULT 0;");
                        break;
                    case 'failed_on':
                        $wpdb->query("ALTER TABLE {$table} ADD `failed_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
                        break;
                    case 'settings_update_on':
                        $wpdb->query("ALTER TABLE {$table} ADD `settings_update_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
                        break;
                    case 'last_activity':
                        $wpdb->query("ALTER TABLE {$table} ADD `last_activity` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
                        break;
                    default:
                        # code...
                        break;
                }
            }
        }
        $table = $this->getTablePrefix() . 'posts';
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        $iteration = false;
        $specified = false;
        // Check if field exists
        foreach ($tablefields as $tablefield) {
            if ('iteration' == $tablefield->Field) {
                $iteration = true;
            }
            if ('specified' == $tablefield->Field) {
                $specified = true;
            }
        }
        if (!$iteration) {
            if (empty($grands)) {
                ?>
				<div class="error"><p>
					<?php 
                printf(__('<b>%s Plugin</b>: Current sql user %s doesn\'t have ALTER privileges', 'pmwi_plugin'), self::getInstance()->getName(), DB_USER);
                ?>
				</p></div>
				<?php 
                return false;
            }
            $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;");
        }
        if (!$specified and !empty($grands)) {
            $wpdb->query("ALTER TABLE {$table} ADD `specified` BOOL NOT NULL DEFAULT 0;");
        }
        if (!empty($wpdb->charset)) {
            $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
        }
        if (!empty($wpdb->collate)) {
            $charset_collate .= " COLLATE {$wpdb->collate}";
        }
        $table_prefix = $this->getTablePrefix();
        $wpdb->query("CREATE TABLE IF NOT EXISTS {$table_prefix}history (\n\t\t\tid BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n\t\t\timport_id BIGINT(20) UNSIGNED NOT NULL,\n\t\t\ttype ENUM('manual','processing','trigger','continue','') NOT NULL DEFAULT '',\t\n\t\t\ttime_run TEXT,\t\n\t\t\tdate DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\t\t\n\t\t\tsummary TEXT,\n\t\t\tPRIMARY KEY  (id)\n\t\t) {$charset_collate};");
        return true;
    }
Beispiel #8
0
 public function index()
 {
     $this->data['post'] = $post = $this->input->post(PMXI_Plugin::getInstance()->getOption());
     /*$addons = new PMXI_Admin_Addons();
     
     		$this->data['addons'] = $addons->get_premium_addons();*/
     $this->data['addons']['PMXI_Plugin'] = array('title' => __('WP All Import', 'wp_all_import_plugin'), 'active' => class_exists('PMXI_Plugin') and defined('PMXI_EDITION') and PMXI_EDITION == 'paid');
     $this->data['addons'] = array_reverse($this->data['addons']);
     if ($this->input->post('is_settings_submitted')) {
         // save settings form
         check_admin_referer('edit-settings', '_wpnonce_edit-settings');
         if (!preg_match('%^\\d+$%', $post['history_file_count'])) {
             $this->errors->add('form-validation', __('History File Count must be a non-negative integer', 'wp_all_import_plugin'));
         }
         if (!preg_match('%^\\d+$%', $post['history_file_age'])) {
             $this->errors->add('form-validation', __('History Age must be a non-negative integer', 'wp_all_import_plugin'));
         }
         if (empty($post['html_entities'])) {
             $post['html_entities'] = 0;
         }
         if (empty($post['utf8_decode'])) {
             $post['utf8_decode'] = 0;
         }
         if (!$this->errors->get_error_codes()) {
             // no validation errors detected
             PMXI_Plugin::getInstance()->updateOption($post);
             if (empty($_POST['pmxi_license_activate']) and empty($_POST['pmxi_license_deactivate'])) {
                 foreach ($this->data['addons'] as $class => $addon) {
                     $post['statuses'][$class] = $this->check_license($class);
                 }
                 PMXI_Plugin::getInstance()->updateOption($post);
             }
             isset($_POST['pmxi_license_activate']) and $this->activate_licenses();
             $files = new PMXI_File_List();
             $files->sweepHistory();
             // adjust file history to new settings specified
             wp_redirect(add_query_arg('pmxi_nt', urlencode(__('Settings saved', 'wp_all_import_plugin')), $this->baseUrl));
             die;
         }
     }
     /*else{			
     
     			foreach ($this->data['addons'] as $class => $addon) {
     				$post['statuses'][$class] = $this->check_license($class);
     			}								
     
     			PMXI_Plugin::getInstance()->updateOption($post);	
     		}*/
     if ($this->input->post('is_templates_submitted')) {
         // delete templates form
         check_admin_referer('delete-templates', '_wpnonce_delete-templates');
         if ($this->input->post('import_templates')) {
             if (!empty($_FILES)) {
                 $file_name = $_FILES['template_file']['name'];
                 $file_size = $_FILES['template_file']['size'];
                 $tmp_name = $_FILES['template_file']['tmp_name'];
                 if (isset($file_name)) {
                     $filename = stripslashes($file_name);
                     $extension = strtolower(pmxi_getExtension($filename));
                     if ($extension != "txt") {
                         $this->errors->add('form-validation', __('Unknown File extension. Only txt files are permitted', 'wp_all_import_plugin'));
                     } else {
                         $import_data = @file_get_contents($tmp_name);
                         if (!empty($import_data)) {
                             $templates_data = json_decode($import_data, true);
                             if (!empty($templates_data)) {
                                 $template = new PMXI_Template_Record();
                                 foreach ($templates_data as $template_data) {
                                     unset($template_data['id']);
                                     $template->clear()->set($template_data)->insert();
                                 }
                                 wp_redirect(add_query_arg('pmxi_nt', urlencode(sprintf(_n('%d template imported', '%d templates imported', count($templates_data), 'wp_all_import_plugin'), count($templates_data))), $this->baseUrl));
                                 die;
                             } else {
                                 $this->errors->add('form-validation', __('Wrong imported data format', 'wp_all_import_plugin'));
                             }
                         } else {
                             $this->errors->add('form-validation', __('File is empty or doesn\'t exests', 'wp_all_import_plugin'));
                         }
                     }
                 } else {
                     $this->errors->add('form-validation', __('Undefined entry!', 'wp_all_import_plugin'));
                 }
             } else {
                 $this->errors->add('form-validation', __('Please select file.', 'wp_all_import_plugin'));
             }
         } else {
             $templates_ids = $this->input->post('templates', array());
             if (empty($templates_ids)) {
                 $this->errors->add('form-validation', __('Templates must be selected', 'wp_all_import_plugin'));
             }
             if (!$this->errors->get_error_codes()) {
                 // no validation errors detected
                 if ($this->input->post('delete_templates')) {
                     $template = new PMXI_Template_Record();
                     foreach ($templates_ids as $template_id) {
                         $template->clear()->set('id', $template_id)->delete();
                     }
                     wp_redirect(add_query_arg('pmxi_nt', urlencode(sprintf(_n('%d template deleted', '%d templates deleted', count($templates_ids), 'wp_all_import_plugin'), count($templates_ids))), $this->baseUrl));
                     die;
                 }
                 if ($this->input->post('export_templates')) {
                     $export_data = array();
                     $template = new PMXI_Template_Record();
                     foreach ($templates_ids as $template_id) {
                         $export_data[] = $template->clear()->getBy('id', $template_id)->toArray(TRUE);
                     }
                     $uploads = wp_upload_dir();
                     $targetDir = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::TEMP_DIRECTORY;
                     $export_file_name = "templates_" . uniqid() . ".txt";
                     file_put_contents($targetDir . DIRECTORY_SEPARATOR . $export_file_name, json_encode($export_data));
                     PMXI_download::csv($targetDir . DIRECTORY_SEPARATOR . $export_file_name);
                 }
             }
         }
     }
     $this->render();
 }
Beispiel #9
0
    /**
     * Import processing step (status console)
     */
    public function process($save_history = true)
    {
        $wp_uploads = wp_upload_dir();
        $import = $this->data['update_previous'];
        $logger = create_function('$m', 'echo "<div class=\\"progress-msg\\">$m</div>\\n"; if ( "" != strip_tags(pmxi_strip_tags_content($m)) and !empty(PMXI_Plugin::$session[\'pmxi_import\'][\'log\'])) { PMXI_Plugin::$session[\'pmxi_import\'][\'log\'] .= "<p>".strip_tags(pmxi_strip_tags_content($m))."</p>"; flush(); }');
        if (!PMXI_Plugin::is_ajax()) {
            $import->set((empty(PMXI_Plugin::$session->data['pmxi_import']['source']) ? array() : PMXI_Plugin::$session->data['pmxi_import']['source']) + array('xpath' => PMXI_Plugin::$session->data['pmxi_import']['xpath'], 'template' => PMXI_Plugin::$session->data['pmxi_import']['template'], 'options' => PMXI_Plugin::$session->data['pmxi_import']['options'], 'count' => PMXI_Plugin::$session->data['pmxi_import']['count'], 'friendly_name' => PMXI_Plugin::$session->data['pmxi_import']['options']['friendly_name'], 'feed_type' => PMXI_Plugin::$session->data['pmxi_import']['feed_type'], 'parent_import_id' => $this->data['update_previous']->isEmpty() ? PMXI_Plugin::$session->data['pmxi_import']['parent_import_id'] : $this->data['update_previous']->parent_import_id, 'queue_chunk_number' => 0, 'triggered' => 0, 'processing' => 0, 'iteration' => !empty($import->iteration) ? $import->iteration : 0))->save();
            if (PMXI_Plugin::$session->data['pmxi_import']['action'] != 'continue') {
                // store import info in database
                $import->set(array('imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0))->update();
            }
            foreach (get_taxonomies() as $tax) {
                delete_transient("pmxi_{$tax}_terms");
            }
            do_action('pmxi_before_xml_import', $import->id);
            PMXI_Plugin::$session['pmxi_import']['update_previous'] = $import->id;
            // unlick 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->data['pmxi_import']['filePath']) {
                        @unlink($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->data['pmxi_import']['filePath'], 'contents' => $this->get_xml(), '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->data['pmxi_import']['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->data['pmxi_import']['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 = $wp_uploads['path'] . "/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['pmxi_import']['local_paths'] = $chunk_files;
                }
            }
            pmxi_session_commit();
            $this->render();
            wp_ob_end_flush_all();
            flush();
            @set_time_limit(0);
            if ("ajax" == $import->options['import_processing']) {
                die;
            }
        } elseif (empty($import->id)) {
            $import = new PMXI_Import_Record();
            $import->getById(PMXI_Plugin::$session->data['pmxi_import']['update_previous']);
        }
        $ajax_processing = "ajax" == $import->options['import_processing'] ? true : false;
        PMXI_Plugin::$session['pmxi_import']['start_time'] = empty(PMXI_Plugin::$session->data['pmxi_import']['start_time']) ? time() : PMXI_Plugin::$session->data['pmxi_import']['start_time'];
        wp_cache_flush();
        if (PMXI_Plugin::is_ajax() or !$ajax_processing) {
            if ("ajax" == $import->options['import_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];
            $records_per_request = (!$ajax_processing and $import->options['records_per_request'] < 50) ? 50 : $import->options['records_per_request'];
            if (!empty(PMXI_Plugin::$session->data['pmxi_import']['local_paths'])) {
                $feed = "<?xml version=\"1.0\" encoding=\"" . $import->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>";
                foreach (PMXI_Plugin::$session->data['pmxi_import']['local_paths'] as $key => $path) {
                    $import_done = $import->imported + $import->skipped + PMXI_Plugin::$session->data['pmxi_import']['errors'] == $records_to_import ? true : false;
                    if ($import_done) {
                        if (strpos($path, "pmxi_chunk_") !== false and @file_exists($path)) {
                            @unlink($path);
                        }
                        PMXI_Plugin::$session['pmxi_import']['local_paths'] = array();
                        pmxi_session_commit();
                        break;
                    }
                    $file = new PMXI_Chunk($path, array('element' => $import->root_element, 'encoding' => $import->options['encoding'], 'pointer' => PMXI_Plugin::$session->data['pmxi_import']['pointer']));
                    // 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);
                            $pointer++;
                            if ($this->data['elements'] = $elements = @$xpath->query($import->xpath) and $elements->length) {
                                // continue action
                                if ($import->imported + $import->skipped >= PMXI_Plugin::$session->data['pmxi_import']['chunk_number'] + $elements->length - 1) {
                                    PMXI_Plugin::$session['pmxi_import']['chunk_number'] = PMXI_Plugin::$session->data['pmxi_import']['chunk_number'] + $elements->length;
                                    pmxi_session_commit();
                                    continue;
                                }
                                if (!$loop and $ajax_processing) {
                                    ob_start();
                                }
                                $feed .= $xml;
                                $loop += $elements->length;
                                $processed_records = $import->imported + $import->skipped + PMXI_Plugin::$session->data['pmxi_import']['errors'];
                                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->data['pmxi_import']['chunk_number'], false, '/pmxi_records');
                                    unset($dom, $xpath);
                                    if (!$ajax_processing) {
                                        $feed = "<?xml version=\"1.0\" encoding=\"" . $import->options['encoding'] . "\"?>" . "\n" . "<pmxi_records>";
                                        $loop = 0;
                                    } else {
                                        unset($file);
                                        PMXI_Plugin::$session['pmxi_import']['pointer'] = PMXI_Plugin::$session->data['pmxi_import']['pointer'] + $pointer;
                                        pmxi_session_commit();
                                        wp_send_json(array('created' => $import->created, 'updated' => $import->updated, 'percentage' => ceil($processed_records / $import->count * 100), 'warnings' => PMXI_Plugin::$session->data['pmxi_import']['warnings'], 'errors' => PMXI_Plugin::$session->data['pmxi_import']['errors'], 'log' => ob_get_clean(), 'done' => false));
                                    }
                                }
                            }
                        }
                    }
                    // 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['pmxi_import']['pointer'] = 1;
                        $pointer = 0;
                        array_shift(PMXI_Plugin::$session->data['pmxi_import']['local_paths']);
                        PMXI_Plugin::$session['pmxi_import']['local_paths'] = PMXI_Plugin::$session->data['pmxi_import']['local_paths'];
                        pmxi_session_commit();
                    } else {
                        break;
                    }
                }
            }
        }
        if (PMXI_Plugin::is_ajax() and empty(PMXI_Plugin::$session->data['pmxi_import']['local_paths']) or !$ajax_processing) {
            // Save import process log
            $log_file = $wp_uploads['basedir'] . '/wpallimport_logs/' . $import->id . '.html';
            if (file_exists($log_file)) {
                unlink($log_file);
            }
            @file_put_contents($log_file, PMXI_Plugin::$session->data['pmxi_import']['log']);
            if (!empty(PMXI_Plugin::$session->data['pmxi_import'])) {
                do_action('pmxi_after_xml_import', $import->id);
            }
            wp_cache_flush();
            foreach (get_taxonomies() as $tax) {
                delete_option("{$tax}_children");
                _get_term_hierarchy($tax);
            }
            $import->set('registered_on', date('Y-m-d H:i:s'))->update();
            // clear import session
            pmxi_session_unset();
            // clear session data (prevent from reimporting the same data on page refresh)
            // [indicate in header process is complete]
            $msg = 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\$('#import_finished').show();
\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->data['pmxi_import']['warnings'], 'errors' => PMXI_Plugin::$session->data['pmxi_import']['errors'], 'log' => ob_get_clean(), 'done' => true));
            }
        }
    }
Beispiel #10
0
 /**
  * Method perfoms transition from version when file uploads has been stored in dabase to the solution when it stored on disk
  * NOTE: the function can be removed when plugin version progress and it's sure matter nobody has ver 1.03
  */
 public function __ver_1_04_transition_fix()
 {
     $uploads = wp_upload_dir();
     if (!is_dir($uploads['basedir'] . '/wpallimport_history') or !is_writable($uploads['basedir'] . '/wpallimport_history')) {
         die(sprintf(__('Uploads folder %s must be writable', 'pmxi_plugin'), $uploads['basedir'] . '/wpallimport_history'));
     }
     if (!is_dir($uploads['basedir'] . '/wpallimport_logs') or !is_writable($uploads['basedir'] . '/wpallimport_logs')) {
         die(sprintf(__('Uploads folder %s must be writable', 'pmxi_plugin'), $uploads['basedir'] . '/wpallimport_logs'));
     }
     $table = $table = $this->getTablePrefix() . 'files';
     global $wpdb;
     $tablefields = $wpdb->get_results("DESCRIBE {$table};");
     // For every field in the table
     foreach ($tablefields as $tablefield) {
         if ('contents' == $tablefield->Field) {
             $list = new PMXI_File_List();
             for ($i = 1; $list->getBy(NULL, 'id', $i, 1)->count(); $i++) {
                 foreach ($list->convertRecords() as $file) {
                     $file->save();
                     // resave file for file to be stored in uploads folder
                 }
             }
             $wpdb->query("ALTER TABLE {$table} DROP " . $tablefield->Field);
             break;
         }
     }
 }
 public function __fix_db_schema()
 {
     $uploads = wp_upload_dir();
     if (!is_dir($uploads['basedir'] . '/wpallimport/logs') or !is_writable($uploads['basedir'] . '/wpallimport/logs')) {
         die(sprintf(__('Uploads folder %s must be writable', 'pmxi_plugin'), $uploads['basedir'] . '/wpallimport/logs'));
     }
     if (!is_dir($uploads['basedir'] . '/wpallimport') or !is_writable($uploads['basedir'] . '/wpallimport')) {
         die(sprintf(__('Uploads folder %s must be writable', 'pmxi_plugin'), $uploads['basedir'] . '/wpallimport'));
     }
     $table = $table = $this->getTablePrefix() . 'files';
     global $wpdb;
     $tablefields = $wpdb->get_results("DESCRIBE {$table};");
     // For every field in the table
     foreach ($tablefields as $tablefield) {
         if ('contents' == $tablefield->Field) {
             $list = new PMXI_File_List();
             for ($i = 1; $list->getBy(NULL, 'id', $i, 1)->count(); $i++) {
                 foreach ($list->convertRecords() as $file) {
                     $file->save();
                     // resave file for file to be stored in uploads folder
                 }
             }
             $wpdb->query("ALTER TABLE {$table} DROP " . $tablefield->Field);
             break;
         }
     }
     $table = $this->getTablePrefix() . 'imports';
     $tablefields = $wpdb->get_results("DESCRIBE {$table};");
     $parent_import_id = false;
     $iteration = false;
     $deleted = false;
     $executing = false;
     $canceled = false;
     $canceled_on = false;
     $failed = false;
     $failed_on = false;
     $settings_update_on = false;
     $last_activity = false;
     // Check if field exists
     foreach ($tablefields as $tablefield) {
         if ('parent_import_id' == $tablefield->Field) {
             $parent_import_id = true;
         }
         if ('iteration' == $tablefield->Field) {
             $iteration = true;
         }
         if ('deleted' == $tablefield->Field) {
             $deleted = true;
         }
         if ('executing' == $tablefield->Field) {
             $executing = true;
         }
         if ('canceled' == $tablefield->Field) {
             $canceled = true;
         }
         if ('canceled_on' == $tablefield->Field) {
             $canceled_on = true;
         }
         if ('failed' == $tablefield->Field) {
             $failed = true;
         }
         if ('failed_on' == $tablefield->Field) {
             $failed_on = true;
         }
         if ('settings_update_on' == $tablefield->Field) {
             $settings_update_on = true;
         }
         if ('last_activity' == $tablefield->Field) {
             $last_activity = true;
         }
     }
     if (!$parent_import_id) {
         $wpdb->query("ALTER TABLE {$table} ADD `parent_import_id` BIGINT(20) NOT NULL DEFAULT 0;");
     }
     if (!$iteration) {
         $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;");
     }
     if (!$deleted) {
         $wpdb->query("ALTER TABLE {$table} ADD `deleted` BIGINT(20) NOT NULL DEFAULT 0;");
     }
     if (!$executing) {
         $wpdb->query("ALTER TABLE {$table} ADD `executing` BOOL NOT NULL DEFAULT 0;");
     }
     if (!$canceled) {
         $wpdb->query("ALTER TABLE {$table} ADD `canceled` BOOL NOT NULL DEFAULT 0;");
     }
     if (!$canceled_on) {
         $wpdb->query("ALTER TABLE {$table} ADD `canceled_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
     }
     if (!$failed) {
         $wpdb->query("ALTER TABLE {$table} ADD `failed` BOOL NOT NULL DEFAULT 0;");
     }
     if (!$failed_on) {
         $wpdb->query("ALTER TABLE {$table} ADD `failed_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
     }
     if (!$settings_update_on) {
         $wpdb->query("ALTER TABLE {$table} ADD `settings_update_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
     }
     if (!$last_activity) {
         $wpdb->query("ALTER TABLE {$table} ADD `last_activity` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';");
     }
     $table = $this->getTablePrefix() . 'posts';
     $tablefields = $wpdb->get_results("DESCRIBE {$table};");
     $iteration = false;
     // Check if field exists
     foreach ($tablefields as $tablefield) {
         if ('iteration' == $tablefield->Field) {
             $iteration = true;
         }
     }
     if (!$iteration) {
         $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;");
     }
     if (!empty($wpdb->charset)) {
         $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
     }
     if (!empty($wpdb->collate)) {
         $charset_collate .= " COLLATE {$wpdb->collate}";
     }
     $table_prefix = $this->getTablePrefix();
     $wpdb->query("CREATE TABLE IF NOT EXISTS {$table_prefix}history (\n\t\t\t\tid BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n\t\t\t\timport_id BIGINT(20) UNSIGNED NOT NULL,\n\t\t\t\ttype ENUM('manual','processing','trigger','continue','') NOT NULL DEFAULT '',\t\n\t\t\t\ttime_run TEXT,\t\n\t\t\t\tdate DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\t\t\n\t\t\t\tsummary TEXT,\n\t\t\t\tPRIMARY KEY  (id)\n\t\t\t) {$charset_collate};");
 }