From 736b9bf4f111f2ccbfc7d7ef68a31fa0960249ef Mon Sep 17 00:00:00 2001 From: lpaulsen93 Date: Sat, 21 Nov 2020 14:37:49 +0100 Subject: [PATCH 1/2] Fix array operations --- syntax.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/syntax.php b/syntax.php index 7eeff8e..8ecfc1e 100644 --- a/syntax.php +++ b/syntax.php @@ -80,8 +80,8 @@ function render($mode, Doku_Renderer $renderer, $data) { // be careful with some senisitive arrays of $meta } elseif (in_array($key, $protected)) { - if (array_key_exists($key, $renderer->meta)) { - $renderer->meta[$key] = array_merge($renderer->meta[$key], $value); + if (is_array($renderer->meta) && array_key_exists($key, $renderer->meta)) { + $renderer->meta[$key] = array_merge($renderer->meta[$key], array($value)); } else { $renderer->meta[$key] = $value; } From c37c776851505fb9f4d58b98138abced0a98ffa6 Mon Sep 17 00:00:00 2001 From: lpaulsen93 Date: Sat, 21 Nov 2020 14:59:28 +0100 Subject: [PATCH 2/2] Code cleanup --- syntax.php | 123 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 43 deletions(-) diff --git a/syntax.php b/syntax.php index 8ecfc1e..835fe1e 100644 --- a/syntax.php +++ b/syntax.php @@ -13,24 +13,41 @@ * All DokuWiki plugins to extend the parser/rendering mechanism * need to inherit from this class */ -class syntax_plugin_meta extends DokuWiki_Syntax_Plugin { - function getType() { return 'substition'; } - function getSort() { return 99; } - function connectTo($mode) { $this->Lexer->addSpecialPattern('~~META:.*?~~',$mode,'plugin_meta');} +class syntax_plugin_meta extends DokuWiki_Syntax_Plugin +{ + function getType() + { + return 'substition'; + } + + function getSort() + { + return 99; + } + + function connectTo($mode) + { + $this->Lexer->addSpecialPattern('~~META:.*?~~',$mode,'plugin_meta'); + } /** * Handle the match */ - function handle($match, $state, $pos, Doku_Handler $handler){ - $match = substr($match,7,-2); //strip ~~META: from start and ~~ from end + public function handle($match, $state, $pos, Doku_Handler $handler) + { + // strip ~~META: from start and ~~ from end + $match = substr($match,7,-2); $data = array(); $pairs = explode('&', $match); foreach ($pairs as $pair) { list($key, $value) = explode('=', $pair, 2); list($key, $subkey) = explode(' ', $key, 2); - if (trim($subkey)) $data[trim($key)][trim($subkey)] = trim($value); - else $data[trim($key)] = trim($value); + if (trim($subkey)) { + $data[trim($key)][trim($subkey)] = trim($value); + } else { + $data[trim($key)] = trim($value); + } } $data = array_change_key_case($data, CASE_LOWER); @@ -40,9 +57,11 @@ function handle($match, $state, $pos, Doku_Handler $handler){ /** * Create output */ - function render($mode, Doku_Renderer $renderer, $data) { + public function render($mode, Doku_Renderer $renderer, $data) + { if ($mode == 'xthml') { - return true; // don't output anything + // don't output anything + return true; } elseif ($mode == 'metadata') { /** @var Doku_Renderer_metadata $renderer */ @@ -50,9 +69,12 @@ function render($mode, Doku_Renderer $renderer, $data) { if (isset($data['date'])) { if (is_array($data['date'])) { foreach ($data['date'] as $key => $date) { - $date = $this->_convertDate(trim($date)); - if (!$date) unset($data['date'][$key]); - else $data['date'][$key] = $date; + $date = $this->convertDate(trim($date)); + if (!$date) { + unset($data['date'][$key]); + } else { + $data['date'][$key] = $date; + } } } else { unset($data['date']); @@ -73,21 +95,20 @@ function render($mode, Doku_Renderer $renderer, $data) { Therefore the only thing we can do is setting this internal value by calling _firstimage. This fails if there has already been a first image saved. */ $renderer->_firstimage($subvalue); - } else { // for everything else assume that we have a page id + } else { + // for everything else assume that we have a page id $renderer->meta[$key][$subkey][cleanID($subvalue)] = page_exists($subvalue); } } - - // be careful with some senisitive arrays of $meta } elseif (in_array($key, $protected)) { + // be careful with some sensitive arrays of $meta if (is_array($renderer->meta) && array_key_exists($key, $renderer->meta)) { $renderer->meta[$key] = array_merge($renderer->meta[$key], array($value)); } else { $renderer->meta[$key] = $value; } - - // no special treatment for the rest } else { + // no special treatment for the rest $renderer->meta[$key] = $value; } } @@ -97,69 +118,85 @@ function render($mode, Doku_Renderer $renderer, $data) { /** * converts YYYY-MM-DD[ hh:mm:ss][ -> [YYYY-MM-DD ]hh:mm:ss] to PHP timestamps */ - function _convertDate($date) { + private function convertDate($date) + { list($start, $end) = explode('->', $date, 2); - // single date if (!$end) { + // single date list($date, $time) = explode(' ', trim($start), 2); - if (!preg_match('/\d{4}\-\d{2}\-\d{2}/', $date)) return false; - $time = $this->_autocompleteTime($time); + if (!preg_match('/\d{4}\-\d{2}\-\d{2}/', $date)) { + return false; + } + $time = $this->autocompleteTime($time); return strtotime($date.' '.$time); - - // duration } else { + // duration // start list($startdate, $starttime) = explode(' ', trim($start), 2); - $startdate = $this->_autocompleteDate($startdate); - if (!$startdate) return false; - $starttime = $this->_autocompleteTime($starttime); + $startdate = $this->autocompleteDate($startdate); + if (!$startdate) { + return false; + } + $starttime = $this->autocompleteTime($starttime); // end list($enddate, $endtime) = explode(' ', trim($end), 2); - if (!trim($endtime)) { // only time given - $end_date = $this->_autocompleteDate($enddate, true); + if (!trim($endtime)) { + // only time given + $end_date = $this->autocompleteDate($enddate, true); if (!$end_date) { - $endtime = $this->_autocompleteTime($enddate, true); + $endtime = $this->autocompleteTime($enddate, true); $enddate = $startdate; - } else { // only date given + } else { + // only date given $enddate = $end_date; $endtime = '23:59:59'; } } else { - $enddate = $this->_autocompleteDate($enddate, true); - if (!$enddate) $enddate = $startdate; - $endtime = $this->_autocompleteTime($endtime, true); + $enddate = $this->autocompleteDate($enddate, true); + if (!$enddate) { + $enddate = $startdate; + } + $endtime = $this->autocompleteTime($endtime, true); } $start = strtotime($startdate.' '.$starttime); $end = strtotime($enddate.' '.$endtime); - if (!$start || !$end) return false; + if (!$start || !$end) { + return false; + } return array('start' => $start, 'end' => $end); } } - function _autocompleteDate($date, $end=false) { + private function autocompleteDate($date, $end=false) + { if (!preg_match('/^\d{4}\-\d{2}\-\d{2}$/', $date)) { - if (preg_match('/^\d{4}\-\d{2}$/', $date)) + if (preg_match('/^\d{4}\-\d{2}$/', $date)) { // we don't know which month return ($end) ? $date.'-28' : $date.'-01'; - elseif (preg_match('/^\d{4}$/', $date)) + } elseif (preg_match('/^\d{4}$/', $date)) { return ($end) ? $date.'-12-31' : $date.'-01-01'; - else return false; + } else { + return false; + } } else { return $date; } } - function _autocompleteTime($time, $end=false) { + private function autocompleteTime($time, $end=false) + { if (!preg_match('/^\d{2}:\d{2}:\d{2}$/', $time)) { - if (preg_match('/^\d{2}:\d{2}$/', $time)) + if (preg_match('/^\d{2}:\d{2}$/', $time)) { return ($end) ? $time.':59' : $time.':00'; - elseif (preg_match('/^\d{2}$/', $time)) + } elseif (preg_match('/^\d{2}$/', $time)) { return ($end) ? $time.':59:59': $time.':00:00'; - else return ($end) ? '23:59:59' : '00:00:00'; + } else { + return ($end) ? '23:59:59' : '00:00:00'; + } } else { return $time; }