]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/twitterstatusfetcher.php
Merge branch 'master' into 0.8.x
[quix0rs-gnu-social.git] / scripts / twitterstatusfetcher.php
index d4cf7ea9e08b46597aaef8c12775a2dead1afce0..e1745cfc089614f41aa8a5766f179e72a924f836 100755 (executable)
@@ -2,7 +2,7 @@
 <?php
 /**
  * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, Control Yourself, Inc.
+ * Copyright (C) 2008, 2009, Control Yourself, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  * along with this program.     If not, see <http://www.gnu.org/licenses/>.
  */
 
-// Abort if called from a web server
-if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
-    print "This script must be run from the command line\n";
-    exit();
-}
-
 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
-define('LACONICA', true);
 
 // Tune number of processes and how often to poll Twitter
 // XXX: Should these things be in config.php?
 define('MAXCHILDREN', 2);
 define('POLL_INTERVAL', 60); // in seconds
 
-// Uncomment this to get useful logging
-define('SCRIPT_DEBUG', true);
+$shortoptions = 'di::';
+$longoptions = array('id::', 'debug');
+
+$helptext = <<<END_OF_TRIM_HELP
+Batch script for retrieving Twitter messages from foreign service.
+
+  -i --id              Identity (default 'generic')
+  -d --debug           Debug (lots of log output)
+
+END_OF_TRIM_HELP;
 
-require_once INSTALLDIR . '/lib/common.php';
+require_once INSTALLDIR .'/scripts/commandline.inc';
 require_once INSTALLDIR . '/lib/daemon.php';
 
 /**
@@ -59,6 +60,15 @@ class TwitterStatusFetcher extends Daemon
 {
     private $_children = array();
 
+    function __construct($id=null, $daemonize=true)
+    {
+        parent::__construct($daemonize);
+
+        if ($id) {
+            $this->set_id($id);
+        }
+    }
+
     /**
      * Name of this daemon
      *
@@ -67,7 +77,7 @@ class TwitterStatusFetcher extends Daemon
 
     function name()
     {
-        return ('twitterstatusfetcher.generic');
+        return ('twitterstatusfetcher.'.$this->_id);
     }
 
     /**
@@ -78,6 +88,11 @@ class TwitterStatusFetcher extends Daemon
 
     function run()
     {
+        if (defined('SCRIPT_DEBUG')) {
+            common_debug($this->name() .
+                ': debugging log output enabled.');
+        }
+
         do {
 
             $flinks = $this->refreshFlinks();
@@ -315,7 +330,7 @@ class TwitterStatusFetcher extends Daemon
             $notice->rendered   = common_render_content($notice->content, $notice);
             $notice->source     = 'twitter';
             $notice->reply_to   = null; // XXX lookup reply
-            $notice->is_local   = NOTICE_GATEWAY;
+            $notice->is_local   = Notice::GATEWAY;
 
             if (Event::handle('StartNoticeSave', array(&$notice))) {
                 $id = $notice->insert();
@@ -626,12 +641,22 @@ class TwitterStatusFetcher extends Daemon
     }
 }
 
-ini_set("max_execution_time", "0");
-ini_set("max_input_time", "0");
-set_time_limit(0);
-mb_internal_encoding('UTF-8');
 declare(ticks = 1);
 
-$fetcher = new TwitterStatusFetcher();
+if (have_option('i')) {
+    $id = get_option_value('i');
+} else if (have_option('--id')) {
+    $id = get_option_value('--id');
+} else if (count($args) > 0) {
+    $id = $args[0];
+} else {
+    $id = null;
+}
+
+if (have_option('d') || have_option('debug')) {
+    define('SCRIPT_DEBUG', true);
+}
+
+$fetcher = new TwitterStatusFetcher($id);
 $fetcher->runOnce();