PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/parsetodb.php

https://gitlab.com/bjwebb-codedump/plingconica
PHP | 219 lines | 171 code | 24 blank | 24 comment | 66 complexity | a2f70a04420d236a179d53e063b8bb8a MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * Plingconica - creating a Laconica instance full of plings data (http://plings.net)
  4. * Copyright (C) 2009 Ben Webb <bjwebb@freedomdreams.co.uk>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. require_once "config.php";
  20. $con = mysql_connect($db_host,$db_name,$db_pass);
  21. if (!$con) {
  22. die('Could not connect: ' . mysql_error());
  23. }
  24. if (mysql_select_db($db_db, $con)); else die(mysql_error());
  25. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  26. define('LACONICA', true);
  27. require_once "../lib/common.php";
  28. function insert_into_db($tablename,$array) {
  29. global $con;
  30. if ($result = mysql_query("SELECT * FROM `".$tablename."` WHERE id='".$array["id"]."'", $con));
  31. else die(mysql_error());
  32. if (mysql_fetch_row($result)) {
  33. $sets = "";
  34. $i=0;
  35. foreach ($array as $field => $value) {
  36. if ($i != 0) {
  37. $sets .= ", ";
  38. }
  39. if (is_array($value)) $value = implode(";", $value);
  40. $sets .= "`".mysql_real_escape_string($field)."`='".mysql_real_escape_string($value)."'";
  41. $i++;
  42. }
  43. $query = "UPDATE `".$tablename."` SET ".$sets." WHERE id='".$array["id"]."'";
  44. }
  45. else {
  46. $fields = "";
  47. $values = "";
  48. $i=0;
  49. foreach ($array as $field => $value) {
  50. if ($i != 0) {
  51. $fields .= ", ";
  52. $values .= ", ";
  53. }
  54. if (is_array($value)) $value = implode(";", $value);
  55. $fields .= "`".mysql_real_escape_string($field)."`";
  56. $values .= "'".mysql_real_escape_string($value)."'";
  57. $i++;
  58. }
  59. $query = "INSERT INTO `".$tablename."` (".$fields.")"." VALUES "." (".$values.");";
  60. }
  61. if ($query) {
  62. if (mysql_query($query));
  63. else {
  64. #echo $query;
  65. die(mysql_error());
  66. }
  67. #echo "<br/>";
  68. }
  69. }
  70. function todb($aarray, $varray, $parray) {
  71. global $days;
  72. global $lacode;
  73. global $con;
  74. global $town;
  75. global $isPostcode;
  76. global $laconicaid;
  77. global $postcode;
  78. $details = strip_tags($aarray["details"]);
  79. # if details in placeholder.....
  80. if ($varray["name"] == "not known") $varray["name"] = $varray["buildingnameno"];
  81. $details .= " Venue: ".$varray["name"];
  82. if ($varray["buildingnameno"]) {
  83. $varray["loc"] .= $varray["buildingnameno"];
  84. if (!is_numeric($varray["buildingnameno"]))
  85. $varray["loc"] .= ",";
  86. }
  87. if ($varray["street"]) $varray["loc"] .= " ".$varray["street"];
  88. if ($varray["town"]) $varray["loc"] .= ", ".$varray["town"];
  89. if ($varray["posttown"] && $varray["posttown"] != $varray ["town"]) $varray["loc"] .= ", ".$varray["posttown"];
  90. if ($varray["county"]) $varray["loc"] .= ", ".$varray["county"];
  91. if ($varray["postcode"]) $varray["loc"] .= ", ".$varray["postcode"];
  92. $details .= ", ".$varray["loc"];
  93. $starts = strtotime($aarray["starts"]);
  94. $ends = strtotime($aarray["ends"]);
  95. $times = date("g:ia",$ends);
  96. if (date("a",$starts) == date("a",$ends)) $format = "g:i";
  97. else $format = "g:ia";
  98. $times = date($format,$starts)."-".$times;
  99. if ($times == "12:00-12:00am" || $times == "12:00am-12:00pm") $times = " all day";
  100. else $times = " ".$times;
  101. $day = date("D",$starts);
  102. $laname = strtolower($varray["laname"]);
  103. $town = $laname;
  104. $url = "http://m.plings.net/".$aarray["id"];
  105. $tags = "";
  106. $i = 0;
  107. if ($aarray["keyws"]) {
  108. foreach ($aarray["keyws"] as $key) {
  109. $i ++;
  110. if ($i > 4) break;
  111. $key = preg_replace("/[^A-Za-z0-9]/","",$key);
  112. $tags = $tags."#".$key." ";
  113. }
  114. }
  115. $head = $aarray["name"]." ($day$times): !".preg_replace("/[^A-Za-z0-9]/","",$town)." ";
  116. $tail = " ".$tags.$url;
  117. $space = 140 - strlen($head) - strlen($tail) - 2;
  118. $main = substr($details,0,$space)."..";
  119. $aarray["notice"] = $head.$main.$tail;
  120. $aarray["vid"] = $varray["id"];
  121. $aarray["pid"] = $parray["id"];
  122. #print_r($aarray);
  123. #print_r($varray);
  124. #print_r($parray);
  125. insert_into_db("activities",$aarray);
  126. insert_into_db("venues", $varray);
  127. insert_into_db("providers", $parray);
  128. }
  129. function contents($parser, $data){
  130. global $aarray; global $varray; global $parray;
  131. global $act; global $ven; global $prov;
  132. global $tag;
  133. global $level;
  134. if (trim($data) != "") {
  135. if ($level == 4 && $act) $aarray[strtolower($tag)] = trim($data);
  136. if ($level == 5 && $ven) $varray[strtolower($tag)] = trim($data);
  137. if ($level == 5 && $prov) $parray[strtolower($tag)] = trim($data);
  138. if ($level == 5 && $tag == "KEYWORD") $aarray["keyws"][] = trim($data);
  139. }
  140. }
  141. function startTag($parser, $data, $attr){
  142. global $aarray; global $varray; global $parray;
  143. global $act; global $ven; global $prov;
  144. global $tag;
  145. global $level;
  146. $level++;
  147. $tag = $data;
  148. if ($data == "ACTIVITY") {
  149. $aarray = array();
  150. $aarray["id"] = $attr["ID"];
  151. $varray = array();
  152. $parray = array();
  153. $act = true;
  154. }
  155. if ($data == "VENUE") {
  156. $ven = true;
  157. $varray["id"] = $attr["ID"];
  158. }
  159. if ($data == "PROVIDER") {
  160. $prov = true;
  161. $parray["id"] = $attr["ID"];
  162. }
  163. }
  164. function endTag($parser, $data){
  165. global $aarray; global $varray; global $parray;
  166. global $act; global $ven; global $prov;
  167. global $tag;
  168. global $level;
  169. $level--;
  170. $tag = "";
  171. if ($data == "ACTIVITY") {
  172. todb($aarray, $varray, $parray);
  173. $act = false;
  174. }
  175. if ($data == "VENUE") {
  176. $ven = false;
  177. }
  178. if ($data == "PROVIDER") {
  179. $ven = false;
  180. }
  181. }
  182. function parse($feed) {
  183. $parser = xml_parser_create('');
  184. xml_set_element_handler($parser, "startTag", "endTag");
  185. xml_set_character_data_handler($parser, "contents");
  186. xml_parse($parser, $feed);
  187. xml_parser_free($parser);
  188. }
  189. $days = 7;
  190. #echo "http://feeds.plings.net/xml.activity.php/$days?APIKey=$apikey";
  191. $feed = file_get_contents("plingconica.xml");
  192. parse($feed);
  193. if (mysql_query("UPDATE `custom` SET `uptodate`=0 WHERE `uptodate`=1"));
  194. else die(mysql_error());
  195. ?>