Jump to content

Please help importing pages directly into DB, or help with API


picarica
 Share

Recommended Posts

so hello i have a database from where i am exporting and processing data and i want them to import into processwire, i wanted to use processire's API but icant seem to find out how to also edit page created parameter, or adding info directly into fields, into body etc, i am trying to read up on this https://processwire.com/api/ref/pages/add/  but there isnt any mention how to add data directly into custom fields in template imported

so i tired something like this 

    // start  tranzakciu
    $targetConn->begin_transaction();
    // testuje zatial iba tento jeden artikel
    $articleId = 177;

    // querujem vsetky data z original DB a table
    $stmt = $sourceConn->prepare("
        SELECT `id`, `id_category`, `id_gallery`, `author`, `title`, `perex_desc`,
               `perex`, `content`, `click_count`, `date_publish`, `date_create`, `date_update`
        FROM `article`
        WHERE `id` = ?
    ");
    
    // execute podla arrticleID
    $stmt->bind_param('i', $articleId);
    $stmt->execute();
    $sourceData = $stmt->get_result();

    if ($sourceData->num_rows === 0) {
        throw new Exception("No article found with ID: $articleId");
    }

    // vysledok SELECT query
    $row = $sourceData->fetch_assoc();


    //$pages->add('clanok',1017,$row['title']);
    
    $pages->new([
        'template' => 'clanok',
        'parent' => '/clanok/',
        'title' => $row['title'],
        'created' => $row['date_create']
      ]);

i added this import.php file as  template and then i just visited the site so it ran, is there better way to run .php files with processwire api?  anyways when i visit imported page i get The process returned no content. error

 

i wanted to import it like this but i was getting error with some encoding


 

<?php
// obecne noviny
$sourceHost = 'localhost';
$sourceUser = 'REDACTED';
$sourcePass = 'REDACTED';
$sourceDB   = 'REDACTED';

// PW databaza
$targetHost = 'localhost';
$targetUser = 'REDACTED';
$targetPass = 'REDACTED';
$targetDB   = 'REDACTED';

try {
    // robim pripojenie
    $sourceConn = new mysqli($sourceHost, $sourceUser, $sourcePass, $sourceDB);
    $targetConn = new mysqli($targetHost, $targetUser, $targetPass, $targetDB);

    // check maly
    if ($sourceConn->connect_error || $targetConn->connect_error) {
        throw new Exception("Database connection failed: " . $conn->connect_error);
    }

    // skusam tu explicitne nastavit kodeky ale nejde to ajtak
    //$sourceConn->set_charset("utf8mb3");
    //$targetConn->set_charset("utf8mb4");
    

    // start  tranzakciu
    $targetConn->begin_transaction();
    // testuje zatial iba tento jeden artikel
    $articleId = 177;

    // querujem vsetky data z original DB a table
    $stmt = $sourceConn->prepare("
        SELECT `id`, `id_category`, `id_gallery`, `author`, `title`, `perex_desc`,
               `perex`, `content`, `click_count`, `date_publish`, `date_create`, `date_update`
        FROM `article`
        WHERE `id` = ?
    ");
    
    // execute podla arrticleID
    $stmt->bind_param('i', $articleId);
    $stmt->execute();
    $sourceData = $stmt->get_result();

    if ($sourceData->num_rows === 0) {
        throw new Exception("No article found with ID: $articleId");
    }

    // vysledok SELECT query
    $row = $sourceData->fetch_assoc();


    // ziskavam najvysie posledne pageID v processwire a pridam +1, ked bude sa robit viac clankov iterovat +1 pri castom repeate
    $result = $targetConn->query("SELECT MAX(id) AS max_id FROM pages");
    $rowMax = $result->fetch_assoc();
    $newPageId = $rowMax['max_id'] + 1;
    // niake info z original DB aby som zachoval spravnu strukturu
    $parentId= 1017;
    $templateId = 29;
    $status = 1;
    $user = 41;
    $sort = 0;
    //$title = mb_convert_encoding($row['title'], 'UTF-8', 'UTF-8mb3');
    //$title = $row['title'];
    // skusal som tu konvertoval ale nejde to ajtak
    //$title = mb_convert_encoding($row['title'], 'UTF-8', 'UTF-8');

    
    // Insert do PW databazy
    $pageStmt = $targetConn->prepare("
    INSERT INTO pages (id,parent_id,templates_id,name,status,modified,modified_users_id,created,created_users_id,published,sort )
    VALUES (?,?,?,?,?,?,?,?,?,?,?)
    ");

    //bindujem uz import, tu zapisujem zdrojove data odkial to ma brat info
    $pageStmt->bind_param('iiisisisisi',
        $newPageId,
        $parentId,
        $templateId,
        $row['title'],
        $status,
        $row['date_update'],
        $user,
        $row['date_create'],
        $user,
        $row['date_publish'],
        $sort
    );

    if ($targetConn->error) {
        echo "Target DB Error: " . $targetConn->error;
    }

    if (!$pageStmt->execute()) {
        throw new Exception("Page insert failed: " . $pageStmt->error);
    } else {
        echo "Insert successful.";
    } 
  
  
} catch (Exception $e) {
    $targetConn->rollback();
    echo "Error: " . $e->getMessage();
} finally {
    // Close connections 
    $sourceConn->close();
    $targetConn->close();
    if (isset($pageStmt)) $pageStmt->close();
    if (isset($updateDateStmt)) $updateDateStmt->close();
    if (isset($categoryStmt)) $categoryStmt->close();
}
?>

i am running this on one table row, but later i want to modify for this to run for like 6000 rows, but i am unable to get it working only on one also i am getting this error with above import.php, and i am launching it from terminal with $ php import.php

Error: Page insert failed: Incorrect string value: '\xC5\xA0araf...' for column pw_obecnenoviny.pages.name at row 1

i am not sure how can i achieve my goal, is it possible with processwire API?  am i missing something? thank you for your help

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...