public function save() { if (!$this->isSaved()) { $this->date = date('Y-m-d H:i:s'); } parent::save(); }
public function testInitJoinTableSelfReferencingInsertingData() { $e = new Entity(); $e->name = "Entity test"; $this->assertTrue($e->Entity[0] instanceof Entity); $this->assertTrue($e->Entity[1] instanceof Entity); $this->assertEqual($e->Entity[0]->state(), Doctrine_Record::STATE_TCLEAN); $this->assertEqual($e->Entity[1]->state(), Doctrine_Record::STATE_TCLEAN); $e->Entity[0]->name = 'Friend 1'; $e->Entity[1]->name = 'Friend 2'; $e->Entity[0]->Entity[0]->name = 'Friend 1 1'; $e->Entity[0]->Entity[1]->name = 'Friend 1 2'; $e->Entity[1]->Entity[0]->name = 'Friend 2 1'; $e->Entity[1]->Entity[1]->name = 'Friend 2 2'; $this->assertEqual($e->Entity[0]->name, 'Friend 1'); $this->assertEqual($e->Entity[1]->name, 'Friend 2'); $this->assertEqual($e->Entity[0]->Entity[0]->name, 'Friend 1 1'); $this->assertEqual($e->Entity[0]->Entity[1]->name, 'Friend 1 2'); $this->assertEqual($e->Entity[1]->Entity[0]->name, 'Friend 2 1'); $this->assertEqual($e->Entity[1]->Entity[1]->name, 'Friend 2 2'); $this->assertEqual($e->Entity[0]->state(), Doctrine_Record::STATE_TDIRTY); $this->assertEqual($e->Entity[1]->state(), Doctrine_Record::STATE_TDIRTY); $count = count($this->conn); $e->save(); $this->assertEqual($count + 13, $this->conn->count()); }
public function processRow($row) { $arr = str_getcsv($row); $ticker = $arr[0]; $name = $arr[1]; $name = str_replace(''', "'", $name); if ($name == 'Name') { return false; } $cap = $arr[3]; if ($cap < $this->min_market_cap) { $this->too_small_ct++; return false; } else { $corp = Doctrine::getTable('PublicCompany')->findOneByTicker($ticker); if ($corp) { $this->existing_ct++; return false; } else { $corp = new Entity(); $corp->addExtension('Org'); $corp->addExtension('Business'); $corp->addExtension('PublicCompany'); $corp->ticker = $ticker; $corp->name = $name; $corp->save(); $this->printDebug("New company added: " . $name); $this->added_ct++; } } }
public function save($query_conditions) { $query_conditions["creation"] = date("d/m/y"); $query_conditions["creation_time"] = date("H:i:s"); $query_conditions["owner"] = $_SESSION["user"]["username"]; return parent::save($query_conditions); }
public function add($data) { global $wpdb; $entity = new Entity(); $option = new Option(); $dataEntity = ['activate' => $data['activate'], 'sm_entity_id' => $data['sm_entity_id'], 'author_id' => wp_get_current_user()->ID, 'name' => $data['name'], 'last_published_message' => date('Y-m-d H:i:s')]; $entityID = $entity->save($dataEntity); foreach ($data['category'] as $key => $value) { $dataOption = ['option_id' => 1, 'value' => $value]; $option->save($dataOption, $entityID); } $dataOption = ['option_id' => 2, 'value' => $data['display_type']]; $option->save($dataOption, $entityID); $dataOption = ['option_id' => 3, 'value' => $data['publish_type']]; $option->save($dataOption, $entityID); $dataOption = ['option_id' => 4, 'value' => $data['link_canonical']]; $option->save($dataOption, $entityID); $dataOption = ['option_id' => 5, 'value' => $data['image']]; $option->save($dataOption, $entityID); $dataOption = ['option_id' => 6, 'value' => $data['readmore']]; $option->save($dataOption, $entityID); $dataOption = ['option_id' => 7, 'value' => $data['noIndex']]; $option->save($dataOption, $entityID); $dataOption = ['option_id' => 8, 'value' => $data['noFollow']]; $option->save($dataOption, $entityID); $dataOption = ['option_id' => 9, 'value' => $data['morebalise']]; $option->save($dataOption, $entityID); }
public function testInit() { $entity = new Entity(); $entity->name = 'myname'; $entity->loginname = 'test'; $entity->save(); }
public function prepareData() { parent::prepareData(); //we create a test entity that is not a user and not a group $entity = new Entity(); $entity->name='Other Entity'; $entity->type = 2; $entity->save(); $this->otherEntity = $entity; }
public function testRelationSaving2() { $e = new Entity(); $e->name = 'test'; $e->save(); $nr = new Phonenumber(); $nr->phonenumber = '1234556'; $nr->save(); $nr->Entity = $e; }
public function testBug() { $entity = new Entity(); $entity->name = 'myname'; $entity->save(); // load our user and our collection of pages $user = Doctrine_Query::create()->select('id')->from('Entity')->fetchOne(); $this->assertEqual($user->name, 'myname'); // load our user and our collection of pages $user = Doctrine_Query::create()->select('*')->from('Entity')->fetchOne(); $this->assertEqual($user->name, 'myname'); }
/** * 注册entity */ protected function beforeSave() { if (!$this->entityId) { $entity = new Entity(); $entity->type = lcfirst(get_class($this)); if ($entity->save()) { $this->entityId = $entity->getPrimaryKey(); $this->_entity = $entity; } } return parent::beforeSave(); }
public function save() { $this->changed = time(); if (empty($this->created)) { $this->created = time(); } if (empty($this->uid)) { global $user; $this->uid = $user->uid; } return parent::save(); // TODO: Change the autogenerated stub }
public function store() { $price = Input::get('price'); $price = str_replace(",", ".", $price); $price = str_replace(".", "", $price); $price = substr($price, 0, -2); $entity = new Entity(); $entity->location_id = Auth::user()->location_id; $entity->code = Input::get('code'); $entity->name = Input::get('name'); $entity->price = $price; $entity->feasible = Input::get('feasible'); $entity->infeasible = Input::get('infeasible'); $entity->last_opname = date('Y-m-d H:i:s'); $entity->employee_id = Auth::user()->employee_id; $entity->save(); Session::flash('message', 'Sukses menambahkan aset baru!'); }
/** * 发送Notice */ private function sendNotice($courseId, $announcementId, $entityType = 'announcement_added') { $courseMember = new CourseMember(); $members = $courseMember->findAllByAttributes(array('courseId' => $courseId)); foreach ($members as $k => $v) { $entity = new Entity(); $eData['type'] = 'announcement'; $entity->attributes = $eData; $entity->save(); $notice = new Notice(); $nData['userId'] = $v->userId; $nData['type'] = $entityType; $nData['addTime'] = time(); $nData['data'] = serialize(Announcement::model()->findByPk($announcementId)->id); $notice->attributes = $nData; $notice->save(); } }
public function save($values_condition) { if (Settings::getOperativeMode() == 'debug') { echo '<br /> save File to folder'; var_dump($values_condition); } $values_condition["owner"] = $_SESSION["user"]["username"]; if (isset($values_condition['file'])) { $values_condition['filetype'] = $values_condition['file']['type']; $values_condition['size'] = $values_condition['file']['size']; $values_condition['filename'] = 'upload/' . $values_condition['file']['name']; if (file_exists("upload/" . $values_condition["file"]["name"])) { echo Message::getInstance()->getMessage(MSG_ERROR_FILE_EXIST) . " (" . basename(__FILE__) . ":" . __LINE__ . ")"; } else { move_uploaded_file($values_condition["file"]["tmp_name"], $values_condition["filename"]); } } unset($values_condition['file']); return parent::save($values_condition); }
public function import($school) { if (EntityTable::getByExtensionQuery('Org')->addWhere('LOWER(org.name) LIKE ?', '%' . strtolower($school->instnm) . "%")->fetchOne()) { $this->printDebug("School exists in database: " . $school->instnm); } else { $address = new Address(); $address->street1 = isset($school->addr) ? $school->addr : null; $address->street2 = isset($school->street2) ? $school->street2 : null; $address->city = $school->city; if ($state = AddressStateTable::retrieveByText($school->stabbr)) { $address->State = $state; } $address->postal = $school->zip; $aliases = explode("|", $school->ialias); $website = null; if (!preg_match('/^http\\:\\/\\//i', trim($school->webaddr))) { $website = "http://" . strtolower($school->webaddr); } $this->printDebug($website); $newschool = new Entity(); $newschool->addExtension('Org'); $newschool->addExtension('School'); $newschool->name = $school->instnm; $newschool->website = $website; $newschool->addAddress($address); $newschool->save(); foreach ($aliases as $alias) { try { $newalias = new Alias(); $newalias->Entity = $newschool; $newalias->name = $alias; $newalias->save(); } catch (Exception $e) { $this->printDebug("An alias exception. No biggie. It's most likely that the name already exists. so we ignore it and move on: " . $e); } } $this->printDebug("Adding new school: " . $school->instnm); } }
/** * Execute the console command. * * @return void */ public function fire() { $user = User::where('username', '=', $this->option('user'))->first(); if (!isset($user)) { $this->comment("This user don't exist."); return; } // get thing's name do { $valid = true; $name = $this->ask("Name : "); if (empty($name)) { $this->comment("Your name is empty"); $valid = false; } if (Entity::where('name', '=', $name)->where('user_id', '=', $user->id)->first() != null) { $this->comment("A thing with this name already exist."); $valid = false; } } while (!$valid); // get thing's type do { $type = $this->ask("Type (i.e. room) : "); if (empty($type)) { $this->comment("Your type is empty"); } } while (empty($type)); // get thing's description do { $description = $this->ask("Description : "); if (empty($description)) { $this->comment("Your description is empty"); } } while (empty($description)); // get thing's opening hours $opening_hours = array(); $this->info("\t{$name} - schedule"); $add = $this->ask("\t\tAdd opening days ? Y/n") == "Y" ? 1 : 0; while ($add) { do { $day = $this->ask("\t\tDay of week : "); if ($day < 1 || $day > 7) { $this->comment("\t\tYour day must be an integer between 1 and 8."); } } while ($day < 1 || $day > 7); do { do { $valid_from = strtotime($this->ask("\t\tValid from (d-m-Y) : ")); if (empty($valid_from)) { $this->comment("\t\tYour date is empty"); } if ($valid_from < time()) { $this->comment("\t\tYour valid from value is before now"); } } while (empty($valid_from) || $valid_from < time()); do { $valid_through = strtotime($this->ask("\t\tValid through (d-m-Y) : ")); if (empty($valid_through)) { $this->comment("\t\tYour date is empty"); } if ($valid_through < time()) { $this->comment("\t\tYour valid through value is before now"); } } while (empty($valid_through) || $valid_through < time()); if ($valid_from > $valid_through) { $this->comment("\t\tYour valid through date is before valid from date."); } } while ($valid_from > $valid_through); $opens = array(); $closes = array(); $this->info("\t\t\t{$name} - schedule [day {$day} - opening hours]"); do { do { $valid = true; $open_close = $this->ask("\t\t\t\tOpening / Closing hour (H:m - H:m) : "); $open_close = explode("-", $open_close); if (count($open_close) < 2) { $this->comment("\t\t\t\tYour opening closing hours are not valid"); $valid = false; } if (!preg_match("/(2[0-3]|[01][0-9]):[0-5][0-9]/", $open_close[0])) { $this->comment("\t\t\t\tYour opening hour is not valid."); $valid = false; } if (!preg_match("/(2[0-3]|[01][0-9]):[0-5][0-9]/", $open_close[1])) { $this->comment("\t\t\t\tYour closing hour is not valid."); $valid = false; } } while (!$valid); array_push($opens, $open_close[0]); array_push($closes, $open_close[1]); $add = $this->ask("\t\t\tAdd opening hours ? Y/n") == "Y" ? 1 : 0; } while ($add); array_push($opening_hours, array('validFrom' => date('c', $valid_from), 'validThrough' => date('c', $valid_through), 'dayOfWeek' => $day, 'opens' => $opens, 'closes' => $closes)); $add = $this->ask("\t\tAdd opening day ? Y/n") == "Y" ? 1 : 0; } //get thing's price rates $this->info("\t{$name} - price rates"); do { $currency = $this->ask("\t\tCurrency (ISO4217 format) : "); if (empty($currency) || !in_array($currency, $this->ISO4217)) { $this->comment("\t\tYour currency value is invalid"); } } while (empty($currency) || !in_array($currency, $this->ISO4217)); $price_rate = array(); $price_rate['currency'] = $currency; $rates = array('hourly', 'daily', 'weekly', 'monthly', 'yearly'); do { do { $rate = $this->ask("\t\tRate (hourly, daily, weekly, monthly, yearly) : "); if (!in_array($rate, $rates)) { $this->comment("\t\tYour rate value is invalid"); } } while (!in_array($rate, $rates)); do { $price = $this->ask("\t\tPrice for {$rate} rate in {$currency} : "); if (empty($price) || $price < 0) { $this->comment("\t\tYour price value is invalid"); } } while (empty($price) || $price < 0); $price_rate[$rate] = $price; $add = $this->ask("\t\tAdd another price rate ? Y/n : ") == "Y" ? 1 : 0; } while ($add); //get thing's location $this->info("\t{$name} - location"); do { $building_name = $this->ask("\t\tBuilding name : "); if (empty($building_name)) { $this->comment("\t\tYour building name value is invalid"); } } while (empty($building_name)); do { $floor = $this->ask("\t\tFloor : "); if (empty($floor)) { $this->comment("\t\tYour floor value is invalid"); } } while (empty($floor)); $this->info("\t\t\t{$name} - location [map]"); do { $img = $this->ask("\t\t\tMap image URL : "); if (empty($img) || !filter_var($img, FILTER_VALIDATE_URL)) { $this->comment("\t\t\tYour map image URL is invalid"); } } while (empty($img) || !filter_var($img, FILTER_VALIDATE_URL)); do { $reference = $this->ask("\t\t\tMap reference : "); if (empty($reference)) { $this->comment("\t\t\tYour map reference is invalid"); } } while (empty($reference)); // get thing's contact and support vcard URLs do { $contact = $this->ask("Contact vcard URL : "); if (empty($contact) || !filter_var($contact, FILTER_VALIDATE_URL)) { $this->comment("Your contact vcard URL is invalid"); } } while (empty($contact) || !filter_var($contact, FILTER_VALIDATE_URL)); do { $support = $this->ask("Support vcard URL : "); if (empty($support) || !filter_var($support, FILTER_VALIDATE_URL)) { $this->comment("Your support vcard URL is invalid"); } } while (empty($support) || !filter_var($support, FILTER_VALIDATE_URL)); //get thing's amenities and fill properties $this->info("\t{$name} amenities"); $amenities = Entity::where('type', '=', 'amenity')->where('user_id', '=', $user->id)->get(); $add = $this->ask("\t\tAdd amenities ? Y/n") == "Y" ? 1 : 0; $_amenities = array(); while ($add) { do { $this->info("\t\tAvailable amenities : "); foreach ($amenities as $amenity) { $this->info("\t\t\t[{$amenity->id}] {$amenity->name}"); } $id = $this->ask("\t\tAmenity id : "); $present = false; foreach ($amenities as $amenity) { if ($amenity->id == $id) { $present = true; } } if (empty($id) || !$present) { $this->comment("\t\tYour amenity id is invalid"); } } while (empty($id) || !$present); foreach ($amenities as $amenity) { if ($amenity->id == $id) { $this->info("\t\t\t{$amenity->name} properties"); $schema = json_decode($amenity->body); foreach ($schema->properties as $property_name => $property) { do { $value = $this->ask("\t\t\t\t{$property_name} ({$property->description}) : "); if (empty($value)) { $this->comment("\t\t\t\tYour {$property_name} value is invalid."); } } while (empty($value)); array_push($_amenities, array(Config::get('app.url') . $user->username . '/amenities/' . $property_name => $value)); } } } $add = $this->ask("\t\tAdd another amenity ? Y/n") == "Y" ? 1 : 0; } //create thing and save it in database $thing = new Entity(); $thing->name = $name; $thing->type = $type; $thing->user_id = $user->id; // create thing's body $body = array(); $body['name'] = $name; $body['type'] = $type; $body['description'] = $description; $body['opening_hours'] = $opening_hours; $body['price'] = $price_rate; $body['location'] = array(); $body['location']['building_name'] = $building_name; $body['location']['floor'] = $floor; $body['location']['map'] = array(); $body['location']['map']['img'] = $img; $body['location']['map']['reference'] = $reference; $body['contact'] = $contact; $body['support'] = $support; $body['amenities'] = $_amenities; $thing->body = json_encode($body); $thing->save(); $this->info("Thing successfully saved"); }
/** * Creates and returns a user-activity entity. Returns FALSE if such an * entity could not be created. * * @param string $user_identifier * @param array $user_data * @return \Apache\Usergrid\Entity|bool */ public function create_user_activity($user_identifier, $user_data) { $user_data['type'] = "users/{$user_identifier}/activities"; $entity = new Entity($this, $user_data); $response = $entity->save(); return $response->get_error() ? FALSE : $entity; }
public function save($values_condition) { $values_condition["owner"] = $_SESSION["user"]["username"]; return parent::save($values_condition); }
public function storeVideoSegments($parentEntity, $videoSegmenting) { $tempEntityID = null; $status = array(); try { $this->createVideoSegmentingSoftwareAgent(); } catch (Exception $e) { $status['error']['videosegmenting'] = $e->getMessage(); return $status; } try { $activity = new Activity(); $activity->softwareAgent_id = "videosegmenting"; $activity->save(); } catch (Exception $e) { // Something went wrong with creating the Activity $activity->forceDelete(); $status['error'][$title] = $e->getMessage(); return $status; } for ($i = 0; $i < sizeof($videoSegmenting); $i++) { $videoSegmentName = explode("/", $videoSegmenting[$i]["storage_url"]); $title = $videoSegmentName[sizeof($videoSegmentName) - 1]; try { $entity = new Entity(); $entity->_id = $tempEntityID; $entity->title = strtolower($title); $entity->domain = $parentEntity->domain; $entity->format = "video"; $entity->documentType = "videosegment"; $entity->parents = array($parentEntity->_id); $entity->source = $parentEntity->source; $entity->content = $videoSegmenting[$i]; //unset($relexStructuredSentenceKeyVal['properties']); $entity->hash = md5(serialize($videoSegmenting[$i])); $entity->activity_id = $activity->_id; $entity->save(); $status['success'][$title] = $title . " was successfully processed into a video segment. (URI: {$entity->_id})"; } catch (Exception $e) { // Something went wrong with creating the Entity $entity->forceDelete(); $status['error'][$title] = $e->getMessage(); } $tempEntityID = $entity->_id; } $status['success']['noEntitiesCreated'] = sizeof($videoSegmenting); //dd($status); return $status; }
public function updateEntry($newEntry) { $transaction = $this->beginTransaction(); try { $entity = new Entity($this->getIdEntity()); $entity->setAlias($newEntry); $entity->save(); $entry = new Entry(); $entry->updateEntry($this->getEntry(), $newEntry); $this->setEntry($newEntry); parent::save(); $transaction->commit(); } catch (\Exception $e) { $transaction->rollback(); throw new \Exception($e->getMessage()); } }
private function updateCommitteeDetails(Entity $committee) { $this->printDebug(" Updating committee details: " . $this->fecCommitteeUrl . $committee->fec_id); if (!$this->browser->get($this->fecCommitteeUrl . $committee->fec_id)->responseIsError()) { $text = $this->browser->getResponseText(); if (preg_match('/Treasurer Name:<\\/B><\\/TD><TD>(.+)<\\/TD><\\/TR>/', $text, $treasurer_name)) { //nothing } $committee_designation = null; if (preg_match('/Committee Designation: <\\/B><\\/TD><TD>(\\w)/', $text, $committee_designation)) { switch (trim($committee_designation[1])) { case 'P': $committee->addExtension('IndividualCampaignCommittee'); $committee_designation = "Principal Campaign Committee"; break; case 'A': $committee->addExtension('IndividualCampaignCommittee'); $committee_designation = "Authorized Campaign Committee"; break; default: $committee->addExtension('OtherCampaignCommittee'); $committee_designation = "Other Campaign Committee"; } } else { $committee->addExtension('OtherCampaignCommittee'); $committee_designation = "Other Campaign Committee"; } if (preg_match('/Committee Type: <\\/B><\\/TD><TD WIDTH=300>([^<]+)<\\/TD><\\/TR>/', $text, $committee_type)) { //var_dump($committee_type); $type = null; switch (trim($committee_type[1])) { case 'PRESIDENTIAL': $type = 'pres'; break; case 'HOUSE': $type = 'house'; break; case 'SENATE': $type = 'senate'; break; } } if (preg_match('/Candidate State:<\\/B><\\/TD><TD>([^<]+)<\\/TD><\\/TR>/', $text, $candidate_state)) { //var_dump($candidate_state); if (trim($candidate_state[1]) != "Presidential Candidate") { $committee->State = AddressStateTable::retrieveByText($candidate_state[1]); } } if (preg_match_all('#cgi-bin/can_detail/([^"]+)?\\/">([^<]+)</A>#i', $text, $candidates, PREG_PATTERN_ORDER)) { /* * $candidates[1] = FEC ID * $candidates[2] = NAME */ foreach ($candidates[1] as $key => $candidate_id) { $candidate_name = $candidates[2][$key]; $found_candidate = null; //look for candidate by their FEC IDs $found_candidate = $this->getCandidatesQuery()->addWhere('politicalcandidate.senate_fec_id = ? OR politicalcandidate.house_fec_id = ? OR politicalcandidate.pres_fec_id = ?', array($candidate_id, $candidate_id, $candidate_id))->fetchOne(); $found_msg = $found_candidate ? " FOUND" : " NOT FOUND"; $this->printDebug(" + Looking for candidate by ID: " . $found_msg); //next look for them by their names $where_clause = null; $p1 = null; if (!$found_candidate) { $p1 = PersonTable::parseCommaName($candidate_name); $candidate_query = $this->getCandidatesQuery()->addWhere("person.name_first = ? AND person.name_last = ?", array($p1->name_first, $p1->name_last)); if (strlen($p1->name_middle)) { $candidate_query = $candidate_query->addWhere('person.name_middle LIKE ?', "%" . $p1->name_middle); } $found_candidate = $candidate_query->fetchOne(); $found_msg = $found_candidate ? " FOUND" : " NOT FOUND"; $this->printDebug(" + Looking for candidate by name: " . $p1->name_first . " " . $p1->name_last . $found_msg . " "); //candidate is not in database. we should add them if (!$found_candidate) { if (!$this->browser->get($this->fecCandidateUrl . $candidate_id)->responseIsError()) { $text = $this->browser->getResponseText(); $office_sought_arr = $this->getOfficeSought($text); if (!$office_sought_arr[1][0]) { continue; } $office_sought = $office_sought_arr[1][0]; $found_candidate = PersonTable::parseCommaName($candidate_name); $found_candidate->addExtension('PoliticalCandidate'); $found_candidate->is_federal = true; $found_candidate->is_state = false; $found_candidate->is_local = false; $found_candidate->pres_fec_id = $office_sought == 'President' ? $candidate_id : null; $found_candidate->senate_fec_id = $office_sought == 'Senate' ? $candidate_id : null; $found_candidate->house_fec_id = $office_sought == 'House' ? $candidate_id : null; $found_candidate->save(); $found_candidate->addReference($source = $this->fecCandidateUrl . $candidate_id, $excerpt = null, $fields = array('name_first', 'name_middle', 'name_last', 'name_prefix', 'name_suffix', 'pres_fec_id', 'senate_fec_id', 'house_fec_id'), $name = 'FEC Disclosure Report', $detail = null, $date = null, false); $this->printDebug(" Adding new candidate: " . $p1->name_first . " " . $p1->name_middle . " " . $p1->name_last); } } } $q = LsDoctrineQuery::create()->from('Relationship r')->where('r.entity1_id = ? AND r.entity2_id = ? AND r.category_id = ? AND description1 = ?', array($committee->id, $found_candidate->id, RelationshipTable::DONATION_CATEGORY, $committee_designation)); if (!$q->count()) { $relationship = new Relationship(); $relationship->Entity1 = $committee; $relationship->Entity2 = $found_candidate; $relationship->setCategory('Donation'); //$relationship->amount = $donation_amount; $relationship->description1 = $committee_designation; //$relationship->fec_donation_id = $donation_fec_id; //$relationship->start_date = $donation_date; //$relationship->end_date = $donation_date; $relationship->is_current = 1; $relationship->save(); //campaign contribution $relationship->addReference($source = $this->fecCandidateUrl . $candidate_id, $excerpt = null, $fields = array('description1'), $name = 'FEC Disclosure Report', $detail = null, $date = null); } } } } else { $this->printDebug("Couldn't get " . $this->fecCommitteeUrl); } $committee->save(); $this->printDebug(" Updating committee details: COMPLETE"); }
/** * Execute the console command * * @return void */ public function fire() { $allowed_types = array('array', 'boolean', 'integer', 'number', 'null', 'object', 'string'); $user = User::where('username', '=', $this->option('user'))->first(); if (!isset($user)) { $this->comment("This user don't exist."); return; } do { // get amenity's name do { $name = $this->ask("Amenity name : "); if (empty($name)) { $this->comment("Your amenity name is empty."); } } while (empty($name)); // check if an amenity with this name and user already exist $amenity = Entity::where('type', '=', 'amenity')->where('name', '=', $name)->first(); if (isset($amenity)) { $this->comment("An amenity with this name already exists, please choose another one."); } } while (isset($amenity)); // get amenity's title do { $title = $this->ask("Amenity title : "); if (empty($title)) { $this->comment("Your amenity title is empty."); } } while (empty($title)); // get amenity's description do { $description = $this->ask("Amenity description : "); if (empty($description)) { $this->comment("Your amenity description is empty."); } } while (empty($description)); $amenity = new Entity(); $amenity->user_id = $user->id; $amenity->type = "amenity"; $amenity->name = $name; $json_schema = array(); $json_schema['$schema'] = "http://json-schema.org/draft-04/schema#"; $json_schema['title'] = $title; $json_schema['description'] = $description; $json_schema['type'] = 'object'; $json_schema['properties'] = array(); $i = 0; $this->info("\n\n{$amenity->name} properties."); do { $this->comment("\n# Property {$i}"); // get property name do { $name = $this->ask("\tProperty name : "); if (in_array($name, array_keys($json_schema['properties']))) { $this->comment("A property with this name already exist."); } if (empty($name)) { $this->comment("Your property name is empty."); } } while (in_array($name, array_keys($json_schema['properties'])) || empty($name)); // get property description do { $description = $this->ask("\tProperty description : "); if (empty($description)) { $this->comment("Your property description is empty."); } } while (empty($description)); // set property name and description $json_schema['properties'][$name] = array(); $json_schema['properties'][$name]['description'] = $description; // get property type and verify if it is a valid json-schema core type do { $type = $this->ask("\tProperty type (array, boolean, integer, number, null, object, string) : "); if (!in_array($type, $allowed_types)) { $this->comment("The property type you provided is not valid. Valid types are 'array', 'boolean', 'integer', 'number', 'null', 'object', 'string'."); } } while (!in_array($type, $allowed_types)); // set property type $json_schema['properties'][$name]['type'] = $type; $stop = $this->ask("\nDo you want to add another property ? [Y]/n") == "Y" ? 0 : 1; $i++; } while (!$stop); $amenity->body = json_encode($json_schema); $amenity->save(); $this->info("Amenity '{$amenity->name}' has been saved."); return; }
public function anyTest() { try { if (!($data = Input::get('data'))) { if (!($data = \Request::getContent())) { return false; } } if (empty($data)) { return false; } $data = json_decode($data, true); $data['softwareAgent_id'] = strtolower($data['softwareAgent_id']); try { $this->createPostSoftwareAgent($data); } catch (Exception $e) { return serialize([$e->getMessage()]); } try { $activity = new Activity(); $activity->softwareAgent_id = $data['softwareAgent_id']; $activity->save(); } catch (Exception $e) { // Something went wrong with creating the Activity $activity->forceDelete(); return serialize([$e->getMessage()]); } $entity = new Entity(); $entity->format = 'image'; $entity->domain = $data['domain']; $entity->tags = $data['tags']; $entity->documentType = $data['documentType']; $entity->softwareAgent_configuration = $data['softwareAgent_configuration']; if (isset($data['parents'])) { $entity->parents = $data['parents']; } $entity->content = $data['content']; if (isset($data['threshold'])) { $entity->threshold = $data['threshold']; } if (isset($data['relevantFeatures'])) { $entity->recognizedFeatures = $data['relevantFeatures']; } if (isset($data['hash'])) { $entity->hash = $data['hash']; } else { $entity->hash = md5(serialize($data['content'])); } $entity->activity_id = $activity->_id; if (Entity::where('hash', $entity->hash)->first()) { //dd('asdasd'); } else { $entity->save(); } return Response::json($entity); } catch (Exception $e) { dd($e->getMessage()); } }
protected function importGovernor($row) { $url = $this->_baseUrl . $row['url']; if (!$this->browser->get($url)->responseIsError()) { $text = $this->browser->getResponseText(); $text = LsHtml::replaceEntities($text); //preg_match('/>Family\:<\/b>([^<]*)<br/is',$text,$family_arr); $name = trim(str_ireplace('Gov.', '', $row['name'])); $this->printDebug(''); $this->printDebug($name . ':'); $governor = PersonTable::parseFlatName($name); $governor->addExtension('PoliticalCandidate'); $governor->addExtension('ElectedRepresentative'); $governor->is_state = 1; $similar = $governor->getSimilarEntitiesQuery(true)->execute(); foreach ($similar as $s) { $sim_re = LsString::escapeStringForRegex($s->name_first); $search_re = LsString::escapeStringForRegex($governor->name_first); if (preg_match('/^' . $sim_re . '/su', $governor->name_first) == 0 && preg_match('/^' . $search_re . '/su', $s->name_first) == 0) { continue; } $bio = $s->getExtendedBio(); if (preg_match('/\\bgovernor(ship)?\\b/isu', $bio)) { $governor = $s; $this->printDebug(' Found existing governor: ' . $s->name . ' ' . $s->id); break; } } $governor->save(); $this->printDebug($governor->id); if (!$governor->start_date && preg_match('/>Born\\:<\\/b>([^<]*)<br/is', $text, $birth_arr)) { $this->printDebug(' Birthdate: ' . $birth_arr[1]); $governor->start_date = trim($birth_arr[1]); } if (!$governor->birthplace && preg_match('/>Birth State\\:<\\/b>([^<]*)<br/is', $text, $birth_state_arr)) { $this->printDebug(' Birthplace: ' . trim($birth_state_arr[1])); $governor->birthplace = trim($birth_state_arr[1]); } //PARTY MEMBERSHIP if (preg_match('/>Party\\:<\\/b>([^<]*)<br/is', $text, $party_arr)) { $party_str = $party_arr[1]; $this->printDebug(' Party: ' . $party_str); if (stristr($party_str, 'Democrat')) { $party = EntityTable::getByExtensionQuery('PoliticalParty')->addWhere('name = ?', 'Democratic Party')->fetchOne(); } if (stristr($party_str, 'Republican')) { $party = EntityTable::getByExtensionQuery('PoliticalParty')->addWhere('name = ?', 'Republican Party')->fetchOne(); } if (isset($party) && $party && !$governor->party_id) { $governor->Party = $party; $governor->is_independent = false; $this->printDebug(' Added membership in ' . $party); } else { if (stristr($party_str, 'Independent')) { $governor->is_independent = true; } } } if (!$governor->summary && preg_match_all('/>([^<]{240,})/isu', $text, $bio_match)) { $str = ''; foreach ($bio_match[1] as $b) { if (!stristr($b, 'Javascript')) { $str .= "\n\n" . $b; } } $str = trim($str); if (strlen($str)) { $governor->summary = $str; } } $governor->save(); $governor->addReference($url, null, $governor->getAllModifiedFields(), 'Governors Association'); //SCHOOLS if (preg_match('/>School\\(s\\)\\:<\\/b>([^<]*)<br/is', $text, $school_arr)) { $school_names = explode(';', trim($school_arr[1])); if (count($school_names) == 1) { $school_names = explode(',', $school_names[0]); } foreach ($school_names as $school_name) { $school_name = trim($school_name); if (!($school = EntityTable::getByExtensionQuery('School')->leftJoin('e.Alias a')->addWhere('e.name = ? or a.name = ?', array($school_name, $school_name))->fetchOne())) { $school = new Entity(); $school->addExtension('Org'); $school->addExtension('School'); $school->name = $school_name; $school->save(); $this->printDebug(' Added School: ' . $school_name); } $q = RelationshipTable::getByCategoryQuery('Education')->addWhere('entity1_id = ? and entity2_id = ?', array($governor->id, $school->id))->fetchOne(); if (!$q) { $relationship = new Relationship(); $relationship->setCategory('Education'); $relationship->Entity1 = $governor; $relationship->Entity2 = $school; $relationship->is_current = 0; $relationship->save(); $relationship->addReference($url, null, $relationship->getAllModifiedFields(), 'Governors Association'); $this->printDebug(' Added education: ' . $relationship->name); } } } //GOVERNOR OFFICE AND POSITION $office_name = 'Office of the Governor of ' . $row['state']; if (!($office = EntityTable::getByExtensionQuery('GovernmentBody')->addWhere('name = ?', $office_name)->fetchOne())) { $office = new Entity(); $office->name = $office_name; $office->addExtension('Org'); $office->addExtension('GovernmentBody'); $state = Doctrine::getTable('AddressState')->findOneByName($row['state']); if ($state) { $office->state_id = $state->id; } $office->save(); $office->addReference($url, null, $office->getAllModifiedFields(), 'Governors Association'); $this->printDebug(' Added office: ' . $office->name); } $q = RelationshipTable::getByCategoryQuery('Position')->addWhere('entity1_id = ? and entity2_id = ? and description1 = ?', array($governor->id, $office->id, 'Governor'))->fetchOne(); if (!$q) { sort($row['years']); $i = 0; while ($i < count($row['years'])) { $governorship = new Relationship(); $governorship->setCategory('Position'); $governorship->Entity1 = $governor; $governorship->Entity2 = $office; $governorship->description1 = 'Governor'; $governorship->start_date = $row['years'][$i]; $i++; if (isset($row['years'][$i])) { $governorship->end_date = $row['years'][$i]; $governorship->is_current = 0; if (!$governor->blurb && !isset($row['years'][$i + 1])) { $governor->blurb = 'Former Governor of ' . $row['state']; } } else { $governorship->is_current = 1; if (!$governor->blurb) { $governor->blurb = 'Governor of ' . $row['state']; } } $governor->save(); $i++; $governorship->save(); $governorship->addReference($url, null, $governorship->getAllModifiedFields(), 'Governors Association'); $this->printDebug(' Added governorship: ' . $governorship->name); } } //SPOUSE if (preg_match('/>Spouse\\:<\\/b>(.*?)<br/is', $text, $spouse_arr)) { $spouse = trim(LsHtml::stripTags($spouse_arr[1])); $q = RelationshipTable::getByCategoryQuery('Family')->addWhere('entity1_id = ? or entity2_id = ?', array($governor->id, $governor->id))->fetchOne(); if (!$q && strlen($spouse)) { $spouse = PersonTable::parseFlatName($spouse); $spouse->save(); $this->printDebug(' Added spouse: ' . $spouse->name); $relationship = new Relationship(); $relationship->setCategory('Family'); $relationship->Entity1 = $spouse; $relationship->Entity2 = $governor; $relationship->description1 = 'Spouse'; $relationship->description2 = 'Spouse'; $relationship->save(); $relationship->addReference($url, null, $relationship->getAllModifiedFields(), 'Governors Association'); $this->printDebug(' Added spouse relationship: ' . $relationship->name); } } //ADDRESS --not working, malformed addresses /* if (preg_match('/>Address\:\s*<\/b>(.*?)<b>/is',$text,$address_arr)) { $address = trim(str_replace('<br/>',', ',$address_arr[1])); $this->printDebug($address); if ($governor->Address->count() == 0 && $a = $governor->addAddress($address)) { $this->printDebug(' Address: ' . $a); $governor->save(); } }*/ //PHONE NUMBER if (preg_match('/>Phone\\(s\\)\\:<\\/b>([^<]*)<br/is', $text, $phone_arr)) { $phone_number = trim($phone_arr[1]); if (!$governor->Phone->count()) { $phone = $governor->addPhone($phone_number); $this->printDebug(' Phone: ' . $phone); } } if (!$governor->Image->count() && preg_match('/<img .*?class\\="display" src\\="([^"]*)"/is', $text, $img_arr)) { $url = $img_arr[1]; try { $fileName = ImageTable::createFiles($url, $governor->name_first); } catch (Exception $e) { $fileName = null; } if ($fileName) { //insert image record $image = new Image(); $image->filename = $fileName; $image->entity_id = $governor->id; $image->title = $governor->name; $image->caption = 'From Governors Association website'; $image->is_featured = true; $image->is_free = false; $image->url = $url; $image->save(); $this->printDebug("Imported image: " . $image->filename); } } } }
protected function import($url) { $person = null; $this->printDebug($url); if (!$this->browser->get($url)->responseIsError()) { $text = $this->browser->getResponseText(); $bio = null; $name = null; $netWorth = null; $birthYear = null; $schools = null; $schools = null; $imageUrl = null; $rank = null; //get name & rank if ($this->year > 2005 && preg_match('/<b>#(\\d+) ([^<]+)<\\/b>/', $text, $match)) { $name = trim($match[2]); $rank = $match[1]; } if ($this->year == 2005 && preg_match('/<h2>#(\\d+) ([^<]+)<\\/h2>/', $text, $match)) { $name = trim($match[2]); $rank = $match[1]; } //get net worth if (preg_match('/Net Worth<\\/span> <span class="red">\\$([\\S]+) billion/', $text, $match)) { $netWorth = $match[1] * 1000000000; } //get birth year if (preg_match('/>Age<\\/span> (\\d+)/', $text, $match)) { $birthYear = date("Y") - $match[1] . "-00-00"; } //get schools if (preg_match('/Education<\\/span>(.*)<\\/td>/isU', $text, $match)) { $schools = array(); $schoolParts = explode('<br>', $match[1]); while ($schoolPart = current($schoolParts)) { if (preg_match('/^([^,]+),\\s+<b>([^<]+)<\\/b>/is', trim($schoolPart), $match)) { $schoolOrg = trim($match[1]); if ($schoolOrg == 'High School') { next($schoolParts); continue; } $schoolDegree = trim($match[2]); $schools[] = array('org' => $schoolOrg, 'degree' => $schoolDegree); } next($schoolParts); } } if (preg_match('#<br>[\\n\\s]<br>(.+?)<br>[\\n\\s]<br>[\\n\\s]<img#isU', $text, $match)) { $bio = strip_tags(trim($match[1])); } else { $wikipedia = new LsWikipedia(); if ($wikipedia->request($name)) { $bio = $wikipedia->getIntroduction(); } } //get image $regexp = '#([A-Z1-9]{4}).html#'; if (preg_match($regexp, $url, $match)) { $imageFilename = $match[1] . ".jpg"; $imageUrl = $this->list_urls[$this->year]['img_src'] . $imageFilename; } //echo "Rank: " . $rank . "\n"; $this->printDebug("Rank: " . $rank); $this->printDebug("Name: " . $name); $this->printDebug("Image: " . $imageUrl); $this->printDebug("Net worth: " . $netWorth); $this->printDebug("Birth year: " . $birthYear); $this->printDebug("Bio: " . $bio); $person = $this->generatePerson($name, $bio); $person_exists = $this->getBusinessPersonQuery()->addWhere("person.name_first = ? AND person.name_last = ?", array($person->name_first, $person->name_last))->fetchOne(); if ($person_exists != false) { $this->printDebug('Person exists'); $person = $person_exists; } else { $this->printDebug('Saving new person'); } //parse name and create person object $person->addExtension('BusinessPerson'); $person->start_date = $person->start_date == null ? $birthYear : $person->start_date; $person->summary = $person->summary == null ? $bio : $person->summary; $person->net_worth = $person->net_worth == null ? $netWorth : $person->net_worth; //go through schools person attended foreach ($schools as $school) { //does the current school exist? $current_school = EntityTable::getByExtensionQuery('Org')->addWhere("org.name = ?", $school['org'])->fetchOne(); if ($current_school) { $this->printDebug(" Found School " . $school['org']); } else { //clear cache Doctrine::getTable('ExtensionDefinition')->clear(); $current_school = new Entity(); $current_school->addExtension('Org'); $current_school->addExtension('School'); $current_school->name = LsLanguage::titleize($school['org']); $current_school->save(); $current_school->addReference($source = $url, $excerpt = null, $fields = array('name'), $name = 'Forbes.com', $detail = null, $date = null); $this->printDebug(" Adding new school: " . $school['org']); } //if there is no relationship between person and school. connect them! if (!$person->getRelationshipsWithQuery($current_school, RelationshipTable::EDUCATION_CATEGORY)->fetchOne()) { $this->printDebug(" Creating Relation between " . $current_school->name . " and " . $person->name); $education = new Relationship(); $education->Entity1 = $person; $education->Entity2 = $current_school; $education->setCategory('Education'); $education->description1 = $school['degree']; $education->is_current = 1; $education->save(); $education->addReference($source = $url, $excerpt = null, $fields = array('description1'), $name = 'Forbes.com', $detail = null, $date = null); } } $person->save(); $person->addReference($source = $url, $excerpt = null, $fields = array('name_prefix', 'name_first', 'name_middle', 'name_last', 'name_suffix', 'name_nick', 'summary', 'net_worth', 'start_date'), $name = 'Forbes.com', $detail = null, $date = null); $this->saveToList($person, $rank); $this->attachImage($person, $imageUrl); } else { echo "Couldn't get person: " . $url . "\n"; } }
protected function processRow($row) { foreach ($row as &$r) { $r = trim($r); } $edit = array('Search Name' => $row['name'], 'Affiliation Name' => $row['affiliation1'], 'Similar Names' => array(), 'New Person' => null, 'Existing Person' => null, 'New Org' => null, 'Existing Org' => null, 'New Relationship' => null); try { $this->db->beginTransaction(); $person = null; $search_person = PersonTable::parseFlatName($row['name']); $similar = $search_person->getSimilarEntitiesQuery(true)->execute(); $matched_bio = false; $similar_ids = array(); foreach ($similar as $s) { $similar_ids[] = $s->id; $sim_re = LsString::escapeStringForRegex($s->name_first); $search_re = LsString::escapeStringForRegex($search_person->name_first); if (preg_match('/^' . $sim_re . '/su', $search_person->name_first) == 0 && preg_match('/^' . $search_re . '/su', $s->name_first) == 0) { continue; } $matched = false; $affils = array(); $ct = 1; $matched_affils = array(); $unmatched_affils = array(); while (isset($row['affiliation' . $ct]) && trim($row['affiliation' . $ct]) != '') { $affil = trim($row['affiliation' . $ct]); $org = $s->checkAffiliations(array($affil)); if ($org) { $matched_affils[] = array($org, $affil); $edit['Existing Org'] = $org->id; break; } else { $unmatched_affils[] = $affil; } $ct++; } if (count($matched_affils)) { $person = $s; break; //$ret[] = array('person' => $s, $matched_affils, $unmatched_affils); } else { /*$str = implode(' ', $unmatched_affils); if (isset($row['bio'])) { $str .= ' ' . $row['bio']; }*/ $bio = $s->getExtendedBio(); foreach ($unmatched_affils as $affil) { $affil = OrgTable::removeSuffixes($affil); $this->printDebug($affil); $this->printDebug($bio); if (preg_match('/' . OrgTable::getNameRegex($affil) . '/su', $bio)) { $matched_bio = true; break; } } if ($matched_bio) { $person = $s; break; } else { $this->printDebug(' ' . $s->name . ' failed'); } } } $edit['Similar Names'] = array_slice($similar_ids, 0, 5); $no_match = false; if (!$person) { if (isset($row['bio']) && trim($row['bio']) != '') { $search_person->summary = $row['bio']; } $search_person->save(); $this->printDebug(' not found, new person saved: ' . $search_person->name); $search_person->addReference($this->source_url, null, null, $this->source_name); $no_match = true; $edit['New Person'] = $search_person->id; $person = $search_person; } else { if (isset($row['bio']) && trim($row['bio']) != '' && !$person->summary) { $person->summary = $row['bio']; $person->save(); } $this->printDebug(' **person found: ' . $person->name); $edit['Existing Person'] = $person->id; } if ($matched_bio || $no_match) { $orgs = OrgTable::getOrgsWithSimilarNames($row['affiliation1'], true); $max = -1; $affiliated_org = null; foreach ($orgs as $org) { $this->printDebug(' found match: ' . $org->name); $ct = $org->getRelatedEntitiesQuery('Person', RelationshipTable::POSITION_CATEGORY, null, null, null, false, 2)->count(); if ($ct > $max) { $affiliated_org = $org; $edit['Existing Org'] = $affiliated_org->id; $max = $ct; } } if (!$affiliated_org) { $affiliated_org = new Entity(); $affiliated_org->addExtension('Org'); if (isset($row['affiliation1_extensions']) && $row['affiliation1_extensions'] != '') { $extensions = explode(',', $row['affiliation1_extensions']); foreach ($extensions as $ext) { $ext = trim($ext); if (in_array($ext, ExtensionDefinitionTable::$extensionNames)) { $affiliated_org->addExtension($ext); } } } else { //$affiliated_org->addExtension('Business'); } $affiliated_org->name = $row['affiliation1']; $affiliated_org->save(); $affiliated_org->addReference($this->source_url, null, null, $this->source_name); $edit['New Org'] = $affiliated_org->id; } $rel = new Relationship(); $rel->Entity1 = $person; $rel->Entity2 = $affiliated_org; $rel->setCategory('Position'); if (isset($row['affiliation1_title']) && $row['affiliation1_title'] != '') { $description = trim($row['affiliation1_title']); $rel->description1 = $description; if ($description == 'Director' || $description == 'Trustee' || preg_match('/^Chair/su', $description)) { $rel->is_board = 1; $rel->is_employee = 0; } } $rel->save(); $rel->addReference($this->source_url, null, null, $this->source_name); $edit['New Relationship'] = $rel->id; } if (isset($row['start_date']) && trim($row['start_date']) != '') { $edit['Relationship']['start_date'] = trim($row['start_date']); } if (isset($row['end_date']) && trim($row['end_date']) != '') { $edit['Relationship']['end_date'] = trim($row['end_date']); } if (isset($row['title']) && trim($row['title']) != '') { $edit['Relationship']['title'] = trim($row['title']); } if (isset($row['notes']) && trim($row['notes']) != '') { $edit['Relationship']['notes'] = trim($row['notes']); } if (isset($row['rank']) && $row['rank'] != '') { $edit['rank'] = $row['rank']; } $this->db->commit(); } catch (Exception $e) { $this->db->rollback(); throw $e; } $this->edits[] = $edit; }
function import(Entity $person, $possible_persons) { //loop through the people we found. usually just one. foreach ($possible_persons as $possible_person) { $this->printDebug('Query returned ' . count($possible_person) . ' person named ' . $possible_person->name); //this person does not provide education. we skip if (count($possible_person->education)) { $this->printDebug('Education found'); } else { $this->printDebug('No education history found'); continue; } //get employement info for this possible match $possible_person_bio = $possible_person->summary; if (count($possible_person->employment_history)) { foreach ($possible_person->employment_history as $employment) { $possible_person_bio .= ' ' . $employment->company . " "; } $this->printDebug('Employment found'); } else { $this->printDebug('No employment history found'); continue; } //get employment info for the person in our database $relationship_orgs = $person->getRelatedEntitiesQuery('Org', RelationshipTable::POSITION_CATEGORY, null, null, null, false, 1)->execute(); $person_bio = $person->summary; foreach ($relationship_orgs as $org) { $person_bio .= ' ' . $org->name; } //lets see how many matches we get $matches = LsLanguage::getCommonPronouns($person_bio, trim($possible_person_bio), LsLanguage::$business); if (count($matches)) { foreach ($possible_person->education as $school) { $school->institution = mb_convert_encoding($school->institution, 'UTF-8'); $school->institution = preg_replace('//isu', ' ', $school->institution); $this->printDebug('Looking for the school: ' . $school->institution); $current_school = EntityTable::findByAlias($school->institution, $context = 'bw_school'); //find school if ($current_school) { $this->printDebug('Found school'); } else { $current_school = EntityTable::getByExtensionQuery(array('Org', 'School'))->addWhere('LOWER(org.name) LIKE ?', '%' . strtolower($school->institution) . "%")->fetchOne(); if (!$current_school) { $new_school = new Entity(); $new_school->addExtension('Org'); $new_school->addExtension('School'); $new_school->name = $school->institution; $wikipedia = new LsWikipedia(); $wikipedia->request($school->institution); if ($wikipedia->execute() && !$wikipedia->isDisambiguation()) { $info_box = $wikipedia->getInfoBox(); if (isset($info_box['students']) && preg_match('/([\\d\\,]{2,})/isu', $info_box['students']['clean'], $match)) { $new_school->students = LsNumber::clean($match[1]); } else { $student_types = array('undergrad', 'postgrad', 'grad', 'doctoral'); $num_students = 0; foreach ($student_types as $st) { if (isset($info_box[$st]) && preg_match('/([\\d\\,]{2,})/isu', $info_box[$st]['clean'], $match)) { $num_students += LsNumber::clean($match[1]); } } if ($num_students > 0) { $new_school->students = $num_students; } } if (isset($info_box['faculty']) && preg_match('/([\\d\\,]{2,})/isu', $info_box['faculty']['clean'], $match)) { $new_school->faculty = LsNumber::clean($match[1]); } if (isset($info_box['type'])) { if (stristr($info_box['type']['clean'], 'public')) { $new_school->is_private = 0; } else { if (stristr($info_box['type']['clean'], 'private')) { $new_school->is_private = 1; } } } if (isset($info_box['endowment'])) { if (preg_match('/(\\$[\\d\\,\\.\\s]+)(million|billion)/isu', $info_box['endowment']['clean'], $match)) { if (strtolower($match[2]) == 'billion') { $factor = 1000000000; } else { $factor = 1000000; } $new_school->endowment = LsNumber::formatDollarAmountAsNumber($match[1], $factor); } } if (isset($info_box['established'])) { $year = null; if ($date = LsDate::convertDate($info_box['established']['clean'])) { $new_school->start_date = $date; } else { if (preg_match('/\\b(\\d\\d\\d\\d)\\b/isu', $info_box['established']['clean'], $match)) { $new_school->start_date = $match[1]; } } } $summary = trim($wikipedia->getIntroduction()); $summary = preg_replace('/\\n\\s*\\n/isu', '', $summary); if (strlen($summary) > 10) { $new_school->summary = $summary; } $new_school->save(); $new_school->addReference($source = $wikipedia->getUrl(), $excerpt = null, $fields = array('summary'), $name = 'Wikipedia'); } else { $new_school->save(); } $current_school = $new_school; $this->printDebug('Adding new school'); } $alias = new Alias(); $alias->name = $school->institution; $alias->context = 'bw_school'; $alias->Entity = $current_school; $alias->save(); } //find degree $degree = null; if (!($degree = DegreeTable::getByText($school->degree))) { $degree = DegreeTable::addDegree($school->degree); $this->printDebug('Adding new degree'); } //find relationship $relationship = null; $relationships = $person->getRelationshipsWithQuery($current_school, RelationshipTable::EDUCATION_CATEGORY)->execute(); foreach ($relationships as $existing_relationship) { if ($existing_relationship->degree_id == $degree->id) { $relationship = $existing_relationship; break; } } if ($relationship) { $this->printDebug('Relationship between person and school exists'); } else { $relationship = new Relationship(); $relationship->Entity1 = $person; $relationship->Entity2 = $current_school; $relationship->description1 = 'student'; $relationship->is_current = 0; if ($school->year) { $relationship->end_date = $school->year; } $relationship->setCategory('Education'); $this->printDebug('Creating new relationship between person and school'); } //save $relationship->save(); //add degree and reference if ($relationship->degree_id == null) { $reference_name = strstr($school->source, 'wikipedia') ? "Wikipedia" : "BusinessWeek"; $relationship->Degree = $degree; $relationship->save(); $relationship->addReference($source = $school->source, $excerpt = null, $fields = array('degree_id'), $name = $reference_name, $detail = null, $date = null); $this->printDebug('Adding degree and reference'); } } } else { $this->printDebug('No organization matches'); return false; } } return true; }
public function execute() { if (!$this->safeToRun('uk-mp-candidates')) { $this->printDebug('Script already running'); die; } // Get (or create) the UK local Network $uk = Doctrine::getTable('LsList')->findOneByName('United Kingdom'); if (!$uk) { $uk = new LsList(); $uk->name = 'United Kingdom'; $uk->is_network = 1; $uk->description = 'People and organizations with significant influence on the policies of the United Kingdom'; $uk->display_name = 'uk'; $uk->save(); } // Get the MP list $raw = $this->getMPs(); // Add new MPs to the list foreach ($raw as $mp) { $this->printDebug(sprintf('Processing %s', $mp['name'])); // Split name $entity = PersonTable::parseFlatName($mp['name']); $entity->blurb = 'Prospective Parliamentary Candidate for ' . $mp['constituency']; $q = TagTable::getByTripleQuery('yournextmp', 'url', $mp['url']); $r = $q->count(); if ($r) { $this->printDebug('Already processed, skipping.'); continue; } // Get political party $q = EntityTable::getByExtensionQuery('PoliticalParty')->addWhere('e.name = ?', $mp['party']); if (!($partyEntity = $q->fetchOne())) { $partyEntity = new Entity(); $partyEntity->addExtension('Org'); $partyEntity->addExtension('PoliticalParty'); $partyEntity->name = $mp['party']; $partyEntity->blurb = 'UK Political Party'; $partyEntity->save(null, true, array($uk->id)); $this->printDebug("Created new political party: " . $mp['party']); } // Save entity to UK Network $entity->party_id = $partyEntity->id; $entity->save(null, true, array($uk->id)); // Add party relationship $r = new Relationship(); $r->entity1_id = $entity->id; $r->entity2_id = $partyEntity->id; $r->setCategory('Membership'); $r->description1 = 'Prospective parliamentary candidate'; $r->is_current = true; // $r->start_date = // Don't know where we can get this, and "now" seems kind of wrong $r->save(); // Add YourNextMP triple $entity->addTagByTriple('yournextmp', 'url', $mp['url']); // Add references $ref = new Reference(); $ref->addFields(array('name_first', 'name_last', 'name_middle')); // Don't need this $ref->source = $mp['url']; $ref->name = 'YourNextMP.com - ' . $entity['name']; $ref->object_model = 'Entity'; $ref->object_id = $entity->getId(); $ref->save(); unset($ref); $ref = new Reference(); $ref->addFields(array('name')); $ref->source = $mp['party_url']; $ref->name = 'YourNextMP.com - ' . $partyEntity['name']; $ref->object_model = 'Entity'; $ref->object_id = $partyEntity->getId(); $ref->save(); unset($ref); $ref = new Reference(); $ref->addFields(array('name')); $ref->source = $mp['url']; $ref->name = 'YourNextMP.com - ' . $entity['name']; $ref->object_model = 'Relationship'; $ref->object_id = $r->getId(); $ref->save(); unset($ref); $r->free(true); unset($r); // Add image? if ($mp['image']) { if ($fileName = ImageTable::createFiles($mp['image'])) { //insert image record $image = new Image(); $image->filename = $fileName; $image->title = $entity['name']; $image->caption = 'From YourNextMP under CC-BY-SA license.'; $image->is_featured = true; $image->is_free = true; $image->url = $mp['image']; $this->printDebug("Imported image: " . $image->filename); } $image->Entity = $entity; $image->save(); if ($mp['image']) { //save image source $image->addReference($mp['image']); $this->printDebug("Saved image reference"); } unset($image); } // Add party image? if ($mp['party_image']) { if ($fileName = ImageTable::createFiles($mp['party_image'])) { //insert image record $partyImage = new Image(); $partyImage->filename = $fileName; $partyImage->title = $partyEntity['name']; $partyImage->caption = 'From YourNextMP under CC-BY-SA license.'; $partyImage->is_featured = true; $partyImage->is_free = true; $partyImage->url = $mp['party_image']; $this->printDebug("Imported image: " . $partyImage->filename); } $partyImage->Entity = $partyEntity; $partyImage->save(); if ($mp['party_image']) { //save image source $partyImage->addReference($mp['party_image']); $this->printDebug("Saved image reference"); } unset($partyImage); } unset($entity); unset($partyEntity); } }
public function save($data) { $transaction = $this->beginTransaction(); try { $this->setData($data); if (!$this->isPersistent()) { $entity = new Entity(); $entity->setAlias('lu_' . $data->name . '_' . $data->idFrame); $entity->setType('LU'); $entity->save(); $this->setIdEntity($entity->getId()); } $this->setActive(true); parent::save(); $transaction->commit(); } catch (\Exception $e) { $transaction->rollback(); throw new \Exception($e->getMessage()); } }
public function processCongressMemberRow($row) { $this->_references['bioguide'] = new Reference(); $this->printDebug("\nProcessing member with name " . $row->name . " and ID " . $row->id); try { //we have to begin the transaction here because matchInDatabase might merge Entities and save $this->db->beginTransaction(); //check that congress member isn't a repeat $member = EntityTable::getByExtensionQuery('ElectedRepresentative')->addWhere('electedrepresentative.bioguide_id = ?', $row->id)->fetchOne(); //if member hasn't been imported already as a member of congress, //create with all bio info and look for a match if (!$member) { $member = $this->importNewMember($row); $member = $this->matchInDatabase($member); /* if(!$merged_member = $this->matchInDatabase($member)) { $redundant_member = false; } else { $redundant_member = $member; $member = $merged_member; } */ } else { $this->printDebug("Member exists in database with entity ID: " . $member->id); //if member is tagged with this session, skip if (in_array($member->id, $this->_existingSessionMemberIds)) { $this->db->rollback(); $this->printDebug("Member has already been tagged with session " . current($this->_sessions) . "; skipping"); return $member; } //update member's bio $this->updateBio($member); $this->printDebug("Updated member bio"); //check if member is continuing from previous session $q = $member->getTripleTagsQuery('congress', 'session')->addWhere('tag.triple_value = ?', current($this->_sessions) - 1); if ($q->count()) { $this->printDebug("Continuing member from previous session..."); //if member continuing, look for relationship with opposite chamber to end, //in case the member's switched chambers $oppositeChamberId = $row->type == 'Senator' ? $this->_houseEntityId : $this->_senateEntityId; $q = LsDoctrineQuery::create()->from('Relationship r')->where('r.entity1_id = ? AND r.entity2_id = ?', array($member->id, $oppositeChamberId))->andWhere('r.category_id = ? AND r.end_date IS NULL', RelationshipTable::MEMBERSHIP_CATEGORY); foreach ($q->execute() as $rel) { $rel->end_date = $this->_sessionStartYear - 1 . '-00-00'; $rel->is_current = false; $rel->save(); $this->printDebug("Ended relationship " . $rel->id . " with opposite chamber from previous session"); } //if no current relationships with same chamber, create one $thisChamberId = $row->type == 'Senator' ? $this->_senateEntityId : $this->_houseEntityId; $q = LsDoctrineQuery::create()->from('Relationship r')->where('r.entity1_id = ? AND r.entity2_id = ?', array($member->id, $thisChamberId))->andWhere('r.category_id = ? AND r.end_date IS NULL', RelationshipTable::MEMBERSHIP_CATEGORY); if (!$q->count()) { $this->printDebug("No relationships with this chamber from previous session; creating new one..."); $r = new Relationship(); $r->entity1_id = $member->id; $r->entity2_id = $row->type == 'Senator' ? $this->_senateEntityId : $this->_houseEntityId; $r->setCategory('Membership'); $r->description1 = $row->type; $r->start_date = $row->termStart . '-00-00'; $r->is_current = true; if ($row->type = 'Senator') { $this->_senateRelationships[] = $r; } else { $this->_houseRelationships[] = $r; } } } else { $this->printDebug("Member not continuing from previous session; creating new relationship..."); //if member not continuing, add a new relationship for this session and chamber $r = new Relationship(); $r->entity1_id = $member->id; $r->entity2_id = $row->type == 'Senator' ? $this->_senateEntityId : $this->_houseEntityId; $r->setCategory('Membership'); $r->description1 = $row->type; $r->start_date = $row->termStart . '-00-00'; $r->is_current = true; if ($row->type = 'Senator') { $this->_senateRelationships[] = $r; } else { $this->_houseRelationships[] = $r; } } } //set party name $partyName = null; $blurb = ''; if ($party = $row->party) { if ($party == 'Democrat') { $blurb .= $party . 'ic'; $partyName = 'Democratic Party'; } elseif ($party == 'Independent') { $blurb .= $party; $partyName = null; } else { $blurb .= $party; $partyName = $party . ' Party'; } //if party entity doesn't exist, create one if ($partyName) { $q = EntityTable::getByExtensionQuery('PoliticalParty')->addWhere('e.name = ?', $partyName); if (!($partyEntity = $q->fetchOne())) { $partyEntity = new Entity(); $partyEntity->addExtension('Org'); $partyEntity->addExtension('PoliticalParty'); $partyEntity->name = $partyName; $partyEntity->save(); $this->printDebug("Created new political party: " . $partyName); } } //create current party affiliation if session is member's most recent session if ($member->exists()) { $q = $member->getTripleTagsQuery('congress', 'session')->addWhere('tag.triple_value > ?', current($this->_sessions)); $setParty = $q->count() ? false : true; } else { $setParty = true; } if ($setParty) { if ($partyName) { $member->Party = $partyEntity; $member->is_independent = false; $this->printDebug("Set current political affiliation to " . $partyName); } else { $member->is_independent = true; $member->party_id = null; $this->printDebug("Set current political affiliation to Independent"); } } } //save member $modified = $member->getAllModifiedFields(); $member->save(); $this->printDebug("Saved member with entity ID: " . $member->id); $this->addListMember($member); //set member reference fields $excludeFields = array(); foreach ($this->_references as $key => $ref) { $ref->object_model = 'Entity'; $ref->object_id = $member->id; if ($key != 'bioguide') { $ref->save(); $excludeFields = array_merge($excludeFields, $ref->getFieldsArray()); } } $modified = array_diff($modified, $excludeFields); $this->_references['bioguide']->addFields($modified); $this->_references['bioguide']->save(); $this->printDebug("Saved member references"); //tag member with congress session $member->addTagByTriple('congress', 'session', current($this->_sessions)); $this->printDebug("Added tag for session " . current($this->_sessions)); //save image, if any if ($this->_image) { $this->_image->Entity = $member; $this->_image->save(); $this->printDebug("Saved member image"); if ($this->_photoUrl) { //save image source $this->_image->addReference($this->_photoUrl); $this->printDebug("Saved image reference"); } } //create party membership relationships if ($partyName) { //if membership relationship with party doesn't exist, create it $partyRel = LsQuery::getByModelAndFieldsQuery('Relationship', array('entity1_id' => $member->id, 'entity2_id' => $partyEntity->id, 'category_id' => RelationshipTable::MEMBERSHIP_CATEGORY))->fetchOne(); if (!$partyRel) { $partyRel = new Relationship(); $partyRel->Entity1 = $member; $partyRel->Entity2 = $partyEntity; $partyRel->setCategory('Membership'); $modified = $partyRel->getAllModifiedFields(); $partyRel->save(); $partyRel->addReference($this->_profileUrlBase . $member->bioguide_id, null, $modified, 'Congressional Biographical Directory'); $this->printDebug("Created membership in political party: " . $partyName); } } $senator = null; //create senate relationships foreach ($this->_senateRelationships as $rel) { $modified = $rel->getAllModifiedFields(); $rel->save(); $rel->addReference($this->_profileUrlBase . $member->bioguide_id, null, $modified, 'Congressional Biographical Directory'); $this->printDebug("Saved Senate relationship"); } //create house relationships foreach ($this->_houseRelationships as $rel) { $modified = $rel->getAllModifiedFields(); $rel->save(); $rel->addReference($this->_profileUrlBase . $member->bioguide_id, null, $modified, 'Congressional Biographical Directory'); $this->printDebug("Saved House relationship"); } //save everything if (!$this->testMode) { $this->db->commit(); } } catch (Exception $e) { $this->db->rollback(); throw $e; } }