Created
August 8, 2012 01:58
-
-
Save chimo/3291369 to your computer and use it in GitHub Desktop.
Import StatusNet group aliases to your local instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Quick and dirty script to import StatusNet group aliases to your local instance. | |
| * Could definitely use some improvements, but it seems to works okay for my needs. | |
| * | |
| * HOWTO: | |
| * Change the four variables below to match your configurations and run the script. | |
| */ | |
| // Change this | |
| $host = 'localhost'; | |
| $user = 'username'; | |
| $pass = 'password'; | |
| $db = 'database'; | |
| // Hide warnings | |
| error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); | |
| // Number of inserted notice when script is done | |
| $successes = 0; | |
| $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error()); | |
| mysql_select_db($db) or die('Could not select database'); | |
| $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;'; | |
| $result = mysql_query($query) or die('Query failed: ' . mysql_error()); | |
| while ($row = mysql_fetch_row($result)) { | |
| $ch = curl_init($row[0]); | |
| curl_setopt($ch, CURLOPT_HEADER, 0); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
| curl_setopt($ch, CURLOPT_MAXREDIRS, 3); | |
| if(($html = curl_exec($ch)) === false) { | |
| echo "<p class='error'>" . curl_error($ch) . "</p>"; | |
| curl_close($ch); | |
| continue; | |
| } | |
| curl_close($ch); | |
| echo "Fetched: " . $row[0] . "\n"; | |
| $dom = new DOMDocument(); | |
| $dom->loadHTML($html); | |
| if($dom === false) { | |
| echo "Error loading HTML\n"; | |
| continue; | |
| } | |
| $xpath = new DOMXPath($dom); | |
| $xqry = '//li[@class="group_alias"]/text()'; | |
| $entries = $xpath->query($xqry); | |
| echo "Found $entries->length aliase(s): \n"; | |
| foreach ($entries as $entry) { | |
| echo " * $entry->nodeValue \n"; | |
| } | |
| foreach ($entries as $entry) { | |
| $query = sprintf("INSERT INTO group_alias (group_id, alias) VALUES ('%s', '%s')", | |
| mysql_real_escape_string($row[1]), | |
| mysql_real_escape_string($entry->nodeValue)); | |
| $inserted = mysql_query($query); | |
| if(!$inserted) { | |
| if(mysql_errno() == 1062) { // Duplicate entry (we already have the alias, safely ignore) | |
| $message = 'Alias "' . $entry->nodeValue . '" already in db, skipping.'; | |
| } else { | |
| $message = 'Invalid query: ' . mysql_error() . "\n"; | |
| $message .= 'Whole query: ' . $query; | |
| } | |
| } | |
| else { | |
| $message = 'Successfully inserted alias "' . $entry->nodeValue . '".'; | |
| $successes++; | |
| } | |
| echo $message . "\n"; | |
| } | |
| } | |
| echo "\n*** Done ***\n"; | |
| echo "Inserted $successes new aliase(s)\n"; | |
| ?> |
Author
@mama21mama You need to change the database credentials first so the script can access your database.
Line 11-15:
// Change this
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'database';
After that, you can run it with "php ./getAliases.php" as you mentioned.
If you're still running into problems, let me know what the error message says (if any).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not work for me, or maybe do not know how to use it.