Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI component #14

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/dokuwiki.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: DokuWiki Default Tasks
on:
push:
pull_request:
schedule:
- cron: '11 7 12 * *'


jobs:
all:
uses: dokuwiki/github-action/.github/workflows/all.yml@main
112 changes: 33 additions & 79 deletions action.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

use dokuwiki\plugin\sqlite\SQLiteDB;

/**
* DokuWiki Plugin watchcycle (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Szymon Olewniczak <[email protected]>
*/

// must be run within Dokuwiki
if (!defined('DOKU_INC')) {
die();
}

class action_plugin_watchcycle extends DokuWiki_Action_Plugin
{

/**
* Registers a callback function for a given event
*
Expand All @@ -24,20 +21,20 @@ class action_plugin_watchcycle extends DokuWiki_Action_Plugin
public function register(Doku_Event_Handler $controller)
{

$controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_parser_metadata_render');
$controller->register_hook('PARSER_CACHE_USE', 'AFTER', $this, 'handle_parser_cache_use');
$controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handleParserMetadataRender');
$controller->register_hook('PARSER_CACHE_USE', 'AFTER', $this, 'handleParserCacheUse');
// ensure a page revision is created when summary changes:
$controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before');
$controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handlePagesaveBefore');
$controller->register_hook('SEARCH_RESULT_PAGELOOKUP', 'BEFORE', $this, 'addIconToPageLookupResult');
$controller->register_hook('SEARCH_RESULT_FULLPAGE', 'BEFORE', $this, 'addIconToFullPageResult');
$controller->register_hook('FORM_SEARCH_OUTPUT', 'BEFORE', $this, 'addFilterToSearchForm');
$controller->register_hook('FORM_QUICKSEARCH_OUTPUT', 'BEFORE', $this, 'handle_form_quicksearch_output');
$controller->register_hook('FORM_QUICKSEARCH_OUTPUT', 'BEFORE', $this, 'handleFormQuicksearchOutput');
$controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'filterSearchResults');
$controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'filterSearchResults');

$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'handle_toolbar_define');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_get');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_validate');
$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'handleToolbarDefine');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxGet');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxValidate');
}


Expand All @@ -50,7 +47,7 @@ public function register(Doku_Event_Handler $controller)
*
* @return void
*/
public function handle_toolbar_define(Doku_Event $event, $param)
public function handleToolbarDefine(Doku_Event $event, $param)
{
$event->data[] = [
'type' => 'plugin_watchcycle',
Expand Down Expand Up @@ -83,7 +80,7 @@ public function addFilterToSearchForm(Doku_Event $event, $param)
*
* @return void
*/
public function handle_form_quicksearch_output(Doku_Event $event, $param)
public function handleFormQuicksearchOutput(Doku_Event $event, $param)
{
/** @var \dokuwiki\Form\Form $qsearchForm */
$qsearchForm = $event->data;
Expand Down Expand Up @@ -119,31 +116,30 @@ public function filterSearchResults(Doku_Event $event, $param)
*
* @return void
*/
public function handle_parser_metadata_render(Doku_Event $event, $param)
public function handleParserMetadataRender(Doku_Event $event, $param)
{
global $ID;

/** @var \helper_plugin_sqlite $sqlite */
$sqlite = plugin_load('helper', 'watchcycle_db')->getDB();
if (!$sqlite) {
msg($this->getLang('error sqlite missing'), -1);
return;
}
/** @var \helper_plugin_watchcycle_db $dbHelper */
$dbHelper = plugin_load('helper', 'watchcycle_db');

/** @var SQLiteDB */
$sqlite = $dbHelper->getDB();

/* @var \helper_plugin_watchcycle $helper */
$helper = plugin_load('helper', 'watchcycle');

$page = $event->data['current']['last_change']['id'];

if (isset($event->data['current']['plugin']['watchcycle'])) {
$watchcycle = $event->data['current']['plugin']['watchcycle'];
$res = $sqlite->query('SELECT * FROM watchcycle WHERE page=?', $page);
$row = $sqlite->res2row($res);
$row = $sqlite->queryRecord('SELECT * FROM watchcycle WHERE page=?', $page);
$changes = $this->getLastMaintainerRev($event->data, $watchcycle['maintainer'], $last_maintainer_rev);
//false if page needs checking
$uptodate = $helper->daysAgo($last_maintainer_rev) <= (int)$watchcycle['cycle'];

if ($uptodate === false) {
$this->informMaintainer($watchcycle['maintainer'], $ID);
$helper->informMaintainer($watchcycle['maintainer'], $ID);
}

if (!$row) {
Expand All @@ -152,7 +148,7 @@ public function handle_parser_metadata_render(Doku_Event $event, $param)
$entry['last_maintainer_rev'] = $last_maintainer_rev;
// uptodate is an int in the database
$entry['uptodate'] = (int)$uptodate;
$sqlite->storeEntry('watchcycle', $entry);
$sqlite->saveRecord('watchcycle', $entry);
} else { //check if we need to update something
$toupdate = [];

Expand All @@ -168,8 +164,8 @@ public function handle_parser_metadata_render(Doku_Event $event, $param)
$toupdate['last_maintainer_rev'] = $last_maintainer_rev;
}

//uptodate value has changed? compare with the string we got from the database
if ($row['uptodate'] !== (string)(int)$uptodate) {
//uptodate value has changed?
if ($row['uptodate'] !== (int)$uptodate) {
$toupdate['uptodate'] = (int)$uptodate;
}

Expand All @@ -178,7 +174,7 @@ public function handle_parser_metadata_render(Doku_Event $event, $param)
return "$v=?";
}, array_keys($toupdate)));
$toupdate[] = $page;
$sqlite->query("UPDATE watchcycle SET $set WHERE page=?", $toupdate);
$sqlite->query("UPDATE watchcycle SET $set WHERE page=?", array_values($toupdate));
}
}
$event->data['current']['plugin']['watchcycle']['last_maintainer_rev'] = $last_maintainer_rev;
Expand All @@ -194,7 +190,7 @@ public function handle_parser_metadata_render(Doku_Event $event, $param)
* @param Doku_Event $event
* @param string $param
*/
public function handle_ajax_get(Doku_Event $event, $param)
public function handleAjaxGet(Doku_Event $event, $param)
{
if ($event->data != 'plugin_watchcycle_get') return;
$event->preventDefault();
Expand All @@ -204,11 +200,11 @@ public function handle_ajax_get(Doku_Event $event, $param)
header('Content-Type: application/json');
try {
$result = $this->fetchUsersAndGroups();
} catch(\Exception $e) {
} catch (\Exception $e) {
$result = [
'error' => $e->getMessage().' '.basename($e->getFile()).':'.$e->getLine()
'error' => $e->getMessage() . ' ' . basename($e->getFile()) . ':' . $e->getLine()
];
if($conf['allowdebug']) {
if ($conf['allowdebug']) {
$result['stacktrace'] = $e->getTraceAsString();
}
http_status(500);
Expand All @@ -223,7 +219,7 @@ public function handle_ajax_get(Doku_Event $event, $param)
* @param Doku_Event $event
* @param $param
*/
public function handle_ajax_validate(Doku_Event $event, $param)
public function handleAjaxValidate(Doku_Event $event, $param)
{
if ($event->data != 'plugin_watchcycle_validate') return;
$event->preventDefault();
Expand Down Expand Up @@ -269,7 +265,7 @@ protected function fetchUsersAndGroups()

// check cache
$cachedGroups = new cache('retrievedGroups', '.txt');
if($cachedGroups->useCache(['age' => 30])) {
if ($cachedGroups->useCache(['age' => 30])) {
$foundGroups = unserialize($cachedGroups->retrieveCache());
} else {
$foundGroups = $auth->retrieveGroups();
Expand Down Expand Up @@ -329,25 +325,6 @@ protected function getLastMaintainerRev($meta, $maintainer, &$rev)
return -1;
}

/**
* Inform all maintainers that the page needs checking
*
* @param string $def defined maintainers
* @param string $page that needs checking
*/
protected function informMaintainer($def, $page)
{
/* @var DokuWiki_Auth_Plugin $auth */
global $auth;

/* @var \helper_plugin_watchcycle $helper */
$helper = plugin_load('helper', 'watchcycle');
$mails = $helper->getMaintainerMails($def);
foreach ($mails as $mail) {
$this->sendMail($mail, $page);
}
}

/**
* clean the cache every 24 hours
*
Expand All @@ -357,7 +334,7 @@ protected function informMaintainer($def, $page)
*
* @return void
*/
public function handle_parser_cache_use(Doku_Event $event, $param)
public function handleParserCacheUse(Doku_Event $event, $param)
{
/* @var \helper_plugin_watchcycle $helper */
$helper = plugin_load('helper', 'watchcycle');
Expand All @@ -376,7 +353,7 @@ public function handle_parser_cache_use(Doku_Event $event, $param)
*
* @return void
*/
public function handle_pagesave_before(Doku_Event $event, $param)
public function handlePagesaveBefore(Doku_Event $event, $param)
{
if ($event->data['contentChanged']) {
return;
Expand Down Expand Up @@ -422,27 +399,4 @@ public function addIconToFullPageResult(Doku_Event $event, $param)
$event->data['resultHeader'][] = $icon;
}
}

/**
* Sends an email
*
* @param array $mail
* @param string $page
*/
protected function sendMail($mail, $page)
{
$mailer = new Mailer();
$mailer->to($mail);
$mailer->subject($this->getLang('mail subject'));
$text = sprintf($this->getLang('mail body'), $page);
$link = '<a href="' . wl($page, '', true) . '">' . $page . '</a>';
$html = sprintf($this->getLang('mail body'), $link);
$mailer->setBody($text, null, null, $html);

if (!$mailer->send()) {
msg($this->getLang('error mail'), -1);
}
}
}

// vim:ts=4:sw=4:et:
84 changes: 30 additions & 54 deletions admin.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php

use dokuwiki\Form\Form;
use dokuwiki\Form\InputElement;
use dokuwiki\plugin\sqlite\SQLiteDB;

/**
* DokuWiki Plugin watchcycle (Admin Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Szymon Olewniczak <[email protected]>
*/

// must be run within Dokuwiki
if (!defined('DOKU_INC')) {
die();
}

class admin_plugin_watchcycle extends DokuWiki_Admin_Plugin
{

/**
* @return int sort number in admin menu
*/
Expand Down Expand Up @@ -46,17 +45,18 @@ public function html()
/* @var Input */
global $INPUT;

/** @var \helper_plugin_sqlite $sqlite */
$sqlite = plugin_load('helper', 'watchcycle_db')->getDB();
/* @var \helper_plugin_watchcycle */
$helper = plugin_load('helper', 'watchcycle');
/** @var \helper_plugin_watchcycle_db $dbHelper */
$dbHelper = plugin_load('helper', 'watchcycle_db');

/** @var SQLiteDB */
$sqlite = $dbHelper->getDB();

ptln('<h1>' . $this->getLang('menu') . '</h1>');
echo '<h1>' . $this->getLang('menu') . '</h1>';

ptln('<div id="plugin__watchcycle_admin">');
echo '<div id="plugin__watchcycle_admin">';

$form = new \dokuwiki\Form\Form();
$filter_input = new \dokuwiki\Form\InputElement('text', 'filter');
$form = new Form();
$filter_input = new InputElement('text', 'filter');
$filter_input->attr('placeholder', $this->getLang('search page'));
$form->addElement($filter_input);

Expand All @@ -68,9 +68,9 @@ public function html()
$form->addHTML('</label>');


ptln($form->toHTML());
ptln('<table>');
ptln('<tr>');
echo $form->toHTML();
echo '<table>';
echo '<tr>';
$headers = ['page', 'maintainer', 'cycle', 'current', 'uptodate'];
foreach ($headers as $header) {
$lang = $this->getLang("h $header");
Expand All @@ -91,48 +91,24 @@ public function html()
}
$href = wl($ID, $param);

ptln('<th><a href="' . $href . '">' . $icon . ' ' . $lang . '</a></th>');
}
$q = 'SELECT page, maintainer, cycle, DAYS_AGO(last_maintainer_rev) AS current, uptodate FROM watchcycle';
$where = [];
$q_args = [];
if ($INPUT->str('filter') != '') {
$where[] = 'page LIKE ?';
$q_args[] = '%' . $INPUT->str('filter') . '%';
}
if ($INPUT->has('outdated')) {
$where[] = 'uptodate=0';
}

if (count($where) > 0) {
$q .= ' WHERE ';
$q .= implode(' AND ', $where);
echo '<th><a href="' . $href . '">' . $icon . ' ' . $lang . '</a></th>';
}

if ($INPUT->has('sortby') && in_array($INPUT->str('sortby'), $headers)) {
$q .= ' ORDER BY ' . $INPUT->str('sortby');
if ($INPUT->int('desc') == 1) {
$q .= ' DESC';
}
}
$rows = $dbHelper->getAll($headers);

$res = $sqlite->query($q, $q_args);
while ($row = $sqlite->res2row($res)) {
ptln('<tr>');
ptln('<td><a href="' . wl($row['page']) . '" class="wikilink1">' . $row['page'] . '</a></td>');
ptln('<td>' . $row['maintainer'] . '</td>');
ptln('<td>' . $row['cycle'] . '</td>');
ptln('<td>' . $row['current'] . '</td>');
foreach ($rows as $row) {
echo '<tr>';
echo '<td><a href="' . wl($row['page']) . '" class="wikilink1">' . $row['page'] . '</a></td>';
echo '<td>' . $row['maintainer'] . '</td>';
echo '<td>' . $row['cycle'] . '</td>';
echo '<td>' . $row['current'] . '</td>';
$icon = $row['uptodate'] == 1 ? '✓' : '✕';
ptln('<td>' . $icon . '</td>');
ptln('</tr>');
echo '<td>' . $icon . '</td>';
echo '</tr>';
}

ptln('</tr>');
ptln('</table>');

ptln('</div>');
echo '</tr>';
echo '</table>';
echo '</div>';
}
}

// vim:ts=4:sw=4:et:
Loading