Skip to content

Commit

Permalink
Replace deprecated sqlite helper
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Oct 8, 2024
1 parent dafd094 commit 84c6f06
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 126 deletions.
34 changes: 14 additions & 20 deletions action.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<?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
{

Expand Down Expand Up @@ -123,21 +121,20 @@ public function handle_parser_metadata_render(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'];
Expand All @@ -152,7 +149,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 +165,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 +175,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 Down Expand Up @@ -337,9 +334,6 @@ protected function getLastMaintainerRev($meta, $maintainer, &$rev)
*/
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);
Expand Down
63 changes: 31 additions & 32 deletions admin.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?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
{

Expand Down Expand Up @@ -46,17 +46,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');

ptln('<h1>' . $this->getLang('menu') . '</h1>');
/** @var SQLiteDB */
$sqlite = $dbHelper->getDB();

ptln('<div id="plugin__watchcycle_admin">');
echo '<h1>' . $this->getLang('menu') . '</h1>';

$form = new \dokuwiki\Form\Form();
$filter_input = new \dokuwiki\Form\InputElement('text', 'filter');
echo '<div id="plugin__watchcycle_admin">';

$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 +69,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,8 +92,9 @@ public function html()
}
$href = wl($ID, $param);

ptln('<th><a href="' . $href . '">' . $icon . ' ' . $lang . '</a></th>');
echo '<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 = [];
Expand All @@ -116,23 +118,20 @@ public function html()
}
}

$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>');
$rows = $sqlite->queryAll($q, $q_args);
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:
101 changes: 27 additions & 74 deletions helper/db.php
Original file line number Diff line number Diff line change
@@ -1,103 +1,56 @@
<?php

use dokuwiki\ErrorHandler;
use dokuwiki\Extension\Plugin;
use dokuwiki\plugin\sqlite\SQLiteDB;

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

// must be run within Dokuwiki

if (!defined('DOKU_INC')) {
die();
}

class helper_plugin_watchcycle_db extends DokuWiki_Plugin
class helper_plugin_watchcycle_db extends Plugin
{
/** @var helper_plugin_sqlite */
/** @var SQLiteDB */
protected $sqlite;

/**
* helper_plugin_struct_db constructor.
*/
public function __construct()
{
$this->init();
}

/**
* Initialize the database
*
* @throws Exception
*/
protected function init()
{
/** @var helper_plugin_sqlite $sqlite */
$this->sqlite = plugin_load('helper', 'sqlite');
if (!$this->sqlite) {
if (defined('DOKU_UNITTEST')) {
throw new \Exception('Couldn\'t load sqlite.');
}
return;
}

if ($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
if (defined('DOKU_UNITTEST')) {
throw new \Exception('Couldn\'t load PDO sqlite.');
}
$this->sqlite = null;
return;
}
$this->sqlite->getAdapter()->setUseNativeAlter(true);

// initialize the database connection
if (!$this->sqlite->init('watchcycle', DOKU_PLUGIN . 'watchcycle/db/')) {
if (defined('DOKU_UNITTEST')) {
throw new \Exception('Couldn\'t init sqlite.');
}
$this->sqlite = null;
return;
}
$this->sqlite = new SQLiteDB('watchcycle', DOKU_PLUGIN . 'watchcycle/db/');

$helper = plugin_load('helper', 'watchcycle');
$this->sqlite->create_function('DAYS_AGO', [$helper, 'daysAgo'], 1);
$this->sqlite->getPdo()->sqliteCreateFunction('DAYS_AGO', [$helper, 'daysAgo'], 1);
}

/**
* @return helper_plugin_sqlite|null
* @param bool $throw throw an Exception when sqlite not available or fails to load
* @return SQLiteDB|null
* @throws Exception
*/
public function getDB()
public function getDB($throw = true)
{
global $conf;
$len = strlen($conf['metadir']);
if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(), 0, $len)) {
$this->init();
}
if (!$this->sqlite) {
msg($this->getLang('error sqlite missing'), -1);
return false;
}
return $this->sqlite;
}
if (!$this->sqlite instanceof SQLiteDB) {
if (!class_exists(SQLiteDB::class)) {
if ($throw || defined('DOKU_UNITTEST')) throw new StructException('no sqlite');
return null;
}

/**
* Completely remove the database and reinitialize it
*
* You do not want to call this except for testing!
*/
public function resetDB()
{
if (!$this->sqlite) {
return;
}
$file = $this->sqlite->getAdapter()->getDbFile();
if (!$file) {
return;
try {
$this->init();
} catch (\Exception $exception) {
ErrorHandler::logException($exception);
if ($throw) throw $exception;
return null;
}
}
unlink($file);
clearstatcache(true, $file);
$this->init();
return $this->sqlite;
}
}

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

0 comments on commit 84c6f06

Please sign in to comment.