Jump to content
Peter Falkenberg Brown

passing data between PW installations via scripts

Recommended Posts

Dear PW Gurus,

I searched and didn't find this answer -- hope I didn't miss something.

I have 3 production installations of PW on my server --- 3 different domains.

I'm developing a PHP script that will run from the shell prompt, and pull a certain number of pages
from domain A, and then insert those pages into domain B and C. (The field template structure
is identical between the 3.)

My script starts by connecting to the PW installation for domain A by using this include:

include("/home/USER_DIR/public_html/index.php");

which of course connects the script to that particular database. (All 3 domains use
a separate DB and separate installation of PW.)

Install A uses PW v3.0.61 running under CentOS.

I think I'm missing some wonderfulness in PW, but I'm stumped over how to
take the data from A and then do an include statement to the B install (to insert the
pages into B) and then do the same thing with C.

I would think that the second statement:

include("/home/USER_DIR_B/public_html/index.php");

would conflict with all of the variables, etc that are pulled in with the first include,
and then so on, with "C."

Is this a perfect examples of why someone should use name spaces?

I admit that I've never used them, so I'm at the bottom rung of knowledge about the syntax, etc,
to do that.

The script is truly uncomplicated, otherwise, ie.

- read 3 or 4 pages worth of fields into variables

- insert them into B and C's installations.

- done.

Any thoughts?

I'm running this script as root, (which I've done with the "A" part,
so that the permission issue would not be a problem.

Thanks in advance for any help!

Peter

 

Share this post


Link to post
Share on other sites

Dear BitPoet,

Thanks very much for this! I could just feel in my bones that I had forgotten something wonderful about PW.

I think I saw that blog post in my email when it came out, but then the info didn't stick in my brain.

I'm able to read the data from "DOMAIN_1" but when I try to save it into DOMAIN_2, the page object
still belongs to DOMAIN_1 and gets saved into that domain. I have some generic code below.

# FileCompiler=0

require('/home/DOMAIN_1/public_html/wire/core/ProcessWire.php');

$DOMAIN_1_site      = new \ProcessWire\ProcessWire('/home/DOMAIN_1/public_html/', 'http://DOMAIN_1.com/');

$DOMAIN_1_selectors = "SELECTORS HERE";

$DOMAIN_1_results   = $DOMAIN_1_site->pages->find("$DOMAIN_1_selectors");

foreach ( $DOMAIN_1_results as $DOMAIN_1_result )
      {
      $DOMAIN_1_url      = $DOMAIN_1_result->url;
      $DOMAIN_1_name     = $DOMAIN_1_result->name;

      . . . more fields here . . .

      #........................................................................
      ### CODE HERE TO INSERT DOMAIN_1 PAGE INTO DOMAIN_2

      $DOMAIN_2_site     = new \ProcessWire\ProcessWire('/home/DOMAIN_2/public_html/', 'http://DOMAIN_2.com/');

      $DOMAIN_2_new_page = new \ProcessWire\Page();

      # THIS BREAKS: $DOMAIN_2_new_page is still attached to DOMAIN_1
      # and gets saved into the domain 1 database.

      . . . more code here . . .
      }

Any help is much appreciated. Seems like I'm not properly initializing the vars for DOMAIN_2.

Thanks!

Peter

Share this post


Link to post
Share on other sites

Does this help?
In many core modules @ryan uses this method to build multi-instance aware modules. Give your object to wire() function, it injects a specific instance to it

$d1Page = $DOMAIN_1_site->wire(new Page());
$d2Page = $DOMAIN_2_site->wire(new Page());
/**
 * Get an API variable, create an API variable, or inject dependencies.
 *
 * This method provides the following:
 *
 * // ...
 *
 * // Inject dependencies during construct
 * $newPage = $this->wire(new Page());
 * ~~~~~
 *
 * @param string|object $name Name of API variable to retrieve, set, or omit to retrieve the master ProcessWire object.
 * @param null|mixed $value Value to set if using this as a setter, otherwise omit.
 * @param bool $lock When using as a setter, specify true if you want to lock the value from future changes (default=false).
 * @return ProcessWire|Wire|Session|Page|Pages|Modules|User|Users|Roles|Permissions|Templates|Fields|Fieldtypes|Sanitizer|Config|Notices|WireDatabasePDO|WireHooks|WireDateTime|WireFileTools|WireMailTools|WireInput|string|mixed
 * @throws WireException
 *
 *
 */
public function wire($name = '', $value = null, $lock = false) {

    if(is_null($this->_wire)) {
        // this object has not yet been wired! use last known current instance as fallback
        // note this condition is unsafe in multi-instance mode
        $wire = ProcessWire::getCurrentInstance();
        if(!$wire) return null;

        // For live hunting objects that are using the fallback, uncomment the following:
        // echo "<hr /><p>Non-wired object: '$name' in " . get_class($this) . ($value ? " (value=$value)" : "") . "</p>";
        // echo "<pre>" . print_r(debug_backtrace(), true) . "</pre>";
    } else {
        // this instance is wired
        $wire = $this->_wire;
    }

    if(is_object($name)) {
        // make an object wired (inject ProcessWire instance to object)
        if($name instanceof WireFuelable) {
            if($this->_wire) $name->setWire($wire); // inject fuel, PW 3.0
            if(is_string($value) && $value) {
                // set as new API var if API var name specified in $value
                $wire->fuel()->set($value, $name, $lock);
            }
            $value = $name; // return the provided instance
        } else {
            throw new WireException("Wire::wire(\$o) expected WireFuelable for \$o and was given " . get_class($name));
        }

    } 

    // ...
}

 

  • Like 1

Share this post


Link to post
Share on other sites

Oh, try prefixing Page with that namespace:

$d1Page = $DOMAIN_1_site->wire(new \ProcessWire\Page());

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...