<?php
use Shaarli\Config\ConfigJson;
use Shaarli\Config\ConfigPhp;
+use Shaarli\Config\ConfigManager;
/**
* Class Updater.
return true;
}
+
+ /**
+ * Add 'http://' to Piwik URL the setting is set.
+ *
+ * @return bool true if the update is successful, false otherwise.
+ */
+ public function updateMethodPiwikUrl()
+ {
+ if (! $this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) {
+ return true;
+ }
+
+ $this->conf->set('plugins.PIWIK_URL', 'http://'. $this->conf->get('plugins.PIWIK_URL'));
+ $this->conf->write($this->isLoggedIn);
+ return true;
+ }
}
/**
--- /dev/null
+<!-- Piwik -->
+<script type="text/javascript">
+ var _paq = _paq || [];
+ _paq.push(['trackPageView']);
+ _paq.push(['enableLinkTracking']);
+ (function() {
+ var u="%s/";
+ _paq.push(['setTrackerUrl', u+'piwik.php']);
+ _paq.push(['setSiteId', '%s']);
+ var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
+ g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
+ })();
+</script>
+<noscript><p><img src="%s/piwik.php?idsite=%s" style="border:0;" alt="" /></p></noscript>
+<!-- End Piwik Code -->
\ No newline at end of file
}
// Free elements at the end of the page.
- $data['endofpage'][] = '<!-- Piwik -->' .
-'<script type="text/javascript">' .
-' var _paq = _paq || [];' .
-' _paq.push([\'trackPageView\']);' .
-' _paq.push([\'enableLinkTracking\']);' .
-' (function() {' .
-' var u="//' . $piwikUrl . '/";' .
-' _paq.push([\'setTrackerUrl\', u+\'piwik.php\']);' .
-' _paq.push([\'setSiteId\', \'' . $piwikSiteid . '\']);' .
-' var d=document, g=d.createElement(\'script\'), s=d.getElementsByTagName(\'script\')[0];' .
-' g.type=\'text/javascript\'; g.async=true; g.defer=true; g.src=u+\'piwik.js\'; s.parentNode.insertBefore(g,s);' .
-' })();' .
-'</script>' .
-'<noscript><p><img src="//' . $piwikUrl . '/piwik.php?idsite=' . $piwikSiteid . '" style="border:0;" alt="" /></p></noscript>' .
-'<!-- End Piwik Code -->';
+ $data['endofpage'][] = sprintf(
+ file_get_contents(PluginManager::$PLUGINS_PATH . '/piwik/piwik.html'),
+ $piwikUrl,
+ $piwikSiteid,
+ $piwikUrl,
+ $piwikSiteid
+ );
return $data;
}
-
$this->assertTrue($updater->updateMethodEscapeMarkdown());
$this->assertFalse($this->conf->get('security.markdown_escape'));
}
+
+ /**
+ * Test updateMethodPiwikUrl with valid data
+ */
+ public function testUpdatePiwikUrlValid()
+ {
+ $sandboxConf = 'sandbox/config';
+ copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
+ $this->conf = new ConfigManager($sandboxConf);
+ $url = 'mypiwik.tld';
+ $this->conf->set('plugins.PIWIK_URL', $url);
+ $updater = new Updater([], [], $this->conf, true);
+ $this->assertTrue($updater->updateMethodPiwikUrl());
+ $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
+
+ // reload from file
+ $this->conf = new ConfigManager($sandboxConf);
+ $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
+ }
+
+ /**
+ * Test updateMethodPiwikUrl without setting
+ */
+ public function testUpdatePiwikUrlEmpty()
+ {
+ $updater = new Updater([], [], $this->conf, true);
+ $this->assertTrue($updater->updateMethodPiwikUrl());
+ $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
+ }
+
+ /**
+ * Test updateMethodPiwikUrl: valid URL, nothing to do
+ */
+ public function testUpdatePiwikUrlNothingToDo()
+ {
+ $url = 'https://mypiwik.tld';
+ $this->conf->set('plugins.PIWIK_URL', $url);
+ $updater = new Updater([], [], $this->conf, true);
+ $this->assertTrue($updater->updateMethodPiwikUrl());
+ $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
+ }
}