[dokuwiki] Autosubmit popularity data (FS2025)

  • From: Guillaume Turri <guillaume.turri@xxxxxxxxx>
  • To: dokuwiki@xxxxxxxxxxxxx
  • Date: Wed, 24 Nov 2010 20:52:13 +0100

Hi,

I had a look at http://bugs.splitbrain.org/index.php?do=details&task_id=2025 (Make option to autosubmit popularity data once a month), and tried to make a patch.

I'd like to know if it seems right, and if not, what should be changed.

Basically, the main modifications are:
- Introduction of the parameter $conf['autoSubmitPopularity'], which represents how often the data should be submitted - Introduction of the function autoSubmitPopularity(), called from doku.php, which send the data if needed

Regards,
Guillaume
diff --git a/conf/dokuwiki.php b/conf/dokuwiki.php
index 2405494..6892fdd 100644
--- a/conf/dokuwiki.php
+++ b/conf/dokuwiki.php
@@ -83,6 +83,7 @@ $conf['xmlrpcuser']  = '!!not set!!';    //Restrict XML-RPC 
access to this group
 /* Advanced Options */
 
 $conf['updatecheck'] = 1;                //automatically check for new 
releases?
+$conf['autoSubmitPopularity'] = 30;      //How often (in days) should we 
autosubmit popularity data? (0 => never)
 $conf['userewrite']  = 0;                //this makes nice URLs: 0: off 1: 
.htaccess 2: internal
 $conf['useslash']    = 0;                //use slash instead of colon? only 
when rewrite is on
 $conf['usedraft']    = 1;                //automatically save a draft while 
editing (0|1)
diff --git a/doku.php b/doku.php
index 1303f1a..44e9969 100644
--- a/doku.php
+++ b/doku.php
@@ -74,6 +74,8 @@ if ($conf['breadcrumbs']) breadcrumbs();
 // check upstream
 checkUpdateMessages();
 
+autoSubmitPopularity();
+
 $tmp = array(); // No event data
 trigger_event('DOKUWIKI_STARTED',$tmp);
 
diff --git a/inc/infoutils.php b/inc/infoutils.php
index d3c6f29..9776ece 100644
--- a/inc/infoutils.php
+++ b/inc/infoutils.php
@@ -40,6 +40,46 @@ function checkUpdateMessages(){
     }
 }
 
+/**
+ * Send popularit data regularly
+ *
+ * @author Guillaume Turri <guillaume.turri@xxxxxxxxx>
+ */
+function autoSubmitPopularity(){
+    global $conf;
+    $file = $conf['cachedir'].'/'.'autosubmit.txt';
+
+    if ( ! @file_exists($file) ){
+        //It's likely we're using this dokuwiki for the first time
+        //It's therefore useless to send data now, but we should create the 
file
+        io_saveFile($file, time());
+        return;
+    }
+
+    //If the admin disabled autosubmit...
+    if($conf['autoSubmitPopularity'] == 0){
+        return;
+    }
+    //... or if it's too early to autosubmit again
+    $lastSubmit = (int) io_readFile($file);
+    if ( $lastSubmit + 24*60*60 * max($conf['autoSubmitPopularity'], 30) > 
time() ){ //Don't send more than once a month
+        return;
+    }
+
+    //Compute the data...
+    $pluginLoader = new Doku_Plugin_Controller();
+    $plugin = $pluginLoader->load('admin', 'popularity');
+    if ( is_null($plugin) ) return;
+    $data = $plugin->_gatherAsString();
+
+    //... and send them
+    $httpClient = new DokuHTTPClient();
+    $httpClient->sendRequest($plugin->_getUrl(), $data, 'POST');
+
+    //Update the last time with 
+    io_saveFile($file, time());
+}
+
 
 /**
  * Return DokuWiki's version (split up in date and type)
diff --git a/install.php b/install.php
index 9b85297..94f6fdc 100644
--- a/install.php
+++ b/install.php
@@ -46,7 +46,8 @@ $dokuwiki_hash = array(
     '2008-05-04'   => '1e5c42eac3219d9e21927c39e3240aad',
     '2009-02-14'   => 'ec8c04210732a14fdfce0f7f6eead865',
     '2009-12-25'   => '993c4b2b385643efe5abf8e7010e11f4',
-    '2010-11-07'   => '7921d48195f4db21b8ead6d9bea801b8'
+    '2010-11-07'   => '7921d48195f4db21b8ead6d9bea801b8',
+    'dev'          => 'b0b416d6cc886eaee6182406302e6edf'
 );
 
 
diff --git a/lib/plugins/config/lang/en/lang.php 
b/lib/plugins/config/lang/en/lang.php
index a944d6b..b66ba17 100644
--- a/lib/plugins/config/lang/en/lang.php
+++ b/lib/plugins/config/lang/en/lang.php
@@ -111,6 +111,7 @@ $lang['xmlrpcuser']  = 'Restrict XML-RPC access to the 
comma separated groups or
 
 /* Advanced Options */
 $lang['updatecheck'] = 'Check for updates and security warnings? DokuWiki 
needs to contact splitbrain.org for this feature.';
+$lang['autoSubmitPopularity'] = 'How often (in days) should we autosubmit 
popularity data? (0 = never)';
 $lang['userewrite']  = 'Use nice URLs';
 $lang['useslash']    = 'Use slash as namespace separator in URLs';
 $lang['usedraft']    = 'Automatically save a draft while editing';
diff --git a/lib/plugins/config/settings/config.metadata.php 
b/lib/plugins/config/settings/config.metadata.php
index edba652..e0c284d 100644
--- a/lib/plugins/config/settings/config.metadata.php
+++ b/lib/plugins/config/settings/config.metadata.php
@@ -167,6 +167,7 @@ $meta['fetchsize']   = array('numeric');
 
 $meta['_advanced']   = array('fieldset');
 $meta['updatecheck'] = array('onoff');
+$meta['autoSubmitPopularity'] = array('numeric');
 $meta['userewrite']  = array('multichoice','_choices' => array(0,1,2));
 $meta['useslash']    = array('onoff');
 $meta['sepchar']     = array('sepchar');
diff --git a/lib/plugins/popularity/admin.php b/lib/plugins/popularity/admin.php
index 71ea407..2f0fab6 100644
--- a/lib/plugins/popularity/admin.php
+++ b/lib/plugins/popularity/admin.php
@@ -14,6 +14,7 @@ if(!defined('DOKU_INC')) die();
  */
 class admin_plugin_popularity extends DokuWiki_Admin_Plugin {
     var $version = '2010-09-17';
+    var $submitUrl = 'http://update.dokuwiki.org/popularity.php';
 
 
     /**
@@ -65,17 +66,10 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin 
{
         echo $this->locale_xhtml('intro');
 
         flush();
-        $data = $this->_gather();
-        echo '<form method="post" 
action="http://update.dokuwiki.org/popularity.php"; accept-charset="utf-8">';
+        echo '<form method="post" action="'. $this->submitUrl  
.'accept-charset="utf-8">';
         echo '<fieldset style="width: 60%;">';
         echo '<textarea class="edit" rows="10" cols="80" readonly="readonly" 
name="data">';
-        foreach($data as $key => $val){
-            if(is_array($val)) foreach($val as $v){
-                echo hsc($key)."\t".hsc($v)."\n";
-            }else{
-                echo hsc($key)."\t".hsc($val)."\n";
-            }
-        }
+        echo $this->_gatherAsString();
         echo '</textarea><br />';
         echo '<input type="submit" class="button" 
value="'.$this->getLang('submit').'"/>';
         echo '</fieldset>';
@@ -84,6 +78,19 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin {
 //        dbg($data);
     }
 
+    function _gatherAsString(){
+        $data = $this->_gather();
+        $string = '';
+        foreach($data as $key => $val){
+            if(is_array($val)) foreach($val as $v){
+                $string .=  hsc($key)."\t".hsc($v)."\n";
+            }else{
+                $string .= hsc($key)."\t".hsc($val)."\n";
+            }
+        }
+        return $string;
+    }
+
 
     /**
      * Gather all information
@@ -256,4 +263,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;
+    }
+
 }

Other related posts: