book.install 18.5 KB
Newer Older
1
<?php
2
// $Id$
3

4
5
6
/**
 * Implementation of hook_install().
 */
7
function book_install() {
8
9
  // Create tables.
  drupal_install_schema('book');
Dries's avatar
Dries committed
10
11
  // Add the node type.
  _book_install_type_create();
12
}
13
14
15
16
17

/**
 * Implementation of hook_uninstall().
 */
function book_uninstall() {
Dries's avatar
Dries committed
18
19
20
  // Delete menu links.
  db_query("DELETE FROM {menu_links} WHERE module = 'book'");
  menu_cache_clear_all();
21
22
  // Remove tables.
  drupal_uninstall_schema('book');
23
}
Dries's avatar
Dries committed
24
25
26
27
28
29
30

function _book_install_type_create() {
  // Create an additional node type
  $book_node_type = array(
    'type' => 'book',
    'name' => t('Book page'),
    'module' => 'node',
31
    'description' => t('A <em>book page</em> is a page of content, organized into a collection of related entries collectively known as a <em>book</em>. A <em>book page</em> automatically displays links to adjacent pages, providing a simple navigation system for organizing and reviewing structured content.'),
Dries's avatar
Dries committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    'custom' => TRUE,
    'modified' => TRUE,
    'locked' => FALSE,
  );

  $book_node_type = (object)_node_type_set_defaults($book_node_type);
  node_type_save($book_node_type);
  // Default to not promoted.
  variable_set('node_options_book', array('status'));
  // Use this default type for adding content to books.
  variable_set('book_allowed_types', array('book'));
  variable_set('book_child_type', 'book');
}

/**
 * Drupal 5.x to 6.x update.
 *
 * This function moves any existing book hierarchy into the new structure used
 * in the 6.x module.  Rather than storing the hierarchy in the {book} table,
 * the menu API is used to store the hierarchy in the {menu_links} table and the
 * {book} table serves to uniquely connect a node to a menu link.
 *
 * In order to accomplish this, the current hierarchy is processed using a stack.
 * The stack insures that each parent is processed before any of its children
 * in the book hierarchy, and is compatible with batched update processing.
 *
 */
function book_update_6000() {
  $ret = array();

  // Set up for a multi-part update.
  if (!isset($_SESSION['book_update_6000'])) {

    $schema['book'] = array(
      'fields' => array(
        'mlid'    => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'nid'     => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'bid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      ),
      'indexes' => array(
        'nid'     => array('nid'),
        'bid' => array('bid')
      ),
      'primary key' => array('mlid'),
    );
    // Add the node type.
    _book_install_type_create();

    // Fix role permissions to account for the changed names
81
    // Set up the array holding strings to match and the corresponding
Dries's avatar
Dries committed
82
83
84
85
    // strings to replace them with.
    $replace = array(
      'outline posts in books' => 'administer book outlines',
      'create book pages' => 'create book content',
86
      'edit book pages' => 'edit any book content',
Dries's avatar
Dries committed
87
88
89
90
91
92
93
94
95
96
      'edit own book pages' => 'edit own book content',
      'see printer-friendly version' => 'access printer-friendly version',
    );

    // Loop over all the roles, and do the necessary transformations.
    $query = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
    while ($role = db_fetch_object($query)) {
      // Replace all the old permissions with the corresponding new permissions.
      $fixed_perm = strtr($role->perm, $replace);
      // If the user could previously create book pages, they should get the new
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
      // 'add content to books' permission.
      if (strpos($role->perm, 'create book pages') !== FALSE) {
        $fixed_perm .= ', add content to books';
      }
      // Only save if the permissions have changed.
      if ($fixed_perm != $role->perm) {
        $ret[] = update_sql("UPDATE {permission} SET perm = '$fixed_perm' WHERE rid = $role->rid");
      }
    }

    // Determine whether there are any existing nodes in the book hierarchy.
    if (db_result(db_query("SELECT COUNT(*) FROM {book}"))) {
      // Temporary table for the old book hierarchy; we'll discard revision info.
      $schema['book_temp'] = array(
        'fields' => array(
          'nid'    => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'parent' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
          'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny')
        ),
        'indexes' => array(
          'parent' => array('parent')
        ),
        'primary key' => array('nid'),
      );

      db_create_table($ret, 'book_temp', $schema['book_temp']);

      // Insert each node in the old table into the temporary table.
      $ret[] = update_sql("INSERT INTO {book_temp} (nid, parent, weight) SELECT b.nid, b.parent, b.weight FROM {book} b INNER JOIN {node} n on b.vid = n.vid");
      $ret[] = update_sql("DROP TABLE {book}");

      db_create_table($ret, 'book', $schema['book']);

      $_SESSION['book_update_6000_orphans']['from'] = 0;
      $_SESSION['book_update_6000'] = array();
      $result = db_query("SELECT * from {book_temp} WHERE parent = 0");

      // Collect all books - top-level nodes.
      while ($a = db_fetch_array($result)) {
        $_SESSION['book_update_6000'][] = $a;
      }
      $ret['#finished'] = FALSE;
      return $ret;
    }
    else {
      // No exising nodes in the hierarchy, so drop the table and re-create it.
      $ret[] = update_sql("DROP TABLE {book}");
      db_create_table($ret, 'book', $schema['book']);
      return $ret;
    }
  }
  elseif ($_SESSION['book_update_6000_orphans']) {
    // Do the first batched part of the update - collect orphans.
    $update_count = 400; // Update this many at a time

    $result = db_query_range("SELECT * FROM {book_temp}", $_SESSION['book_update_6000_orphans']['from'], $update_count);
    $has_rows = FALSE;
    // Go through the next $update_count book pages and locate the orphans.
    while ($book = db_fetch_array($result)) {
      $has_rows = TRUE;
      // Orphans are defined as nodes whose parent does not exist in the table.
      if ($book['parent'] && !db_result(db_query("SELECT COUNT(*) FROM {book_temp} WHERE nid = %d", $book['parent']))) {
        if (empty($_SESSION['book_update_6000_orphans']['book'])) {
          // The first orphan becomes the parent for all other orphans.
          $book['parent'] = 0;
          $_SESSION['book_update_6000_orphans']['book'] = $book;
          $ret[] = array('success' => TRUE, 'query' => t('Relocated orphan book pages.'));
        }
        else {
          // Re-assign the parent value of the book, and add it to the stack.
          $book['parent'] = $_SESSION['book_update_6000_orphans']['book']['nid'];
          $_SESSION['book_update_6000'][] = $book;
        }
      }
    }
    if ($has_rows) {
      $_SESSION['book_update_6000_orphans']['from'] += $update_count;
    }
    else {
      // Done with this part
      if (!empty($_SESSION['book_update_6000_orphans']['book'])) {
        // The orphans' parent is added last, so it will be processed first.
        $_SESSION['book_update_6000'][] = $_SESSION['book_update_6000_orphans']['book'];
      }
      $_SESSION['book_update_6000_orphans'] = FALSE;
    }
    $ret['#finished'] = FALSE;
    return $ret;
  }
  else {
    // Do the next batched part of the update
    $update_count = 100; // Update this many at a time

    while ($update_count && $_SESSION['book_update_6000']) {
      // Get the last node off the stack.
      $book = array_pop($_SESSION['book_update_6000']);

      // Add all of this node's children to the stack
      $result = db_query("SELECT * FROM {book_temp} WHERE parent = %d", $book['nid']);
      while ($a = db_fetch_array($result)) {
        $_SESSION['book_update_6000'][] = $a;
      }

      if ($book['parent']) {
        // If its not a top level page, get its parent's mlid.
        $parent = db_fetch_array(db_query("SELECT b.mlid AS plid, b.bid FROM {book} b WHERE b.nid = %d", $book['parent']));
        $book = array_merge($book, $parent);
      }
      else {
        // There is not a parent - this is a new book.
        $book['plid'] = 0;
        $book['bid'] = $book['nid'];
      }

      $book += array(
        'module' => 'book',
        'link_path' => 'node/'. $book['nid'],
        'router_path' => 'node/%',
        'menu_name' => book_menu_name($book['bid']),
      );
      $book = array_merge($book, db_fetch_array(db_query("SELECT title AS link_title FROM {node} WHERE nid = %d", $book['nid'])));

      // Items with depth > MENU_MAX_DEPTH cannot be saved.
      if (menu_link_save($book)) {
        db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']);
      }
      else {
        // The depth was greater then MENU_MAX_DEPTH, so attach it to the
        // closest valid parent.
        $book['plid'] = db_result(db_query("SELECT plid FROM {menu_links} WHERE mlid = %d", $book['plid']));
        if (menu_link_save($book)) {
          db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']);
        }
      }
      $update_count--;
    }
    $ret['#finished'] = FALSE;
  }

  if (empty($_SESSION['book_update_6000'])) {
    $ret['#finished'] = TRUE;
    $ret[] = array('success' => TRUE, 'query' => t('Relocated existing book pages.'));
    $ret[] = update_sql("DROP TABLE {book_temp}");
    unset($_SESSION['book_update_6000']);
    unset($_SESSION['book_update_6000_orphans']);
  }

  return $ret;
}

/**
 * Drupal 5.x to 6.x update.
 *
 * This function moves any existing book hierarchy into the new structure used
 * in the 6.x module.  Rather than storing the hierarchy in the {book} table,
 * the menu API is used to store the hierarchy in the {menu_links} table and the
 * {book} table serves to uniquely connect a node to a menu link.
 *
 * In order to accomplish this, the current hierarchy is processed using a stack.
 * The stack insures that each parent is processed before any of its children
 * in the book hierarchy, and is compatible with batched update processing.
 *
 */
function book_update_6000() {
  $ret = array();

  // Set up for a multi-part update.
  if (!isset($_SESSION['book_update_6000'])) {

    $schema['book'] = array(
      'fields' => array(
        'mlid'    => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'nid'     => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'bid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      ),
      'indexes' => array(
        'nid'     => array('nid'),
        'bid' => array('bid')
      ),
      'primary key' => array('mlid'),
    );
    // Add the node type.
    _book_install_type_create();

    // Fix role permissions to account for the changed names
    // Setup the array holding strings to match and the corresponding
    // strings to replace them with.
    $replace = array(
      'outline posts in books' => 'administer book outlines',
      'create book pages' => 'create book content',
      'edit book pages' => 'edit book content',
      'edit own book pages' => 'edit own book content',
      'see printer-friendly version' => 'access printer-friendly version',
    );

    // Loop over all the roles, and do the necessary transformations.
    $query = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
    while ($role = db_fetch_object($query)) {
      // Replace all the old permissions with the corresponding new permissions.
      $fixed_perm = strtr($role->perm, $replace);
      // If the user could previously create book pages, they should get the new
Dries's avatar
Dries committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
      // 'add content to books' permission.
      if (strpos($role->perm, 'create book pages') !== FALSE) {
        $fixed_perm .= ', add content to books';
      }
      // Only save if the permissions have changed.
      if ($fixed_perm != $role->perm) {
        $ret[] = update_sql("UPDATE {permission} SET perm = '$fixed_perm' WHERE rid = $role->rid");
      }
    }

    // Determine whether there are any existing nodes in the book hierarchy.
    if (db_result(db_query("SELECT COUNT(*) FROM {book}"))) {
      // Temporary table for the old book hierarchy; we'll discard revision info.
      $schema['book_temp'] = array(
        'fields' => array(
          'nid'    => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'parent' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
          'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny')
        ),
        'indexes' => array(
          'parent' => array('parent')
        ),
        'primary key' => array('nid'),
      );

      db_create_table($ret, 'book_temp', $schema['book_temp']);

      // Insert each node in the old table into the temporary table.
      $ret[] = update_sql("INSERT INTO {book_temp} (nid, parent, weight) SELECT b.nid, b.parent, b.weight FROM {book} b INNER JOIN {node} n on b.vid = n.vid");
      $ret[] = update_sql("DROP TABLE {book}");

      db_create_table($ret, 'book', $schema['book']);

      $_SESSION['book_update_6000_orphans']['from'] = 0;
      $_SESSION['book_update_6000'] = array();
      $result = db_query("SELECT * from {book_temp} WHERE parent = 0");

      // Collect all books - top-level nodes.
      while ($a = db_fetch_array($result)) {
        $_SESSION['book_update_6000'][] = $a;
      }
      $ret['#finished'] = FALSE;
      return $ret;
    }
    else {
      // No exising nodes in the hierarchy, so drop the table and re-create it.
      $ret[] = update_sql("DROP TABLE {book}");
      db_create_table($ret, 'book', $schema['book']);
      return $ret;
    }
  }
  elseif ($_SESSION['book_update_6000_orphans']) {
    // Do the first batched part of the update - collect orphans.
    $update_count = 400; // Update this many at a time

    $result = db_query_range("SELECT * FROM {book_temp}", $_SESSION['book_update_6000_orphans']['from'], $update_count);
354
    $has_rows = FALSE;
Dries's avatar
Dries committed
355
356
    // Go through the next $update_count book pages and locate the orphans.
    while ($book = db_fetch_array($result)) {
357
      $has_rows = TRUE;
Dries's avatar
Dries committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
      // Orphans are defined as nodes whose parent does not exist in the table.
      if ($book['parent'] && !db_result(db_query("SELECT COUNT(*) FROM {book_temp} WHERE nid = %d", $book['parent']))) {
        if (empty($_SESSION['book_update_6000_orphans']['book'])) {
          // The first orphan becomes the parent for all other orphans.
          $book['parent'] = 0;
          $_SESSION['book_update_6000_orphans']['book'] = $book;
          $ret[] = array('success' => TRUE, 'query' => t('Relocated orphan book pages.'));
        }
        else {
          // Re-assign the parent value of the book, and add it to the stack.
          $book['parent'] = $_SESSION['book_update_6000_orphans']['book']['nid'];
          $_SESSION['book_update_6000'][] = $book;
        }
      }
    }
373
374
375
376
377
378
379
380
381
382
383
    if ($has_rows) {
      $_SESSION['book_update_6000_orphans']['from'] += $update_count;
    }
    else {
      // Done with this part
      if (!empty($_SESSION['book_update_6000_orphans']['book'])) {
        // The orphans' parent is added last, so it will be processed first.
        $_SESSION['book_update_6000'][] = $_SESSION['book_update_6000_orphans']['book'];
      }
      $_SESSION['book_update_6000_orphans'] = FALSE;
    }
Dries's avatar
Dries committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
    $ret['#finished'] = FALSE;
    return $ret;
  }
  else {
    // Do the next batched part of the update
    $update_count = 100; // Update this many at a time

    while ($update_count && $_SESSION['book_update_6000']) {
      // Get the last node off the stack.
      $book = array_pop($_SESSION['book_update_6000']);

      // Add all of this node's children to the stack
      $result = db_query("SELECT * FROM {book_temp} WHERE parent = %d", $book['nid']);
      while ($a = db_fetch_array($result)) {
        $_SESSION['book_update_6000'][] = $a;
      }

      if ($book['parent']) {
        // If its not a top level page, get its parent's mlid.
        $parent = db_fetch_array(db_query("SELECT b.mlid AS plid, b.bid FROM {book} b WHERE b.nid = %d", $book['parent']));
        $book = array_merge($book, $parent);
      }
      else {
        // There is not a parent - this is a new book.
        $book['plid'] = 0;
        $book['bid'] = $book['nid'];
      }

      $book += array(
        'module' => 'book',
        'link_path' => 'node/'. $book['nid'],
        'router_path' => 'node/%',
        'menu_name' => book_menu_name($book['bid']),
      );
      $book = array_merge($book, db_fetch_array(db_query("SELECT title AS link_title FROM {node} WHERE nid = %d", $book['nid'])));

      // Items with depth > MENU_MAX_DEPTH cannot be saved.
      if (menu_link_save($book)) {
        db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']);
      }
      else {
        // The depth was greater then MENU_MAX_DEPTH, so attach it to the
        // closest valid parent.
        $book['plid'] = db_result(db_query("SELECT plid FROM {menu_links} WHERE mlid = %d", $book['plid']));
        if (menu_link_save($book)) {
          db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']);
        }
      }
      $update_count--;
    }
    $ret['#finished'] = FALSE;
  }

  if (empty($_SESSION['book_update_6000'])) {
    $ret['#finished'] = TRUE;
    $ret[] = array('success' => TRUE, 'query' => t('Relocated existing book pages.'));
    $ret[] = update_sql("DROP TABLE {book_temp}");
    unset($_SESSION['book_update_6000']);
    unset($_SESSION['book_update_6000_orphans']);
  }

  return $ret;
}

448
449
450
451
452
/**
 * Implementation of hook_schema().
 */
function book_schema() {
  $schema['book'] = array(
453
  'description' => t('Stores book outline information. Uniquely connects each node in the outline to a link in {menu_links}'),
454
    'fields' => array(
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
      'mlid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t("The book page's {menu_links}.mlid."),
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t("The book page's {node}.nid."),
      ),
      'bid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t("The book ID is the {book}.nid of the top-level page."),
      ),
476
477
478
479
480
481
482
483
484
485
486
487
    ),
    'indexes' => array(
      'nid'     => array('nid'),
      'bid' => array('bid')
    ),
    'primary key' => array('mlid'),
  );

  return $schema;
}