From 646cb6794ad5bfbccca63498805c4f78cb563e33 Mon Sep 17 00:00:00 2001
From: Bruno Nocera Zanette <brunonzanette@gmail.com>
Date: Tue, 17 Sep 2013 16:14:08 -0300
Subject: [PATCH] Reorganize Lib's folder structure

This commit reorganized library's folder structure to remove duplicated files and functions's definitions, as it was happening with "queries_definitions.py" file. Also, it were removed 'xml' and 'json' subfolders, and their contents were moved to lib file with "xml_" and "json_" prefixes.

Signed-off-by: Bruno Nocera Zanette <brunonzanette@gmail.com>
---
 lib/json_group_section.py     | 489 ++++++++++++++++++++++++++++++++++
 lib/json_support_functions.py | 148 ++++++++++
 lib/json_user_section.py      | 459 +++++++++++++++++++++++++++++++
 lib/queries_definition.py     | 219 +++++++++++++++
 lib/xml_group_section.py      | 382 ++++++++++++++++++++++++++
 lib/xml_support_functions.py  | 160 +++++++++++
 lib/xml_user_section.py       | 377 ++++++++++++++++++++++++++
 opendata_json_version.py      |  12 +-
 opendata_xml_version.py       |  12 +-
 9 files changed, 2246 insertions(+), 12 deletions(-)
 create mode 100644 lib/json_group_section.py
 create mode 100644 lib/json_support_functions.py
 create mode 100644 lib/json_user_section.py
 create mode 100644 lib/queries_definition.py
 create mode 100644 lib/xml_group_section.py
 create mode 100644 lib/xml_support_functions.py
 create mode 100644 lib/xml_user_section.py

diff --git a/lib/json_group_section.py b/lib/json_group_section.py
new file mode 100644
index 0000000..f0bb2b8
--- /dev/null
+++ b/lib/json_group_section.py
@@ -0,0 +1,489 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
+# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+#
+# This file is part of participatorio/opendata
+#
+# participatorio/opendata is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+import MySQLdb
+
+import queries_definition as qry
+import json_support_functions as wrt
+
+######################################################################
+# Functions that write on JSON file
+
+#-------------------------------------------------------------------#
+def write_groupmembers_subsection (db, json, group_guid):
+    group_members = db.cursor()
+    group_members.execute(qry.qry_group_members, (group_guid,))
+    
+    qty=str(group_members.rowcount)
+    wrt.write_tag(json,2,"quantidadeMembros",qty,",")
+                
+    wrt.write_open_tag(json,2,"membros","[")
+    
+    row=0
+    for (user_id, user_name, user_username)\
+        in group_members:
+            
+        row=row+1
+            
+        wrt.write_open_tag(json,3,"","{")
+        
+        prefix='profile/'
+        user_attr=wrt.urlparticipa(prefix,user_username)
+        
+        wrt.write_open_tag(json,4,"usuario","{")
+        wrt.write_tag(json,5,"uid",user_attr,",")
+        wrt.write_tag(json,5,"nome",user_name,"")
+        wrt.write_close_tag(json,4,"}",False)
+        
+        wrt.write_close_tag(json,3,"}",(row < group_members.rowcount))
+        
+    wrt.write_close_tag(json,2,"]",True)
+    
+    group_members.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupfiles_subsection (db, json, group_guid):
+    group_files = db.cursor()
+    
+    # 1 = select * from elgg_entity_subtypes where subtype='file';
+    group_files.execute(qry.qry_group_posts, (group_guid, 1,))
+    
+    # 50 = select * from elgg_metastrings where string='file_enable';
+    perm=qry.postcontent_permission(db, group_guid, 50)
+    
+    wrt.write_tag(json,2,"arquivosHabilitado",perm,",")
+    wrt.write_open_tag(json,2,"arquivos","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, \
+            owner_id, owner_name, owner_username, time)\
+        in group_files:
+            
+        row=row+1
+        
+        wrt.write_open_tag(json,3,"","{")
+        
+        prefix='file/download/'
+        file_link=wrt.urlparticipa(prefix,str(post_guid))
+        
+        prefix='file/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        wrt.write_tag(json,4,"pid",post_attr,",")
+
+        prefix='profile/'
+        owner_attr=wrt.urlparticipa(prefix,owner_username)
+        
+        wrt.write_open_tag(json,4,"autor","{")
+        wrt.write_tag(json,5,"uid",owner_attr,",")
+        wrt.write_tag(json,5,"nome",owner_name,"")
+        wrt.write_close_tag(json,4,"}",True)
+        
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"link",file_link,",")
+        wrt.write_tag(json,4,"descricao",post_desc,",")
+                    
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < group_files.rowcount))
+        
+    wrt.write_close_tag(json,2,"]",True)
+    
+    group_files.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupforumtopics_subsection (db, json, group_guid):
+    group_forumtopics = db.cursor()
+    
+    # 7 = select * from elgg_entity_subtypes where subtype='groupforumtopic';
+    group_forumtopics.execute(qry.qry_group_posts, (group_guid, 7,))
+    
+    # 52 = select * from elgg_metastrings where string='forum_enable';
+    perm=qry.postcontent_permission(db, group_guid, 52)
+    
+    wrt.write_tag(json,2,"debatesHabilitado",perm,",")
+    wrt.write_open_tag(json,2,"debates","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, \
+        owner_id, owner_name, owner_username, time)\
+        in group_forumtopics:
+            
+        row=row+1
+        
+        wrt.write_open_tag(json,3,"","{")
+        
+        prefix='discussion/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        wrt.write_tag(json,4,"pid",post_attr,",")
+
+        prefix='profile/'
+        owner_attr=wrt.urlparticipa(prefix,owner_username)
+        
+        wrt.write_open_tag(json,4,"autor","{")
+        wrt.write_tag(json,5,"uid",owner_attr,",")
+        wrt.write_tag(json,5,"nome",owner_name,"")
+        wrt.write_close_tag(json,4,"}",True)
+        
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"texto",post_desc,",")
+            
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < group_forumtopics.rowcount))
+        
+    wrt.write_close_tag(json,2,"]",True)
+    
+    group_forumtopics.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupbookmarks_subsection (db, json, group_guid):
+    group_bookmarks = db.cursor()
+    
+    # 13 = select * from elgg_entity_subtypes where subtype='bookmarks';
+    group_bookmarks.execute(qry.qry_group_posts, (group_guid, 13,))
+    
+    # 49 = select * from elgg_metastrings where string='bookmarks_enable';
+    perm=qry.postcontent_permission(db, group_guid, 49)
+    
+    wrt.write_tag(json,2,"favoritosHabilitado",perm,",")
+    wrt.write_open_tag(json,2,"favoritos","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, \
+            owner_id, owner_name, owner_username, time)\
+        in group_bookmarks:
+            
+        row=row+1
+        
+        wrt.write_open_tag(json,3,"","{")
+        
+        # 90 = select * from elgg_metastrings where string='address';
+        bookmark_link=qry.post_content(db,post_guid,90)
+        
+        prefix='bookmarks/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        wrt.write_tag(json,4,"pid",post_attr,",")
+
+        prefix='profile/'
+        owner_attr=wrt.urlparticipa(prefix,owner_username)
+        
+        wrt.write_open_tag(json,4,"autor","{")
+        wrt.write_tag(json,5,"uid",owner_attr,",")
+        wrt.write_tag(json,5,"nome",owner_name,"")
+        wrt.write_close_tag(json,4,"}",True)
+        
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"link",bookmark_link,",")
+        wrt.write_tag(json,4,"descricao",post_desc,",")
+                            
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < group_bookmarks.rowcount))
+    
+    wrt.write_close_tag(json,2,"]",True)
+    
+    group_bookmarks.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_grouppages_subsection (db, json, group_guid):
+    group_pages = db.cursor()
+    
+    # 14 = select * from elgg_entity_subtypes where subtype='page_top';
+    group_pages.execute(qry.qry_group_posts, (group_guid, 14,))
+    
+    # 53 = select * from elgg_metastrings where string='pages_enable';
+    perm=qry.postcontent_permission(db, group_guid, 53)
+    
+    wrt.write_tag(json,2,"paginasHabilitado",perm,",")
+    wrt.write_open_tag(json,2,"paginas","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc,
+            owner_id, owner_name, owner_username, time)\
+        in group_pages:
+            
+        row=row+1
+        
+        wrt.write_open_tag(json,3,"","{")
+        
+        prefix='pages/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        wrt.write_tag(json,4,"pid",post_attr,",")
+
+        prefix='profile/'
+        owner_attr=wrt.urlparticipa(prefix,owner_username)
+        
+        wrt.write_open_tag(json,4,"autor","{")
+        wrt.write_tag(json,5,"uid",owner_attr,",")
+        wrt.write_tag(json,5,"nome",owner_name,"")
+        wrt.write_close_tag(json,4,"}",True)
+        
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"texto",post_desc,",")
+                    
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < group_pages.rowcount))
+        
+    wrt.write_close_tag(json,2,"]",True)
+    
+    group_pages.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupvideos_subsection (db, json, group_guid):
+    group_videos = db.cursor()
+    
+    # 12 = select * from elgg_entity_subtypes where subtype='videos';
+    group_videos.execute(qry.qry_group_posts, (group_guid, 12,))
+    
+    # 399 = select * from elgg_metastrings where string='videos_enable';
+    perm=qry.postcontent_permission(db, group_guid, 399)
+    
+    wrt.write_tag(json,2,"videosHabilitado",perm,",")
+    wrt.write_open_tag(json,2,"videos","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, \
+            owner_id, owner_name, owner_username, time)\
+        in group_videos:
+            
+        row=row+1
+            
+        # 477 = select * from elgg_metastrings where string='video_url';
+        video_link=qry.post_content(db,post_guid, 477)
+        
+        wrt.write_open_tag(json,3,"","{")
+            
+        prefix='videos/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        wrt.write_tag(json,4,"pid",post_attr,",")
+        
+        prefix='profile/'
+        owner_attr=wrt.urlparticipa(prefix,owner_username)
+        
+        wrt.write_open_tag(json,4,"autor","{")
+        wrt.write_tag(json,5,"uid",owner_attr,",")
+        wrt.write_tag(json,5,"nome",owner_name,"")
+        wrt.write_close_tag(json,4,"}",True)
+        
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"link",video_link,",")
+        wrt.write_tag(json,4,"descricao",post_desc,",")
+            
+        wrt.write_comments(db,json,post_guid)
+
+        wrt.write_close_tag(json,3,"}",(row < group_videos.rowcount))
+        
+    wrt.write_close_tag(json,2,"]",True)
+    
+    group_videos.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupevents_subsection (db, json, group_guid):
+    group_events = db.cursor()
+    
+    # 6 = select * from elgg_entity_subtypes where subtype='calendar_event';
+    group_events.execute(qry.qry_group_posts, (group_guid, 6,))
+    
+    # 54 = select * from elgg_metastrings where string='event_calendar_enable';
+    perm=qry.postcontent_permission(db, group_guid, 54)
+    
+    wrt.write_tag(json,2,"eventosHabilitado",perm,",")
+    wrt.write_open_tag(json,2,"eventos","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, \
+            owner_id, owner_name, owner_username, time)\
+        in group_events:
+            
+        row=row+1
+            
+        wrt.write_open_tag(json,3,"","{")
+            
+        # 18 = select * from elgg_metastrings where string='venue';
+        venue=qry.post_content(db, post_guid, 18)
+        
+        # 20 = select * from elgg_metastrings where string='start_date';
+        time_start=qry.post_content(db, post_guid, 20)
+
+        # 22 = select * from elgg_metastrings where string='end_date';
+        time_end=qry.post_content(db, post_guid, 22)
+        
+        # 26 = select * from elgg_metastrings where string='fees';
+        fees=qry.post_content(db, post_guid, 26)
+        
+        # 28 = select * from elgg_metastrings where string='contact';
+        contact=qry.post_content(db, post_guid, 28)
+        
+        # 30 = select * from elgg_metastrings where string='organizer';
+        organizer=qry.post_content(db, post_guid, 30)
+
+        prefix='event_calendar/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        wrt.write_tag(json,4,"pid",post_attr,",")
+                
+        prefix='profile/'
+        owner_attr=wrt.urlparticipa(prefix,owner_username)
+        
+        wrt.write_open_tag(json,4,"autor","{")
+        wrt.write_tag(json,5,"uid",owner_attr,",")
+        wrt.write_tag(json,5,"nome",owner_name,"")
+        wrt.write_close_tag(json,4,"}",True)
+        
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"organizador",organizer,",")
+        wrt.write_tag(json,4,"contato",contact,",")
+        wrt.write_tag(json,4,"endereco",venue,",")
+        wrt.write_tag(json,4,"dataInicio",wrt.datestr(time_start),",")
+        wrt.write_tag(json,4,"dataFim",wrt.datestr(time_end),",")
+        wrt.write_tag(json,4,"taxaParticipacao",fees,",")
+        wrt.write_tag(json,4,"descricao",post_desc,",")
+        
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < group_events.rowcount))
+    
+    wrt.write_close_tag(json,2,"]",False)
+    
+    group_events.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groups_section (db, json,\
+    guid, title, desc, owner_id, owner_name, owner_username, time):
+    
+    # 45 = select * from elgg_metastrings where string='briefdescription';
+    brief_desc=qry.post_content(db,guid, 45)
+        
+    prefix='groups/profile/'
+    group_attr=wrt.urlparticipa(prefix,str(guid))
+    wrt.write_tag(json,2,"cid",group_attr,",")
+    
+    # Write all group's information
+    prefix='profile/'
+    owner_attr=wrt.urlparticipa(prefix,owner_username)
+    
+    wrt.write_open_tag(json,2,"proprietario","{")
+    wrt.write_tag(json,3,"uid",owner_attr,",")
+    wrt.write_tag(json,3,"nome",owner_name,"")
+    wrt.write_close_tag(json,2,"}",True)
+            
+    wrt.write_tag(json,2,"titulo",title,",")
+    wrt.write_tag(json,2,"data",wrt.datestr(time),",")
+    wrt.write_tag(json,2,"descricao",desc,",")
+
+    group_access = qry.groupaccess_permission(db, guid)
+    
+    if group_access == 'public':
+        comma=","
+    else:
+        comma=""
+        
+    wrt.write_tag(json,2,"breveDescricao",brief_desc,comma)
+                                        
+    if group_access == 'public':
+        
+        # Write a list of group member's name
+        write_groupmembers_subsection(db, json, guid)
+    
+        # Write a list, and all the info, of all posts made on the group.
+        write_groupfiles_subsection(db, json, guid)
+        write_groupforumtopics_subsection(db, json, guid)
+        write_groupbookmarks_subsection(db, json, guid)
+        write_grouppages_subsection(db, json, guid)
+        write_groupvideos_subsection(db, json, guid)
+        write_groupevents_subsection(db, json, guid)
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_singlefile_groups_section (db, dir_results):
+
+    groups_info = db.cursor()
+    groups_info.execute(qry.qry_groups_info)
+
+    json_filename=dir_results+wrt.date_today()+"_comunidades"+".json"
+    json = wrt.open_json_file(json_filename)
+    
+    wrt.write_open_tag(json,0,"","{")
+    wrt.write_open_tag(json,0,"comunidades","[")
+    
+    row=0
+    for (guid, title, desc, owner_id, owner_name, owner_username, time)\
+        in groups_info:
+            
+        row=row+1
+        
+        wrt.write_open_tag(json,1,"","{")
+            
+        write_groups_section(db,json,\
+            guid,title,desc,owner_id,owner_name,owner_username,time)
+            
+        wrt.write_close_tag(json,1,"}",(row < groups_info.rowcount))
+        
+    wrt.write_close_tag(json,0,"]",False)
+    wrt.write_close_tag(json,0,"}",False)
+    
+    groups_info.close()
+    
+    json.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_multifile_groups_section (db, dir_results):
+
+    groups_info = db.cursor()
+    groups_info.execute(qry.qry_groups_info)
+
+    for (guid, title, desc, owner_id, owner_name, owner_username, time)\
+        in groups_info:
+    
+        json_filename=dir_results+'/groups/'+str(guid)+'.json'
+        json = wrt.open_json_file(json_filename)
+        
+        wrt.write_open_tag(json,0,"","{")
+        wrt.write_open_tag(json,1,"usuario","{")
+            
+        write_groups_section(db,json,\
+            guid,title,desc,owner_id,owner_name,owner_username,time)
+            
+        wrt.write_close_tag(json,1,"}",False)
+        wrt.write_close_tag(json,0,"}",False)
+        
+        json.close()
+    
+    groups_info.close()
+#--------------------------------------------------------------------#
+
+######################################################################
diff --git a/lib/json_support_functions.py b/lib/json_support_functions.py
new file mode 100644
index 0000000..88e416c
--- /dev/null
+++ b/lib/json_support_functions.py
@@ -0,0 +1,148 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
+# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+#
+# This file is part of participatorio/opendata
+#
+# participatorio/opendata is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+import MySQLdb
+import codecs
+import datetime
+import base64
+
+import queries_definition as qry
+
+######################################################################
+# Support functions:
+
+#--------------------------------------------------------------------#
+def open_json_file (json_filename):
+    json_file = codecs.open(json_filename,'w',encoding='utf-8')
+    return json_file
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def date_today():
+    date = str(datetime.date.today())
+    return date
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def lvl (l):
+    if l == 1:
+        return "\t"
+    elif l == 2:
+        return "\t\t"
+    elif l == 3:
+        return "\t\t\t"
+    elif l == 4:
+        return "\t\t\t\t"
+    elif l == 5:
+        return "\t\t\t\t\t"
+    elif l == 6:
+        return "\t\t\t\t\t\t"
+    elif l == 7:
+        return "\t\t\t\t\t\t\t"
+    else:
+        return ""
+#--------------------------------------------------------------------#    
+
+#--------------------------------------------------------------------#    
+def substbadc (string):
+    string = string.replace('\\','\\\\')
+    string = string.replace('"','\\"')
+    string = string.replace('\t',' ')
+    string = string.replace('\n',' ')
+    string = string.replace('\r',' ')
+    return string
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def urlparticipa (prefix, guid):
+    http_str="http://participatorio.juventude.gov.br/"
+    url_participa=http_str+prefix+guid
+    return url_participa
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def datestr (time):
+    if time != '':
+        date=str(datetime.datetime.fromtimestamp(int(time)))
+    else:
+        date=''
+    return date
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_open_tag (xml, l, tag_name, sep):
+    if len(tag_name) > 0:
+        xml.write(lvl(l)+"\""+tag_name+"\""+":"+sep+"\n")
+    else:
+        xml.write(lvl(l)+sep+"\n")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_close_tag (xml, l, sep, comma_flag):
+    if comma_flag == True:
+        xml.write(lvl(l)+sep+","+"\n")
+    else:
+        xml.write(lvl(l)+sep+"\n")    
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_tag (xml, l, tag_name, info_str, comma):
+    name="\""+tag_name+"\""
+    info="\""+substbadc(info_str)+"\""
+    xml.write(lvl(l)+name+":"+info+comma+"\n")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_comments (db, xml, post_guid):
+    post_comments = db.cursor()
+    post_comments.execute(qry.qry_post_comments, (post_guid,))
+    
+    write_open_tag(xml,4,"comentarios","[")
+    
+    row=0
+    for (user_id, user_name, user_username, string, time)\
+        in post_comments:
+        
+        row=row+1
+        
+        write_open_tag(xml,5,"","{")
+        
+        prefix='profile/'
+        user_attr=urlparticipa(prefix,user_username)
+                
+        write_open_tag(xml,6,"usuario","{")
+        write_tag(xml,7,"uid",user_attr,",")
+        write_tag(xml,7,"nome",user_name,"")
+        write_close_tag(xml,6,"}",True)
+        
+        write_tag(xml,6,"data",datestr(time),",")
+        write_tag(xml,6,"mensagem",string,"")
+        
+        write_close_tag(xml,5,"}",(row < post_comments.rowcount))
+        
+    write_close_tag(xml,4,"]",False)
+    
+    post_comments.close()
+#--------------------------------------------------------------------#
+
+######################################################################
diff --git a/lib/json_user_section.py b/lib/json_user_section.py
new file mode 100644
index 0000000..716176e
--- /dev/null
+++ b/lib/json_user_section.py
@@ -0,0 +1,459 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
+# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+#
+# This file is part of participatorio/opendata
+#
+# participatorio/opendata is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+import MySQLdb
+
+import queries_definition as qry
+import json_support_functions as wrt
+
+######################################################################
+# Functions that write on JSON file
+
+#--------------------------------------------------------------------#
+def write_userfriends_subsection (db, json, user_guid):
+    friends_info = db.cursor()
+    friends_info.execute(qry.qry_user_friends, (user_guid))
+    
+    qty=str(friends_info.rowcount)
+    wrt.write_tag(json,2,"quantidadeAmigos",qty,",")
+    
+    wrt.write_open_tag(json,2,"amigos","[")
+    
+    row=0
+    for (friend_id, friend_name, friend_username)\
+        in friends_info:
+        
+        row=row+1
+            
+        wrt.write_open_tag(json,3,"","{")
+        
+        prefix='profile/'
+        friend_attr=wrt.urlparticipa(prefix,friend_username)
+        
+        wrt.write_tag(json,4,"uid",friend_attr,",")
+        wrt.write_tag(json,4,"usuario",friend_name,"")
+        
+        wrt.write_close_tag(json,3,"}",(row < friends_info.rowcount))
+        
+    wrt.write_close_tag(json,2,"]",True)
+        
+    friends_info.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userowngroup_subsection (db, json, user_guid):        
+    user_owngroups = db.cursor()
+    user_owngroups.execute(qry.qry_user_owngroups, (user_guid, user_guid, ))
+        
+    wrt.write_open_tag(json,3,"dono","[")
+    
+    row=0
+    for (group_id, group_title)\
+        in user_owngroups:
+            
+        row=row+1
+            
+        wrt.write_open_tag(json,4,"","{")
+        
+        prefix='groups/profile/'
+        group_attr=wrt.urlparticipa(prefix,str(group_id))
+        
+        wrt.write_tag(json,5,"cid",group_attr,",")
+        wrt.write_tag(json,5,"titulo",group_title,"")
+        
+        wrt.write_close_tag(json,4,"}",(row < user_owngroups.rowcount))
+        
+    wrt.write_close_tag(json,3,"]",True)
+        
+    user_owngroups.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_usermembergroup_subsection (db, json, user_guid):
+    user_membergroups = db.cursor()
+    user_membergroups.execute(qry.qry_user_membergroups, (user_guid, ))
+        
+    wrt.write_open_tag(json,3,"participante","[")
+    
+    row=0
+    for (group_id, group_title)\
+        in user_membergroups:
+            
+        row=row+1
+            
+        wrt.write_open_tag(json,4,"","{")
+        
+        prefix='groups/profile/'
+        group_attr=wrt.urlparticipa(prefix,str(group_id))
+        
+        wrt.write_tag(json,5,"cid",group_attr,",")
+        wrt.write_tag(json,5,"titulo",group_title,"")
+        
+        wrt.write_close_tag(json,4,"}",(row < user_membergroups.rowcount))
+        
+    wrt.write_close_tag(json,3,"]",False)
+        
+    user_membergroups.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_usergroups_subsection (db, json, user_guid):
+    wrt.write_open_tag(json,2,"comunidades","{")
+    write_userowngroup_subsection(db, json, user_guid)
+    write_usermembergroup_subsection(db, json, user_guid)
+    wrt.write_close_tag(json,2,"}",True)
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userfiles_subsection (db, json, user_guid):
+    user_files = db.cursor()
+    
+    # 1 = select * from elgg_entity_subtypes where subtype='file';
+    user_files.execute(qry.qry_user_posts, (user_guid, user_guid, 1,))
+    
+    wrt.write_open_tag(json,2,"arquivos","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, time)\
+        in user_files:
+            
+        row=row+1
+        
+        prefix="file/download/"
+        file_link=wrt.urlparticipa(prefix,str(post_guid))
+        
+        prefix='file/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        
+        wrt.write_open_tag(json,3,"","{")
+        
+        wrt.write_tag(json,4,"pid",post_attr,",")
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"link",file_link,",")
+        wrt.write_tag(json,4,"descricao",post_desc,",")
+            
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < user_files.rowcount))
+    
+    wrt.write_close_tag(json,2,"]",True)
+    
+    user_files.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userblogs_subsection (db, json, user_guid):
+    user_blogs = db.cursor()
+    
+    # 4 = select * from elgg_entity_subtypes where subtype='blog';
+    user_blogs.execute(qry.qry_user_posts, (user_guid, user_guid, 4,))
+    
+    wrt.write_open_tag(json,2,"blogs","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, time)\
+        in user_blogs:
+            
+        row=row+1
+                    
+        post_excerpt = db.cursor()
+        
+        # 64 = select * from elgg_metastrings where string='excerpt';
+        post_excerpt=qry.post_content(db,post_guid,64)
+            
+        prefix='blog/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        wrt.write_open_tag(json,3,"","{")
+
+        wrt.write_tag(json,4,"pid",post_attr,",")
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"resumo",post_excerpt,",")
+        wrt.write_tag(json,4,"texto",post_desc,",")
+                    
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < user_blogs.rowcount))
+            
+    wrt.write_close_tag(json,2,"]",True)
+    
+    user_blogs.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userbookmarks_subsection (db, json, user_guid):
+    user_bookmarks = db.cursor()
+    
+    # 13 = select * from elgg_entity_subtypes where subtype='bookmarks';
+    user_bookmarks.execute(qry.qry_user_posts, (user_guid, user_guid, 13,))
+    
+    wrt.write_open_tag(json,2,"favoritos","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, time)\
+        in user_bookmarks:
+            
+        row=row+1
+                    
+        # 90 = select * from elgg_metastrings where string='address';
+        bookmark_link=qry.post_content(db,post_guid,90)
+  
+        prefix='bookmarks/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        wrt.write_open_tag(json,3,"","{")
+    
+        wrt.write_tag(json,4,"pid",post_attr,",")
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"link",bookmark_link,",")
+        wrt.write_tag(json,4,"descricao",post_desc,",")
+                    
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < user_bookmarks.rowcount))
+                
+    wrt.write_close_tag(json,2,"]",True)
+    
+    user_bookmarks.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#    
+def write_userpages_subsection (db, json, user_guid):
+    user_pages = db.cursor()
+    
+    # 14 = select * from elgg_entity_subtypes where subtype='page_top';
+    user_pages.execute(qry.qry_user_posts, (user_guid, user_guid, 14,))
+    
+    wrt.write_open_tag(json,2,"paginas","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, time)\
+        in user_pages:
+        
+        row=row+1
+        
+        prefix='pages/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        
+        wrt.write_open_tag(json,3,"","{")
+
+        wrt.write_tag(json,4,"pid",post_attr,",")
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"texto",post_desc,",")
+                    
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < user_pages.rowcount))
+        
+    wrt.write_close_tag(json,2,"]",True)
+    
+    user_pages.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_uservideos_subsection (db, json, user_guid):
+    user_videos = db.cursor()
+    
+    # 12 = select * from elgg_entity_subtypes where subtype='videos';
+    user_videos.execute(qry.qry_user_posts, (user_guid, user_guid, 12,))
+    
+    wrt.write_open_tag(json,2,"videos","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, time)\
+        in user_videos:
+        
+        row=row+1
+                    
+        # 477 = select * from elgg_metastrings where string='video_url';
+        video_link=qry.post_content(db, post_guid, 477)
+        
+        prefix='videos/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        
+        wrt.write_open_tag(json,3,"","{")
+        
+        wrt.write_tag(json,4,"pid",post_attr,",")
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"link",video_link,",")
+        wrt.write_tag(json,4,"descricao",post_desc,",")
+        
+        wrt.write_comments(db,json,post_guid)
+        
+        wrt.write_close_tag(json,3,"}",(row < user_videos.rowcount))
+        
+    wrt.write_close_tag(json,2,"]",True)
+    
+    user_videos.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userevents_subsection (db, json, user_guid):
+    user_events = db.cursor()
+    
+    # 6 = select * from elgg_entity_subtypes where subtype='calendar_event';
+    user_events.execute(qry.qry_user_posts, (user_guid, user_guid, 6,))
+    
+    
+    wrt.write_open_tag(json,2,"eventos","[")
+    
+    row=0
+    for (post_guid, post_title, post_desc, time)\
+        in user_events:
+            
+        row=row+1
+            
+        # 18 = select * from elgg_metastrings where string='venue';
+        venue=qry.post_content(db, post_guid, 18)
+        
+        # 20 = select * from elgg_metastrings where string='start_date';
+        time_start=qry.post_content(db, post_guid, 20)
+
+        # 22 = select * from elgg_metastrings where string='end_date';
+        time_end=qry.post_content(db, post_guid, 22)
+        
+        # 26 = select * from elgg_metastrings where string='fees';
+        fees=qry.post_content(db, post_guid, 26)
+        
+        # 28 = select * from elgg_metastrings where string='contact';
+        contact=qry.post_content(db, post_guid, 28)
+        
+        # 30 = select * from elgg_metastrings where string='organizer';
+        organizer=qry.post_content(db, post_guid, 30)
+        
+        prefix='event_calendar/view/'
+        post_attr=wrt.urlparticipa(prefix,str(post_guid))
+        
+        wrt.write_open_tag(json,3,"","{")
+        
+        wrt.write_tag(json,4,"pid",post_attr,",")
+        wrt.write_tag(json,4,"titulo",post_title,",")
+        wrt.write_tag(json,4,"data",wrt.datestr(time),",")
+        wrt.write_tag(json,4,"organizador",organizer,",")
+        wrt.write_tag(json,4,"contato",contact,",")
+        wrt.write_tag(json,4,"endereco",venue,",")
+        wrt.write_tag(json,4,"dataInicio",time_start,",")
+        wrt.write_tag(json,4,"dataFim",time_end,",")
+        wrt.write_tag(json,4,"taxaParticipacao",fees,",")
+        wrt.write_tag(json,4,"descricao",post_desc,",")
+        
+        wrt.write_comments(db,json,post_guid)
+            
+        wrt.write_close_tag(json,3,"}",(row < user_events.rowcount))
+    
+    wrt.write_close_tag(json,2,"]",False)
+    
+    user_events.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_users_section (db, json, \
+    guid, name, username):
+
+    prefix='profile/'
+    user_attr=wrt.urlparticipa(prefix,username)
+    
+    # Write all user's information
+    wrt.write_tag(json,2,"uid",user_attr,",")
+    wrt.write_tag(json,2,"nome",name,",")
+    
+    # Write a list of user friend's names
+    write_userfriends_subsection(db, json, guid)
+    
+    # Write a list of all groups that the user owns or belongs
+    write_usergroups_subsection(db, json, guid)
+    
+    # Write a list, and all the info, of all posts made by the user
+    write_userfiles_subsection(db, json, guid)
+    write_userblogs_subsection(db, json, guid)
+    write_userbookmarks_subsection(db, json, guid)
+    write_userpages_subsection(db, json, guid)
+    write_uservideos_subsection(db, json, guid)
+    write_userevents_subsection(db, json, guid)
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#    
+def write_singlefile_users_section (db, dir_results):
+    
+    users_info = db.cursor()
+    users_info.execute(qry.qry_users_info)
+    
+    return
+
+    json_filename=dir_results+wrt.date_today()+"_usuarios"+".json"
+    json = wrt.open_json_file(json_filename)
+    
+    wrt.write_open_tag(json,0,"","{")
+    wrt.write_open_tag(json,0,"usuarios","[")
+    
+    row=0
+    for (guid, name, username)\
+        in users_info:
+            
+        row=row+1
+        
+        wrt.write_open_tag(json,1,"","{")
+        
+        write_users_section(db,json,\
+            guid,name,username)
+        
+        wrt.write_close_tag(json,1,"}",(row < users_info.rowcount))
+    
+    wrt.write_close_tag(json,0,"]",False)
+    wrt.write_close_tag(json,0,"}",False)
+    
+    users_info.close()
+    
+    json.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#    
+def write_multifile_users_section (db, dir_results):
+
+    users_info = db.cursor()
+    users_info.execute(qry.qry_users_info)
+    
+    for (guid, name, username)\
+        in users_info:
+            
+        json_filename=dir_results+'/users/'+str(guid)+'.json'
+        json = wrt.open_json_file(json_filename)
+            
+        wrt.write_open_tag(json,0,"","{")
+        wrt.write_open_tag(json,1,"usuario","{")
+                
+        write_users_section(db,json,\
+            guid,name,username)
+        
+        wrt.write_close_tag(json,1,"}",False)
+        wrt.write_close_tag(json,0,"}",False)
+        
+        json.close()
+    
+    users_info.close()
+#--------------------------------------------------------------------#
+
+######################################################################
diff --git a/lib/queries_definition.py b/lib/queries_definition.py
new file mode 100644
index 0000000..24ffd64
--- /dev/null
+++ b/lib/queries_definition.py
@@ -0,0 +1,219 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
+# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+#
+# This file is part of participatorio/opendata
+#
+# participatorio/opendata is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+######################################################################
+# Users Section Query definitions:
+
+#--------------------------------------------------------------------#
+# Argument: None
+# Return: All member instances, and its info, from database.
+qry_users_info = \
+("	SELECT u.guid, u.name, u.username \
+	FROM elgg_entities e, elgg_users_entity u \
+	WHERE e.guid = u.guid \
+    AND (e.access_id = 1 OR e.access_id = 2); ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: Group_ID / InfoType_ID
+# Return: All InfoTypes' posts made by this group.
+qry_user_posts = \
+("	SELECT o.guid, o.title, o.description, e.time_created \
+	FROM elgg_entities e, elgg_objects_entity o \
+	WHERE e.guid = o.guid \
+    AND (e.access_id = 1 OR e.access_id = 2) \
+    AND e.owner_guid = %s AND e.container_guid = %s AND e.subtype = %s \
+    ORDER BY e.time_created ASC; ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: User_ID
+# Return: All user friends' name
+qry_user_friends = \
+("	SELECT u.guid, u.name, u.username \
+	FROM elgg_users_entity u, elgg_entity_relationships r \
+	WHERE u.guid = r.guid_two \
+	AND r.guid_one = %s AND r.relationship = 'friend'; ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: User_ID
+# Return: Name of all the groups owned/created by the user.
+qry_user_owngroups = \
+("	SELECT g.guid, g.name \
+	FROM elgg_entities e, elgg_groups_entity g \
+	WHERE e.guid = g.guid \
+	AND e.owner_guid = %s AND e.container_guid = %s; ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: User_ID
+# Return: Name of all the groups that the user is a member.
+qry_user_membergroups = \
+("	SELECT g.guid, g.name \
+	FROM elgg_entity_relationships r, elgg_groups_entity g \
+	WHERE r.guid_two = g.guid \
+	AND r.relationship = 'member' AND r.guid_one = %s; ")
+#--------------------------------------------------------------------#
+
+######################################################################
+
+######################################################################
+# Group Section Query definitions:
+
+#--------------------------------------------------------------------#
+# Argument: None
+# Return: All group instances, and its info, from database.
+qry_groups_info = \
+("	SELECT g.guid, g.name, g.description, \
+        u.guid, u.name, u.username, e.time_created \
+	FROM elgg_entities e, elgg_groups_entity g, elgg_users_entity u\
+	WHERE e.guid = g.guid AND e.owner_guid = u.guid \
+    AND (e.access_id = 1 OR e.access_id = 2) \
+    ORDER BY e.time_created ASC; ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: Group_ID / InfoType_ID
+# Return: All InfoTypes' posts from this group.
+qry_group_posts = \
+("	SELECT o.guid, o.title, o.description, \
+        u.guid, u.name, u.username, e.time_created\
+	FROM elgg_entities e, elgg_objects_entity o, elgg_users_entity u\
+	WHERE e.guid = o.guid AND e.owner_guid = u.guid\
+    AND (e.access_id = 1 OR e.access_id = 2) \
+    AND e.container_guid = %s AND e.subtype = %s \
+    ORDER BY e.time_created ASC; ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: Group_ID
+# Return: Flag containing the access control to become a group member
+qry_group_access_permission = \
+("  SELECT CASE WHEN s.string = '2' THEN 'public' ELSE 'private' END \
+    FROM elgg_metadata d, elgg_metastrings s \
+    WHERE d.value_id = s.id \
+    AND d.entity_guid = %s AND d.name_id = %s; ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: Group_ID / InfoType_ID
+# Return: Yes or No, depending on the permission given by the owner.
+qry_post_content_permission = \
+("  SELECT CASE WHEN s.string = 'yes' THEN 'Sim' ELSE 'Nao' END \
+    FROM elgg_metadata d, elgg_metastrings s \
+    WHERE d.value_id = s.id \
+    AND d.entity_guid = %s AND d.name_id = %s;")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: Group_ID
+# Return: All members' name from this group.
+qry_group_members = \
+("  SELECT u.guid, u.name, u.username \
+    FROM elgg_users_entity u, elgg_entity_relationships r \
+    WHERE u.guid = r.guid_one \
+    AND r.relationship = 'member' AND r.guid_two = %s; ")
+#--------------------------------------------------------------------#
+    
+######################################################################
+
+######################################################################
+
+#--------------------------------------------------------------------#
+# Argument: Post_ID
+# Return: All comments, and its info, made on the post.
+qry_post_comments = \
+("	SELECT u.guid, u.name, u.username, m.string, a.time_created \
+	FROM elgg_annotations a, elgg_metastrings m, elgg_users_entity u \
+	WHERE a.value_id = m.id AND a.owner_guid = u.guid \
+    AND (a.access_id = 1 OR a.access_id = 2) \
+    AND a.entity_guid = %s \
+    ORDER BY a.time_created ASC; ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+# Argument: Post_ID / InfoType_ID
+# Return: Essential information about the group or post, such as
+#   blog's excerpts and bookmark's link
+qry_post_content = \
+("  SELECT s.string\
+    FROM elgg_metadata d, elgg_metastrings s \
+    WHERE d.value_id = s.id \
+    AND d.entity_guid = %s AND d.name_id = %s; ")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def post_content (db, post_guid, content_typeid):
+    
+    content = db.cursor()
+    content.execute(qry_post_content, (post_guid, content_typeid,))
+    
+    if content.rowcount == 1:
+        post_content = content.fetchone()[0]
+    else:
+        post_content=''
+        print "ERRO! Nenhum ou Mais do que um resultado para a query"
+        
+    content.close()
+    
+    return post_content
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def postcontent_permission (db, group_guid, content_id):
+        
+    perm = db.cursor()
+    perm.execute(qry_post_content_permission, (group_guid, content_id,))
+
+    if perm.rowcount == 1:
+       permission=perm.fetchone()[0]
+    else:
+        permission=''
+        print "ERRO! Nenhum ou Mais do que um resultado para a query"
+    
+    perm.close()
+    
+    return permission        
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def groupaccess_permission (db, group_guid):
+        
+    access = db.cursor()
+    
+    # 55 = select * from elgg_metastrings where string = 'membership';
+    access.execute(qry_group_access_permission, (group_guid, 55,))
+
+    if access.rowcount == 1:
+       access_control=access.fetchone()[0]
+    else:
+        access_control=''
+        print "ERRO! Nenhum ou Mais do que um resultado para a query"
+    
+    access.close()
+    
+    return access_control        
+#--------------------------------------------------------------------#
+
+######################################################################
diff --git a/lib/xml_group_section.py b/lib/xml_group_section.py
new file mode 100644
index 0000000..3c800c3
--- /dev/null
+++ b/lib/xml_group_section.py
@@ -0,0 +1,382 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
+# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+#
+# This file is part of participatorio/opendata
+#
+# participatorio/opendata is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+import MySQLdb
+
+import queries_definition as qry
+import xml_support_functions as wrt
+
+######################################################################
+# Functions that write on XML file
+
+#-------------------------------------------------------------------#
+def write_groupmembers_subsection (db, xml, group_guid):
+    group_members = db.cursor()
+    group_members.execute(qry.qry_group_members, (group_guid,))
+    
+    qty=str(group_members.rowcount)
+    wrt.write_tag(xml,2,"quantidade_membros",qty,'')
+                    
+    wrt.write_open_tag(xml,2,"membros",'')
+    for (user_id, user_name, user_username) in group_members:
+        prefix='profile/'
+        user_attr=wrt.uidstr(wrt.urlparticipa(prefix,user_username))
+        wrt.write_tag(xml,3,"usuario",user_name,user_attr)
+    wrt.write_close_tag(xml,2,"membros")
+    
+    group_members.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupfiles_subsection (db, xml, group_guid):
+    group_files = db.cursor()
+    
+    # 1 = select * from elgg_entity_subtypes where subtype='file';
+    group_files.execute(qry.qry_group_posts, (group_guid, 1,))
+    
+    # 50 = select * from elgg_metastrings where string='file_enable';
+    perm=qry.postcontent_permission(db, group_guid, 50)
+    
+    wrt.write_open_tag(xml,2,"arquivos",wrt.permstr(perm))
+    
+    for (post_guid, post_title, post_desc, \
+            owner_id, owner_name, owner_username, time)\
+        in group_files:
+        
+        prefix='file/download/'
+        file_link=wrt.urlparticipa(prefix,str(post_guid))
+        
+        prefix='file/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"arquivo",post_attr)
+
+        prefix='profile/'
+        owner_attr=wrt.uidstr(wrt.urlparticipa(prefix,owner_username))
+        wrt.write_tag(xml,4,"autor",owner_name,owner_attr)
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"link",'',wrt.hrefstr(file_link))
+        wrt.write_tag(xml,4,"descricao",wrt.cdata(post_desc),'')
+                    
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"arquivo")
+        
+    wrt.write_close_tag(xml,2,"arquivos")
+    
+    group_files.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupforumtopics_subsection (db, xml, group_guid):
+    group_forumtopics = db.cursor()
+    
+    # 7 = select * from elgg_entity_subtypes where subtype='groupforumtopic';
+    group_forumtopics.execute(qry.qry_group_posts, (group_guid, 7,))
+    
+    # 52 = select * from elgg_metastrings where string='forum_enable';
+    perm=qry.postcontent_permission(db, group_guid, 52)
+    
+    wrt.write_open_tag(xml,2,"debates",wrt.permstr(perm))
+    
+    for (post_guid, post_title, post_desc, \
+        owner_id, owner_name, owner_username, time)\
+        in group_forumtopics:
+        
+        prefix='discussion/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"debate",post_attr)
+
+        prefix='profile/'
+        owner_attr=wrt.uidstr(wrt.urlparticipa(prefix,owner_username))
+        wrt.write_tag(xml,4,"autor",owner_name,owner_attr)
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"texto",wrt.cdata(post_desc),'')
+            
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"debate")
+        
+    wrt.write_close_tag(xml,2,"debates")
+    
+    group_forumtopics.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupbookmarks_subsection (db, xml, group_guid):
+    group_bookmarks = db.cursor()
+    
+    # 13 = select * from elgg_entity_subtypes where subtype='bookmarks';
+    group_bookmarks.execute(qry.qry_group_posts, (group_guid, 13,))
+    
+    # 49 = select * from elgg_metastrings where string='bookmarks_enable';
+    perm=qry.postcontent_permission(db, group_guid, 49)
+    
+    wrt.write_open_tag(xml,2,"favoritos",wrt.permstr(perm))
+    
+    for (post_guid, post_title, post_desc, \
+            owner_id, owner_name, owner_username, time)\
+        in group_bookmarks:
+            
+        # 90 = select * from elgg_metastrings where string='address';
+        bookmark_link=qry.post_content(db,post_guid,90)
+        
+        prefix='bookmarks/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"favorito",post_attr)
+
+        prefix='profile/'
+        owner_attr=wrt.uidstr(wrt.urlparticipa(prefix,owner_username))
+        wrt.write_tag(xml,4,"autor",owner_name,owner_attr)
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"link",'',wrt.hrefstr(bookmark_link))
+        wrt.write_tag(xml,4,"descricao",wrt.cdata(post_desc),'')
+                            
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"favorito")
+    
+    wrt.write_close_tag(xml,2,"favoritos")
+    
+    group_bookmarks.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_grouppages_subsection (db, xml, group_guid):
+    group_pages = db.cursor()
+    
+    # 14 = select * from elgg_entity_subtypes where subtype='page_top';
+    group_pages.execute(qry.qry_group_posts, (group_guid, 14,))
+    
+    # 53 = select * from elgg_metastrings where string='pages_enable';
+    perm=qry.postcontent_permission(db, group_guid, 53)
+    
+    wrt.write_open_tag(xml,2,"paginas",wrt.permstr(perm))
+    
+    for (post_guid, post_title, post_desc,
+            owner_id, owner_name, owner_username, time)\
+        in group_pages:
+        
+        prefix='pages/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"pagina",post_attr)
+
+        prefix='profile/'
+        owner_attr=wrt.uidstr(wrt.urlparticipa(prefix,owner_username))
+        wrt.write_tag(xml,4,"autor",owner_name,owner_attr)
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"texto",wrt.cdata(post_desc),'')
+                    
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"pagina")
+        
+    wrt.write_close_tag(xml,2,"paginas")
+    
+    group_pages.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupvideos_subsection (db, xml, group_guid):
+    group_videos = db.cursor()
+    
+    # 12 = select * from elgg_entity_subtypes where subtype='videos';
+    group_videos.execute(qry.qry_group_posts, (group_guid, 12,))
+    
+    # 399 = select * from elgg_metastrings where string='videos_enable';
+    perm=qry.postcontent_permission(db, group_guid, 399)
+    
+    wrt.write_open_tag(xml,2,"videos",wrt.permstr(perm))
+    
+    for (post_guid, post_title, post_desc, \
+            owner_id, owner_name, owner_username, time)\
+        in group_videos:
+            
+        # 477 = select * from elgg_metastrings where string='video_url';
+        video_link=qry.post_content(db,post_guid, 477)
+            
+        prefix='videos/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"video",post_attr)
+
+        prefix='profile/'
+        owner_attr=wrt.uidstr(wrt.urlparticipa(prefix,owner_username))
+        wrt.write_tag(xml,4,"autor",owner_name,owner_attr)
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"link",'',wrt.hrefstr(video_link))
+        wrt.write_tag(xml,4,"descricao",wrt.cdata(post_desc),'')
+            
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"video")
+        
+    wrt.write_close_tag(xml,2,"videos")
+    
+    group_videos.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groupevents_subsection (db, xml, group_guid):
+    group_events = db.cursor()
+    
+    # 6 = select * from elgg_entity_subtypes where subtype='calendar_event';
+    group_events.execute(qry.qry_group_posts, (group_guid, 6,))
+    
+    # 54 = select * from elgg_metastrings where string='event_calendar_enable';
+    perm=qry.postcontent_permission(db, group_guid, 54)
+    
+    wrt.write_open_tag(xml,2,"eventos",wrt.permstr(perm))
+    
+    for (post_guid, post_title, post_desc, \
+            owner_id, owner_name, owner_username, time)\
+        in group_events:
+            
+        # 18 = select * from elgg_metastrings where string='venue';
+        venue=qry.post_content(db, post_guid, 18)
+        
+        # 20 = select * from elgg_metastrings where string='start_date';
+        time_start=qry.post_content(db, post_guid, 20)
+
+        # 22 = select * from elgg_metastrings where string='end_date';
+        time_end=qry.post_content(db, post_guid, 22)
+        
+        # 26 = select * from elgg_metastrings where string='fees';
+        fees=qry.post_content(db, post_guid, 26)
+        
+        # 28 = select * from elgg_metastrings where string='contact';
+        contact=qry.post_content(db, post_guid, 28)
+        
+        # 30 = select * from elgg_metastrings where string='organizer';
+        organizer=qry.post_content(db, post_guid, 30)
+
+        prefix='event_calendar/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"evento",post_attr)
+        
+        prefix='profile/'
+        owner_attr=wrt.uidstr(wrt.urlparticipa(prefix,owner_username))
+        wrt.write_tag(xml,4,"autor",owner_name,owner_attr)
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"organizador",organizer,'')
+        wrt.write_tag(xml,4,"contato",contact,'')
+        wrt.write_tag(xml,4,"endereco",venue,'')
+        wrt.write_tag(xml,4,"data_inicio",wrt.datestr(time_start),'')
+        wrt.write_tag(xml,4,"data_fim",wrt.datestr(time_end),'')
+        wrt.write_tag(xml,4,"taxa_participacao",fees,'')
+        wrt.write_tag(xml,4,"descricao",wrt.cdata(post_desc),'')
+        
+        wrt.write_close_tag(xml,3,"evento")
+        
+        wrt.write_comments(db,xml,post_guid)
+    
+    wrt.write_close_tag(xml,2,"eventos")
+    
+    group_events.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_groups_section(db, xml, \
+    guid, title, desc, owner_id, owner_name, owner_username, time):
+
+    # 45 = select * from elgg_metastrings where string='briefdescription';
+    brief_desc=qry.post_content(db,guid, 45)
+    
+    prefix='groups/profile/'
+    group_attr=wrt.cidstr(wrt.urlparticipa(prefix,str(guid)))
+    wrt.write_open_tag(xml,1,"comunidade",group_attr)
+    
+    # Write all group's information
+    prefix='profile/'
+    owner_attr=wrt.uidstr(wrt.urlparticipa(prefix,owner_username))
+    wrt.write_tag(xml,2,"proprietario",owner_name,owner_attr)
+    wrt.write_tag(xml,2,"titulo",title,'')
+    wrt.write_tag(xml,2,"data",wrt.datestr(time),'')
+    wrt.write_tag(xml,2,"descricao",wrt.cdata(desc),'')
+    wrt.write_tag(xml,2,"breve_descricao",wrt.cdata(brief_desc),'')
+                                
+    if qry.groupaccess_permission(db, guid) == 'public':
+            
+        # Write a list of group member's name
+        write_groupmembers_subsection(db, xml, guid)
+        
+        # Write a list, and all the info, of all posts made on the group.
+        write_groupfiles_subsection(db, xml, guid)
+        write_groupforumtopics_subsection(db, xml, guid)
+        write_groupbookmarks_subsection(db, xml, guid)
+        write_grouppages_subsection(db, xml, guid)
+        write_groupvideos_subsection(db, xml, guid)
+        write_groupevents_subsection(db, xml, guid)
+        
+    wrt.write_close_tag(xml,1,"comunidade")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_singlefile_groups_section (db, dir_results):
+    
+    groups_info = db.cursor()
+    groups_info.execute(qry.qry_groups_info)
+
+    xml_filename=dir_results+wrt.date_today()+"_comunidades"+".xml"
+    xml = wrt.open_xml_file(xml_filename)
+
+    wrt.write_open_tag(xml,0,"comunidades",'')
+    
+    for (guid, title, desc, owner_id, owner_name, owner_username, time)\
+        in groups_info:
+        
+        write_groups_section(db,xml,\
+            guid,title,desc,owner_id,owner_name,owner_username,time)
+    
+    wrt.write_close_tag(xml,0,"comunidades")
+    
+    groups_info.close()
+    
+    xml.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_multifile_groups_section (db, dir_results):
+
+    groups_info = db.cursor()
+    groups_info.execute(qry.qry_groups_info)
+
+    for (guid, title, desc, owner_id, owner_name, owner_username, time)\
+        in groups_info:
+            
+        xml_filename=dir_results+'/groups/'+str(guid)+'.xml'
+        xml = wrt.open_xml_file(xml_filename)
+        
+        write_groups_section(db,xml,\
+            guid,title,desc,owner_id,owner_name,owner_username,time)
+            
+        xml.close()
+        
+    groups_info.close()
+#--------------------------------------------------------------------#
+
+######################################################################
diff --git a/lib/xml_support_functions.py b/lib/xml_support_functions.py
new file mode 100644
index 0000000..dd49756
--- /dev/null
+++ b/lib/xml_support_functions.py
@@ -0,0 +1,160 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
+# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+#
+# This file is part of participatorio/opendata
+#
+# participatorio/opendata is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+import MySQLdb
+import codecs
+import datetime
+
+import queries_definition as qry
+
+######################################################################
+# Support functions:
+
+#--------------------------------------------------------------------#
+def open_xml_file (xml_filename):
+    xml_file = codecs.open(xml_filename,'w',encoding='utf-8')
+    xml_file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+    return xml_file
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def date_today():
+    return str(datetime.date.today())
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def lvl (l):
+    if l == 1:
+        return "\t"
+    elif l == 2:
+        return "\t\t"
+    elif l == 3:
+        return "\t\t\t"
+    elif l == 4:
+        return "\t\t\t\t"
+    elif l == 5:
+        return "\t\t\t\t\t"
+    elif l == 6:
+        return "\t\t\t\t\t\t"
+    elif l == 7:
+        return "\t\t\t\t\t\t\t"
+    else:
+        return ""
+#--------------------------------------------------------------------#    
+
+#--------------------------------------------------------------------#
+def cdata (string):
+    if len(string) > 0:
+        return "<![CDATA["+string+"]]>"
+    else:
+        return ""
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def uidstr (guid):
+    return " uid="+"\""+guid+"\""
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def cidstr (guid):
+    return " cid="+"\""+guid+"\""
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def pidstr (guid):
+    return " pid="+"\""+guid+"\""
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def permstr (perm):
+    return " habilitado="+"\""+str(perm)+"\""
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def qtystr (quantity):
+    return " quantidade="+"\""+str(quantity)+"\""
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def urlparticipa (prefix, guid):
+    return "http://participatorio.juventude.gov.br/"+prefix+guid
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def hrefstr (url):
+    return " href="+"\""+url+"\""
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def datestr (time):
+    if time != "":
+        return str(datetime.datetime.fromtimestamp(int(time)))
+    else:
+        return ""
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_open_tag (xml, l, tag_name, attr_str):
+    xml.write(lvl(l)+"<"+tag_name+attr_str+">"+"\n")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_close_tag (xml, l, tag_name):
+    xml.write(lvl(l)+"</"+tag_name+">"+"\n")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_tag (xml, l, tag_name, info_str, attr_str):
+    level=lvl(l)
+    if len(info_str) > 0:
+        tag_begin=("<"+tag_name+attr_str+">")
+        tag_end=("</"+tag_name+">")
+        xml.write(level+tag_begin+info_str+tag_end+"\n")
+    else:
+        xml.write(level+"<"+tag_name+attr_str+"/>"+"\n")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_comments (db, xml, post_guid):
+    post_comments = db.cursor()
+    post_comments.execute(qry.qry_post_comments, (post_guid,))
+            
+    write_open_tag(xml,4,"comentarios",'')
+    for (user_id, user_name, user_username, string, time) in post_comments:
+        
+        write_open_tag(xml,5,"comentario",'')
+        
+        prefix='profile/'
+        user_attr=uidstr(urlparticipa(prefix,user_username))
+        write_tag(xml,6,"usuario",user_name,user_attr)
+        write_tag(xml,6,"data",datestr(time),'')
+        write_tag(xml,6,"mensagem",cdata(string),'')
+        
+        write_close_tag(xml,5,"comentario")
+        
+    write_close_tag(xml,4,"comentarios")
+    
+    post_comments.close()
+#--------------------------------------------------------------------#
+
+######################################################################
diff --git a/lib/xml_user_section.py b/lib/xml_user_section.py
new file mode 100644
index 0000000..0ab1635
--- /dev/null
+++ b/lib/xml_user_section.py
@@ -0,0 +1,377 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
+# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+#
+# This file is part of participatorio/opendata
+#
+# participatorio/opendata is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+import MySQLdb
+
+import queries_definition as qry
+import xml_support_functions as wrt
+
+######################################################################
+# Functions that write on XML file
+
+#--------------------------------------------------------------------#
+def write_userfriends_subsection (db, xml, user_guid):
+    friends_info = db.cursor()
+    friends_info.execute(qry.qry_user_friends, (user_guid))
+    
+    qty=str(friends_info.rowcount)
+    wrt.write_tag(xml,2,"quantidade_amigos",qty,'')
+    
+    wrt.write_open_tag(xml,2,"amigos",'')
+    for (friend_id, friend_name, friend_username) in friends_info:
+        prefix='profile/'
+        friend_attr=wrt.uidstr(wrt.urlparticipa(prefix,friend_username))
+        wrt.write_tag(xml,3,"usuario",friend_name,friend_attr)
+    wrt.write_close_tag(xml,2,"amigos")
+        
+    friends_info.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userowngroup_subsection (db, xml, user_guid):        
+    user_owngroups = db.cursor()
+    user_owngroups.execute(qry.qry_user_owngroups, (user_guid, user_guid, ))
+        
+    wrt.write_open_tag(xml,3,"dono",'')
+    for (group_id, group_title) in user_owngroups:
+        prefix='groups/profile/'
+        group_attr=wrt.cidstr(wrt.urlparticipa(prefix,str(group_id)))
+        wrt.write_tag(xml,4,"comunidade",group_title,group_attr)
+    wrt.write_close_tag(xml,3,"dono")
+        
+    user_owngroups.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_usermembergroup_subsection (db, xml, user_guid):
+    user_membergroups = db.cursor()
+    user_membergroups.execute(qry.qry_user_membergroups, (user_guid, ))
+        
+    wrt.write_open_tag(xml,3,"participante",'')
+    for (group_id, group_title) in user_membergroups:
+        prefix='groups/profile/'
+        group_attr=wrt.cidstr(wrt.urlparticipa(prefix,str(group_id)))
+        wrt.write_tag(xml,4,"comunidade",group_title,group_attr)
+    wrt.write_close_tag(xml,3,"participante")
+        
+    user_membergroups.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_usergroups_subsection (db, xml, user_guid):
+    wrt.write_open_tag(xml,2,"comunidades",'')
+    write_userowngroup_subsection(db, xml, user_guid)
+    write_usermembergroup_subsection(db, xml, user_guid)
+    wrt.write_close_tag(xml,2,"comunidades")
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userfiles_subsection (db, xml, user_guid):
+    user_files = db.cursor()
+    
+    # 1 = select * from elgg_entity_subtypes where subtype='file';
+    user_files.execute(qry.qry_user_posts, (user_guid, user_guid, 1,))
+    
+    wrt.write_open_tag(xml,2,"arquivos",'')
+    
+    for (post_guid, post_title, post_desc, time)\
+        in user_files:
+        
+        prefix="file/download/"
+        file_link=wrt.urlparticipa(prefix,str(post_guid))
+        
+        prefix='file/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"arquivo",post_attr)
+        
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"link",'',wrt.hrefstr(file_link))
+        wrt.write_tag(xml,4,"descricao",wrt.cdata(post_desc),'')
+            
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"arquivo")
+    
+    wrt.write_close_tag(xml,2,"arquivos")
+    
+    user_files.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userblogs_subsection (db, xml, user_guid):
+    user_blogs = db.cursor()
+    
+    # 4 = select * from elgg_entity_subtypes where subtype='blog';
+    user_blogs.execute(qry.qry_user_posts, (user_guid, user_guid, 4,))
+    
+    wrt.write_open_tag(xml,2,"blogs",'')
+    
+    for (post_guid, post_title, post_desc, time)\
+        in user_blogs:
+                    
+        post_excerpt = db.cursor()
+        
+        # 64 = select * from elgg_metastrings where string='excerpt';
+        post_excerpt=qry.post_content(db,post_guid,64)
+            
+        prefix='blog/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"blog",post_attr)
+
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"resumo",wrt.cdata(post_excerpt),'')
+        wrt.write_tag(xml,4,"texto",wrt.cdata(post_desc),'')
+                    
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"blog")
+            
+    wrt.write_close_tag(xml,2,"blogs")
+    
+    user_blogs.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userbookmarks_subsection (db, xml, user_guid):
+    user_bookmarks = db.cursor()
+    
+    # 13 = select * from elgg_entity_subtypes where subtype='bookmarks';
+    user_bookmarks.execute(qry.qry_user_posts, (user_guid, user_guid, 13,))
+    
+    wrt.write_open_tag(xml,2,"favoritos",'')
+    
+    for (post_guid, post_title, post_desc, time)\
+        in user_bookmarks:
+                    
+        # 90 = select * from elgg_metastrings where string='address';
+        bookmark_link=qry.post_content(db,post_guid,90)
+  
+        prefix='bookmarks/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"favorito",post_attr)
+    
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"link",'',wrt.hrefstr(bookmark_link))
+        wrt.write_tag(xml,4,"descricao",wrt.cdata(post_desc),'')
+                    
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"favorito")
+                
+    wrt.write_close_tag(xml,2,"favoritos")
+    
+    user_bookmarks.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#    
+def write_userpages_subsection (db, xml, user_guid):
+    user_pages = db.cursor()
+    
+    # 14 = select * from elgg_entity_subtypes where subtype='page_top';
+    user_pages.execute(qry.qry_user_posts, (user_guid, user_guid, 14,))
+    
+    wrt.write_open_tag(xml,2,"paginas",'')
+    
+    for (post_guid, post_title, post_desc, time)\
+        in user_pages:
+        
+        prefix='pages/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"pagina",post_attr)
+
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"texto",wrt.cdata(post_desc),'')
+                    
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"pagina")
+        
+    wrt.write_close_tag(xml,2,"paginas")
+    
+    user_pages.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_uservideos_subsection (db, xml, user_guid):
+    user_videos = db.cursor()
+    
+    # 12 = select * from elgg_entity_subtypes where subtype='videos';
+    user_videos.execute(qry.qry_user_posts, (user_guid, user_guid, 12,))
+    
+    wrt.write_open_tag(xml,2,"videos",'')
+    
+    for (post_guid, post_title, post_desc, time)\
+        in user_videos:
+                    
+        # 477 = select * from elgg_metastrings where string='video_url';
+        video_link=qry.post_content(db, post_guid, 477)
+        
+        prefix='videos/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"video",post_attr)
+        
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"link",'',wrt.hrefstr(video_link))
+        wrt.write_tag(xml,4,"descricao",wrt.cdata(post_desc),'')
+        
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"video")
+        
+    wrt.write_close_tag(xml,2,"videos")
+    
+    user_videos.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#
+def write_userevents_subsection (db, xml, user_guid):
+    user_events = db.cursor()
+    
+    # 6 = select * from elgg_entity_subtypes where subtype='calendar_event';
+    user_events.execute(qry.qry_user_posts, (user_guid, user_guid, 6,))
+    
+    
+    wrt.write_open_tag(xml,2,"eventos",'')
+    
+    for (post_guid, post_title, post_desc, time)\
+        in user_events:
+            
+        # 18 = select * from elgg_metastrings where string='venue';
+        venue=qry.post_content(db, post_guid, 18)
+        
+        # 20 = select * from elgg_metastrings where string='start_date';
+        time_start=qry.post_content(db, post_guid, 20)
+
+        # 22 = select * from elgg_metastrings where string='end_date';
+        time_end=qry.post_content(db, post_guid, 22)
+        
+        # 26 = select * from elgg_metastrings where string='fees';
+        fees=qry.post_content(db, post_guid, 26)
+        
+        # 28 = select * from elgg_metastrings where string='contact';
+        contact=qry.post_content(db, post_guid, 28)
+        
+        # 30 = select * from elgg_metastrings where string='organizer';
+        organizer=qry.post_content(db, post_guid, 30)
+        
+        prefix='event_calendar/view/'
+        post_attr=wrt.pidstr(wrt.urlparticipa(prefix,str(post_guid)))
+        wrt.write_open_tag(xml,3,"evento",post_attr)
+        
+        wrt.write_tag(xml,4,"titulo",post_title,'')
+        wrt.write_tag(xml,4,"data",wrt.datestr(time),'')
+        wrt.write_tag(xml,4,"organizador",organizer,'')
+        wrt.write_tag(xml,4,"contato",contact,'')
+        wrt.write_tag(xml,4,"endereco",venue,'')
+        wrt.write_tag(xml,4,"data_inicio",wrt.datestr(time_start),'')
+        wrt.write_tag(xml,4,"data_fim",wrt.datestr(time_end),'')
+        wrt.write_tag(xml,4,"taxa_participacao",fees,'')
+        wrt.write_tag(xml,4,"descricao",wrt.cdata(post_desc),'')
+        
+        wrt.write_comments(db,xml,post_guid)
+        
+        wrt.write_close_tag(xml,3,"evento")
+    
+    wrt.write_close_tag(xml,2,"eventos")
+    
+    user_events.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#    
+def write_users_section (db, xml, \
+    guid, name, username):
+
+    prefix='profile/'
+    user_attr=wrt.uidstr(wrt.urlparticipa(prefix,username))
+    wrt.write_open_tag(xml,1,"usuario",user_attr)
+    
+    # Write all user's information
+    wrt.write_tag(xml,2,"nome",name,'')
+        
+    # Write a list of user friend's names
+    write_userfriends_subsection(db, xml, guid)
+    
+    # Write a list of all groups that the user owns or belongs
+    write_usergroups_subsection(db, xml, guid)
+    
+    # Write a list, and all the info, of all posts made by the user
+    write_userfiles_subsection(db, xml, guid)
+    write_userblogs_subsection(db, xml, guid)
+    write_userbookmarks_subsection(db, xml, guid)
+    write_userpages_subsection(db, xml, guid)
+    write_uservideos_subsection(db, xml, guid)
+    write_userevents_subsection(db, xml, guid)
+    
+    wrt.write_close_tag(xml,1,"usuario")
+#--------------------------------------------------------------------#    
+
+#--------------------------------------------------------------------#    
+def write_singlefile_users_section (db, dir_results):
+   
+    users_info = db.cursor()
+    users_info.execute(qry.qry_users_info)
+    
+    xml_filename=dir_results+wrt.date_today()+"_usuarios"+".xml"
+    xml = wrt.open_xml_file(xml_filename)
+
+    wrt.write_open_tag(xml,0,"usuarios",'')
+    
+    for (guid, name, username)\
+        in users_info:
+            
+        write_users_section(db,xml,\
+            guid,name,username)        
+    
+    wrt.write_close_tag(xml,0,"usuarios")
+    
+    users_info.close()
+    
+    xml.close()
+#--------------------------------------------------------------------#
+
+#--------------------------------------------------------------------#    
+def write_multifile_users_section (db, dir_results):
+   
+    users_info = db.cursor()
+    users_info.execute(qry.qry_users_info)
+    
+    for (guid, name, username)\
+        in users_info:
+        
+        xml_filename=dir_results+'/users/'+str(guid)+'.xml'
+        xml = wrt.open_xml_file(xml_filename)
+    
+        write_users_section(db,xml,\
+            guid,name,username)        
+        
+        xml.close()
+    
+    users_info.close()
+#--------------------------------------------------------------------#
+
+######################################################################
diff --git a/opendata_json_version.py b/opendata_json_version.py
index 12ca510..c24c5f2 100644
--- a/opendata_json_version.py
+++ b/opendata_json_version.py
@@ -24,11 +24,11 @@
 import MySQLdb
 import datetime
 
-from lib.json.user_section import write_singlefile_users_section
-from lib.json.group_section import write_singlefile_groups_section
+from lib.json_user_section import write_singlefile_users_section
+from lib.json_group_section import write_singlefile_groups_section
 
-from lib.json.user_section import write_multifile_users_section
-from lib.json.group_section import write_multifile_groups_section
+from lib.json_user_section import write_multifile_users_section
+from lib.json_group_section import write_multifile_groups_section
 
 def main():
     
@@ -50,8 +50,8 @@ def main():
     write_singlefile_groups_section(db,dir_results)    
     
     # Call functions to write Multiple Dumps JSON files
-    write_multifile_users_section(db,dir_results)
-    write_multifile_groups_section(db,dir_results)
+    #write_multifile_users_section(db,dir_results)
+    #write_multifile_groups_section(db,dir_results)
     
     # Calculate and Print script time duration
     script_duration=datetime.datetime.now()-time_script_start
diff --git a/opendata_xml_version.py b/opendata_xml_version.py
index f15645a..d2c269a 100644
--- a/opendata_xml_version.py
+++ b/opendata_xml_version.py
@@ -24,11 +24,11 @@
 import MySQLdb
 import datetime
 
-from lib.xml.user_section import write_singlefile_users_section
-from lib.xml.group_section import write_singlefile_groups_section
+from lib.xml_user_section import write_singlefile_users_section
+from lib.xml_group_section import write_singlefile_groups_section
 
-from lib.xml.user_section import write_multifile_users_section
-from lib.xml.group_section import write_multifile_groups_section
+from lib.xml_user_section import write_multifile_users_section
+from lib.xml_group_section import write_multifile_groups_section
 
 def main():
     
@@ -50,8 +50,8 @@ def main():
     write_singlefile_groups_section(db,dir_results)
     
     # Call functions to write Multiple Dump XML files
-    write_multifile_users_section(db,dir_results)
-    write_multifile_groups_section(db,dir_results)
+    #write_multifile_users_section(db,dir_results)
+    #write_multifile_groups_section(db,dir_results)
     
     # Calculate and Print script time duration
     script_duration=datetime.datetime.now()-time_script_start
-- 
GitLab