/** Imports role translations from a text files into database */
 function importRoles($lines, $language)
 {
     if (empty($lines)) {
         logError("importRoles: file is empty");
         return;
     }
     reset($lines);
     while (list(, $line) = each($lines)) {
         if (preg_match('/^\\s*$/', $line) || preg_match('/^#/', $line)) {
             continue;
         }
         if (!preg_match('/^\\s*(\\d+)\\s+(.*)/', $line, $items)) {
             logError("bad line syntax: {$line}");
             continue;
         }
         debug("role item", $items);
         $id = $items[1];
         $name = trim($items[2]);
         $oid = sotf_NodeObject::makeId(0, "sotf_roles", $id);
         $o1 = new sotf_NodeObject("sotf_roles", $oid);
         if (!$o1->exists()) {
             $o1->set('role_id', $id);
             $o1->set('creator', 'f');
             $o1->create();
         }
         $oid = sotf_NodeObject::makeId(0, "sotf_role_names", $id . $language);
         $o2 = new sotf_NodeObject("sotf_role_names", $oid);
         $o2->set('role_id', $id);
         $o2->set('language', $language);
         $o2->set('name', $name);
         $o2->create();
     }
 }
 function processPortalEvent($event)
 {
     debug("processing event", $event);
     $progId = $event['prog_id'];
     if ($progId) {
         if ($this->looksLikeId($progId)) {
             $prg =& $this->getObject($progId);
         }
         if (!$prg) {
             debug("Invalid prog_id arrived in portal event", $progId);
             return -1;
         }
     }
     switch ($event['name']) {
         case 'programme_added':
             $obj = new sotf_NodeObject('sotf_prog_refs');
             $obj->set('prog_id', $event['value']);
             $obj->set('url', $event['url']);
             $obj->find();
             $obj->set('station_id', $prg->get('station_id'));
             $obj->set('start_date', $event['timestamp']);
             $obj->set('portal_name', $event['portal_name']);
             $obj->save();
             break;
         case 'programme_deleted':
             $obj = new sotf_NodeObject('sotf_prog_refs');
             $obj->set('prog_id', $event['value']);
             $obj->set('url', $event['url']);
             $obj->find();
             if (!$obj->exists()) {
                 debug("unknown prog ref arrives: " . $event['value'] . ' - ' . $event['url']);
                 $obj->set('portal_name', $event['portal_name']);
             }
             $obj->set('station_id', $prg->get('station_id'));
             $obj->set('end_date', $event['timestamp']);
             //$obj->set('portal_name', $event['portal_name']);
             $obj->save();
             break;
         case 'visit':
             $obj = new sotf_NodeObject('sotf_prog_refs');
             $obj->set('prog_id', $event['value']['prog_id']);
             $obj->set('url', $event['url']);
             $obj->find();
             if (!$obj->exists()) {
                 // TODO: how can this happen? It happens too many times!
                 debug("unknown prog ref arrives: " . $event['value']['prog_id'] . ' - ' . $event['url']);
                 $obj->set('start_date', $event['timestamp']);
                 $obj->set('portal_name', $event['portal_name']);
             }
             $obj->set('station_id', $prg->get('station_id'));
             $obj->set('visits', (int) $obj->get('visits') + 1);
             // TODO: count unique accesses
             $obj->save();
             break;
         case 'page_impression':
             $obj = new sotf_NodeObject('sotf_portals');
             $obj->set('url', $event['url']);
             $obj->find();
             $obj->set('name', $event['portal_name']);
             $obj->set('page_impression', $event['value']);
             $obj->set('last_access', $event['timestamp']);
             $obj->save();
             break;
         case 'portal_updated':
             $obj = new sotf_NodeObject('sotf_portals');
             $obj->set('url', $event['url']);
             $obj->find();
             $obj->set('name', $event['portal_name']);
             $obj->set('last_update', $event['timestamp']);
             $obj->save();
             break;
         case 'users':
             $obj = new sotf_NodeObject('sotf_portals');
             $obj->set('url', $event['url']);
             $obj->find();
             if (!$obj->exists()) {
                 $obj->set('name', $event['portal_name']);
             }
             $obj->set('last_update', $event['timestamp']);
             $obj->set('reg_users', $event['value']);
             if (!$obj->get('name') || !$obj->get('url')) {
                 logError("Bad portal even teceived", implode(" | ", $event));
             } else {
                 $obj->save();
             }
             break;
         case 'rating':
             // first save in prog_refs
             $obj = new sotf_NodeObject('sotf_prog_refs');
             $obj->set('prog_id', $event['value']['prog_id']);
             $obj->set('url', $event['url']);
             $obj->find();
             if (!$obj->exists()) {
                 debug("unknown prog ref arrives: " . $event['url']);
                 $obj->set('start_date', $event['timestamp']);
                 $obj->set('portal_name', $event['portal_name']);
             }
             $obj->set('station_id', $prg->get('station_id'));
             $obj->set('rating', $event['value']['RATING_VALUE']);
             $obj->set('raters', $event['value']['RATING_COUNT']);
             $obj->save();
             // TODO second, put into global rating database
             /*
             $rating = new sotf_Rating();
             $id = $event['value']['prog_id'];
             $obj = & $this->getObject($id);
             if($obj->isLocal()) {
               $data = $event['value'];
               $rating->setRemoteRating($data);
             } else {
               logError("received rating for non-local object!");
             }
             */
             break;
         case 'comment':
             // first save in prog_refs
             $obj = new sotf_NodeObject('sotf_prog_refs');
             $obj->set('prog_id', $event['value']['prog_id']);
             $obj->set('url', $event['url']);
             $obj->find();
             if (!$obj->exists()) {
                 logError("unknown prog ref arrives: " . $event['value']['prog_id'] . ' - ' . $event['url']);
                 $obj->set('start_date', $event['timestamp']);
                 $obj->set('portal_name', $event['portal_name']);
             }
             $obj->set('station_id', $prg->get('station_id'));
             $obj->set('comments', (int) $obj->get('comments') + 1);
             $obj->save();
             // save comment
             $obj = new sotf_Object('sotf_comments');
             $obj->set('prog_id', $event['value']['prog_id']);
             $obj->set('portal', $event['url']);
             $obj->set('entered', $event['timestamp']);
             $obj->set('comment_title', $event['value']['title']);
             $obj->set('comment_text', $event['value']['comment']);
             $obj->set('from_name', $event['value']['user_name']);
             $obj->set('from_email', $event['value']['email']);
             $obj->create();
             // TODO forward to authors
             break;
         case 'query_added':
             //debug("query from portal", $event);
         //debug("query from portal", $event);
         case 'query_deleted':
         case 'file_uploaded':
             // silently ignored
             break;
         default:
             logError("unknown portal event: " . $event['name']);
     }
 }