Commits

caedes committed 18be75a

fixing some things here and there

  • Participants
  • Parent commits f858834

Comments (0)

Files changed (1)

             self._type = type
         else:
             self._type = self._db.get_entity_property(self.guid, 'type')
+    def __repr__(self):
+        return self.__str__()
     def __str__(self):
         try:
             name = self.name
         except:
             name = self.title
+        if not name:
+            print self.type,self.keys()
         return "ElggEntity(%s, '%s:%s', '%s')" % (self.guid, self.type, self.getSubtype(), name)
     def __setattr__(self, name, value):
         if name in ['guid','_db','_type']:
         else:
             self.setProperty(name, value)
     def __eq__(self, other):
-        return self.guid == other.guid
+        return other.__class__ == self.__class__ and self.guid == other.guid
     def __ne__(self, other):
         return not self.__eq__(other)
     def __getattr__(self, name):
         return self._db.set_entity_property(self.guid, name, value, self._type)
     def keys(self):
         return self._db._entity_props + self._db.get_pertype_props(self._type) + self._db.get_metadata_keys(self.guid)
+    def printAll(self):
+        print self
+        for key in self.keys():
+            print " *",key,self.getProperty(key)
+
+        for relationship in self.getRelationships():
+            print " -", relationship
+            for a in set(self._db.get_relationship(self.guid, relationship)).union(self._db.get_relationship_inverse(self.guid, relationship)):
+                print "  -",self._db.getEntity(a)
+
 
 class ElggConnect(object):
-    def __init__(self, host, user, passwd, db, prefix='elgg'):
+    def __init__(self, host, user, passwd, db, prefix='elgg', connect=True):
         self._prefix = prefix
-        self._sql = MySQLdb.connect(host, user, passwd, db);
-        self._sql.set_character_set("utf8")
+        self.connect(host, user, passwd, db)
         self._entity_props = self.select_multi("describe "+self._prefix+"entities;")
         self._pertype_props = {}
         self._topicid = self.get_entity_subtype('groupforumtopic')
         self._metastring_cache = {}
         self._metastringid_cache = {}
         self.threaded_forums = True
+    def connect(self, host, user, passwd, db):
+        self._sql = MySQLdb.connect(host, user, passwd, db)
+        self._sql.set_character_set("utf8")
+    def close(self):
+        self._sql.close()
     def get_pertype_props(self, type):
         if not type in self._pertype_props:
             self._pertype_props[type] = self.select_multi("describe "+self._prefix+type+"s_entity;")
     def query(self, query_string):
         return self._sql.query(query_string)
     def insert(self, query_string):
-        #print "insert",query_string
         self._sql.query(query_string)
         return self._sql.insert_id()
     def select_single(self, query_string):
+        #print "insert",query_string
         self._sql.query(query_string)
         res = self._sql.store_result()
         row = res.fetch_row()
             return None
 
     def select_multi(self, query_string):
+        #print "select_multi: ", query_string
         objs = []
         self._sql.query(query_string)
         res = self._sql.store_result()
     # good
     def getEntity(self, *args):
         if len(args) == 1:
-            guid = args[0]
-            return ElggEntity(guid, self)
+            if isinstance(args[0],(int,long)):
+		    guid = args[0]
+		    if guid:
+			return ElggEntity(guid, self)
         elif len(args) == 2:
             guid = self.get_entity_frommeta(*args)
-            return ElggEntity(guid, self)
+            if guid:
+                return ElggEntity(guid, self)
         elif len(args) == 3:
             guid = self.get_entity_fromproperty(*args)
-            return ElggEntity(guid, self, args[2])
+            if guid:
+                return ElggEntity(guid, self, args[2])
+    def getEntities(self, *args):
+        if len(args) == 1:
+            type = str(args[0])
+            if type in ['user','site','object','group']:
+                guids = self.select_multi("select guid from "+self._prefix+"entities where type='"+type+"'")
+                return map(lambda guid: ElggEntity(guid, self),guids)
+            else:
+                guids = self.get_entities_withmeta(type)
+                return map(lambda guid: ElggEntity(guid, self),guids)
+        elif len(args) == 2:
+            guids = self.get_entities_frommeta(*args)
+            return map(lambda guid: ElggEntity(guid, self),guids)
+        elif len(args) == 3:
+            guids = self.get_entities_fromproperty(*args)
+            return map(lambda guid: ElggEntity(guid, self, args[2]),guids)
+
     def createEntity(self, type, subtype=0, owner_guid=0, group_guid=0, now=False, access=False, args={}):
         if type=='user' and not access:
             access = ACCESS_PUBLIC # public
         e = ElggEntity(guid, self, type)
         for a in extra_props:
             e.setProperty(a, args[a])
-        return ElggEntity(e, self, type)
+        return e
 
     def get_metastring_id(self, astring, autocreate=False):
         if astring in self._metastringid_cache:
             return self._metastringid_cache[astring]
         id = self.select_single("select id from "+self._prefix+"metastrings where \
-                                string='%s';"%(astring.replace("'","''"),))
+                                string=%s;"%(format_sql(astring),))
         if autocreate and id == None:
             id = self.insert("insert into "+self._prefix+"metastrings (string)\
-                             values ('%s')"%(astring.replace("'","''"),))
+                             values (%s)"%(format_sql(astring),))
         if id:
             self._metastringid_cache[astring] = id
             self._metastring_cache[id] = astring
             values(%s, '%s', %s);" % (guid_one, relationship, guid_two))
         return id
 
-    def get_entity_frommeta(self, name, value):
+    def get_entities_frommeta(self, name, value):
         msg_id = None
         topicid = None
         name_id = self.get_metastring_id(name)
         value_id = self.get_metastring_id(value)
         if name_id and value_id:
-            return self.select_single("select entity_guid from "+self._prefix+"metadata where value_id='"+str(value_id)+"' and name_id='"+str(name_id)+"'")
-        return None
+            return self.select_multi("select entity_guid from "+self._prefix+"metadata where value_id='"+str(value_id)+"' and name_id='"+str(name_id)+"'")
+        return []
+    def get_entities_withmeta(self, name):
+        name_id = self.get_metastring_id(name)
+        if name_id:
+            return set(self.select_multi("select entity_guid from "+self._prefix+"metadata where name_id='"+str(name_id)+"'"))
+        return []
+
+    def get_entity_frommeta(self, name, value):
+        ents = self.get_entities_frommeta(name, value)
+        if ents:
+             return ents[0]
+
+    def get_entities_fromproperty(self, name, value, type):
+        if name in self._entity_props:
+            return self.select_multi("select guid from "+self._prefix+"entities where %s=%s"%(name, format_sql(value)))
+        if name in self.get_pertype_props(type):
+            return self.select_multi("select guid from "+self._prefix+type+"s_entity where %s=%s"%(name, format_sql(value)))
+        return self.get_entities_frommeta(name, value)
 
     def get_entity_fromproperty(self, name, value, type):
-        if name in self._entity_props:
-            return self.select_single("select guid from "+self._prefix+"entities where %s=%s"%(name, format_sql(value)))
-        if name in self.get_pertype_props(type):
-            return self.select_single("select guid from "+self._prefix+type+"s_entity where %s=%s"%(name, format_sql(value)))
-        if name in self.get_pertype_props(type):
-            return self.get_entity_frommeta(name, value)
+        ents = self.get_entities_fromproperty(name, value, type)
+        if ents:
+            return ents[0]
 
     def set_entity_property(self, guid, name, value, type=''):
         if name in self._entity_props: