[dokuwiki] Re: Autosubmit popularity data (FS2025)

  • From: Guillaume Turri <guillaume.turri@xxxxxxxxx>
  • To: dokuwiki@xxxxxxxxxxxxx
  • Date: Sat, 27 Nov 2010 09:03:40 +0100

Hi,


Aye. Gathering all the data is somewhat time consuming and thus should
> be separated from the main script. The indexer would be that place.
>
> > we thought about adding something
> > like that you submit the data manually for the first time and when
> > submitting that data an option is given to automatically submit that
> > data from now on
>
> For simplicity I wouldn't even add the option to our standard config but
> would instead save some state file for it in /data/cache/
>

Here is an other patch for FS2025, which follows this advice

I think some things may still be improved (in particular, regarding the UI),
but I first would like to know what you think of it.

Regards,
Guillaume
diff --git a/lib/plugins/popularity/action.php 
b/lib/plugins/popularity/action.php
new file mode 100644
index 0000000..b8c4102
--- /dev/null
+++ b/lib/plugins/popularity/action.php
@@ -0,0 +1,50 @@
+<?php
+
+require_once(DOKU_PLUGIN.'action.php');
+require_once(DOKU_PLUGIN.'popularity/admin.php');
+
+class action_plugin_popularity extends Dokuwiki_Action_Plugin {
+    var $adminPlg;
+
+    function action_plugin_popularity(){
+        $this->adminPlg = plugin_load('admin', 'popularity');
+    }
+
+    /**
+     * return some info
+     */
+    function getInfo(){
+        return confToHash(dirname(__FILE__).'/plugin.info.txt');
+    }
+
+    /**
+     * Register its handlers with the dokuwiki's event controller
+     */
+    function register(&$controller) {
+        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER',  $this, 
'_autosubmit', array());
+    }
+
+    function _autosubmit(&$event, $param){
+        if ( !$this->adminPlg->_isAutosubmitEnabled() || 
$this->_isTooEarlyToSubmit() ){
+            return;
+        }
+
+        //Update the last time we sent data before we actually send it,
+        //to avoid sending it several time in a fraction of second
+        touch ( $this->adminPlg->autosubmitFile );
+
+        $this->adminPlg->_sendData( $this->adminPlg->_gatherAsString() );
+
+        $event->stopPropagation();
+        $event->preventDefault();
+    }
+
+    /**
+     * Check if it's time to send autosubmit data
+     * (we should have check the autosubmit is enabled first)
+     */
+    function _isTooEarlyToSubmit(){
+        $lastSubmit = @filemtime($this->adminPlg->autosubmitFile);
+        return $lastSubmit + 24*60*60*30 > time();
+    }
+}
diff --git a/lib/plugins/popularity/admin.php b/lib/plugins/popularity/admin.php
index 71ea407..8266315 100644
--- a/lib/plugins/popularity/admin.php
+++ b/lib/plugins/popularity/admin.php
@@ -13,21 +13,29 @@ if(!defined('DOKU_INC')) die();
  * need to inherit from this class
  */
 class admin_plugin_popularity extends DokuWiki_Admin_Plugin {
-    var $version = '2010-09-17';
+    var $version;
+    var $submitUrl = 'http://update.dokuwiki.org/popularity.php';
+
+    /**
+     * Name of the file which determine if the the autosubmit is enabled,
+     * and when it was submited for the las time
+     */
+    var $autosubmitFile;
+
+    function admin_plugin_popularity(){
+        global $conf;
+        $this->autosubmitFile = $conf['cachedir'].'/autosubmit.txt';
+
+        $pluginInfo = $this->getInfo();
+        $this->version = $pluginInfo['date'];
+    }
 
 
     /**
      * return some info
      */
     function getInfo(){
-        return array(
-            'author' => 'Andreas Gohr',
-            'email'  => 'andi@xxxxxxxxxxxxxx',
-            'date'   => $this->version,
-            'name'   => 'Popularity Feedback Plugin',
-            'desc'   => 'Send anonymous data about your wiki to the 
developers.',
-            'url'    => 'http://www.dokuwiki.org/plugin:popularity',
-        );
+        return confToHash(dirname(__FILE__).'/plugin.info.txt');
     }
 
     /**
@@ -56,32 +64,78 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin 
{
      * handle user request
      */
     function handle() {
+        //Send the data
+        if ( isset($_REQUEST['data']) ){
+            $this->_sendData( $_REQUEST['data'] );
+            //Deal with the autosubmit option
+            $this->_enableAutosubmit( isset($_REQUEST['autosubmit']) );
+        }
+    }
+
+    function _enableAutosubmit( $enable ){
+        if ( $enable ){
+            io_saveFile( $this->autosubmitFile, ' ');
+        } else {
+            @unlink($this->autosubmitFile);
+        }
     }
 
     /**
      * Output HTML form
      */
     function html() {
-        echo $this->locale_xhtml('intro');
+        if ( isset($_REQUEST['data']) ){
+            echo $this->locale_xhtml('submitted');
+        } else {
+
+            echo $this->locale_xhtml('intro');
+
+            flush();
+            echo '<form method="post" action="'. script()  .'" 
accept-charset="utf-8">';
+            echo '<fieldset style="width: 60%;">';
+            echo '<textarea class="edit" rows="10" cols="80" 
readonly="readonly" name="data">';
+            echo $this->_gatherAsString();
+            echo '</textarea><br />';
+            echo '<label for="autosubmit">';
+            echo '<input type="checkbox" name="autosubmit" id="autosubmit" '
+                . ($this->_isAutosubmitEnabled() ? 'checked' : '' )
+                . '/>' . $this->getLang('autosubmit') .'<br />';
+            echo '<input type="submit" class="button" 
value="'.$this->getLang('submit').'"/>';
+            echo '</label>';
+            echo '<input type="hidden" name="do" value="admin">';
+            echo '<input type="hidden" name="page" value="popularity">';
+            echo '</fieldset>';
+            echo '</form>';
+
+            //        dbg($data);
+        }
+    }
 
-        flush();
+    /**
+     * Check if autosubmit is enabled
+     * @return TRUE if we should send data once a month, FALSE otherwise
+     */
+    function _isAutosubmitEnabled(){
+        return @file_exists($this->autosubmitFile);
+    }
+
+
+    function _sendData($data){
+        $httpClient = new DokuHTTPClient();
+        $httpClient->sendRequest($this->_getUrl(), $data, 'POST');
+    }
+
+    function _gatherAsString(){
         $data = $this->_gather();
-        echo '<form method="post" 
action="http://update.dokuwiki.org/popularity.php"; accept-charset="utf-8">';
-        echo '<fieldset style="width: 60%;">';
-        echo '<textarea class="edit" rows="10" cols="80" readonly="readonly" 
name="data">';
+        $string = '';
         foreach($data as $key => $val){
             if(is_array($val)) foreach($val as $v){
-                echo hsc($key)."\t".hsc($v)."\n";
+                $string .=  hsc($key)."\t".hsc($v)."\n";
             }else{
-                echo hsc($key)."\t".hsc($val)."\n";
+                $string .= hsc($key)."\t".hsc($val)."\n";
             }
         }
-        echo '</textarea><br />';
-        echo '<input type="submit" class="button" 
value="'.$this->getLang('submit').'"/>';
-        echo '</fieldset>';
-        echo '</form>';
-
-//        dbg($data);
+        return $string;
     }
 
 
@@ -256,4 +310,12 @@ class admin_plugin_popularity extends 
DokuWiki_Admin_Plugin {
         }
         return $ret;
     }
+
+    /**
+     * Returns the url where the data should be sent
+     */
+    function _getUrl(){
+        return $this->submitUrl;
+    }
+
 }
diff --git a/lib/plugins/popularity/lang/en/lang.php 
b/lib/plugins/popularity/lang/en/lang.php
index c9912b7..01e4435 100644
--- a/lib/plugins/popularity/lang/en/lang.php
+++ b/lib/plugins/popularity/lang/en/lang.php
@@ -2,3 +2,4 @@
 
 $lang['name'] = 'Popularity Feedback (may take some time to load)';
 $lang['submit'] = 'Send Data';
+$lang['autosubmit'] = 'Automatically send data once a month (may be 
(un)selected later)';
diff --git a/lib/plugins/popularity/lang/en/submitted.txt 
b/lib/plugins/popularity/lang/en/submitted.txt
new file mode 100644
index 0000000..30f2784
--- /dev/null
+++ b/lib/plugins/popularity/lang/en/submitted.txt
@@ -0,0 +1,3 @@
+====== Popularity Feedback ======
+
+The data has been sent succesfully.
diff --git a/lib/plugins/popularity/plugin.info.txt 
b/lib/plugins/popularity/plugin.info.txt
new file mode 100644
index 0000000..d475d20
--- /dev/null
+++ b/lib/plugins/popularity/plugin.info.txt
@@ -0,0 +1,7 @@
+base    popularity
+author  Andreas Gohr
+email   andi@xxxxxxxxxxxxxx
+date    2010-09-17
+name    Popularity Feedback Plugin
+desc    Send anonymous data about your wiki to the developers.
+url     http://www.dokuwiki.org/plugin:popularity

Other related posts: