/[drupal]/contributions/tricks/import_mt3.2_drupal4.7_template.php
ViewVC logotype

Contents of /contributions/tricks/import_mt3.2_drupal4.7_template.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Mon May 29 02:07:53 2006 UTC (3 years, 6 months ago) by halkeye
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Initial Commit of my mt3.2-> drupal4.7 conversion script
1 <?php
2 /****
3 MT 3.2 -> Drupal 4.7 Converstion Script
4
5 Contact: Gavin <halkeye@halkeye.net>
6
7 Usage:
8 *) Create a new template for the blog in MT you want to export
9 *) Setup the output file to a php script in your drupal director (ie /path/to/drupal/importmt.php)
10 *) Link or copy this file into the template editor
11 *) Rebuild the template
12 *) Run the newly created importmt.php script
13
14 Note:
15 If you have more than 9999 entries, goto
16 MTEntries lastn="9999" sort_order="ascend"
17 and up the lastn, or refer to the MT documents about how to do offets.
18
19 ****/
20 require "./includes/bootstrap.inc";
21 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
22 /**
23 * CONFIG ME
24 */
25 // Story or blog probably.. unless you need something else
26 $nodeType = 'story';
27 // 3 = Full HTML on default installs
28 $filterType = 3;
29
30 /**
31 * Nothign should change below here
32 */
33 $characters = array_merge(range('0','9'), range('A', 'z'));
34
35 function generatePassword() {
36 global $characters;
37 $password = '';
38 foreach (range(0,9)as $num) {
39 $password .= $characters[array_rand($characters,1)];
40 }
41 return $password;
42 }
43
44 function getUser($username, $email, $link, $name) {
45 static $users;
46 if (!$users) { $users = array(); }
47 if (!isset($users[$username])) {
48 $users[$username] = array(
49 email => $email,
50 link => $link,
51 name => $name,
52 );
53 $users[$username]['user'] = user_load(array('name' => $username));
54 if (!$users[$username]['user']) {
55 $array = array("name" => $username, "pass" => generatePassword(), "mail" => $email, "status" => 1, "link" => $link);
56 user_save('', $array);
57 $users[$username]['user'] = user_load(array('name' => $username));
58 }
59 }
60 return $users[$username]['user'];
61 }
62
63 $db_prefix = 'drupal_';
64
65 $baseArchiveUrl = '<$MTBlogArchiveURL encode_php="q"$>';
66 $blogRelativeUrl = '<$MTBlogRelativeURL encode_php="q"$>';
67
68 $categories = array();
69 $vocab = array_pop(taxonomy_get_vocabularies($nodeType));
70 if (!$vocab) {
71 $edit = array(
72 'name' => $nodeType,
73 'multiple' => 0,
74 'required' => 1,
75 'hierarchy' => 1,
76 'relations' => 0,
77 'module' => $nodeType,
78 'nodes' => array($nodeType => 1)
79 );
80
81 taxonomy_save_vocabulary($edit);
82 $vid = $edit['vid'];
83
84 $vocab = taxonomy_get_vocabulary($vid);
85 }
86
87 <MTCategories show_empty="1">
88 {
89 $id = '<$MTCategoryID encode_php="q"$>';
90 $categories[$id] = array(
91 id => $id,
92 description => '<$MTCategoryDescription encode_php="q"$>',
93 label => '<$MTCategoryLabel encode_php="q"$>',
94 );
95 $term = taxonomy_get_term_by_name($categories[$id]['label']);
96 if (!$term) {
97 $edit = array('name' => $categories[$id]['label'], 'vid' => $vocab->vid);
98 taxonomy_save_term($edit);
99 $categories[$id]['termID'] = $edit['tid'];
100 $categories[$id]['object'] = array_pop(taxonomy_get_term($categories[$id]['termID']));
101 }
102 else {
103 $term = array_pop($term);
104 $categories[$id]['termID'] = $term->tid;
105 $categories[$id]['object'] = $term;
106 }
107 }
108 </MTCategories>
109
110 <MTEntries lastn="9999" sort_order="ascend">
111 {
112
113 $node_teaser = <<<NE
114 <$MTEntryBody encode_php="heredoc"$>
115 NE;
116
117 $node_body = <<<NB
118 <$MTEntryBody encode_php="heredoc"$><$MTEntryMore encode_php="heredoc"$>
119 NB;
120
121 $nid = '<$MTEntryID encode_php="q"$>';
122
123 /** Finish Fetching data, now store it */
124 $node = node_load(array('vid' => $nid));
125 if (!$node) {
126 $node = new StdClass();
127 $node->vid = $nid;
128 }
129 $node->teaser = preg_replace('/<p>(.*?)<\/p>/ims', '\1',$node_teaser);
130 $node->body = preg_replace('/<p>(.*?)<\/p>/ims', '\1',$node_body);
131 $node->type = $nodeType;
132 $node->title = <<<NT
133 <$MTEntryTitle encode_php="heredoc"$>
134 NT;
135 $user = getUser(
136 '<$MTEntryAuthorUsername encode_php="q"$>',
137 '<$MTEntryAuthorEmail encode_php="q"$>',
138 '<$MTEntryAuthorLink show_email="0" show_url="0" encode_php="q"$>',
139 '<$MTEntryAuthorDisplayName encode_php="q"$>'
140 );
141
142 $node->uid = $user->uid;
143 $node->created = $node->changed = db_result(db_query("SELECT UNIX_TIMESTAMP('%s')", '<$MTEntryDate format="%Y-%m-%d %H:%M:%S"$><$MTBlogTimezone$>'));
144 <MTIfCommentsAccepted>
145 $node->comment = 2;
146 </MTIfCommentsAccepted>
147 if (strtolower('<$MTEntryStatus encode_php="q"$>') == 'publish') {
148 $node->promote = 1;
149 $node->moderate = 0;
150 $node->status = 1;
151 }
152 else {
153 $node->promote = 0;
154 $node->moderate = 0;
155 $node->status = 0;
156 }
157 $node->format = $filterType;
158 $node->sticky = 0;
159 $path = parse_url('<$MTEntryLink archive_type="Individual" encode_php="q"$>');
160 $node->path = preg_replace("@^$blogRelativeUrl@", '', $path['path'], 1) ;
161 #$node->path = $path['path'];
162 $node->taxonomy = array();
163 foreach (explode(',', '<MTEntryCategories glue=","><$MTCategoryID encode_php="q"$></MTEntryCategories>') as $categoryID) {
164 $node->taxonomy[] = $categories[$categoryID]['object'];
165 }
166
167 node_save($node);
168
169 $node_comment_count = 0;
170 <MTComments sort_order="ascend">
171 {
172 $comment_text = <<<CT
173 <$MTCommentBody encode_php="heredoc"$>
174 CT;
175 $comment_text = preg_replace('/<p>(.*?)<\/p>/ims', '\1',$comment_text);
176
177 // grab the first five words of the comment as the comment subject
178 $subject = "";
179 $arr = explode(" ",$comment_text);
180
181 for($i=0; $i<5; $i++) {
182 if (isset($arr[$i])) {
183 $subject .= $arr[$i]." ";
184 }
185 }
186
187
188
189 $comment_timestamp = '<$MTCommentDate format="%Y-%m-%d %H:%M:%S"$><$MTBlogTimezone$>';
190
191 $comment = _comment_load($cid);
192 if (!$comment) {
193 $comment = new StdClass();
194 }
195 $comment->nid = $node->nid;
196 $comment->subject = $subject;
197 $comment->comment = $comment_text;
198 $comment->hostname = trim('<$MTCommentID encode_php="q"$>');
199 $comment->timestamp = $comment_timestamp;
200 $comment->status = COMMENT_PUBLISHED;
201
202 $comment->uid = 0; // Anonymous
203 $comment->format = 0;
204
205 $comment->name = trim('<$MTCommentAuthor encode_php="q"$>');
206 $comment->mail = trim('<$MTCommentEmail encode_php="q"$>');
207 $comment->homepage = trim('<$MTCommentURL encode_php="q"$>');
208
209 // Allow modules to respond to the updating of a comment.
210 if ($comment->cid) {
211 // Update the comment in the database.
212 db_query("UPDATE {comments} SET status = %d, timestamp = UNIX_TIMESTAMP('%s'), subject = '%s', comment = '%s', format = %d, uid = %d, name = '%s', mail = '%s', homepage = '%s' WHERE cid = %d",
213 $comment->status, $comment->timestamp, $comment->subject, $comment->comment, $comment->format,
214 $comment->uid, $comment->name, $comment->mail, $comment->homepage, $comment->cid);
215
216 _comment_update_node_statistics($comment->nid);
217
218 // Allow modules to respond to the updating of a comment.
219 comment_invoke_comment(get_object_vars( $comment ), 'update');
220 }
221 else {
222 // Add the comment to database.
223 $score = (int) $comment->score;
224 $users = serialize(array(0 => $score));
225
226 // This is a comment with no parent comment (depth 0): we start
227 // by retrieving the maximum thread level.
228 $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $comment->nid));
229
230 // Strip the "/" from the end of the thread.
231 $max = rtrim($max, '/');
232
233 // Finally, build the thread field for this new comment.
234 $comment->thread = int2vancode(vancode2int($max) + 1) .'/';
235
236 $comment->cid = <$MTCommentID encode_php="heredoc"$>;
237 if (!$comment->cid) {
238 $comment->cid = db_next_id('{comments}_cid');
239 }
240
241 db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage)
242 VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', UNIX_TIMESTAMP('%s'), %d, %d, '%s', '%s', '%s', '%s', '%s' )",
243 $comment->cid, $comment->nid, $comment->pid, $comment->uid, $comment->subject, $comment->comment, $comment->format,
244 $comment->hostname, $comment->timestamp, $status, $score, $users, $thread, $comment->name, $comment->mail, $comment->homepage);
245
246 _comment_update_node_statistics($comment->nid);
247 }
248 }
249 </MTComments>
250
251 }
252 </MTEntries>
253 ?>

  ViewVC Help
Powered by ViewVC 1.1.2