Updated: Comment #N

Problem/Motivation

We want to generate book structures.

How to test

  1. Navigate to admin/config/development/generate/content
  2. Check the book settings
  3. Enable the Book navigation block on admin/structure/block
  4. Visit the frontpage and check the new books and child pages

drush (not working yet)

To see what book related options are available use

drush @drupal.d7 genc --help

Next you can try to generate book structures with

drush @drupal.d7 genc \
  --types=book \
  --book-depth=6 \
  --book-options=create-and-add-to-books \
 100 0

Proposed resolution

See patch

Remaining tasks

Should we always show the book options? Running $ drush @drupal.d7 help genc does not show the options as we are not sure the book module is enabled. (not fully bootstrapped)

User interface changes

API changes

Original report by @username

With the attached patch books are generated in a hierarchical structure.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

clemens.tolboom’s picture

Status: Needs review » Active

This patch is useful when testing http://drupal.org/project/export_docbook

moshe weitzman’s picture

Status: Active » Needs review

oops. thats a self reference. i think you copied wrong link or posted into wrong window.

clemens.tolboom’s picture

Status: Active » Needs review

I edited #1 (sorry for that). Would it be wise to provide a patch for D6 and D7 too?

moshe weitzman’s picture

Yes, that would be great. Note that book.module changed substantially in D6.

clemens.tolboom’s picture

Status: Needs review » Active
clemens.tolboom’s picture

Status: Active » Needs review
aclight’s picture

Subscribing. This will probably be necessary for #143805: Auto-populate handbooks to be completed.

clemens.tolboom’s picture

Maybe #307562: Patch: Generate bookpages with parents helps ... I haven't got time to finish that patch :=(

moshe weitzman’s picture

Status: Needs review » Needs work

patches must be for d7 and then backported.

Dave Reid’s picture

Title: When generating books this makes it hierarchical » Generate hierarchical books
Version: 5.x-1.x-dev » 7.x-1.x-dev

Pushing to 7.x-1.x. Also marked #307562: Patch: Generate bookpages with parents as a duplicate of this issue.

clemens.tolboom’s picture

I would prefer the other way around ... this was a D5 patch and so not a duplicate :p

marcp’s picture

A D7 patch for this would be great.

I'd like to see this:

1. A checkbox on admin/config/development/generate/content - "Add nodes to existing books"
2. A checkbox on admin/config/development/generate/content - "Create new books"

And I'll supply the patch if I get a chance, but would be even happier to test out someone else's patch.

marcp’s picture

Status: Needs work » Needs review
FileSize
3.62 KB

Here's a patch that:

1. Adds a new file to devel_generate called book.devel_generate.inc that implements the book functionality
2. Adds radio buttons to devel_generate_content_form() to let you choose how you want to associate generated "book allowed" nodes with books

If you choose to add nodes to existing books, nodes will go in there 1 level deep. I'd like to get this patch in and then enhance that if anyone cares about getting book data further into the hierarchy.

To apply:

1. Place patch file in modules/devel/devel_generate
2. patch < devel-generate-book-303143-13.patch

The patch will create the new file for you.

Status: Needs review » Needs work

The last submitted patch, devel-generate-book-303143-13.patch, failed testing.

marcp’s picture

Here's another attempt with a patch that can be run with patch -p 0 < devel-generate-book-303143-15.patch

marcp’s picture

Status: Needs work » Needs review

Setting to "needs review".

marcp’s picture

Here's a patch that also lets you choose "max depth" for book pages, so you can generate books of any depth (up to MENU_MAX_DEPTH, of course).

This patch supersedes the previous ones.

The only thing missing here would be some javascript to disable the max depth field when either not creating books or creating only new books.

This would be nice to get into devel so other folks could test out the core patch in #589440: Reordering fails with more than 31 book pages in a book.

moshe weitzman’s picture

Status: Needs review » Needs work

That ORDER BY RAND() looks like a performance killer. We have solved similar problem in devel_generate elsewhere (comment parents?).

Seems like a good patch otherwise. Needs a code and functionality review from someone. That javascript is nice to have but not a prerequisite.

marcp’s picture

Yeah, I'll look into what other ways there are of doing that. I did it differently at first but found an ORDER BY RAND() somewhere in devel and thought that it made the code simpler.

marcp’s picture

Status: Needs work » Needs review

Looking back at the rest of the devel_generate code, ORDER BY RAND() is still around for comment parents:

    switch ($i % 3) {
      case 1:
        $comment->pid = db_query_range("SELECT cid FROM {comment} WHERE pid = 0 AND nid = :nid ORDER BY RAND()", 0, 1, array(':nid' => $comment->nid))->fetchField();
        break;
      case 2:
        $comment->pid = db_query_range("SELECT cid FROM {comment} WHERE pid > 0 AND nid = :nid ORDER BY RAND()", 0, 1, array(':nid' => $comment->nid))->fetchField();
        break;
      default:
        $comment->pid = 0;
    }

and taxonomy:

    $sql = "SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid ORDER BY RAND()";

I think it makes sense to commit this and then possibly open a separate issue to remove the ORDER BY RAND() calls, but since we're just generating random content, I don't think it's a big deal for this to be slow. It's really only slow, as far as I can tell from doing some reading, when the tables get really large.

moshe weitzman’s picture

Status: Needs review » Needs work

Tables getting really big is a use case we support heartily.

See devel_generate_terms() and _taxonomy_devel_generate()

marcp’s picture

#836330: Generate menu and menu links has some info on this -- I'll use that as a reference.

marcp’s picture

Status: Needs work » Needs review
FileSize
5.94 KB

ORDER BY RAND() is out; looping around mt_rand() looking for a candidate plid is in.

This strategy runs the risk of causing lots of attempts to find a good candidate plid when the menu_links table has interspersed items that don't belong to books. We first look for the min and max mlid for a book-related menu_link and then take stabs in that range in order to find a good book parent. Also, if there are lots of deep book links and you want to generate only shallow links, you'll see the same thing where it takes a lot of attempts to find a valid plid.

Still, though, it seems better than ORDER BY RAND(), and it's more D7-like.

To apply, put it in sites/all/modules/devel and run patch -p 0 < devel-generate-book-303143-23.patch

marcp’s picture

The previous patch caused an error during validation of the devel_generate_content form if the book module wasn't enabled.

To apply, put it in sites/all/modules/devel and run patch -p 0 < devel-generate-book-303143-24.patch

moshe weitzman’s picture

Code looks good. Would be good to get someone to try it and assure it works. Also, would be good to get integration into drush genc commmand.

clemens.tolboom’s picture

Hmmm ... how do I apply this one

$ pwd
.../Sites/drupal-devs/devel
$ git apply ../devel-generate-book-303143-24.patch 
../devel-generate-book-303143-24.patch:58: trailing whitespace.
  
error: devel_generate.inc: No such file or directory
error: devel_generate.module: No such file or directory

Guess my git skill are not helping yet :(

marcp’s picture

@clemens.tolboom - That last patch is from the good old CVS days. Here's a re-rolled one that applies with one whitespace error line. I manually re-created it and haven't tested it out yet, but you should be able to apply with:

   git apply devel-generate-book-303143-27-D7.patch

Make sure to put the patch in sites/all/modules/devel (or wherever devel is on your site).

marcp’s picture

And here's a patch that gets rid of the whitespace issue so it should apply cleanly with:

   git apply devel-generate-book-303143-28-D7.patch
moshe weitzman’s picture

Status: Needs review » Needs work

CNW for drush integration

clemens.tolboom’s picture

Status: Needs work » Needs review
FileSize
5.52 KB

I added the options for Drush.

It works for

drush genc --help
drush genc --book-options=create-and-add-to-books --book-depth=3 --kill 30 --types=book,article

but fails when running

drush

For inclusion of the options we need them from book.devel_generate.inc somehow. This has something to do with the drush bootstrap level but I don't know how to solve that.

the attached patch needs review

Powered by Dreditor (triage sandbox) and Triage transitions

marcp’s picture

@clemens.tolboom - I'm not seeing the drush genc options in your patch. I started working on this also but stopped when the book module didn't magically add its stuff to the newly created node in "drush genc" mode.

When you re-roll your patch with the genc options, please stick the comment number in the part of the patch name before the "-D7" - I'll test it out and see if we can work together to finish this up.

clemens.tolboom’s picture

@marcp : thanks for the numbering remark. That triggered the 'bug'. netbeans did not allow for symlink debugging so I diff-ed the wrong git checkout :(

clemens.tolboom’s picture

I worked around the module_exists

+++ b/devel_generate/devel_generate.drush.inc
@@ -59,6 +59,12 @@ function devel_generate_drush_command() {
+  if (module_exists('book')) {

Replaced by

  if (function_exists('module_exists') && module_exists('book')) {

which I don't like. I file a new issue for this.

clemens.tolboom’s picture

marcp’s picture

Status: Needs review » Needs work

@clemens.tolboom - Generating top level books appears to work via drush genc with:

drush --types="book" --book-options=create-books genc 3

but, adding to books isn't working:

drush --types="book" --book-options=add-to-books genc 60

should add 60 child book pages. Instead, it adds 60 book pages that are not associated with any book.

 

For books that are created via admin/config/development/generate/content here's what we see in the menu_links table:

mysql> select menu_name, mlid, plid, link_path, module, link_title from menu_links where menu_name like 'book%';
+-------------+------+------+-----------+--------+---------------+
| menu_name   | mlid | plid | link_path | module | link_title    |
+-------------+------+------+-----------+--------+---------------+
| book-toc-15 |  363 |    0 | node/15   | book   | Molior Scisco |
| book-toc-16 |  364 |    0 | node/16   | book   | Abbas Ut      |
| book-toc-17 |  365 |    0 | node/17   | book   | Exerci Natu   |
+-------------+------+------+-----------+--------+---------------+
3 rows in set (0.00 sec)

If you generate 3 top level books with drush genc, here's what you'll see:

mysql> select menu_name, mlid, plid, link_path, module, link_title from menu_links where menu_name like 'book%';
+-------------+------+------+-----------+--------+-------------------------------------------+
| menu_name   | mlid | plid | link_path | module | link_title                                |
+-------------+------+------+-----------+--------+-------------------------------------------+
| book-toc-18 |  366 |    0 | node/18   | menu   | Erat Meus Quis                            |
| book-toc-19 |  367 |    0 | node/19   | menu   | Comis Consequat Ideo Pertineo Venio Vicis |
| book-toc-20 |  368 |    0 | node/20   | menu   | Iusto Probo Sudo                          |
+-------------+------+------+-----------+--------+-------------------------------------------+
3 rows in set (0.00 sec)

Note that the 'module' that owns the menu links is menu in the drush genc case, but it's book when done via the devel_generate gui. So, it looks like the book module is not inserting itself into the node creation process during drush genc.

clemens.tolboom’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
7.49 KB

I need this patch for #2139847: Book printer friendly pages has 'Add Comment' all over to test. It needed to move code somewhat around.

It works for the UI to generate 500 book pages with at least a nesting of 6 levels.

We should obviously bump this to D8 but I have no idea how to properly code for the latest D8. I missed the hook_menu changes.

clemens.tolboom’s picture

Issue summary: View changes
clemens.tolboom’s picture

Issue summary: View changes
clemens.tolboom’s picture

In #35 @marcp mentioned the failure to generate book trees through drush. That is still not solved.

I have no idea yet but see a plid appearing in our patch has

function devel_generate_book(&$node, $option, $depth) {
...
      if ($result['bid']) {
        $node->book['bid'] = $result['bid'];
        $node->book['plid'] = $result['mlid'];
      }

while book.module has no plid in it's hook_node_presave / insert / update. That still does not explain the difference between drush and using the UI :-/

clemens.tolboom’s picture

Hmmm plid is surely used in

function _book_update_outline($node) {
...
    if (empty($node->book['plid']) || !$parent || $parent['bid'] != $node->book['bid']) {
      $node->book['plid'] = db_query("SELECT mlid FROM {book} WHERE nid = :nid", array(
        ':nid' => $node->book['bid'],
      ))->fetchField();
      $node->book['parent_mismatch'] = TRUE; // Likely when JS is disabled.
    }
...

Could that explain our drush bug?

willzyx’s picture

Status: Needs review » Closed (outdated)

Closing for lack of activity. Feel free to reopen if the issue still exists