From 7c39480944ada69e60d2d787fd9fde03f0e39e69 Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Fri, 16 Dec 2011 12:53:13 +0100 Subject: [PATCH] Added support for reference building (experimental) --- serendipity_event_wikilinks/ChangeLog | 6 + .../UTF-8/lang_de.inc.php | 2 +- serendipity_event_wikilinks/lang_de.inc.php | 2 +- serendipity_event_wikilinks/lang_en.inc.php | 35 ++- .../serendipity_event_wikilinks.php | 248 +++++++++++++++++- 5 files changed, 286 insertions(+), 7 deletions(-) diff --git a/serendipity_event_wikilinks/ChangeLog b/serendipity_event_wikilinks/ChangeLog index 3153b973..d551c0f2 100644 --- a/serendipity_event_wikilinks/ChangeLog +++ b/serendipity_event_wikilinks/ChangeLog @@ -1,5 +1,11 @@ # $Id$ +0.20: +------------------------------------------------------------------------ + + * Added support for reference building via ...reference... + + 0.18: * Suppress html_entity_decode() warnings. diff --git a/serendipity_event_wikilinks/UTF-8/lang_de.inc.php b/serendipity_event_wikilinks/UTF-8/lang_de.inc.php index cfb6aa57..7cc8b4b9 100644 --- a/serendipity_event_wikilinks/UTF-8/lang_de.inc.php +++ b/serendipity_event_wikilinks/UTF-8/lang_de.inc.php @@ -1,7 +1,7 @@ yyy - where xxx is an optional name of a reference (see below) and yyy the actual text, where yyy can be any HTML or markup you like.'); +@define('PLUGIN_EVENT_WIKILINKS_REFDOC', 'Re-Using references

If you want to use references on multiple occasions, it\'s helpful to only specify them once and reuse them later. If you have this text for example:
+
+Serendipity<ref><a href="http://www.s9y.org">Serendipity Weblog</a> - also, Serendipity stands for several other interpretations like a movie, or a dancer in a movie, or a movie of a dancer in a movie. can be found in many places. +
+

+Since you surely will mention Serendipity a lot of times in your blog, you should make a referenced mention, which works by adding a name attribute to the <ref> tag, and make it look like this: +
+Serendipity<ref name="Serendipity"><a href="http://www.s9y.org">Serendipity Weblog</a> - also, Serendipity stands for several other interpretations like a movie, or a dancer in a movie, or a movie of a dancer in a movie. can be found in many places. +
+

+Now you only need to do this for the first occurence of your reference. Whenever you want to use the same reference in future blog entries you simply write this: +
+Serendipity<ref name="Serendipity"></ref> +
+

+This will take care of fetching the existing, named reference text from the database. Please note that you must use the <ref>...</ref> notation, <ref.../> is not supported due to the regular expression syntax not properly supporting this. +'); + +@define('PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET_NAME', 'Format for replaced reference output'); +@define('PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET_DESC', 'Here you can enter how the captured reference will be replaced, usually a number linking to the list of references. {count} and {text} are placeholders for the replaced reference number and its original text. {refname} corresponds to an optional name of the reference.'); + +@define('PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET2_NAME', 'Format for reference list output'); +@define('PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET2_DESC', 'Here you can enter how the captured reference will be displayed in the list. If set to "-", then no output will take place (useful if you want to style the output via smarty yourself!)'); + +@define('PLUGIN_EVENT_WIKILINKS_MAINT', 'Maintain reference index'); +@define('PLUGIN_EVENT_WIKILINKS_MAINT_DESC', 'Here you can edit stored references. Be sure to note that if you edit the original entry where the reference occured in, that the text of a reference there always takes precedence over anything you edit here. If you frequently re-edit old entries, you should better maintain the texts of references inside the entries, and not here.'); + +@define('PLUGIN_EVENT_WIKILINKS_DB_REFNAME', 'Reference name'); +@define('PLUGIN_EVENT_WIKILINKS_DB_REF', 'Reference content'); +@define('PLUGIN_EVENT_WIKILINKS_DB_ENTRYDID', 'Defined in:'); diff --git a/serendipity_event_wikilinks/serendipity_event_wikilinks.php b/serendipity_event_wikilinks/serendipity_event_wikilinks.php index feeb9dc0..1c5683c7 100644 --- a/serendipity_event_wikilinks/serendipity_event_wikilinks.php +++ b/serendipity_event_wikilinks/serendipity_event_wikilinks.php @@ -1,5 +1,8 @@ add('description', PLUGIN_EVENT_WIKILINKS_DESC); $propbag->add('stackable', false); $propbag->add('author', 'Garvin Hicking, Grischa Brockhaus'); - $propbag->add('version', '0.18'); + $propbag->add('version', '0.20'); $propbag->add('requirements', array( 'serendipity' => '1.0', 'smarty' => '2.6.7', @@ -36,7 +40,11 @@ class serendipity_event_wikilinks extends serendipity_event 'frontend_display' => true, 'backend_entry_toolbar_extended' => true, 'backend_entry_toolbar_body' => true, - 'external_plugin' => true + 'external_plugin' => true, + 'backend_publish' => true, + 'backend_save' => true, + 'backend_sidebar_entries_event_display_wikireferences' => true, + 'backend_sidebar_entries' => true, )); $this->markup_elements = array( @@ -65,6 +73,10 @@ class serendipity_event_wikilinks extends serendipity_event $conf_array[] = 'imgpath'; $conf_array[] = 'generate_draft_links'; $conf_array[] = 'generate_future_links'; + $conf_array[] = 'reference_match'; + $conf_array[] = 'reference_info'; + $conf_array[] = 'target_match'; + $conf_array[] = 'target_match2'; $propbag->add('configuration', $conf_array); } @@ -82,12 +94,14 @@ class serendipity_event_wikilinks extends serendipity_event $propbag->add('description', PLUGIN_EVENT_WIKILINKS_IMGPATH_DESC); $propbag->add('default', $serendipity['serendipityHTTPPath'] . 'plugins/serendipity_event_wikilinks/'); break; + case 'generate_draft_links': $propbag->add('name', PLUGIN_EVENT_WIKILINKS_SHOWDRAFTLINKS_NAME); $propbag->add('description', PLUGIN_EVENT_WIKILINKS_SHOWDRAFTLINKS_DESC); $propbag->add('type', 'boolean'); $propbag->add('default', 'true'); break; + case 'generate_future_links': $propbag->add('name', PLUGIN_EVENT_WIKILINKS_SHOWFUTURELINKS_NAME); $propbag->add('description', PLUGIN_EVENT_WIKILINKS_SHOWFUTURELINKS_DESK); @@ -95,9 +109,40 @@ class serendipity_event_wikilinks extends serendipity_event $propbag->add('default', 'true'); break; + case 'reference_match': + $propbag->add('name', PLUGIN_EVENT_WIKILINKS_REFMATCH_NAME); + $propbag->add('description', PLUGIN_EVENT_WIKILINKS_REFMATCH_DESC); + $propbag->add('type', 'string'); + $propbag->add('default', '.+)["\'])?>(?P.*)'); + break; + + case 'target_match': + $propbag->add('name', PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET_NAME); + $propbag->add('description', PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET_DESC); + $propbag->add('type', 'string'); + $propbag->add('default', '{count}'); + break; + + case 'target_match2': + $propbag->add('name', PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET2_NAME); + $propbag->add('description', PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET2_DESC); + $propbag->add('type', 'string'); + $propbag->add('default', '
  • {text}
  • '); + break; + + case 'reference_info': + $propbag->add('name', PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET2_NAME); + $propbag->add('description', PLUGIN_EVENT_WIKILINKS_REFMATCHTARGET2_DESC); + $propbag->add('type', 'content'); + $propbag->add('default', PLUGIN_EVENT_WIKILINKS_REFDOC); + break; + + case 'db_built': + return false; + default: $propbag->add('type', 'boolean'); - $propbag->add('name', constant($name)); + $propbag->add('name', @constant($name)); $propbag->add('description', sprintf(APPLY_MARKUP_TO, constant($name))); $propbag->add('default', 'true'); } @@ -111,6 +156,68 @@ class serendipity_event_wikilinks extends serendipity_event if (isset($hooks[$event])) { switch($event) { + + case 'backend_publish': + case 'backend_save': + // Purge, so that the data within the entry takes precedence over other changes + serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}wikireferences WHERE entryid = " . (int)$eventData['id']); + break; + + case 'backend_sidebar_entries': + $this->setupDB(); + echo ''; + break; + + + case 'backend_sidebar_entries_event_display_wikireferences': + $entries = serendipity_db_query("SELECT id, refname FROM {$serendipity['dbPrefix']}wikireferences ORDER BY refname ASC"); + + echo '

    ' . PLUGIN_EVENT_WIKILINKS_MAINT_DESC . '

    '; + + echo '
    '; + echo ''; + echo ''; + echo ''; + echo ''; + echo '

    '; + + if ($serendipity['POST']['wikireference'] > 0) { + + if ($serendipity['POST']['saveSubmit']) { + serendipity_db_update('wikireferences', array('id' => $serendipity['POST']['wikireference']), array('refname' => $serendipity['POST']['wikireference_refname'], 'ref' => $serendipity['POST']['wikireference_ref'])); + echo '
    ' . DONE .': '. sprintf(SETTINGS_SAVED_AT, serendipity_strftime('%H:%M:%S')) . '
    '; + } + + $ref = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}wikireferences WHERE id = " . (int)$serendipity['POST']['wikireference'], 'assoc', true); + $entry = serendipity_fetchEntry('id', $ref['entryid']); + + echo '
    '; + echo '
    '; + echo ''; + echo ''; + echo '
    '; + + echo '
    '; + echo '
    '; + echo ''; + echo '
    '; + + echo '
    '; + echo ''; + echo '' . $entry['title'] . ''; + echo '

    ' . EDIT_ENTRY . '

    '; + + echo '
    '; + } + echo '
    '; + + break; + case 'frontend_display': foreach ($this->markup_elements as $temp) { if (serendipity_db_bool($this->get_config($temp['name'], true)) && isset($eventData[$temp['element']]) && @@ -118,21 +225,36 @@ class serendipity_event_wikilinks extends serendipity_event !isset($serendipity['POST']['properties']['disable_markup_' . $this->instance])) { $element = $temp['element']; + $is_body = false; if ($element == 'body' || $element == 'extended') { $source =& $this->getFieldReference($element, $eventData); if ($source === '') { // Prevent bug from serendipity 0.9 $source =& $eventData[$element]; } + $is_body = true; } else { $source =& $eventData[$element]; } + + $this->references = $this->refcount = array(); + $this->ref_entry = $eventData['id']; + $source = preg_replace_callback( + '^' . $this->get_config('reference_match') . '^imsU', + array($this, '_reference'), + $source + ); $source = preg_replace_callback( "#(\[\[|\(\(|\{\{)(.+)(\]\]|\)\)|\}\})#isUm", array($this, '_wikify'), $source ); + + $source .= $this->reference_parse(); + if ($is_body) { + $eventData['properties']['references'] = $this->references; + } } } @@ -299,6 +421,124 @@ function use_link_(txt) { } } + function install() { + $this->setupDB(); + } + + function setupDB() { + global $serendipity; + + $built = $this->get_config('db_built', null); + if (empty($built)) { + serendipity_db_schema_import("CREATE TABLE {$serendipity['dbPrefix']}wikireferences ( + id {AUTOINCREMENT} {PRIMARY}, + entryid int(11) default '0', + refname text, + ref text)"); + serendipity_db_schema_import("CREATE INDEX wikiref_refname ON {$serendipity['dbPrefix']}wikireferences (refname(200));"); + serendipity_db_schema_import("CREATE INDEX wikiref_comb ON {$serendipity['dbPrefix']}wikireferences (entryid,refname(200));"); + serendipity_db_schema_import("CREATE INDEX wikiref_entry ON {$serendipity['dbPrefix']}wikireferences (entryid);"); + $this->set_config('db_built', 1); + } + } + + function _reference($buffer) { + global $serendipity; + static $count = 0; + + $count++; + + if (!empty($buffer['ref']) && !empty($buffer['refname']) && !empty($this->ref_entry)) { + // New refname, needs to be stored in the database IF NOT CURRENTLY EXISTING + $exists = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}wikireferences WHERE refname = '" . serendipity_db_escape_string($buffer['refname']) . "'", 'assoc', true); + + if ($exists['entryid'] == $this->ref_entry) { + #serendipity_db_update('wikireferences', array('entryid' => $this->ref_entry, 'refname' => $buffer['refname']), array('ref' => $buffer['ref'])); + } elseif (empty($exists['entryid'])) { + serendipity_db_insert('wikireferences', array('entryid' => $this->ref_entry, 'refname' => $buffer['refname'], 'ref' => $buffer['ref'])); + } + } + + if (empty($buffer['ref']) && !empty($buffer['refname'])) { + // We found a referenced pattern like , so let's fetch that from the database! + $exists = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}wikireferences WHERE refname = '" . serendipity_db_escape_string($buffer['refname']) . "'", 'assoc', true); + + $buffer['ref'] = $exists['ref']; + } + + if (empty($buffer['refname'])) { + $buffer['refname'] = $count; + } + + $refix = $count; + if (isset($this->references[$buffer['refname']])) { + if ($this->references[$buffer['refname']] == $buffer['ref']) { + $refix = $this->refcount[$buffer['refname']]; + } else { + $this->references[$buffer['refname'] . $count] = $buffer['ref']; + $this->refcount[$buffer['refname'] . $count] = $count; + } + } else { + $this->references[$buffer['refname']] = $buffer['ref']; + $this->refcount[$buffer['refname']] = $count; + } + + $result = $this->get_config('target_match'); + $result = str_replace( + array( + '{count}', + '{text}', + '{refname}' + ), + + array( + $refix, + htmlspecialchars($buffer['ref']), + htmlspecialchars($buffer['refname']), + ), + $result + ); + + return $result; + } + + function reference_parse() { + global $serendipity; + static $count = 0; + static $count2 = 0; + + $count++; + + $format = $this->get_config('target_match2'); + + if ($format == '-') return; + + $block = "\n\n" . '
      ' . "\n"; + + foreach($this->references AS $key => $buffer) { + $count2++; + $result = str_replace( + array( + '{count}', + '{text}', + '{refname}' + ), + + array( + $count2, + htmlspecialchars($buffer), + $key + ), + $format + ); + + $block .= $result . "\n"; + } + + $block .= '
    ' . "\n"; + + return $block; + } /** * Wikifies: @@ -309,7 +549,7 @@ function use_link_(txt) { global $serendipity; $debug = true; - $$admin_url = false; + $admin_url = false; $cidx = 2;