xref: /plugin/bookmark2wiki/action.php (revision 9a41d28d7d222930eab5283e2c62f303b5ad376f)
1*9a41d28dSedwardcodelab<?php
2*9a41d28dSedwardcodelab/**
3*9a41d28dSedwardcodelab * DokuWiki Plugin bookmark2wiki (Action Component)
4*9a41d28dSedwardcodelab * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
5*9a41d28dSedwardcodelab * @author  dodotori <dodotori@localhost>
6*9a41d28dSedwardcodelab * forked from post2wiki.php by riny [at] bk [dot] ru
7*9a41d28dSedwardcodelab * To bookmark webpage using bookmarklet
8*9a41d28dSedwardcodelab * The app will add the url, title and hightlighed text you want to the end of the content of the targeted namespace. It does not directly read/white the dokuwiki page folder.
9*9a41d28dSedwardcodelab * Version 1.0
10*9a41d28dSedwardcodelab * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11*9a41d28dSedwardcodelab * @author     dodotori https://github.com/edwardcodelab
12*9a41d28dSedwardcodelab         **/
13*9a41d28dSedwardcodelab
14*9a41d28dSedwardcodelab	// TYPICAL USAGE :
15*9a41d28dSedwardcodelab        //                 Create bookmarklet as shown in the bookmarklet part below:
16*9a41d28dSedwardcodelab        //                 Change the window.open statement to reflect the location of the bookmark2wiki.php script.
17*9a41d28dSedwardcodelab        //                 Drag bookmarklet to your toolbar.
18*9a41d28dSedwardcodelab        //  BOOKMARKLET :
19*9a41d28dSedwardcodelab	//  javascript:Q=document.selection?document.selection.createRange().text:document.getSelection(); void(window.open('https://myserver/doku.php?do-bookmark2wiki&te='+escape(Q)+'&ur='+ escape(location.href)+'&ti='+escape(document.title),'dokuwikiadd','scrollbars=yes,resizable=yes,toolbars=yes,width=200,height=100,left=200,top=200,status=yes'));
20*9a41d28dSedwardcodelab
21*9a41d28dSedwardcodelab
22*9a41d28dSedwardcodelab
23*9a41d28dSedwardcodelabclass action_plugin_bookmark2wiki extends \dokuwiki\Extension\ActionPlugin
24*9a41d28dSedwardcodelab{
25*9a41d28dSedwardcodelab
26*9a41d28dSedwardcodelab    /** @inheritDoc */
27*9a41d28dSedwardcodelab    public function register(Doku_Event_Handler $controller)
28*9a41d28dSedwardcodelab    {
29*9a41d28dSedwardcodelab        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE',  $this, 'allowMyAction');
30*9a41d28dSedwardcodelab        $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE',  $this, 'performMyAction');
31*9a41d28dSedwardcodelab
32*9a41d28dSedwardcodelab    }
33*9a41d28dSedwardcodelab
34*9a41d28dSedwardcodelab    /**
35*9a41d28dSedwardcodelab     * FIXME Event handler for
36*9a41d28dSedwardcodelab     *
37*9a41d28dSedwardcodelab     * @param Doku_Event $event  event object by reference
38*9a41d28dSedwardcodelab     * @param mixed      $param  optional parameter passed when event was registered
39*9a41d28dSedwardcodelab     * @return void
40*9a41d28dSedwardcodelab     */
41*9a41d28dSedwardcodelab
42*9a41d28dSedwardcodelab
43*9a41d28dSedwardcodelab     public function allowMyAction(Doku_Event $event, $param) {
44*9a41d28dSedwardcodelab        if($event->data != 'bookmark2wiki') return;
45*9a41d28dSedwardcodelab        $event->preventDefault();
46*9a41d28dSedwardcodelab    }
47*9a41d28dSedwardcodelab
48*9a41d28dSedwardcodelab    public function performMyAction(Doku_Event $event, $param) {
49*9a41d28dSedwardcodelab       if($event->data != 'bookmark2wiki') return;
50*9a41d28dSedwardcodelab       $event->preventDefault();
51*9a41d28dSedwardcodelab
52*9a41d28dSedwardcodelab        echo'<button id="closedb" onclick ="window.close()">Back to Dokuwiki</button>';
53*9a41d28dSedwardcodelab      	// SETUP SECTION
54*9a41d28dSedwardcodelab	$namespace="new_bookmarks";  // default namespace for bookmark
55*9a41d28dSedwardcodelab	// POST TO WIKI
56*9a41d28dSedwardcodelab	$timestamp = date("Y:m:d:H:i:s"); //timestamp
57*9a41d28dSedwardcodelab	$wikitext=$_GET['te'];  // things to log : Selected text
58*9a41d28dSedwardcodelab	$url=$_GET['ur'];       // things to log : URL
59*9a41d28dSedwardcodelab    $title=$_GET['ti'];     // things to log : title
60*9a41d28dSedwardcodelab	$string = preg_replace('/%u([0-9A-F]+)/', '&#x$1;', $title); // convert the unicode
61*9a41d28dSedwardcodelab    $title = html_entity_decode($string, ENT_COMPAT, 'UTF-8');
62*9a41d28dSedwardcodelab    $string = preg_replace('/%u([0-9A-F]+)/', '&#x$1;', $wikitext);
63*9a41d28dSedwardcodelab    $wikitext = html_entity_decode($string, ENT_COMPAT, 'UTF-8');
64*9a41d28dSedwardcodelab	$bookmarktext="$url";
65*9a41d28dSedwardcodelab	$targeturl = DOKU_BASE."doku.php?id=".$namespace."&do=edit";
66*9a41d28dSedwardcodelab	      echo '<script>';
67*9a41d28dSedwardcodelab          echo 'function loadFunc(){';
68*9a41d28dSedwardcodelab          echo 'if(document.getElementById("top").contentWindow.document.getElementsByTagName("textarea")[0].innerHTML.length==0){document.getElementById("top").contentWindow.document.getElementsByTagName("textarea")[0].innerHTML = " ====== New Bookmarks ======"};';
69*9a41d28dSedwardcodelab          echo 'temp = document.getElementById("top").contentWindow.document.getElementsByTagName("textarea")[0].innerHTML.split(/\n/);';
70*9a41d28dSedwardcodelab          echo 'temp[1] = "    * [['.$url.'|'.$title.']] \\\\\\\\ '.$wikitext.' -- '.$timestamp.'\r\n" + temp[1];';
71*9a41d28dSedwardcodelab          echo 'document.getElementById("top").contentWindow.document.getElementsByTagName("textarea")[0].innerHTML = temp.join("\n");';
72*9a41d28dSedwardcodelab          echo '};';
73*9a41d28dSedwardcodelab          echo '</script>';
74*9a41d28dSedwardcodelab          echo '<iframe src="'.$targeturl.'" id="top" width="100%" height= "1000 px" onload="loadFunc()"></iframe>';
75*9a41d28dSedwardcodelab    }
76*9a41d28dSedwardcodelab
77*9a41d28dSedwardcodelab}