public function create_scope($from, $to, $anchor_text_id = NULL, $webpage_id = NULL) { if (is_int($anchor_text_id) === FALSE) { $text = $anchor_text_id; $this->_CI_load('library', 'scope/Scope_anchor_text'); $anchor_text_id = $this->CI->scope_anchor_text->filter_anchor_text_id($text); } if (NULL !== $webpage_id) { $key = $from . '-' . $to; if (is_object($webpage_id)) { $webpage_id = $webpage_id->get_id(); } $value = $webpage_id; $scope = get_cache($this, $key, $value); if (is_null($scope)) { $data = $this->_create_cond($webpage_id, $from, $to, $anchor_text_id); $scope = $this->find($data); if (is_null($scope)) { //test_msg('Annotation_scope->create_scope() not find, before create', $data); $scope = $this->create($data); } set_cache($scope, $key, $value); } return $scope; } else { $key = $from; $value = $to; $scope = get_cache($this, $key, $value); if (is_null($scope)) { $scope = new Annotation_scope(); $scope->set_index($from, $to); if (NULL != $anchor_text_id) { $scope->set_anchor_text($anchor_text_id); } set_cache($scope, $key, $value); } return $scope; } }
public function friend() { //先建立scope $url = 'http://www.plurk.com/p/6bk0s3#response-1771001736'; $this->load->library('scope/Annotation_scope'); $scope = new Annotation_scope(); $scope->set_index(48, 19); $scope->set_anchor_text('頭髮越來越長了……等系統做完回家再剪。所以頭髮越長就表示系統作越久 >_<'); $scope->set_webpage($url); //$scope->update(); //建立user_author user1 user2 $email_author = '*****@*****.**'; $email1 = '*****@*****.**'; $email2 = '*****@*****.**'; $user_author = $this->user->create_user($url, $email_author); $user1 = $this->user->create_user($url, $email1); $user2 = $this->user->create_user($url, $email2); //檢查一下是否大家都有ID $this->unit->run(NULL !== $user_author->get_id() && NULL !== $user1->get_id() && NULL !== $user2->get_id(), TRUE, '檢查一下是否大家都有ID'); $friend_count = $user_author->get_friends(TRUE)->length(); $this->unit->run($friend_count, 'is_int', '檢查一下user_author的friend數量'); //建立Annotation $this->load->library('kals_resource/Annotation'); $annotation = $this->annotation->create_annotation($user_author, $scope); $this->unit->run($user_author->get_friends(TRUE)->length(), $friend_count, '檢查一下user_author的friend數量,建立annotation之後'); //加入權限 $this->load->library('policy/Authorize_manager'); $this->load->library('policy/Action_annotation_read'); $action = new Action_annotation_read(); $this->authorize_manager->set_resource($annotation)->policy_add_actor($action, $user1); $this->unit->run($user_author->get_friends(TRUE)->length(), $friend_count + 1, '檢查一下user_author的friend數量,加入$user1'); $this->authorize_manager->policy_add_actor($action, $user2); $this->unit->run($user_author->get_friends(TRUE)->length(), $friend_count + 2, '檢查一下user_author的friend數量,加入user2'); $this->authorize_manager->policy_remove_actor($action, $user1); $this->unit->run($user_author->get_friends(TRUE)->length(), $friend_count + 1, '檢查一下user_author的friend數量,移除user1'); $match = FALSE; foreach ($user_author->get_friends() as $friends) { if ($friends->get_id() == $user2->get_id()) { $match = TRUE; break; } } $this->unit->run($match, TRUE, '檢查一下user_author的friend中,應該只剩下user1'); unit_test_report($this, __METHOD__); }
public static function exclude_scopes($scopes, $exclude_scopes) { if (count($scopes) === 0 || count($exclude_scopes) === 0) { return $scopes; } foreach ($exclude_scopes as $ex_key => $ex) { $ex_from = $ex->get_from_index(); $ex_to = $ex->get_to_index(); $ex_webpage_id = $ex->get_webpage_id(); //test_msg('Annotation_scope_collection->exclude_scopes() $exclude_scopes', array($ex_key, $ex_from, $ex_to, $ex_webpage_id)); foreach ($scopes as $key => $s) { $from = $s->get_from_index(); $to = $s->get_to_index(); $webpage_id = $s->get_webpage_id(); //只能減去相同網頁的範圍 if ($ex_webpage_id != $webpage_id) { continue; } if ($to < $ex_from or $from > $ex_to) { //SCOPE ########## //EXCLUDE ###### //NEW ########## //SCOPE ########## //EXCLUDE ###### //NEW ########## continue; } else { if ($from == $ex_from && $to == $ex_to or $from >= $ex_from && $to <= $ex_to) { //SCOPE ###### //EXCLUDE ###### //SCOPE ###### //EXCLUDE ######## //SCOPE ###### //EXCLUDE ######## //SCOPE ###### //EXCLUDE ######## unset($scopes[$key]); continue; } else { if ($from <= $ex_from && $to <= $ex_to) { //SCOPE ########## //EXCLUDE ###### //NEW ###### // 0123456 //test_msg('Annotation_scope_collection->exclude_scopes() $scopes', array($key, $from, $to, $webpage_id)); $new_from = $from; $new_to = $ex_from - 1; $scope = new Annotation_scope(); $scope->set_index($new_from, $new_to); $scope->set_webpage($webpage_id); $text = $s->get_text(); if (NULL !== $text) { $len = $ex_from - $from - 1; $text = mb_substr($text, 0, $len, 'UTF-8'); //test_msg(array($text, $len)); $scope->set_anchor_text($text); } $scopes[$key] = $scope; } else { if ($from >= $ex_from && $to >= $ex_to) { //SCOPE ########## //EXCLUDE ###### //NEW ######## // 12345678901234 $new_from = $ex_to + 1; $new_to = $to; $scope = new Annotation_scope(); $scope->set_index($new_from, $new_to); $scope->set_webpage($webpage_id); $text = $s->get_text(); if (NULL !== $text) { $len = $ex_to - $from + 1; $text = mb_substr($text, $len, strlen($text), 'UTF-8'); $scope->set_anchor_text($text); } $scopes[$key] = $scope; } else { if ($from < $ex_from && $to > $ex_to) { //SCOPE ########## //EXCLUDE ###### //NEW ## ## // 0123456789 $text = $s->get_text(); $new_from1 = $from; $new_to1 = $ex_from - 1; $scope1 = new Annotation_scope(); $scope1->set_index($new_from1, $new_to1); $scope1->set_webpage($webpage_id); if (NULL !== $text) { $len = $ex_from - $from; $text1 = mb_substr($text, 0, $len, 'UTF-8'); $scope1->set_anchor_text($text1); } $scopes[$key] = $scope1; //第二部分 $new_from2 = $ex_to + 1; $new_to2 = $to; $scope2 = new Annotation_scope(); $scope2->set_index($new_from2, $new_to2); $scope2->set_webpage($webpage_id); if (NULL !== $text) { $len = $ex_to - $from; $text2 = mb_substr($text, $len, strlen($text), 'UTF-8'); $scope2->set_anchor_text($text2); } $scopes[] = $scope2; } } } } } } } //$scopes = self::sort_scopes($scopes); return $scopes; }