Beispiel #1
0
/**
 * Imports an xml file into iblock. File may be an .tar.gz archive.
 *
 * @param string $file_name Name of the file to import
 * @param string $iblock_type IBlock type ID to import iblock to
 * @param string|array $site_id ID of the site or array of IDs to bind iblock to
 * @param string $section_action What to do with sections missed in the file. D - delete or A - deactivate.
 * @param string $element_action What to do with elements missed in the file. D - delete or A - deactivate.
 * @param bool $use_crc Whenever to use CRC check for optimizi=ation or force an update
 * @param bool $preview If true when use iblock settings to generate preview pictures from detail.
 * @param bool $sync If true uses alternative set of tables in order not to interfere with other import processes
 * @param bool $return_last_error If true will return string with error description in case of failure
 * @param bool $return_iblock_id If true will return iblock identifier (int) in case of success
 * @return bool|int|string
 */
function ImportXMLFile($file_name, $iblock_type = "-", $site_id = '', $section_action = "D", $element_action = "D", $use_crc = false, $preview = false, $sync = false, $return_last_error = false, $return_iblock_id = false)
{
    /** @global CMain $APPLICATION */
    global $APPLICATION;
    $ABS_FILE_NAME = false;
    if (strlen($file_name) > 0) {
        if (file_exists($file_name) && is_file($file_name) && (substr($file_name, -4) === ".xml" || substr($file_name, -7) === ".tar.gz")) {
            $ABS_FILE_NAME = $file_name;
        } else {
            $filename = trim(str_replace("\\", "/", trim($file_name)), "/");
            $FILE_NAME = rel2abs($_SERVER["DOCUMENT_ROOT"], "/" . $filename);
            if (strlen($FILE_NAME) > 1 && $FILE_NAME === "/" . $filename && $APPLICATION->GetFileAccessPermission($FILE_NAME) >= "W") {
                $ABS_FILE_NAME = $_SERVER["DOCUMENT_ROOT"] . $FILE_NAME;
            }
        }
    }
    if (!$ABS_FILE_NAME) {
        return GetMessage("IBLOCK_XML2_FILE_ERROR");
    }
    $WORK_DIR_NAME = substr($ABS_FILE_NAME, 0, strrpos($ABS_FILE_NAME, "/") + 1);
    if (substr($ABS_FILE_NAME, -7) == ".tar.gz") {
        include_once $_SERVER["DOCUMENT_ROOT"] . BX_ROOT . "/modules/main/classes/general/tar_gz.php";
        $obArchiver = new CArchiver($ABS_FILE_NAME);
        if (!$obArchiver->ExtractFiles($WORK_DIR_NAME)) {
            $strError = "";
            if (is_object($APPLICATION)) {
                $arErrors = $obArchiver->GetErrors();
                if (count($arErrors)) {
                    foreach ($arErrors as $error) {
                        $strError .= $error[1] . "<br>";
                    }
                }
            }
            if ($strError != "") {
                return $strError;
            } else {
                return GetMessage("IBLOCK_XML2_FILE_ERROR");
            }
        }
        $IMP_FILE_NAME = substr($ABS_FILE_NAME, 0, -7) . ".xml";
    } else {
        $IMP_FILE_NAME = $ABS_FILE_NAME;
    }
    $fp = fopen($IMP_FILE_NAME, "rb");
    if (!$fp) {
        return GetMessage("IBLOCK_XML2_FILE_ERROR");
    }
    if ($sync) {
        $table_name = "b_xml_tree_sync";
    } else {
        $table_name = "b_xml_tree";
    }
    $NS = array("STEP" => 0);
    $obCatalog = new CIBlockCMLImport();
    $obCatalog->Init($NS, $WORK_DIR_NAME, $use_crc, $preview, false, false, false, $table_name);
    if ($sync) {
        if (!$obCatalog->StartSession(bitrix_sessid())) {
            return GetMessage("IBLOCK_XML2_TABLE_CREATE_ERROR");
        }
        $obCatalog->ReadXMLToDatabase($fp, $NS, 0, 1024);
        $xml_root = $obCatalog->GetSessionRoot();
        $bUpdateIBlock = false;
    } else {
        $obCatalog->DropTemporaryTables();
        if (!$obCatalog->CreateTemporaryTables()) {
            return GetMessage("IBLOCK_XML2_TABLE_CREATE_ERROR");
        }
        $obCatalog->ReadXMLToDatabase($fp, $NS, 0, 1024);
        if (!$obCatalog->IndexTemporaryTables()) {
            return GetMessage("IBLOCK_XML2_INDEX_ERROR");
        }
        $xml_root = 1;
        $bUpdateIBlock = true;
    }
    fclose($fp);
    $result = $obCatalog->ImportMetaData($xml_root, $iblock_type, $site_id, $bUpdateIBlock);
    if ($result !== true) {
        return GetMessage("IBLOCK_XML2_METADATA_ERROR") . implode("\n", $result);
    }
    $obCatalog->ImportSections();
    $obCatalog->DeactivateSections($section_action);
    $obCatalog->SectionsResort();
    $obCatalog = new CIBlockCMLImport();
    $obCatalog->Init($NS, $WORK_DIR_NAME, $use_crc, $preview, false, false, false, $table_name);
    if ($sync) {
        if (!$obCatalog->StartSession(bitrix_sessid())) {
            return GetMessage("IBLOCK_XML2_TABLE_CREATE_ERROR");
        }
    }
    $SECTION_MAP = false;
    $PRICES_MAP = false;
    $obCatalog->ReadCatalogData($SECTION_MAP, $PRICES_MAP);
    $obCatalog->ImportElements(time(), 0);
    $obCatalog->ImportProductSets();
    $obCatalog->DeactivateElement($element_action, time(), 0);
    if ($sync) {
        $obCatalog->EndSession();
    }
    if ($return_last_error) {
        if (strlen($obCatalog->LAST_ERROR)) {
            return $obCatalog->LAST_ERROR;
        }
    }
    if ($return_iblock_id) {
        return intval($NS["IBLOCK_ID"]);
    } else {
        return true;
    }
}
Beispiel #2
0
 } elseif ($NS["STEP"] == 4) {
     $obCatalog = new CIBlockCMLImport();
     $obCatalog->InitEx($NS, array("files_dir" => $WORK_DIR_NAME, "use_crc" => $arParams["USE_CRC"], "preview" => $preview, "detail" => $detail, "use_offers" => $arParams["USE_OFFERS"], "force_offers" => $arParams["FORCE_OFFERS"], "use_iblock_type_id" => $arParams["USE_IBLOCK_TYPE_ID"], "translit_on_add" => $arParams["TRANSLIT_ON_ADD"], "translit_on_update" => $arParams["TRANSLIT_ON_UPDATE"], "translit_params" => $arTranslitParams, "skip_root_section" => $arParams["SKIP_ROOT_SECTION"], "disable_change_price_name" => $arParams["DISABLE_CHANGE_PRICE_NAME"]));
     $result = $obCatalog->ImportMetaData(array(1, 2), $arParams["IBLOCK_TYPE"], $arParams["SITE_LIST"]);
     if ($result === true) {
         $strMessage = GetMessage("CC_BSC1_METADATA_IMPORTED");
         $NS["STEP"] = 5;
     } elseif (is_array($result)) {
         $strError = GetMessage("CC_BSC1_METADATA_ERROR") . implode("\n", $result);
     } else {
         $strError = GetMessage("CC_BSC1_METADATA_ERROR") . $result;
     }
 } elseif ($NS["STEP"] == 5) {
     $obCatalog = new CIBlockCMLImport();
     $obCatalog->InitEx($NS, array("files_dir" => $WORK_DIR_NAME, "use_crc" => $arParams["USE_CRC"], "preview" => $preview, "detail" => $detail, "use_offers" => $arParams["USE_OFFERS"], "force_offers" => $arParams["FORCE_OFFERS"], "use_iblock_type_id" => $arParams["USE_IBLOCK_TYPE_ID"], "translit_on_add" => $arParams["TRANSLIT_ON_ADD"], "translit_on_update" => $arParams["TRANSLIT_ON_UPDATE"], "translit_params" => $arTranslitParams, "skip_root_section" => $arParams["SKIP_ROOT_SECTION"], "disable_change_price_name" => $arParams["DISABLE_CHANGE_PRICE_NAME"]));
     $result = $obCatalog->ImportSections();
     if ($result === true) {
         $strMessage = GetMessage("CC_BSC1_SECTIONS_IMPORTED");
         $NS["STEP"] = 6;
     } else {
         $strError = GetMessage("CC_BSC1_METADATA_ERROR") . $result;
     }
 } elseif ($NS["STEP"] == 6) {
     $obCatalog = new CIBlockCMLImport();
     $obCatalog->InitEx($NS, array("files_dir" => $WORK_DIR_NAME, "use_crc" => $arParams["USE_CRC"], "preview" => $preview, "detail" => $detail, "use_offers" => $arParams["USE_OFFERS"], "force_offers" => $arParams["FORCE_OFFERS"], "use_iblock_type_id" => $arParams["USE_IBLOCK_TYPE_ID"], "translit_on_add" => $arParams["TRANSLIT_ON_ADD"], "translit_on_update" => $arParams["TRANSLIT_ON_UPDATE"], "translit_params" => $arTranslitParams, "skip_root_section" => $arParams["SKIP_ROOT_SECTION"], "disable_change_price_name" => $arParams["DISABLE_CHANGE_PRICE_NAME"]));
     $obCatalog->DeactivateSections($arParams["SECTION_ACTION"]);
     $obCatalog->SectionsResort();
     $strMessage = GetMessage("CC_BSC1_SECTION_DEA_DONE");
     $NS["STEP"] = 7;
 } elseif ($NS["STEP"] == 7) {
     if ($NS["DONE"]["ALL"] <= 0 && $NS["XML_ELEMENTS_PARENT"]) {