Skip to:
Content

bbPress.org

Ticket #2421: e107v1.php

File e107v1.php, 15.8 KB (added by netweb, 11 years ago)
Line 
1<?php
2
3/**
4 * Implementation of e107 v1.x Forum converter.
5 *
6 * @since bbPress (rXXXX)
7 * @link Codex Docs http://codex.bbpress.org/import-forums/e107
8 */
9class e107v1 extends BBP_Converter_Base {
10
11        /**
12         * Main Constructor
13         *
14         * @uses e107v1::setup_globals()
15         */
16        function __construct() {
17                parent::__construct();
18                $this->setup_globals();
19        }
20
21        /**
22         * Sets up the field mappings
23         */
24        public function setup_globals() {
25
26                /** Forum Section *****************************************************/
27
28                // Forum id (Stored in postmeta)
29                $this->field_map[] = array(
30                        'from_tablename' => 'forum',
31                        'from_fieldname' => 'forum_id',
32                        'to_type'        => 'forum',
33                        'to_fieldname'   => '_bbp_forum_id'
34                );
35
36                // Forum parent id (If no parent, then 0, Stored in postmeta)
37                $this->field_map[] = array(
38                        'from_tablename'  => 'forum',
39                        'from_fieldname'  => 'forum_parent',
40                        'to_type'         => 'forum',
41                        'to_fieldname'    => '_bbp_forum_parent_id'
42                );
43
44                // Forum topic count (Stored in postmeta)
45                $this->field_map[] = array(
46                        'from_tablename' => 'forum',
47                        'from_fieldname' => 'forum_threads',
48                        'to_type'        => 'forum',
49                        'to_fieldname'   => '_bbp_topic_count'
50                );
51
52                // Forum reply count (Stored in postmeta)
53                $this->field_map[] = array(
54                        'from_tablename' => 'forum',
55                        'from_fieldname' => 'forum_replies',
56                        'to_type'        => 'forum',
57                        'to_fieldname'   => '_bbp_reply_count'
58                );
59
60                // Forum total topic count (Includes unpublished topics, Stored in postmeta)
61                $this->field_map[] = array(
62                        'from_tablename' => 'forum',
63                        'from_fieldname' => 'forum_threads',
64                        'to_type'        => 'forum',
65                        'to_fieldname'   => '_bbp_total_topic_count'
66                );
67
68                // Forum total reply count (Includes unpublished replies, Stored in postmeta)
69                $this->field_map[] = array(
70                        'from_tablename' => 'forum',
71                        'from_fieldname' => 'forum_replies',
72                        'to_type'        => 'forum',
73                        'to_fieldname'   => '_bbp_total_reply_count'
74                );
75
76                // Forum title.
77                $this->field_map[] = array(
78                        'from_tablename' => 'forum',
79                        'from_fieldname' => 'forum_name',
80                        'to_type'        => 'forum',
81                        'to_fieldname'   => 'post_title'
82                );
83
84                // Forum slug (Clean name to avoid conflicts)
85                $this->field_map[] = array(
86                        'from_tablename'  => 'forum',
87                        'from_fieldname'  => 'forum_name',
88                        'to_type'         => 'forum',
89                        'to_fieldname'    => 'post_name',
90                        'callback_method' => 'callback_slug'
91                );
92
93                // Forum description.
94                $this->field_map[] = array(
95                        'from_tablename'  => 'forum',
96                        'from_fieldname'  => 'forum_description',
97                        'to_type'         => 'forum',
98                        'to_fieldname'    => 'post_content',
99                        'callback_method' => 'callback_null'
100                );
101
102                // Forum display order (Starts from 1)
103                $this->field_map[] = array(
104                        'from_tablename' => 'forum',
105                        'from_fieldname' => 'forum_order',
106                        'to_type'        => 'forum',
107                        'to_fieldname'   => 'menu_order'
108                );
109
110                // Forum type (Category = 0 or Forum > 0, Stored in postmeta)
111                $this->field_map[] = array(
112                        'from_tablename'  => 'forum',
113                        'from_fieldname'  => 'forum_parent',
114                        'to_type'         => 'forum',
115                        'to_fieldname'    => '_bbp_forum_type',
116                        'callback_method' => 'callback_forum_type'
117                );
118
119                // Forum dates.
120                $this->field_map[] = array(
121                        'to_type'      => 'forum',
122                        'to_fieldname' => 'post_date',
123                        'default'      => date('Y-m-d H:i:s')
124                );
125                $this->field_map[] = array(
126                        'to_type'      => 'forum',
127                        'to_fieldname' => 'post_date_gmt',
128                        'default'      => date('Y-m-d H:i:s')
129                );
130                $this->field_map[] = array(
131                        'to_type'      => 'forum',
132                        'to_fieldname' => 'post_modified',
133                        'default'      => date('Y-m-d H:i:s')
134                );
135                $this->field_map[] = array(
136                        'to_type'      => 'forum',
137                        'to_fieldname' => 'post_modified_gmt',
138                        'default'      => date('Y-m-d H:i:s')
139                );
140
141                /** Topic Section *****************************************************/
142
143                // Topic id (Stored in postmeta)
144                $this->field_map[] = array(
145                        'from_tablename'  => 'forum_t',
146                        'from_fieldname'  => 'thread_id',
147                        'from_expression' => 'WHERE thread_parent = 0',
148                        'to_type'         => 'topic',
149                        'to_fieldname'    => '_bbp_topic_id'
150                );
151
152                // Topic reply count (Stored in postmeta)
153                $this->field_map[] = array(
154                        'from_tablename'  => 'forum_t',
155                        'from_fieldname'  => 'thread_total_replies',
156                        'to_type'         => 'topic',
157                        'to_fieldname'    => '_bbp_reply_count',
158                        'callback_method' => 'callback_topic_reply_count'
159                );
160
161                // Topic total reply count (Includes unpublished replies, Stored in postmeta)
162                $this->field_map[] = array(
163                        'from_tablename'  => 'forum_t',
164                        'from_fieldname'  => 'thread_total_replies',
165                        'to_type'         => 'topic',
166                        'to_fieldname'    => '_bbp_total_reply_count',
167                        'callback_method' => 'callback_topic_reply_count'
168                );
169
170                // Topic parent forum id (If no parent, then 0. Stored in postmeta)
171                $this->field_map[] = array(
172                        'from_tablename'  => 'forum_t',
173                        'from_fieldname'  => 'thread_forum_id',
174                        'to_type'         => 'topic',
175                        'to_fieldname'    => '_bbp_forum_id',
176                        'callback_method' => 'callback_forumid'
177                );
178
179                // Topic author.
180                // Note: Uses a custom callback to transform user id from '1.Administrator e107v1' to numeric user id.
181                $this->field_map[] = array(
182                        'from_tablename'  => 'forum_t',
183                        'from_fieldname'  => 'thread_user',
184                        'to_type'         => 'topic',
185                        'to_fieldname'    => 'post_author',
186                        'callback_method' => 'callback_e107v1_userid'
187                );
188
189                // Topic content.
190                $this->field_map[] = array(
191                        'from_tablename'  => 'forum_t',
192                        'from_fieldname'  => 'thread_thread',
193                        'to_type'         => 'topic',
194                        'to_fieldname'    => 'post_content',
195                        'callback_method' => 'callback_html'
196                );
197
198                // Topic title.
199                $this->field_map[] = array(
200                        'from_tablename' => 'forum_t',
201                        'from_fieldname' => 'thread_name',
202                        'to_type'        => 'topic',
203                        'to_fieldname'   => 'post_title'
204                );
205
206                // Topic slug (Clean name to avoid conflicts)
207                $this->field_map[] = array(
208                        'from_tablename'  => 'forum_t',
209                        'from_fieldname'  => 'thread_name',
210                        'to_type'         => 'topic',
211                        'to_fieldname'    => 'post_name',
212                        'callback_method' => 'callback_slug'
213                );
214
215                // Topic parent forum id (If no parent, then 0)
216                $this->field_map[] = array(
217                        'from_tablename'  => 'forum_t',
218                        'from_fieldname'  => 'thread_forum_id',
219                        'to_type'         => 'topic',
220                        'to_fieldname'    => 'post_parent',
221                        'callback_method' => 'callback_forumid'
222                );
223
224                // Topic dates.
225                $this->field_map[] = array(
226                        'from_tablename'  => 'forum_t',
227                        'from_fieldname'  => 'thread_datestamp',
228                        'to_type'         => 'topic',
229                        'to_fieldname'    => 'post_date',
230                        'callback_method' => 'callback_datetime'
231                );
232                $this->field_map[] = array(
233                        'from_tablename'  => 'forum_t',
234                        'from_fieldname'  => 'thread_datestamp',
235                        'to_type'         => 'topic',
236                        'to_fieldname'    => 'post_date_gmt',
237                        'callback_method' => 'callback_datetime'
238                );
239                $this->field_map[] = array(
240                        'from_tablename'  => 'forum_t',
241                        'from_fieldname'  => 'thread_datestamp',
242                        'to_type'         => 'topic',
243                        'to_fieldname'    => 'post_modified',
244                        'callback_method' => 'callback_datetime'
245                );
246                $this->field_map[] = array(
247                        'from_tablename'  => 'forum_t',
248                        'from_fieldname'  => 'thread_datestamp',
249                        'to_type'         => 'topic',
250                        'to_fieldname'    => 'post_modified_gmt',
251                        'callback_method' => 'callback_datetime'
252                );
253                $this->field_map[] = array(
254                        'from_tablename'  => 'forum_t',
255                        'from_fieldname'  => 'thread_datestamp',
256                        'to_type'         => 'topic',
257                        'to_fieldname'    => '_bbp_last_active_time',
258                        'callback_method' => 'callback_datetime'
259                );
260
261                // Topic status (Open or Closed, e107 v1.x open = 1 & closed = 0)
262                $this->field_map[] = array(
263                        'from_tablename'  => 'forum_t',
264                        'from_fieldname'  => 'thread_active',
265                        'to_type'         => 'topic',
266                        'to_fieldname'    => 'post_status',
267                        'callback_method' => 'callback_topic_status'
268                );
269
270                /** Tags Section ******************************************************/
271
272                /**
273                 * e107 v1.x Forums do not support topic tags out of the box
274                 */
275
276                /** Reply Section *****************************************************/
277
278                // Reply id (Stored in postmeta)
279                $this->field_map[] = array(
280                        'from_tablename'  => 'forum_t',
281                        'from_fieldname'  => 'thread_id',
282                        'from_expression' => 'WHERE thread_parent != 0',
283                        'to_type'         => 'reply',
284                        'to_fieldname'    => '_bbp_post_id'
285                );
286
287                // Reply parent forum id (If no parent, then 0. Stored in postmeta)
288                $this->field_map[] = array(
289                        'from_tablename'  => 'forum_t',
290                        'from_fieldname'  => 'thread_forum_id',
291                        'to_type'         => 'reply',
292                        'to_fieldname'    => '_bbp_forum_id',
293                        'callback_method' => 'callback_topicid_to_forumid'
294                );
295
296                // Reply parent topic id (If no parent, then 0. Stored in postmeta)
297                $this->field_map[] = array(
298                        'from_tablename'  => 'forum_t',
299                        'from_fieldname'  => 'thread_parent',
300                        'to_type'         => 'reply',
301                        'to_fieldname'    => '_bbp_topic_id',
302                        'callback_method' => 'callback_topicid'
303                );
304
305                // Reply author.
306                // Note: Uses a custom callback to transform user id from '1.Administrator e107v1' to numeric user id.
307                $this->field_map[] = array(
308                        'from_tablename'  => 'forum_t',
309                        'from_fieldname'  => 'thread_user',
310                        'to_type'         => 'reply',
311                        'to_fieldname'    => 'post_author',
312                        'callback_method' => 'callback_e107v1_userid'
313                );
314
315                // Reply content.
316                $this->field_map[] = array(
317                        'from_tablename'  => 'forum_t',
318                        'from_fieldname'  => 'thread_thread',
319                        'to_type'         => 'reply',
320                        'to_fieldname'    => 'post_content',
321                        'callback_method' => 'callback_html'
322                );
323
324                // Reply parent topic id (If no parent, then 0)
325                $this->field_map[] = array(
326                        'from_tablename'  => 'forum_t',
327                        'from_fieldname'  => 'thread_parent',
328                        'to_type'         => 'reply',
329                        'to_fieldname'    => 'post_parent',
330                        'callback_method' => 'callback_topicid'
331                );
332
333                // Reply dates.
334                $this->field_map[] = array(
335                        'from_tablename'  => 'forum_t',
336                        'from_fieldname'  => 'thread_datestamp',
337                        'to_type'         => 'reply',
338                        'to_fieldname'    => 'post_date',
339                        'callback_method' => 'callback_datetime'
340                );
341                $this->field_map[] = array(
342                        'from_tablename'  => 'forum_t',
343                        'from_fieldname'  => 'thread_datestamp',
344                        'to_type'         => 'reply',
345                        'to_fieldname'    => 'post_date_gmt',
346                        'callback_method' => 'callback_datetime'
347                );
348                $this->field_map[] = array(
349                        'from_tablename'  => 'forum_t',
350                        'from_fieldname'  => 'thread_datestamp',
351                        'to_type'         => 'reply',
352                        'to_fieldname'    => 'post_modified',
353                        'callback_method' => 'callback_datetime'
354                );
355                $this->field_map[] = array(
356                        'from_tablename'  => 'forum_t',
357                        'from_fieldname'  => 'thread_datestamp',
358                        'to_type'         => 'reply',
359                        'to_fieldname'    => 'post_modified_gmt',
360                        'callback_method' => 'callback_datetime'
361                );
362
363                /** User Section ******************************************************/
364
365                // Store old User id (Stored in usermeta)
366                $this->field_map[] = array(
367                        'from_tablename' => 'user',
368                        'from_fieldname' => 'user_id',
369                        'to_type'        => 'user',
370                        'to_fieldname'   => '_bbp_user_id'
371                );
372
373                // Store old User password (Stored in usermeta serialized with salt)
374                $this->field_map[] = array(
375                        'from_tablename'  => 'user',
376                        'from_fieldname'  => 'user_password',
377                        'to_type'         => 'user',
378                        'to_fieldname'    => '_bbp_password'
379//                      'callback_method' => 'callback_savepass'
380                );
381
382                // Store old User Salt (This is only used for the SELECT row info for the above password save)
383        //      $this->field_map[] = array(
384        //              'from_tablename' => 'user',
385        //              'from_fieldname' => 'pass',
386        //              'to_type'        => 'user',
387        //              'to_fieldname'   => ''
388        //      );
389
390                // User password verify class (Stored in usermeta for verifying password)
391                $this->field_map[] = array(
392                        'to_type'      => 'user',
393                        'to_fieldname' => '_bbp_class',
394                        'default'      => 'e107v1'
395                );
396
397                // User name.
398                $this->field_map[] = array(
399                        'from_tablename' => 'user',
400                        'from_fieldname' => 'user_loginname',
401                        'to_type'        => 'user',
402                        'to_fieldname'   => 'user_login'
403                );
404
405                // User nice name.
406                $this->field_map[] = array(
407                        'from_tablename' => 'user',
408                        'from_fieldname' => 'user_loginname',
409                        'to_type'        => 'user',
410                        'to_fieldname'   => 'user_nicename'
411                );
412
413                // User email.
414                $this->field_map[] = array(
415                        'from_tablename' => 'user',
416                        'from_fieldname' => 'user_email',
417                        'to_type'        => 'user',
418                        'to_fieldname'   => 'user_email'
419                );
420
421                // User registered.
422                $this->field_map[] = array(
423                        'from_tablename'  => 'user',
424                        'from_fieldname'  => 'user_join',
425                        'to_type'         => 'user',
426                        'to_fieldname'    => 'user_registered',
427                        'callback_method' => 'callback_datetime'
428                );
429
430                // User display name.
431                $this->field_map[] = array(
432                        'from_tablename' => 'user',
433                        'from_fieldname' => 'user_name',
434                        'to_type'        => 'user',
435                        'to_fieldname'   => 'display_name'
436                );
437
438                // Store Signature (Stored in usermeta)
439                $this->field_map[] = array(
440                        'from_tablename'  => 'user',
441                        'from_fieldname'  => 'user_signature',
442                        'to_fieldname'    => '_bbp_e107v1_user_sig',
443                        'to_type'         => 'user',
444                        'callback_method' => 'callback_html'
445                );
446        }
447
448        /**
449         * This method allows us to indicates what is or is not converted for each
450         * converter.
451         */
452        public function info()
453        {
454                return '';
455        }
456
457        /**
458         * This method is to save the salt and password together. That
459         * way when we authenticate it we can get it out of the database
460         * as one value. Array values are auto sanitized by WordPress.
461         */
462        public function callback_savepass( $field, $row )
463        {
464                $pass_array = array( 'hash' => $field, 'salt' => $row['salt'] );
465                return $pass_array;
466        }
467
468        /**
469         * This method is to take the pass out of the database and compare
470         * to a pass the user has typed in.
471         */
472        public function authenticate_pass( $password, $serialized_pass )
473        {
474                $pass_array = unserialize( $serialized_pass );
475                return ( $pass_array['hash'] == md5( md5( $password ). $pass_array['salt'] ) );
476        }
477
478        /**
479         * Translate the forum type from e107 v1.x numeric's to WordPress's strings.
480         *
481         * @param int $status e107 v1.x numeric forum type
482         * @return string WordPress safe
483         */
484        public function callback_forum_type( $status = 0 ) {
485                if ( $status == 0 ) {
486                        $status = 'category';
487                } else {
488                        $status = 'forum';
489                }
490                return $status;
491        }
492
493        /**
494         * Translate the post status from e107 v1.x numeric's to WordPress's strings.
495         *
496         * @param int $status e107 v1.x numeric topic status
497         * @return string WordPress safe
498         */
499        public function callback_topic_status( $status = 1 ) {
500                switch ( $status ) {
501                        case 0 :
502                                $status = 'closed';
503                                break;
504
505                        case 1  :
506                        default :
507                                $status = 'publish';
508                                break;
509                }
510                return $status;
511        }
512
513        /**
514         * Verify the topic/reply count.
515         *
516         * @param int $count e107 v1.x topic/reply counts
517         * @return string WordPress safe
518         */
519        public function callback_topic_reply_count( $count = 1 ) {
520                $count = absint( (int) $count - 1 );
521                return $count;
522        }
523
524        /**
525         * Override the `callback_user` function in 'converter.php' for custom e107v1 imports
526         *
527         * A mini cache system to reduce database calls to user ID's
528         *
529         * @param string $field
530         * @return string
531         */
532        protected function callback_e107v1_userid( $field ) {
533
534                // Strip only the user id from the topic and reply authors
535                $field = preg_replace( '/(\d+?)+\.[\S\s]+/', '$1', $field );
536
537                if ( !isset( $this->map_userid[$field] ) ) {
538                        if ( !empty( $this->sync_table ) ) {
539                                $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_user_id" AND meta_value = "%s" LIMIT 1', $field ) );
540                        } else {
541                                $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" AND meta_value = "%s" LIMIT 1', $field ) );
542                        }
543
544                        if ( !is_null( $row ) ) {
545                                $this->map_userid[$field] = $row->value_id;
546                        } else {
547                                if ( !empty( $_POST['_bbp_converter_convert_users'] ) && ( $_POST['_bbp_converter_convert_users'] == 1 ) ) {
548                                        $this->map_userid[$field] = 0;
549                                } else {
550                                        $this->map_userid[$field] = $field;
551                                }
552                        }
553                }
554                return $this->map_userid[$field];
555        }
556}