Guest User

Untitled

a guest
Nov 30th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Quick and dirty script to import StatusNet group aliases to your local instance.
  5. * Could definitely use some improvements, but it seems to works okay for my needs.
  6. *
  7. * HOWTO:
  8. * Change the four variables below to match your configurations and run the script.
  9. */
  10.  
  11. // Change this
  12. $host = 'localhost';
  13. $user = 'username';
  14. $pass = 'password';
  15. $db = 'database';
  16.  
  17. // Hide warnings
  18. error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
  19.  
  20. // Number of inserted notice when script is done
  21. $successes = 0;
  22.  
  23. $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());
  24. mysql_select_db($db) or die('Could not select database');
  25.  
  26. $query = 'SELECT user_group.uri, user_group.id FROM (SELECT DISTINCTROW group_id FROM group_member) gm INNER JOIN user_group ON user_group.id = gm.group_id;';
  27. $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  28.  
  29. while ($row = mysql_fetch_row($result)) {
  30. $ch = curl_init($row[0]);
  31.  
  32. curl_setopt($ch, CURLOPT_HEADER, 0);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  35. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  36. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  37.  
  38. if(($html = curl_exec($ch)) === false) {
  39. echo "<p class='error'>" . curl_error($ch) . "</p>";
  40. curl_close($ch);
  41. continue;
  42. }
  43.  
  44. curl_close($ch);
  45.  
  46. echo "Fetched: " . $row[0] . "\n";
  47.  
  48. $dom = new DOMDocument();
  49. $dom->loadHTML($html);
  50.  
  51. if($dom === false) {
  52. echo "Error loading HTML\n";
  53. continue;
  54. }
  55.  
  56. $xpath = new DOMXPath($dom);
  57. $xqry = '//li[@class="group_alias"]/text()';
  58.  
  59. $entries = $xpath->query($xqry);
  60.  
  61. echo "Found $entries->length aliase(s): \n";
  62.  
  63. foreach ($entries as $entry) {
  64. echo " * $entry->nodeValue \n";
  65. }
  66.  
  67. foreach ($entries as $entry) {
  68. $query = sprintf("INSERT INTO group_alias (group_id, alias) VALUES ('%s', '%s')",
  69. mysql_real_escape_string($row[1]),
  70. mysql_real_escape_string($entry->nodeValue));
  71.  
  72. $inserted = mysql_query($query);
  73.  
  74. if(!$inserted) {
  75. if(mysql_errno() == 1062) { // Duplicate entry (we already have the alias, safely ignore)
  76. $message = 'Alias "' . $entry->nodeValue . '" already in db, skipping.';
  77. } else {
  78. $message = 'Invalid query: ' . mysql_error() . "\n";
  79. $message .= 'Whole query: ' . $query;
  80. }
  81. }
  82. else {
  83. $message = 'Successfully inserted alias "' . $entry->nodeValue . '".';
  84. $successes++;
  85. }
  86.  
  87. echo $message . "\n";
  88. }
  89. }
  90.  
  91. echo "\n*** Done ***\n";
  92. echo "Inserted $successes new aliase(s)\n";
  93.  
  94. ?>
Add Comment
Please, Sign In to add comment