1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Implementation of hook_install(). |
---|
5 | */ |
---|
6 | function book_install() { |
---|
7 | // Create tables. |
---|
8 | drupal_install_schema('book'); |
---|
9 | // Add the node type. |
---|
10 | _book_install_type_create(); |
---|
11 | } |
---|
12 | |
---|
13 | /** |
---|
14 | * Implementation of hook_uninstall(). |
---|
15 | */ |
---|
16 | function book_uninstall() { |
---|
17 | // Delete menu links. |
---|
18 | db_query("DELETE FROM {menu_links} WHERE module = 'book'"); |
---|
19 | menu_cache_clear_all(); |
---|
20 | // Remove tables. |
---|
21 | drupal_uninstall_schema('book'); |
---|
22 | } |
---|
23 | |
---|
24 | function _book_install_type_create() { |
---|
25 | // Create an additional node type |
---|
26 | $book_node_type = array( |
---|
27 | 'type' => 'book', |
---|
28 | 'name' => t('Book page'), |
---|
29 | 'module' => 'node', |
---|
30 | '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.'), |
---|
31 | 'custom' => TRUE, |
---|
32 | 'modified' => TRUE, |
---|
33 | 'locked' => FALSE, |
---|
34 | ); |
---|
35 | |
---|
36 | $book_node_type = (object)_node_type_set_defaults($book_node_type); |
---|
37 | node_type_save($book_node_type); |
---|
38 | // Default to not promoted. |
---|
39 | variable_set('node_options_book', array('status')); |
---|
40 | // Use this default type for adding content to books. |
---|
41 | variable_set('book_allowed_types', array('book')); |
---|
42 | variable_set('book_child_type', 'book'); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Drupal 5.x to 6.x update. |
---|
47 | * |
---|
48 | * This function moves any existing book hierarchy into the new structure used |
---|
49 | * in the 6.x module. Rather than storing the hierarchy in the {book} table, |
---|
50 | * the menu API is used to store the hierarchy in the {menu_links} table and the |
---|
51 | * {book} table serves to uniquely connect a node to a menu link. |
---|
52 | * |
---|
53 | * In order to accomplish this, the current hierarchy is processed using a stack. |
---|
54 | * The stack insures that each parent is processed before any of its children |
---|
55 | * in the book hierarchy, and is compatible with batched update processing. |
---|
56 | * |
---|
57 | */ |
---|
58 | function book_update_6000() { |
---|
59 | $ret = array(); |
---|
60 | |
---|
61 | // Set up for a multi-part update. |
---|
62 | if (!isset($_SESSION['book_update_6000'])) { |
---|
63 | |
---|
64 | $schema['book'] = array( |
---|
65 | 'fields' => array( |
---|
66 | 'mlid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), |
---|
67 | 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), |
---|
68 | 'bid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), |
---|
69 | ), |
---|
70 | 'primary key' => array('mlid'), |
---|
71 | 'unique keys' => array( |
---|
72 | 'nid' => array('nid'), |
---|
73 | ), |
---|
74 | 'indexes' => array( |
---|
75 | 'bid' => array('bid'), |
---|
76 | ), |
---|
77 | ); |
---|
78 | // Add the node type. |
---|
79 | _book_install_type_create(); |
---|
80 | |
---|
81 | // Fix role permissions to account for the changed names |
---|
82 | // Setup the array holding strings to match and the corresponding |
---|
83 | // strings to replace them with. |
---|
84 | $replace = array( |
---|
85 | 'outline posts in books' => 'administer book outlines', |
---|
86 | 'create book pages' => 'create book content', |
---|
87 | 'edit book pages' => 'edit any book content', |
---|
88 | 'edit own book pages' => 'edit own book content', |
---|
89 | 'see printer-friendly version' => 'access printer-friendly version', |
---|
90 | ); |
---|
91 | |
---|
92 | // Loop over all the roles, and do the necessary transformations. |
---|
93 | $query = db_query("SELECT rid, perm FROM {permission} ORDER BY rid"); |
---|
94 | while ($role = db_fetch_object($query)) { |
---|
95 | // Replace all the old permissions with the corresponding new permissions. |
---|
96 | $fixed_perm = strtr($role->perm, $replace); |
---|
97 | // If the user could previously create book pages, they should get the new |
---|
98 | // 'add content to books' permission. |
---|
99 | if (strpos($role->perm, 'create book pages') !== FALSE) { |
---|
100 | $fixed_perm .= ', add content to books'; |
---|
101 | } |
---|
102 | // Only save if the permissions have changed. |
---|
103 | if ($fixed_perm != $role->perm) { |
---|
104 | $ret[] = update_sql("UPDATE {permission} SET perm = '$fixed_perm' WHERE rid = $role->rid"); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | // Determine whether there are any existing nodes in the book hierarchy. |
---|
109 | if (db_result(db_query("SELECT COUNT(*) FROM {book}"))) { |
---|
110 | // Temporary table for the old book hierarchy; we'll discard revision info. |
---|
111 | $schema['book_temp'] = array( |
---|
112 | 'fields' => array( |
---|
113 | 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), |
---|
114 | 'parent' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), |
---|
115 | 'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny') |
---|
116 | ), |
---|
117 | 'indexes' => array( |
---|
118 | 'parent' => array('parent') |
---|
119 | ), |
---|
120 | 'primary key' => array('nid'), |
---|
121 | ); |
---|
122 | |
---|
123 | db_create_table($ret, 'book_temp', $schema['book_temp']); |
---|
124 | |
---|
125 | // Insert each node in the old table into the temporary table. |
---|
126 | $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"); |
---|
127 | $ret[] = update_sql("DROP TABLE {book}"); |
---|
128 | |
---|
129 | db_create_table($ret, 'book', $schema['book']); |
---|
130 | |
---|
131 | $_SESSION['book_update_6000_orphans']['from'] = 0; |
---|
132 | $_SESSION['book_update_6000'] = array(); |
---|
133 | $result = db_query("SELECT * from {book_temp} WHERE parent = 0"); |
---|
134 | |
---|
135 | // Collect all books - top-level nodes. |
---|
136 | while ($a = db_fetch_array($result)) { |
---|
137 | $_SESSION['book_update_6000'][] = $a; |
---|
138 | } |
---|
139 | $ret['#finished'] = FALSE; |
---|
140 | return $ret; |
---|
141 | } |
---|
142 | else { |
---|
143 | // No exising nodes in the hierarchy, so drop the table and re-create it. |
---|
144 | $ret[] = update_sql("DROP TABLE {book}"); |
---|
145 | db_create_table($ret, 'book', $schema['book']); |
---|
146 | return $ret; |
---|
147 | } |
---|
148 | } |
---|
149 | elseif ($_SESSION['book_update_6000_orphans']) { |
---|
150 | // Do the first batched part of the update - collect orphans. |
---|
151 | $update_count = 400; // Update this many at a time |
---|
152 | |
---|
153 | $result = db_query_range("SELECT * FROM {book_temp}", $_SESSION['book_update_6000_orphans']['from'], $update_count); |
---|
154 | $has_rows = FALSE; |
---|
155 | // Go through the next $update_count book pages and locate the orphans. |
---|
156 | while ($book = db_fetch_array($result)) { |
---|
157 | $has_rows = TRUE; |
---|
158 | // Orphans are defined as nodes whose parent does not exist in the table. |
---|
159 | if ($book['parent'] && !db_result(db_query("SELECT COUNT(*) FROM {book_temp} WHERE nid = %d", $book['parent']))) { |
---|
160 | if (empty($_SESSION['book_update_6000_orphans']['book'])) { |
---|
161 | // The first orphan becomes the parent for all other orphans. |
---|
162 | $book['parent'] = 0; |
---|
163 | $_SESSION['book_update_6000_orphans']['book'] = $book; |
---|
164 | $ret[] = array('success' => TRUE, 'query' => 'Relocated orphan book pages.'); |
---|
165 | } |
---|
166 | else { |
---|
167 | // Re-assign the parent value of the book, and add it to the stack. |
---|
168 | $book['parent'] = $_SESSION['book_update_6000_orphans']['book']['nid']; |
---|
169 | $_SESSION['book_update_6000'][] = $book; |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | if ($has_rows) { |
---|
174 | $_SESSION['book_update_6000_orphans']['from'] += $update_count; |
---|
175 | } |
---|
176 | else { |
---|
177 | // Done with this part |
---|
178 | if (!empty($_SESSION['book_update_6000_orphans']['book'])) { |
---|
179 | // The orphans' parent is added last, so it will be processed first. |
---|
180 | $_SESSION['book_update_6000'][] = $_SESSION['book_update_6000_orphans']['book']; |
---|
181 | } |
---|
182 | $_SESSION['book_update_6000_orphans'] = FALSE; |
---|
183 | } |
---|
184 | $ret['#finished'] = FALSE; |
---|
185 | return $ret; |
---|
186 | } |
---|
187 | else { |
---|
188 | // Do the next batched part of the update |
---|
189 | $update_count = 100; // Update this many at a time |
---|
190 | |
---|
191 | while ($update_count && $_SESSION['book_update_6000']) { |
---|
192 | // Get the last node off the stack. |
---|
193 | $book = array_pop($_SESSION['book_update_6000']); |
---|
194 | |
---|
195 | // Add all of this node's children to the stack |
---|
196 | $result = db_query("SELECT * FROM {book_temp} WHERE parent = %d", $book['nid']); |
---|
197 | while ($a = db_fetch_array($result)) { |
---|
198 | $_SESSION['book_update_6000'][] = $a; |
---|
199 | } |
---|
200 | |
---|
201 | if ($book['parent']) { |
---|
202 | // If its not a top level page, get its parent's mlid. |
---|
203 | $parent = db_fetch_array(db_query("SELECT b.mlid AS plid, b.bid FROM {book} b WHERE b.nid = %d", $book['parent'])); |
---|
204 | $book = array_merge($book, $parent); |
---|
205 | } |
---|
206 | else { |
---|
207 | // There is not a parent - this is a new book. |
---|
208 | $book['plid'] = 0; |
---|
209 | $book['bid'] = $book['nid']; |
---|
210 | } |
---|
211 | |
---|
212 | $book += array( |
---|
213 | 'module' => 'book', |
---|
214 | 'link_path' => 'node/'. $book['nid'], |
---|
215 | 'router_path' => 'node/%', |
---|
216 | 'menu_name' => 'book-toc-'. $book['bid'], |
---|
217 | ); |
---|
218 | $book = array_merge($book, db_fetch_array(db_query("SELECT title AS link_title FROM {node} WHERE nid = %d", $book['nid']))); |
---|
219 | |
---|
220 | // Items with depth > MENU_MAX_DEPTH cannot be saved. |
---|
221 | if (menu_link_save($book)) { |
---|
222 | db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']); |
---|
223 | } |
---|
224 | else { |
---|
225 | // The depth was greater then MENU_MAX_DEPTH, so attach it to the |
---|
226 | // closest valid parent. |
---|
227 | $book['plid'] = db_result(db_query("SELECT plid FROM {menu_links} WHERE mlid = %d", $book['plid'])); |
---|
228 | if (menu_link_save($book)) { |
---|
229 | db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']); |
---|
230 | } |
---|
231 | } |
---|
232 | $update_count--; |
---|
233 | } |
---|
234 | $ret['#finished'] = FALSE; |
---|
235 | } |
---|
236 | |
---|
237 | if (empty($_SESSION['book_update_6000'])) { |
---|
238 | $ret['#finished'] = TRUE; |
---|
239 | $ret[] = array('success' => TRUE, 'query' => 'Relocated existing book pages.'); |
---|
240 | $ret[] = update_sql("DROP TABLE {book_temp}"); |
---|
241 | unset($_SESSION['book_update_6000']); |
---|
242 | unset($_SESSION['book_update_6000_orphans']); |
---|
243 | } |
---|
244 | |
---|
245 | return $ret; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * Implementation of hook_schema(). |
---|
250 | */ |
---|
251 | function book_schema() { |
---|
252 | $schema['book'] = array( |
---|
253 | 'description' => 'Stores book outline information. Uniquely connects each node in the outline to a link in {menu_links}', |
---|
254 | 'fields' => array( |
---|
255 | 'mlid' => array( |
---|
256 | 'type' => 'int', |
---|
257 | 'unsigned' => TRUE, |
---|
258 | 'not null' => TRUE, |
---|
259 | 'default' => 0, |
---|
260 | 'description' => "The book page's {menu_links}.mlid.", |
---|
261 | ), |
---|
262 | 'nid' => array( |
---|
263 | 'type' => 'int', |
---|
264 | 'unsigned' => TRUE, |
---|
265 | 'not null' => TRUE, |
---|
266 | 'default' => 0, |
---|
267 | 'description' => "The book page's {node}.nid.", |
---|
268 | ), |
---|
269 | 'bid' => array( |
---|
270 | 'type' => 'int', |
---|
271 | 'unsigned' => TRUE, |
---|
272 | 'not null' => TRUE, |
---|
273 | 'default' => 0, |
---|
274 | 'description' => "The book ID is the {book}.nid of the top-level page.", |
---|
275 | ), |
---|
276 | ), |
---|
277 | 'primary key' => array('mlid'), |
---|
278 | 'unique keys' => array( |
---|
279 | 'nid' => array('nid'), |
---|
280 | ), |
---|
281 | 'indexes' => array( |
---|
282 | 'bid' => array('bid'), |
---|
283 | ), |
---|
284 | ); |
---|
285 | |
---|
286 | return $schema; |
---|
287 | } |
---|
288 | |
---|
289 | |
---|