Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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";
- ?>
Add Comment
Please, Sign In to add comment