Jump to content

generate dummy content in api


adrianmak
 Share

Recommended Posts

I'm writing snippet of code to generate dummy content.

For the most basic, I tried to create a page to make sure it is working.

<?php
    /*  include pw bootstrap*/
    include('index.php');

    echo "generate dummy content";
    
    $p = new Page();
    /* page template */
    $p->template = 'post';
    /* parent template */
    $p->parent = 'blog';  
    $p->title ="create page in api";
    $p->body = "dsdsd sd sd sd s d ss";
    $p->post_date ="2015-01-26";
    $p->save();
?>

however,

Error: Uncaught exception 'WireException' with message 'Unknown Selector operator: '' -- was your selector value properly escaped? ' in /var/www/html/pw1/wire/core/Selectors.php:247
Stack trace:
#0 /var/www/html/pw1/wire/core/Selectors.php(284): Selectors->create('blog', '', '')
#1 /var/www/html/pw1/wire/core/Selectors.php(81): Selectors->extractString('blog')
#2 /var/www/html/pw1/wire/core/Pages.php(165): Selectors->__construct('blog')
#3 [internal function]: Pages->___find('blog', Array)
#4 /var/www/html/pw1/wire/core/Wire.php(389): call_user_func_array(Array, Array)
#5 /var/www/html/pw1/wire/core/Wire.php(344): Wire->runHooks('find', Array)
#6 /var/www/html/pw1/wire/core/Pages.php(260): Wire->__call('find', Array)
#7 /var/www/html/pw1/wire/core/Pages.php(260): Pages->find('blog', Array)
#8 /var/www/html/pw1/wire/core/Pages.php(275): Pages->findOne('blog')
#9 /var/www/html/pw1/wire/core/Page.php(400): Pages->get('blog')
#10 /var/www/html/pw1/wire/core/Page.php(820): Page->set('parent', 'blog')
#11 /var/www/html/pw (line 247 of /var/www/html/pw1/wire/core/Selectors.php)
This error message was shown because you are logged in as a Superuser. Error has been logged. 
Link to comment
Share on other sites

You can't assign the parent like that - you need to provide a page object - something like:

$p->parent = $pages->get("/blog/");

You should also get into the habit of setting outputformatting off before saving a page:

$p->of(false);

Correction - even though the above approach works just fine, apeisa's way of assigning the path to the page also works, so my comment about needing a page object is actually not correct!

  • Like 1
Link to comment
Share on other sites

Here's a recipe to do that (haven't tested it thoroughly, but should work here): https://processwire-recipes.com/recipes/activate-all-languages/

it didn't work.

I found that the $languages variable is not available on my php script. it is a NULL  value

since this is my custom php script, run outside of pw.

the $languages variable should be changed to $wire->languages

Then the snippet code will work in this case

Edited by adrianmak
Link to comment
Share on other sites

Full working code

I used following lipsum php generator

https://github.com/alexscottmoore/Lorem-PHPsum

<?php
    include "lorem-phpsum.php";    
    include('index.php');
    
    $startdate = "2013-01-01";
    $enddate = date("Y-m-d H:i:s");

    echo "Generate dummy blog post";
    
    for ($i=1;$i<=30;$i++) {
        $blog = wire('pages')->get('/blog');
        $p = new Page();
        $p->template = 'post';
        $p->parent = $blog;
        $p->title = phpsum(5);
        $p->body = phpsum(10, 20, 3);
        $p->post_date = randomDate($startdate, $enddate);
        $p->of(false);
        echo "Saving post....";
        foreach($wire->languages as $lang) {
          if($lang->isDefault()) continue;
              $p->set("status$lang", 1);
          $p->save();    
        }
    }

    function randomDate($start_date, $end_date)
    {
        // Convert to timetamps
        $min = strtotime($start_date);
        $max = strtotime($end_date);

        // Generate random number using above bounds
        $val = rand($min, $max);

        // Convert back to desired date format
        return date('Y-m-d H:i:s', $val);
    }
?>
  • Like 3
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...