3 // All mysql code should go here
4 // Split into multiple files if needed
6 function processContent($node_content) { return processContent_hack($node_content); } //XXX TODO FIXME HACK
8 // Called for every node view. Updates node views, neurons,
9 function update_nodes($user_id,$node_id,$referer_id) {
10 global $node,$db,$error;
11 $db->update("update nodes set node_views=node_views+1 where".
12 "node_id='".$node_id."'");
13 if (is_numeric($referer_id) && ($referer_id)) {
14 $q="update neurons set synapse=synapse+1 where ".
15 "dst='".$node_id."' and src='$referer_id'";
16 $result=$db->update($q);
18 $q="insert into neurons set synapse_creator='".
19 $user_id."',dst='".$node_id.
20 "',src='$referer_id',synapse=1";
27 // these 4 lines are not the source of kyberia lagging problems.
28 // leave them. started on the 10.4.
29 // data gained will be used for scientific purposes
31 $q="insert delayed into levenshtein set user_id='".
32 $user_id."',node_id='".$node_id."'";
38 function putNode($what,$where,$checkpermissions=true) {
39 global $db,$error,$error_messages;
40 $user_id = $_SESSION['user_id'];
44 if (!is_numeric($what) || !is_numeric($where)) {
45 $error=$error_messages['NUMERIC_NOT_NUMERIC'];
48 $nodeshell = nodes::getNodeById($where,$user_id);
49 $nodeshell_vector = $nodeshell['node_vector'];
51 if ($checkpermissions) {
52 $nodeshell_permissions = permissions::checkPermissions($nodeshell);
53 if (!$nodeshell_permissions['w']) {
54 $error = $error_messages['WRITE_PERMISSION_ERROR'];
59 $dst_vector = $nodeshell_vector.str_pad($chosen,VECTOR_CHARS,0,STR_PAD_LEFT);
60 $q = "update neurons set synapse_created=NOW(),link='hard',synapse=synapse+1, dst_vector='$dst_vector' where src='$what' and dst='$where'";
61 $result=$db->update($q);
63 if (!$result) $db->query("insert into neurons set synapse_creator='$user_id',synapse_created=NOW(), src='$what',dst='$where',dst_vector='$dst_vector',link='hard',synapse=1");
64 $db->query("update nodes set lastdescendant_created=NOW(),node_children_count=node_children_count+1 where node_id='$where'");
67 function addNode($params) {
68 global $db,$node,$error,$error_messages;
69 $parent_id=$params['node_parent'];
71 if (!is_numeric($parent_id)) {
72 $parent_id=$node['node_id'];
75 $parent_permissions=permissions::checkPermissions($parent_id);
76 if (!$parent_permissions['w']) {
77 $error=$error_messages['WRITE_PERMISSION_ERROR'];
78 logger::log('add','error','WRITE_PERMISSION_ERROR');
83 $kset=$db->query("select user_k from users where user_id='$params[node_creator]'");
85 $user_k=$kset->getString('user_k');
87 if ($params['flag']=='registration') $params['node_creator']=UBIK_ID;
89 if (!$user_k && $params['node_creator']!=UBIK_ID) {
90 $error=$error_messages['K_SPENT'];
96 $set=$db->query("select node_vector,node_children_count from nodes where node_id='$parent_id'");
98 $parent_vector=$set->getString('node_vector');
100 if ($set->getInt('node_children_count')>MAX_CHILDREN) {
101 $error=$error_messages['MAX_CHILDREN'];
105 //working with external links
106 $external_link=$params['external_link'];
108 $template_id=$params['template_id'];
109 if (!is_numeric($template_id)) $template_id=DEFAULT_TEMPLATE_ID;
111 if(!isset($params['node_system_access'])) $params['node_system_access'] = $node['node_system_access'];
112 if(!isset($params['node_external_access'])) $params['node_external_access'] = $node['node_external_access'];
114 $q="insert into nodes set
115 node_name='".$params['node_name']."',
116 node_external_access='".$params['node_external_access']."',
117 node_system_access='".$params['node_system_access']."',
118 node_creator='".$params['node_creator']."',
119 template_id='".$template_id."',
120 external_link='".$external_link."',
121 node_parent='".$parent_id."',
122 node_views=0,node_created=NOW(),
123 node_content='".$params['node_content']."',
124 node_vector='".$params['node_vector']."'";
125 $db->query("start transaction");
127 $id=$db->getLastInsertId();
129 //node_content MyIsam only for FULLTEXT !!!
130 // $db->query("insert into node_content set node_id='$id',node_content='".$params['node_content']."'");
132 $new_id=str_pad($id,VECTOR_CHARS,"0",STR_PAD_LEFT);
133 $new_vector=trim($parent_vector,"z").$new_id;
135 if ($params['flag']=='registration') $db->query("update nodes set node_system_access='public',node_external_access='yes',node_creator=$id where node_id=$id");
137 $db->query("update nodes set node_vector='$new_vector' where node_id='$id'");
138 $db->query("update nodes set node_children_count=node_children_count+1 where node_id='$parent_id'");
140 $node_vector=trim(chunk_split($new_vector,VECTOR_CHARS,';'),';');
142 $ancestors=explode(";",$node_vector);
143 foreach($ancestors as $key => $ancestor_id) {
145 $ancestor_id=ltrim($ancestor_id,'0');
146 $db->query("update nodes set node_descendant_count=node_descendant_count+1,lastdescendant_created=NOW() where node_id='$ancestor_id'");
149 $db->query("update node_access set node_user_subchild_count=node_user_subchild_count+1 where node_id='$parent_id'");
151 $db->query("update users set user_k='$user_k' where user_id='$params[node_creator]'");
152 $db->query("commit");
153 logger::log('add','ok',$id);
155 if ($_POST['code']) {
156 $params['node_creator']=UBIK_ID;
157 $params['node_parent']=WARNING_ZONE;
158 $params['node_name']="node $id added with code_parameter";
159 $params['node_content']="node <a href='/id/$id'>$id</a> added with code_parameter";
160 unset($_POST['code']);
161 nodes::addNode($params);
168 function getUserByLogin($login) {
169 global $error, $error_messages;
170 $q2="select user_id from users where login='".$login."'";
171 $userset=$db->query($q2);
173 $id=$userset->getString('user_id');
174 if (is_numeric($id)) return $id;
176 $error = $error_messages['USER_NOT_FOUND'];
181 function getNodeIdByName($name, $external_link=false) {
184 $qh = sprintf('select node_id from nodes where node_name = "%s"', mysql_real_escape_string($name));
186 $qh .= sprintf(' and external_link="%s"', mysql_real_escape_string($external_link));
188 $set = $db->query($qh);
190 return $set->getString('node_id');
193 function getNodeById($node_handle,$user_id, $table_name="nodes") {
195 $q="select length(concat($table_name.node_vector)) as
196 vector_depth,$table_name.*,$table_name.node_creator as
197 node_owner_id,creator.node_name as owner,node_access.*,$table_name.node_id as
198 node_id,node_parent.node_name as node_parent_name
199 from $table_name left join $table_name as creator on creator.node_id=$table_name.node_creator
200 left join $table_name as node_parent on $table_name.node_parent=node_parent.node_id
201 left join node_access on (node_access.node_id='$node_handle' and node_access.user_id='$user_id')
202 where $table_name.node_id='$node_handle'";
204 $result=$db->query($q);
205 if (!$result->next()) {
209 $node=addBase36id($result->getRecord());
210 $node['node_vector']=trim($node['node_vector'],"z");
211 $ancestors=str_split($node['node_vector'],VECTOR_CHARS);
212 foreach ($ancestors as $ancestor) {
213 $node['ancestors'][]=array("name"=>"","link"=>ltrim($ancestor,"0"));
216 transport_process_node($node);
222 function redirByName($node_handle) {
224 $user_id=$_SESSION['user_id'];
225 $set=$db->query("select node_id from nodes where node_name='$node_handle' and node_creator='$user_id'");
227 $node_id=$set->getString('node_id');
228 if (!empty($node_id)) {
229 return nodes::getNodeById($node_id,$_SESSION['user_id']);
233 $set=$db->query("select node_id from nodes where node_name='$node_handle' ");
235 $node_id=$set->getString('node_id');
236 if (!empty($node_id)) {
237 return nodes::getNodeById($node_id,$_SESSION['user_id']);
242 function getNodesByName($node_handle) {
244 $q="select nodes.* from nodes where node_name='$node_handle%'";
246 $result=$db->query($q);
248 while ($result->next()){
249 $record[]=addBase36id($result->getRecord());
257 function getChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
258 global $db, $error, $node;
259 $node_handle=$node['node_id'];
261 $q="select users.*,nodes.* from nodes
262 left join users on users.user_id=nodes.node_creator where
263 node_parent='$node_handle' order by node_created $orderby LIMIT $offset,$limit";
265 $result=$db->query($q);
267 while ($result->next()) {
268 $array[]=addBase36id($result->getRecord());
276 function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
277 global $db, $error, $node;
278 $node_handle=$node['node_id'];
280 $q="select length(node_vector) as depth,users.login,nodes.* from nodes left join users on users.user_id=nodes.node_creator where node_vector like '".$node['node_vector'].";".$node['node_id']."%' and node_type=3 order by concat(node_vector,';',nodes.node_id,';z') desc,depth LIMIT $offset,$limit";
282 $result=$db->query($q);
284 while ($result->next()) {
285 $children_array[]=addBase36id($result->getRecord());
288 return $children_array;
294 function getNodeAccessData() {
296 $q="select users.login,node_access.* from node_access left join users on users.user_id=node_access.user_id where node_id='".$node['node_id']."' and node_permission!=''";
297 $result=$db->query($q);
299 while ($result->next()) {
300 $access_data[]=$result->getRecord();
308 // Simple internal function to set node parrent
310 function setParent($node_id,$parent_id) {
311 global $db,$node,$error,$error_messages;
313 if (!is_numeric($parent_id)) {
317 $q="select node_vector from nodes where node_id='$parent_id'";
320 $parent_vector=$set->getString('node_vector');
321 $new_vector=$parent_vector.str_pad($node_id,VECTOR_CHARS,"0",STR_PAD_LEFT);
323 $q="update nodes set node_parent='$parent_id',node_vector='".$new_vector."
324 ' where node_id='$node_id'";