Skip to content

Commit

Permalink
PSR-2 for popularity plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed May 8, 2018
1 parent 1cdd009 commit 29fc53c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 124 deletions.
34 changes: 17 additions & 17 deletions lib/plugins/popularity/action.php
Expand Up @@ -5,25 +5,23 @@
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/

require_once(DOKU_PLUGIN.'action.php');
require_once(DOKU_PLUGIN.'popularity/admin.php');

class action_plugin_popularity extends Dokuwiki_Action_Plugin {
class action_plugin_popularity extends Dokuwiki_Action_Plugin
{

/**
* @var helper_plugin_popularity
*/
protected $helper;

public function __construct(){
public function __construct()
{
$this->helper = $this->loadHelper('popularity', false);
}

/**
* Register its handlers with the dokuwiki's event controller
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, '_autosubmit', array());
/** @inheritdoc */
public function register(Doku_Event_Handler $controller)
{
$controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'autosubmit', array());
}

/**
Expand All @@ -32,23 +30,24 @@ public function register(Doku_Event_Handler $controller) {
* @param Doku_Event $event
* @param $param
*/
public function _autosubmit(Doku_Event &$event, $param){
public function autosubmit(Doku_Event &$event, $param)
{
//Do we have to send the data now
if ( !$this->helper->isAutosubmitEnabled() || $this->_isTooEarlyToSubmit() ){
if (!$this->helper->isAutosubmitEnabled() || $this->isTooEarlyToSubmit()) {
return;
}

//Actually send it
$status = $this->helper->sendData( $this->helper->gatherAsString() );
$status = $this->helper->sendData($this->helper->gatherAsString());

if ( $status !== '' ){
if ($status !== '') {
//If an error occured, log it
io_saveFile( $this->helper->autosubmitErrorFile, $status );
io_saveFile($this->helper->autosubmitErrorFile, $status);
} else {
//If the data has been sent successfully, previous log of errors are useless
@unlink($this->helper->autosubmitErrorFile);
//Update the last time we sent data
touch ( $this->helper->autosubmitFile );
touch($this->helper->autosubmitFile);
}

$event->stopPropagation();
Expand All @@ -59,7 +58,8 @@ public function _autosubmit(Doku_Event &$event, $param){
* Check if it's time to send autosubmit data
* (we should have check if autosubmit is enabled first)
*/
protected function _isTooEarlyToSubmit(){
protected function isTooEarlyToSubmit()
{
$lastSubmit = $this->helper->lastSentTime();
return $lastSubmit + 24*60*60*30 > time();
}
Expand Down
64 changes: 37 additions & 27 deletions lib/plugins/popularity/admin.php
Expand Up @@ -5,15 +5,18 @@
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/
class admin_plugin_popularity extends DokuWiki_Admin_Plugin {
class admin_plugin_popularity extends DokuWiki_Admin_Plugin
{

/**
* @var helper_plugin_popularity
*/
/** @var helper_plugin_popularity */
protected $helper;
protected $sentStatus = null;

public function __construct(){
/**
* admin_plugin_popularity constructor.
*/
public function __construct()
{
$this->helper = $this->loadHelper('popularity', false);
}

Expand All @@ -22,50 +25,55 @@ public function __construct(){
* @param $language
* @return string
*/
public function getMenuText($language) {
public function getMenuText($language)
{
return $this->getLang('name');
}

/**
* return sort order for position in admin menu
*/
public function getMenuSort() {
public function getMenuSort()
{
return 2000;
}

/**
* Accessible for managers
*/
public function forAdminOnly() {
public function forAdminOnly()
{
return false;
}


/**
* handle user request
*/
public function handle() {
public function handle()
{
global $INPUT;

//Send the data
if ( $INPUT->has('data') ){
$this->sentStatus = $this->helper->sendData( $INPUT->str('data') );
if ( $this->sentStatus === '' ){
if ($INPUT->has('data')) {
$this->sentStatus = $this->helper->sendData($INPUT->str('data'));
if ($this->sentStatus === '') {
//Update the last time we sent the data
touch ( $this->helper->popularityLastSubmitFile );
touch($this->helper->popularityLastSubmitFile);
}
//Deal with the autosubmit option
$this->_enableAutosubmit( $INPUT->has('autosubmit') );
$this->enableAutosubmit($INPUT->has('autosubmit'));
}
}

/**
* Enable or disable autosubmit
* @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it.
*/
protected function _enableAutosubmit( $enable ){
if ( $enable ){
io_saveFile( $this->helper->autosubmitFile, ' ');
protected function enableAutosubmit($enable)
{
if ($enable) {
io_saveFile($this->helper->autosubmitFile, ' ');
} else {
@unlink($this->helper->autosubmitFile);
}
Expand All @@ -74,17 +82,18 @@ protected function _enableAutosubmit( $enable ){
/**
* Output HTML form
*/
public function html() {
public function html()
{
global $INPUT;

if ( ! $INPUT->has('data') ){
if (! $INPUT->has('data')) {
echo $this->locale_xhtml('intro');

//If there was an error the last time we tried to autosubmit, warn the user
if ( $this->helper->isAutoSubmitEnabled() ){
if ( file_exists($this->helper->autosubmitErrorFile) ){
if ($this->helper->isAutoSubmitEnabled()) {
if (file_exists($this->helper->autosubmitErrorFile)) {
echo $this->getLang('autosubmitError');
echo io_readFile( $this->helper->autosubmitErrorFile );
echo io_readFile($this->helper->autosubmitErrorFile);
}
}

Expand All @@ -93,12 +102,12 @@ public function html() {

//Print the last time the data was sent
$lastSent = $this->helper->lastSentTime();
if ( $lastSent !== 0 ){
if ($lastSent !== 0) {
echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent);
}
} else {
//If we just submitted the form
if ( $this->sentStatus === '' ){
if ($this->sentStatus === '') {
//If we successfully sent the data
echo $this->locale_xhtml('submitted');
} else {
Expand All @@ -117,9 +126,10 @@ public function html() {
* @param string $data The popularity data, if it has already been computed. NULL otherwise.
* @return string The form, as an html string
*/
protected function buildForm($submissionMode, $data = null){
protected function buildForm($submissionMode, $data = null)
{
$url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script());
if ( is_null($data) ){
if (is_null($data)) {
$data = $this->helper->gatherAsString();
}

Expand All @@ -130,7 +140,7 @@ protected function buildForm($submissionMode, $data = null){
.'</textarea><br />';

//If we submit via the server, we give the opportunity to suscribe to the autosubmission option
if ( $submissionMode !== 'browser' ){
if ($submissionMode !== 'browser') {
$form .= '<label for="autosubmit">'
.'<input type="checkbox" name="autosubmit" id="autosubmit" '
.($this->helper->isAutosubmitEnabled() ? 'checked' : '' )
Expand Down

0 comments on commit 29fc53c

Please sign in to comment.