File: import_forums.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (109 lines) | stat: -rw-r--r-- 3,797 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

require_once '../env.inc.php';
require_once $gfwww.'include/pre.php';
require_once $gfcommon.'forum/Forum.class.php';
require_once $gfcommon.'forum/ForumMessage.class.php';
require_once $gfcommon.'forum/ForumMessageFactory.class.php';
require_once $gfwww.'forum/include/AttachManager.class.php';

class Forums {

	function __construct($forums, $group_id, $msgonly = false) {
		$this->forums = $forums;
		$this->msgonly = $msgonly;
		$this->group_id = $group_id;
		$group = group_get_object($group_id);
		if (!$group || !is_object($group)) {
			print "error retrieving group from id";
		} elseif ($group->isError()) {
			print "error";
		}
		$this->group = $group;
	}

	function add_monitoring_users($users, $forumObject){
		foreach($users as $user){
			$uid = user_get_object_by_name($user)->getID();
			$forumObject->setMonitor($uid);
		}
	}

	//recursive
	//content is a list of messages

	function forum_fill_content($content, $forumObject, $parentMessageId = '', $thread_id = ''){
		foreach ($content as $message){
			$messageObject = new ForumMessage($forumObject);
			$msg_time = strtotime($message['date']);
			$attachment = false;
			if (array_key_exists('name', $message['attachment'])){
				$attachment = true;
			}
			$messageObject->create($message['subject'], $message['content'], $thread_id, $parentMessageId, $attachment, $msg_time);
			if ($attachment == true){
				$am = new AttachManager();
				$am->SetForumMsg($messageObject);
				$am->Setmsgid($messageObject->getID());
				$userid = $messageObject->getPosterID();
				$path = '/tmp/' . $message['attachment']['url'];
				$filename = addslashes($message['attachment']['name']);
				$filedata = file_get_contents($path);
				$fs = filesize($path);
				$finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // Retourne le type mime
				if (!$finfo) {
					echo "error opening fileinfo";
					exit();
				}
				$mimetype = $finfo->file($path);
				$filehash = md5($filedata);
				$am->AddToDBOnly($userid, $msg_time, $filename, base64_encode($filedata), $fs, 1, $filehash, $mimetype);
			}

			if (count($message['children']) != 0){
				$this->forum_fill_content($message['children'], $forumObject, $messageObject->getID(), $messageObject->getThreadID());
			}
		}
		return $forumObject;
	}

	function create_forum($forum){
		$forumObject = new Forum($this->group);
		if ($this->msgonly){
			$forumObject->create($forum['summary'], $forum['news_content'],1,'',0,0);
			$this->forum_fill_content($forum['forum'], $forumObject);
			return $forumObject->getID();
		}else{
			$bostr = array('Yes'=>0,'No'=>1);
			$modlev = array('No Moderation'=>0, 'Moderation Level 1'=>1, 'Moderation Level 2'=>2);
			$forumObject->create($forum['name'], $forum['description'], $bostr[$forum['admin']['is_public']], $forum['admin']['email_posts_to'], 1, $bostr[$forum['admin']['allow_anonymous_posts']], $modlev[$forum['admin']['moderation_level']]);
			$fFact = new ForumMessageFactory($forumObject);
			$thread = $fFact->getFlat();
			$initMsg=$thread[0];
			$initMsg->delete();
			//Monitoring users can be skipped seeing it's the only part of a project where monitoring users are imported, as of Aug.2010
			$this->add_monitoring_users($forum['monitoring_users'], $forumObject);
			$this->forum_fill_content($forum['content'], $forumObject);
		}
	}

	/**
	 * deleteForums - Delete all existing default forums from a projet
	 */
	function deleteForums(){
		$res = db_query_params ('SELECT group_forum_id FROM forum_group_list
				WHERE group_id=$1',
		array ($this->group_id));
		while($row=db_fetch_array($res)){
			$f = new Forum($this->group, $row['group_forum_id']);
			$f->delete(true,true);
		}
	}

	function forums_fill(){
		$this->deleteForums();
		foreach($this->forums as $forum){
			$this->create_forum($forum);
		}
	}
}