You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

620 lines
19 KiB

  1. <?php
  2. namespace modl;
  3. class PostnDAO extends SQL {
  4. function set(Postn $post) {
  5. $this->_sql = '
  6. update postn
  7. set aname = :aname,
  8. aid = :aid,
  9. aemail = :aemail,
  10. title = :title,
  11. content = :content,
  12. contentraw = :contentraw,
  13. contentcleaned = :contentcleaned,
  14. commentplace = :commentplace,
  15. published = :published,
  16. updated = :updated,
  17. delay = :delay,
  18. lat = :lat,
  19. lon = :lon,
  20. links = :links,
  21. picture = :picture,
  22. hash = :hash
  23. where origin = :origin
  24. and node = :node
  25. and nodeid = :nodeid';
  26. $this->prepare(
  27. 'Postn',
  28. array(
  29. 'aname' => $post->aname,
  30. 'aid' => $post->aid,
  31. 'aemail' => $post->aemail,
  32. 'title' => $post->title,
  33. 'content' => $post->content,
  34. 'contentraw' => $post->contentraw,
  35. 'contentcleaned' => $post->contentcleaned,
  36. 'commentplace' => $post->commentplace,
  37. 'published' => $post->published,
  38. 'updated' => $post->updated,
  39. 'delay' => $post->delay,
  40. 'lat' => $post->lat,
  41. 'lon' => $post->lon,
  42. 'links' => $post->links,
  43. 'picture' => $post->picture,
  44. 'hash' => $post->hash,
  45. 'origin' => $post->origin,
  46. 'node' => $post->node,
  47. 'nodeid' => $post->nodeid
  48. )
  49. );
  50. $this->run('Postn');
  51. if(!$this->_effective) {
  52. $this->_sql ='
  53. insert into postn
  54. (
  55. origin,
  56. node,
  57. nodeid,
  58. aname,
  59. aid,
  60. aemail,
  61. title,
  62. content,
  63. contentraw,
  64. contentcleaned,
  65. commentplace,
  66. published,
  67. updated,
  68. delay,
  69. lat,
  70. lon,
  71. links,
  72. picture,
  73. hash)
  74. values(
  75. :origin,
  76. :node,
  77. :nodeid,
  78. :aname,
  79. :aid,
  80. :aemail,
  81. :title,
  82. :content,
  83. :contentraw,
  84. :contentcleaned,
  85. :commentplace,
  86. :published,
  87. :updated,
  88. :delay,
  89. :lat,
  90. :lon,
  91. :links,
  92. :picture,
  93. :hash
  94. )';
  95. $this->prepare(
  96. 'Postn',
  97. array(
  98. 'aname' => $post->aname,
  99. 'aid' => $post->aid,
  100. 'aemail' => $post->aemail,
  101. 'title' => $post->title,
  102. 'content' => $post->content,
  103. 'contentraw' => $post->contentraw,
  104. 'contentcleaned' => $post->contentcleaned,
  105. 'commentplace' => $post->commentplace,
  106. 'published' => $post->published,
  107. 'updated' => $post->updated,
  108. 'delay' => $post->delay,
  109. 'lat' => $post->lat,
  110. 'lon' => $post->lon,
  111. 'links' => $post->links,
  112. 'picture' => $post->picture,
  113. 'hash' => $post->hash,
  114. 'origin' => $post->origin,
  115. 'node' => $post->node,
  116. 'nodeid' => $post->nodeid
  117. )
  118. );
  119. $this->run('Postn');
  120. }
  121. }
  122. function delete($nodeid) {
  123. $this->_sql = '
  124. delete from postn
  125. where nodeid = :nodeid';
  126. $this->prepare(
  127. 'Postn',
  128. array(
  129. 'nodeid' => $nodeid
  130. )
  131. );
  132. return $this->run('Postn');
  133. }
  134. function deleteNode($origin, $node) {
  135. $this->_sql = '
  136. delete from postn
  137. where origin = :origin
  138. and node = :node';
  139. $this->prepare(
  140. 'Postn',
  141. array(
  142. 'origin' => $origin,
  143. 'node' => $node
  144. )
  145. );
  146. return $this->run('Postn');
  147. }
  148. function getNode($from, $node, $limitf = false, $limitr = false) {
  149. $this->_sql = '
  150. select *, postn.aid, privacy.value as privacy from postn
  151. left outer join contact on postn.aid = contact.jid
  152. left outer join privacy on postn.nodeid = privacy.pkey
  153. where ((postn.origin, node) in (select server, node from subscription where jid = :aid))
  154. and postn.origin = :origin
  155. and postn.node = :node
  156. and postn.node != \'urn:xmpp:microblog:0\'
  157. order by postn.published desc';
  158. if($limitr !== false)
  159. $this->_sql = $this->_sql.' limit '.(int)$limitr.' offset '.(int)$limitf;
  160. $this->prepare(
  161. 'Postn',
  162. array(
  163. 'aid' => $this->_user, // TODO: Little hack to bypass the check, need to fix it in Modl
  164. 'origin' => $from,
  165. 'node' => $node
  166. )
  167. );
  168. return $this->run('ContactPostn');
  169. }
  170. function getPublicTag($tag, $limitf = false, $limitr = false) {
  171. $this->_sql = '
  172. select *, postn.aid, privacy.value as privacy from postn
  173. left outer join contact on postn.aid = contact.jid
  174. left outer join privacy on postn.nodeid = privacy.pkey
  175. where nodeid in (select nodeid from tag where tag = :title)
  176. and privacy.value = 1
  177. order by postn.published desc';
  178. if($limitr !== false)
  179. $this->_sql = $this->_sql.' limit '.(int)$limitr.' offset '.(int)$limitf;
  180. $this->prepare(
  181. 'Postn',
  182. array(
  183. 'title' => $tag # Hack
  184. )
  185. );
  186. return $this->run('ContactPostn');
  187. }
  188. function getNodeUnfiltered($from, $node, $limitf = false, $limitr = false) {
  189. $this->_sql = '
  190. select *, postn.aid, privacy.value as privacy from postn
  191. left outer join contact on postn.aid = contact.jid
  192. left outer join privacy on postn.nodeid = privacy.pkey
  193. where postn.origin = :origin
  194. and postn.node = :node
  195. order by postn.published desc';
  196. if($limitr !== false)
  197. $this->_sql = $this->_sql.' limit '.(int)$limitr.' offset '.(int)$limitf;
  198. $this->prepare(
  199. 'Postn',
  200. array(
  201. 'origin' => $from,
  202. 'node' => $node
  203. )
  204. );
  205. return $this->run('ContactPostn');
  206. }
  207. function getGallery($from, $limitf = false, $limitr = false) {
  208. $this->_sql = '
  209. select *, postn.aid, privacy.value as privacy from postn
  210. left outer join contact on postn.aid = contact.jid
  211. left outer join privacy on postn.nodeid = privacy.pkey
  212. where postn.aid = :aid
  213. and postn.picture = 1
  214. order by postn.published desc';
  215. if($limitr !== false)
  216. $this->_sql = $this->_sql.' limit '.(int)$limitr.' offset '.(int)$limitf;
  217. $this->prepare(
  218. 'Postn',
  219. array(
  220. 'aid' => $from // Another hack
  221. )
  222. );
  223. return $this->run('ContactPostn');
  224. }
  225. function getItem($id) {
  226. $this->_sql = '
  227. select *, postn.aid, privacy.value as privacy from postn
  228. left outer join contact on postn.aid = contact.jid
  229. left outer join privacy on postn.nodeid = privacy.pkey
  230. where postn.nodeid = :nodeid';
  231. $this->prepare(
  232. 'Postn',
  233. array(
  234. 'nodeid' => $id
  235. )
  236. );
  237. return $this->run('ContactPostn', 'item');
  238. }
  239. function getAllPosts($jid = false, $limitf = false, $limitr = false) {
  240. $this->_sql = '
  241. select *, postn.aid, privacy.value as privacy from postn
  242. left outer join contact on postn.aid = contact.jid
  243. left outer join privacy on postn.nodeid = privacy.pkey
  244. where (
  245. (postn.origin in (select jid from rosterlink where session = :origin and rostersubscription in (\'both\', \'to\')) and node = \'urn:xmpp:microblog:0\')
  246. or (postn.origin = :origin and node = \'urn:xmpp:microblog:0\')
  247. or ((postn.origin, node) in (select server, node from subscription where jid = :origin))
  248. )
  249. and postn.node not like \'urn:xmpp:microblog:0:comments/%\'
  250. and postn.node not like \'urn:xmpp:inbox\'
  251. order by postn.published desc
  252. ';
  253. if($limitr)
  254. $this->_sql = $this->_sql.' limit '.$limitr.' offset '.$limitf;
  255. if($jid == false)
  256. $jid = $this->_user;
  257. $this->prepare(
  258. 'Postn',
  259. array(
  260. 'origin' => $jid
  261. )
  262. );
  263. return $this->run('ContactPostn');
  264. }
  265. function getFeed($limitf = false, $limitr = false) {
  266. $this->_sql = '
  267. select *, postn.aid, privacy.value as privacy from postn
  268. left outer join contact on postn.aid = contact.jid
  269. left outer join privacy on postn.nodeid = privacy.pkey
  270. where ((postn.origin in (select jid from rosterlink where session = :origin and rostersubscription in (\'both\', \'to\')) and node = \'urn:xmpp:microblog:0\')
  271. or (postn.origin = :origin and node = \'urn:xmpp:microblog:0\'))
  272. and postn.node not like \'urn:xmpp:microblog:0:comments/%\'
  273. and postn.node not like \'urn:xmpp:inbox\'
  274. order by postn.published desc
  275. ';
  276. if($limitr)
  277. $this->_sql = $this->_sql.' limit '.$limitr.' offset '.$limitf;
  278. $this->prepare(
  279. 'Postn',
  280. array(
  281. 'origin' => $this->_user
  282. )
  283. );
  284. return $this->run('ContactPostn');
  285. }
  286. function getNews($limitf = false, $limitr = false) {
  287. $this->_sql = '
  288. select *, postn.aid, privacy.value as privacy from postn
  289. left outer join contact on postn.aid = contact.jid
  290. left outer join privacy on postn.nodeid = privacy.pkey
  291. where ((postn.origin, node) in (select server, node from subscription where jid = :origin))
  292. order by postn.published desc
  293. ';
  294. if($limitr)
  295. $this->_sql = $this->_sql.' limit '.$limitr.' offset '.$limitf;
  296. $this->prepare(
  297. 'Postn',
  298. array(
  299. 'origin' => $this->_user
  300. )
  301. );
  302. return $this->run('ContactPostn');
  303. }
  304. function getMe($limitf = false, $limitr = false) {
  305. $this->_sql = '
  306. select *, postn.aid, privacy.value as privacy from postn
  307. left outer join contact on postn.aid = contact.jid
  308. left outer join privacy on postn.nodeid = privacy.pkey
  309. where postn.origin = :origin and postn.node = \'urn:xmpp:microblog:0\'
  310. order by postn.published desc
  311. ';
  312. if($limitr)
  313. $this->_sql = $this->_sql.' limit '.$limitr.' offset '.$limitf;
  314. $this->prepare(
  315. 'Postn',
  316. array(
  317. 'origin' => $this->_user
  318. )
  319. );
  320. return $this->run('ContactPostn');
  321. }
  322. function getPublic($origin, $node, $limitf = false, $limitr = false) {
  323. $this->_sql = '
  324. select *, postn.aid, privacy.value as privacy from postn
  325. left outer join contact on postn.aid = contact.jid
  326. left outer join privacy on postn.nodeid = privacy.pkey
  327. where postn.origin = :origin
  328. and postn.node = :node
  329. and privacy.value = 1
  330. order by postn.published desc';
  331. if($limitr !== false)
  332. $this->_sql = $this->_sql.' limit '.(int)$limitr.' offset '.(int)$limitf;
  333. $this->prepare(
  334. 'Postn',
  335. array(
  336. 'origin' => $origin,
  337. 'node' => $node
  338. )
  339. );
  340. return $this->run('ContactPostn');
  341. }
  342. function getPublicItem($origin, $node, $nodeid, $limitf = false, $limitr = false) {
  343. $this->_sql = '
  344. select *, postn.aid, privacy.value as privacy from postn
  345. left outer join contact on postn.aid = contact.jid
  346. left outer join privacy on postn.nodeid = privacy.pkey
  347. where postn.origin = :origin
  348. and postn.node = :node
  349. and privacy.value = 1
  350. and postn.nodeid = :nodeid
  351. order by postn.published desc';
  352. if($limitr)
  353. $this->_sql = $this->_sql.' limit '.$limitr.' offset '.$limitf;
  354. $this->prepare(
  355. 'Postn',
  356. array(
  357. 'origin' => $origin,
  358. 'node' => $node,
  359. 'nodeid' => $nodeid,
  360. )
  361. );
  362. return $this->run('ContactPostn');
  363. }
  364. // TODO: fixme
  365. function getComments($posts) {
  366. $commentsid = '';
  367. if(is_array($posts)) {
  368. $i = 0;
  369. foreach($posts as $post) {
  370. if($i == 0)
  371. $commentsid = "'urn:xmpp:microblog:0:comments/".$post->nodeid."'";
  372. else
  373. $commentsid .= ",'urn:xmpp:microblog:0:comments/".$post->nodeid."'";
  374. $i++;
  375. }
  376. } else {
  377. $commentsid = "'urn:xmpp:microblog:0:comments/".$posts->nodeid."'";
  378. }
  379. // We request all the comments relative to our messages
  380. $this->_sql = '
  381. select *, postn.aid as jid from postn
  382. left outer join contact on postn.aid = contact.jid
  383. where postn.node in ('.$commentsid.')
  384. order by postn.published';
  385. $this->prepare(
  386. 'Postn',
  387. array(
  388. )
  389. );
  390. return $this->run('ContactPostn');
  391. }
  392. function clearPost() {
  393. $this->_sql = '
  394. delete from postn
  395. where session = :session';
  396. $this->prepare(
  397. 'Postn',
  398. array(
  399. 'session' => $this->_user
  400. )
  401. );
  402. return $this->run('Postn');
  403. }
  404. // TODO: fixme
  405. function getStatistics() {
  406. $this->_sql = '
  407. select count(*) as count, extract(month from published) as month, extract(year from published) as year
  408. from postn
  409. where session = :session
  410. group by month, year order by year desc, month desc';
  411. $this->prepare(
  412. 'Postn',
  413. array(
  414. 'session' => $this->_user
  415. )
  416. );
  417. return $this->run(null, 'array');
  418. }
  419. function getCountSince($date) {
  420. $this->_sql = '
  421. select count(*) from postn
  422. where (
  423. (postn.origin in (select jid from rosterlink where session = :origin and rostersubscription in (\'both\', \'to\')) and node = \'urn:xmpp:microblog:0\')
  424. or (postn.origin = :origin and node = \'urn:xmpp:microblog:0\')
  425. or ((postn.origin, node) in (select server, node from subscription where jid = :origin))
  426. )
  427. and postn.node not like \'urn:xmpp:microblog:0:comments/%\'
  428. and postn.node not like \'urn:xmpp:inbox\'
  429. and published > :published
  430. ';
  431. $this->prepare(
  432. 'Postn',
  433. array(
  434. 'origin' => $this->_user,
  435. 'published' => $date
  436. )
  437. );
  438. $arr = $this->run(null, 'array');
  439. if(is_array($arr) && isset($arr[0])) {
  440. $arr = array_values($arr[0]);
  441. return (int)$arr[0];
  442. }
  443. }
  444. function getLastDate() {
  445. $this->_sql = '
  446. select published from postn
  447. where (
  448. (postn.origin in (select jid from rosterlink where session = :origin and rostersubscription in (\'both\', \'to\')) and node = \'urn:xmpp:microblog:0\')
  449. or (postn.origin = :origin and node = \'urn:xmpp:microblog:0\')
  450. or ((postn.origin, node) in (select server, node from subscription where jid = :origin))
  451. )
  452. and postn.node not like \'urn:xmpp:microblog:0:comments/%\'
  453. and postn.node not like \'urn:xmpp:inbox\'
  454. order by postn.published desc
  455. limit 1 offset 0';
  456. $this->prepare(
  457. 'Postn',
  458. array(
  459. 'origin' => $this->_user
  460. )
  461. );
  462. $arr = $this->run(null, 'array');
  463. if(is_array($arr) && isset($arr[0]))
  464. return $arr[0]['published'];
  465. }
  466. function getLastPublished($limitf = false, $limitr = false)
  467. {
  468. $this->_sql = '
  469. select * from postn
  470. where
  471. node != \'urn:xmpp:microblog:0\'
  472. and postn.node not like \'urn:xmpp:microblog:0:comments/%\'
  473. and postn.node not like \'urn:xmpp:inbox\'
  474. and postn.origin not like \'nsfw%\'
  475. and ((postn.origin, node) not in (select server, node from subscription where jid = :origin))
  476. order by published desc
  477. ';
  478. if($limitr)
  479. $this->_sql = $this->_sql.' limit '.$limitr.' offset '.$limitf;
  480. $this->prepare(
  481. 'Postn',
  482. array(
  483. 'origin' => $this->_user
  484. )
  485. );
  486. return $this->run('Postn');
  487. }
  488. function exist($id) {
  489. $this->_sql = '
  490. select count(*) from postn
  491. where postn.nodeid = :nodeid
  492. ';
  493. $this->prepare(
  494. 'Postn',
  495. array(
  496. 'nodeid' => $id
  497. )
  498. );
  499. $arr = $this->run(null, 'array');
  500. if(is_array($arr) && isset($arr[0])) {
  501. $arr = array_values($arr[0]);
  502. return (bool)$arr[0];
  503. }
  504. }
  505. }