Skip to content

Commit

Permalink
Merge pull request #12 from cosmocode/publishable-check
Browse files Browse the repository at this point in the history
Fix isPublisher() check
  • Loading branch information
annda committed Aug 31, 2023
2 parents 9dfb18f + 31e730e commit e7ddd94
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 46 deletions.
1 change: 1 addition & 0 deletions action/cache.php
Expand Up @@ -3,6 +3,7 @@
/**
* Double caching of pages containing struct aggregations:
* one for regular users, one for publishers/approvers
*
* @see action_plugin_struct_cache
*/
class action_plugin_structpublish_cache extends DokuWiki_Action_Plugin
Expand Down
4 changes: 3 additions & 1 deletion action/migration.php
Expand Up @@ -42,7 +42,9 @@ public function handleMigrations(Doku_Event $event)

// check if struct has required version
if ($dbVersionStruct < self::MIN_DB_STRUCT) {
throw new Exception('Plugin struct is outdated. Minimum required database version is ' . self::MIN_DB_STRUCT);
throw new Exception(
'Plugin struct is outdated. Minimum required database version is ' . self::MIN_DB_STRUCT
);
}

// check whether we are already up-to-date
Expand Down
16 changes: 11 additions & 5 deletions action/revisions.php
@@ -1,11 +1,10 @@
<?php

use dokuwiki\plugin\structpublish\meta\Revision;
use \dokuwiki\plugin\structpublish\meta\Constants;
use dokuwiki\plugin\structpublish\meta\Constants;

class action_plugin_structpublish_revisions extends DokuWiki_Action_Plugin
{

public function register(Doku_Event_Handler $controller)
{
$controller->register_hook('FORM_REVISIONS_OUTPUT', 'BEFORE', $this, 'handleRevisions');
Expand All @@ -27,7 +26,9 @@ public function handleRevisions(Doku_Event $event)
/** @var helper_plugin_structpublish_db $helper */
$helper = plugin_load('helper', 'structpublish_db');

if (!$helper->isPublishable()) return;
if (!$helper->isPublishable()) {
return;
}

$elCount = $form->elementCount();
$checkName = 'rev2[]';
Expand All @@ -52,9 +53,14 @@ public function handleRevisions(Doku_Event $event)
$version = $revision->getVersion();

// insert status for published revisions
if (is_a($el, \dokuwiki\Form\HTMLElement::class) && !empty(trim($el->val())) && $status === Constants::STATUS_PUBLISHED) {
if (
is_a($el, \dokuwiki\Form\HTMLElement::class) &&
!empty(trim($el->val())) &&
$status === Constants::STATUS_PUBLISHED
) {
$val = $el->val();
$label = '<span class="plugin-structpublish-version">' . $status . ' (' . $this->getLang('version') . ' ' . $version . ')</span>';
$label = '<span class="plugin-structpublish-version">' .
$status . ' (' . $this->getLang('version') . ' ' . $version . ')</span>';
$el->val("$val $label");
}
}
Expand Down
2 changes: 1 addition & 1 deletion action/save.php
Expand Up @@ -41,7 +41,7 @@ public function handleSave(Doku_Event $event)

try {
$revision->save();
} catch(StructException $e) {
} catch (StructException $e) {
msg($e->getMessage(), -1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion action/show.php
Expand Up @@ -6,7 +6,7 @@
class action_plugin_structpublish_show extends DokuWiki_Action_Plugin
{
/** @var int */
static protected $latestPublishedRev;
protected static $latestPublishedRev;

/** @inheritDoc */
public function register(Doku_Event_Handler $controller)
Expand Down
26 changes: 17 additions & 9 deletions admin.php
Expand Up @@ -37,7 +37,7 @@ public function handle()

try {
$assignments = Assignments::getInstance();
} catch(Exception $e) {
} catch (Exception $e) {
msg($e->getMessage(), -1);
return;
}
Expand All @@ -46,8 +46,11 @@ public function handle()
$assignment = $INPUT->arr('assignment');
if (!blank($assignment['pattern']) && !blank($assignment['status'])) {
if ($INPUT->str('action') === 'delete') {
$ok = $assignments->removePattern($assignment['pattern'], $assignment['user'],
$assignment['status']);
$ok = $assignments->removePattern(
$assignment['pattern'],
$assignment['user'],
$assignment['status']
);
if (!$ok) {
msg('failed to remove pattern', -1);
}
Expand All @@ -56,15 +59,21 @@ public function handle()
if (@preg_match($assignment['pattern'], null) === false) {
msg('Invalid regular expression. Pattern not saved', -1);
} else {
$ok = $assignments->addPattern($assignment['pattern'], $assignment['user'],
$assignment['status']);
$ok = $assignments->addPattern(
$assignment['pattern'],
$assignment['user'],
$assignment['status']
);
if (!$ok) {
msg('failed to add pattern', -1);
}
}
} else {
$ok = $assignments->addPattern($assignment['pattern'], $assignment['user'],
$assignment['status']);
$ok = $assignments->addPattern(
$assignment['pattern'],
$assignment['user'],
$assignment['status']
);
if (!$ok) {
msg('failed to add pattern', -1);
}
Expand All @@ -87,7 +96,7 @@ public function html()

try {
$assignments = Assignments::getInstance();
} catch(Exception $e) {
} catch (Exception $e) {
msg($e->getMessage(), -1);
return;
}
Expand Down Expand Up @@ -152,4 +161,3 @@ public function html()
echo '</form>';
}
}

1 change: 1 addition & 0 deletions conf/default.php
@@ -1,4 +1,5 @@
<?php

/**
* Default settings for the structpublish plugin
*
Expand Down
1 change: 1 addition & 0 deletions conf/metadata.php
@@ -1,4 +1,5 @@
<?php

/**
* Options for the structpublish plugin
*
Expand Down
1 change: 0 additions & 1 deletion helper/assignments.php
Expand Up @@ -16,7 +16,6 @@ class helper_plugin_structpublish_assignments extends DokuWiki_Plugin
* @param string|null $pns optimization, the colon wrapped namespace of the page, set null for automatic
* @return bool
* @author Andreas Gohr
*
*/
public function matchPagePattern($pattern, $page, $pns = null)
{
Expand Down
35 changes: 25 additions & 10 deletions helper/db.php
Expand Up @@ -17,15 +17,17 @@ public function getDB()
{
/** @var helper_plugin_struct_db $struct */
$struct = plugin_load('helper', 'struct_db');
if(!$struct) {
if (!$struct) {
// FIXME show message?
return null;
}
$sqlite = $struct->getDB(false);
if(!$sqlite) return null;
if (!$sqlite) {
return null;
}

// on init
if(!$this->initialized) {
if (!$this->initialized) {
$sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'isPublisher'], -1);
$this->initialized = true;
}
Expand All @@ -35,31 +37,41 @@ public function getDB()

/**
* Get list of all pages known to the plugin
*
* @return array
*/
public function getPages()
{
$sqlite = $this->getDB();
if(!$sqlite) return [];
if (!$sqlite) {
return [];
}

$sql = 'SELECT pid FROM titles';
$list = $sqlite->queryAll($sql);
return array_column($list, 'pid');
}

/**
* Returns true if the current page is included in publishing workflows
* Returns true if the given page is included in publishing workflows.
* If no pid is given, check current page.
*
* @return bool
*/
public function isPublishable()
public function isPublishable($pid = null)
{
global $ID;
$sqlite = $this->getDB();
if(!$sqlite) return false;
if (!$sqlite) {
return false;
}

if (!$pid) {
$pid = $ID;
}

$sql = 'SELECT pid FROM structpublish_assignments WHERE pid = ? AND assigned = 1';
return (bool) $sqlite->queryAll($sql, $ID);
return (bool) $sqlite->queryAll($sql, $pid);
}

/**
Expand All @@ -84,13 +96,17 @@ public function checkAccess($pid, $roles = [])
*/
public function isPublisher()
{
if (!$this->isPublishable()) return 1;

global $USERINFO;
global $INPUT;

$args = func_get_args();
$pid = $args[0];

if (!$pid || !$this->isPublishable($pid)) {
return 1;
}

$userId = $args[1] ?? $INPUT->server->str('REMOTE_USER');
$grps = $args[2] ?? ($USERINFO['grps'] ?? []);

Expand Down Expand Up @@ -143,5 +159,4 @@ public static function userHasRole($pid, $userId = '', $grps = [], $roles = [])

return false;
}

}
16 changes: 12 additions & 4 deletions helper/notify.php
Expand Up @@ -29,15 +29,19 @@ public function __construct()
public function sendEmails($action, $newRevision)
{

if (!$this->triggerNotification($action)) return;
if (!$this->triggerNotification($action)) {
return;
}

// get assignees from DB
$assignments = Assignments::getInstance();
$assignees = $assignments->getPageAssignments($newRevision->getId(), false);

// get recipients for the next workflow step
$nextAction = Constants::workflowSteps($action)['nextAction'];
if (is_null($nextAction)) return;
if (is_null($nextAction)) {
return;
}

if (empty($assignees[$nextAction])) {
msg($this->getLang('email_error_norecipients'), -1);
Expand Down Expand Up @@ -133,7 +137,9 @@ protected function resolveUser(&$resolved, $recipient)
/** @var AuthPlugin $auth */
global $auth;
$user = $auth->getUserData($recipient);
if ($user) $resolved[] = $user['mail'];
if ($user) {
$resolved[] = $user['mail'];
}
}

/**
Expand All @@ -143,7 +149,9 @@ protected function resolveUser(&$resolved, $recipient)
*/
private function triggerNotification($action)
{
if (!$this->getConf('email_enable')) return false;
if (!$this->getConf('email_enable')) {
return false;
}

$actions = array_map('trim', explode(',', $this->getConf('email_status')));
return in_array($action, $actions);
Expand Down
17 changes: 9 additions & 8 deletions helper/publish.php
Expand Up @@ -12,7 +12,6 @@
*/
class helper_plugin_structpublish_publish extends DokuWiki_Plugin
{

/** @var helper_plugin_structpublish_db */
protected $dbHelper;

Expand All @@ -33,9 +32,7 @@ public function saveRevision($action, $newversion = '')
global $ID;
global $INFO;

if (
!$this->dbHelper->checkAccess($ID, [$action])
) {
if (!$this->dbHelper->checkAccess($ID, [$action])) {
throw new \Exception('User may not ' . $action);
}

Expand Down Expand Up @@ -81,10 +78,14 @@ protected function updateSchemaData()
$sqlite->query("UPDATE multi_$table SET published = 0 WHERE pid = ?", [$ID]);

// publish the current revision
$sqlite->query("UPDATE data_$table SET published = 1 WHERE pid = ? AND rev = ?",
[$ID, $INFO['currentrev']]);
$sqlite->query("UPDATE multi_$table SET published = 1 WHERE pid = ? AND rev = ?",
[$ID, $INFO['currentrev']]);
$sqlite->query(
"UPDATE data_$table SET published = 1 WHERE pid = ? AND rev = ?",
[$ID, $INFO['currentrev']]
);
$sqlite->query(
"UPDATE multi_$table SET published = 1 WHERE pid = ? AND rev = ?",
[$ID, $INFO['currentrev']]
);
}
}
}
2 changes: 1 addition & 1 deletion meta/Assignments.php
Expand Up @@ -26,7 +26,7 @@ class Assignments
/**
* Get the singleton instance of the Assignments
*
* @param bool $forcereload create a new instace to reload the assignment data
* @param bool $forcereload create a new instance to reload the assignment data
* @return Assignments
*/
public static function getInstance($forcereload = false)
Expand Down
1 change: 0 additions & 1 deletion meta/Constants.php
Expand Up @@ -18,7 +18,6 @@ class Constants
const ACTION_APPROVE = 'approve';
const ACTION_PUBLISH = 'publish';


/**
* Convenience function mapping transition actions to resulting status
*
Expand Down

0 comments on commit e7ddd94

Please sign in to comment.