3 /* Copyright 2004-2009 $ThePhpWikiProgrammingTeam
4 * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * EDIT Toolbar Initialization.
25 * The default/themes/toolbar.js is from Mediawiki, this PHP is written from scratch.
28 * - save-preview and formatting buttons from mediawiki
29 * - Search&Replace from walterzorn.de
30 * - pageinsert popup by Reini Urban (TODO: should be a pulldown, use acdropdown)
35 function EditToolbar() {
38 $this->tokens = array();
40 //FIXME: enable Undo button for all other buttons also, not only the search/replace button
41 if (JS_SEARCHREPLACE) {
42 $this->tokens['JS_SEARCHREPLACE'] = 1;
43 $undo_btn = $WikiTheme->getImageURL("ed_undo.png");
44 $undo_d_btn = $WikiTheme->getImageURL("ed_undo_d.png");
45 // JS_SEARCHREPLACE from walterzorn.de
47 uri_undo_btn = '".$undo_btn."'
48 msg_undo_alt = '"._("Undo")."'
49 uri_undo_d_btn = '".$undo_d_btn."'
50 msg_undo_d_alt = '"._("Undo disabled")."'
51 msg_do_undo = '"._("Operation undone")."'
52 msg_replfound = '"._("Substring \"\\1\" found \\2 times. Replace with \"\\3\"?")."'
53 msg_replnot = '"._("String \"%s\" not found.")."'
54 msg_repl_title = '"._("Search & Replace")."'
55 msg_repl_search = '"._("Search for")."'
56 msg_repl_replace_with = '"._("Replace with")."'
57 msg_repl_ok = '"._("OK")."'
58 msg_repl_close = '"._("Close")."'
60 if (empty($WikiTheme->_headers_printed)) {
61 $WikiTheme->addMoreHeaders($js);
62 $WikiTheme->addMoreAttr('body', "SearchReplace"," onload='define_f()'");
63 } else { // from an actionpage: WikiBlog, AddComment, WikiForum
67 $WikiTheme->addMoreAttr('body', "editfocus", "document.getElementById('edit-content]').editarea.focus()");
70 if (ENABLE_EDIT_TOOLBAR) {
71 $js = JavaScript('',array('src' => $WikiTheme->_findData("toolbar.js")));
72 if (empty($WikiTheme->_headers_printed))
73 $WikiTheme->addMoreHeaders($js);
74 else { // from an actionpage: WikiBlog, AddComment, WikiForum
76 printXML(JavaScript('define_f()'));
80 require_once("lib/WikiPluginCached.php");
81 $cache = WikiPluginCached::newCache();
82 $dbi = $GLOBALS['request']->getDbh();
83 // regenerate if number of pages changes (categories, pages, templates)
84 $key = $dbi->numPages();
85 $key .= '+categories+plugin' . (isBrowserSafari() ? '+safari' : '');
86 if (TOOLBAR_PAGELINK_PULLDOWN) {
89 if (TOOLBAR_TEMPLATE_PULLDOWN) {
90 $key .= "+templates_" . $dbi->getTimestamp();
92 $id = $cache->generateId($key);
93 $content = $cache->get($id, 'toolbarcache');
95 if (!empty($content)) {
96 $this->tokens['EDIT_TOOLBAR'] =& $content;
98 $content = $this->_generate();
99 // regenerate buttons every 3600 seconds
100 $cache->save($id, $content, '+3600', 'toolbarcache');
101 $this->tokens['EDIT_TOOLBAR'] =& $content;
105 function getTokens () {
106 return $this->tokens;
109 function _generate () {
110 global $WikiTheme, $request;
112 $toolbar = "document.writeln(\"<div class=\\\"edit-toolbar\\\" id=\\\"toolbar\\\">\");\n";
114 if (ENABLE_EDIT_TOOLBAR) {
115 $username = $request->_user->UserName();
116 if (defined('GFORGE') and GFORGE) {
117 $username = '[['.$username.']]';
118 } else if (DISABLE_MARKUP_WIKIWORD or (!isWikiWord($username))) {
119 $username = '['.$username.']';
121 $signature = " ––".$username." ".CTime();
124 "image"=>"ed_format_bold.png",
127 "sample"=>_("Bold text"),
128 "title"=>_("Bold text [alt-b]")),
129 array("image"=>"ed_format_italic.png",
132 "sample"=>_("Italic text"),
133 "title"=>_("Italic text [alt-i]")),
134 array("image"=>"ed_format_strike.png",
137 "sample"=>_("Strike-through text"),
138 "title"=>_("Strike")),
139 array("image"=>"ed_format_color.png",
140 "open"=>"%color=green% ",
142 "sample"=>_("Color text"),
143 "title"=>_("Color")),
144 array("image"=>"ed_pagelink.png",
147 "sample"=>_("PageName|optional label"),
148 "title"=>_("Link to page")),
149 array("image"=>"ed_link.png",
152 "sample"=>_("http://www.example.com|optional label"),
153 "title"=>_("External link (remember http:// prefix)")),
154 array("image"=>"ed_headline.png",
157 "sample"=>_("Headline text"),
158 "title"=>_("Level 1 headline")),
159 array("image"=>"ed_nowiki.png",
160 "open"=>"\\<verbatim\\>\\n",
161 "close"=>"\\n\\</verbatim\\>",
162 "sample"=>_("Insert non-formatted text here"),
163 "title"=>_("Ignore wiki formatting")),
164 array("image"=>"ed_sig.png",
165 "open" => $signature,
168 "title"=>_("Your signature")),
169 array("image"=>"ed_hr.png",
170 "open"=>"\\n----\\n",
173 "title"=>_("Horizontal line")),
174 array("image"=>"ed_table.png",
175 "open"=>"\\n{| class=\"bordered\"\\n|+ This is the table caption\\n|= This is the table summary\\n|-\\n! Header A !! Header B !! Header C\\n|-\\n| Cell A1 || Cell B1 || Cell C1\\n|-\\n| Cell A2 || Cell B2 || Cell C2\\n|-\\n| Cell A3 || Cell B3 || Cell C3\\n|}\\n",
178 "title"=>_("Sample table")),
179 array("image"=>"ed_enumlist.png",
180 "open"=>"\\n# Item 1\\n# Item 2\\n# Item 3\\n",
183 "title"=>_("Enumeration")),
184 array("image"=>"ed_list.png",
185 "open"=>"\\n* Item 1\\n* Item 2\\n* Item 3\\n",
189 array("image"=>"ed_toc.png",
190 "open"=>"<<CreateToc with_toclink||=1>>\\n",
193 "title"=>_("Table of Contents")),
194 array("image"=>"ed_redirect.png",
195 "open"=>"<<RedirectTo page=\"",
197 "sample"=>_("Page Name"),
198 "title"=>_("Redirect")),
199 array("image"=>"ed_templateplugin.png",
202 "sample"=>_("Template Name"),
203 "title"=>_("Template"))
205 $btn = new SubmitImageButton(_("Save"), "edit[save]", 'toolbar',
206 $WikiTheme->getImageURL("ed_save.png"));
207 $btn->addTooltip(_("Save"));
208 $btn->setAccesskey("s");
209 $toolbar .= ('document.writeln("'.addslashes($btn->asXml()).'");'."\n");
210 // preview not supported yet on Wikiblog
211 if (empty($WikiTheme->_headers_printed)) {
212 $btn = new SubmitImageButton(_("Preview"), "edit[preview]", 'toolbar',
213 $WikiTheme->getImageURL("ed_preview.png"));
214 $btn->addTooltip(_("Preview"));
215 $btn->setAccesskey("p");
216 $toolbar .= ('document.writeln("'.addslashes($btn->asXml()).'");'."\n");
219 foreach ($toolarray as $tool) {
221 $image = $WikiTheme->getImageURL($tool["image"]);
222 $open = $tool["open"];
223 $close = $tool["close"];
224 $sample = addslashes( $tool["sample"] );
225 // Note that we use the title both for the ALT tag and the TITLE tag of the image.
226 // Older browsers show a "speedtip" type message only for ALT.
227 // Ideally these should be different, realistically they
228 // probably don't need to be.
229 $tool = $WikiTheme->fixAccesskey($tool);
230 $title = addslashes( $tool["title"] );
231 $toolbar .= ("addTagButton('$image','$title','$open','$close','$sample');\n");
233 /* Fails with Chrome */
234 if (!isBrowserSafari()) {
235 $toolbar .= ("addInfobox('"
236 . addslashes( _("Click a button to get an example text") )
241 if (JS_SEARCHREPLACE) {
242 $undo_d_btn = $WikiTheme->getImageURL("ed_undo_d.png");
243 //$redo_btn = $WikiTheme->getImageURL("ed_redo.png");
244 $sr_btn = $WikiTheme->getImageURL("ed_replace.png");
245 //TODO: generalize the UNDO button and fix it for Search & Replace
246 $sr_html = HTML(HTML::img
247 (array('class'=>"toolbar",
250 'title'=>_("Undo Search & Replace"),
251 'alt' =>_("Undo Search & Replace"),
252 //'disabled'=>"disabled", //non-XHTML conform
253 //'onfocus' =>"if(this.blur && undo_buffer_index==0) this.blur()",
254 'onclick' =>"do_undo()")),
256 (array('class'=>"toolbar",
258 'alt' =>_("Search & Replace"),
259 'title'=>_("Search & Replace"),
260 'onclick'=>"replace()")));
265 //TODO: Delegate this to run-time with showing an hidden input at the right, and do
266 // a seperate moacdropdown and xmlrpc:titleSearch.
268 // Button to generate categories, display in extra window as popup and insert
269 $sr_html = HTML($sr_html, $this->categoriesPulldown());
270 // Button to generate plugins, display in extra window as popup and insert
271 $sr_html = HTML($sr_html, $this->pluginPulldown());
273 // Button to generate pagenames, display in extra window as popup and insert
274 if (TOOLBAR_PAGELINK_PULLDOWN)
275 $sr_html = HTML($sr_html, $this->pagesPulldown(TOOLBAR_PAGELINK_PULLDOWN));
276 // Button to insert from an template, display pagename in extra window as popup and insert
277 if (TOOLBAR_TEMPLATE_PULLDOWN)
278 $sr_html = HTML($sr_html, $this->templatePulldown(TOOLBAR_TEMPLATE_PULLDOWN));
280 // Button to add images, display in extra window as popup and insert
281 if (TOOLBAR_IMAGE_PULLDOWN)
282 $sr_html = HTML($sr_html, $this->imagePulldown(TOOLBAR_IMAGE_PULLDOWN));
284 // don't use document.write for replace, otherwise self.opener is not defined.
285 $toolbar_end = "document.writeln(\"</div>\");";
287 return HTML(Javascript($toolbar),
288 "\n", $sr_html, "\n",
289 Javascript($toolbar_end));
291 return HTML(Javascript($toolbar . $toolbar_end));
295 function categoriesPulldown() {
298 require_once('lib/TextSearchQuery.php');
299 $dbi =& $GLOBALS['request']->_dbi;
300 // KEYWORDS formerly known as $KeywordLinkRegexp
301 $pages = $dbi->titleSearch(new TextSearchQuery(KEYWORDS, true));
303 $categories = array();
304 while ($p = $pages->next()) {
305 $page = $p->getName();
306 if (defined('GFORGE') and GFORGE) {
307 $categories[] = "['$page', '%5B%5B".$page."%5D%5D']";
308 } else if (DISABLE_MARKUP_WIKIWORD or (!isWikiWord($page))) {
309 $categories[] = "['$page', '%5B".$page."%5D']";
311 $categories[] = "['$page', '$page']";
314 if (!$categories) return '';
315 // Ensure this to be inserted at the very end. Hence we added the id to the function.
316 $more_buttons = HTML::img(array('class'=> "toolbar",
317 'id' => 'tb-categories',
318 'src' => $WikiTheme->getImageURL("ed_category.png"),
319 'title'=>_("AddCategory"),
320 'alt'=>"AddCategory", // to detect this at js
321 'onclick'=>"showPulldown('".
322 _("Insert Categories (double-click)")
323 ."',[".join(",",$categories)."],'"
325 ._("Close")."','tb-categories')"));
326 return HTML("\n", $more_buttons);
331 // result is cached. Esp. the args are expensive
332 function pluginPulldown() {
335 $plugin_dir = 'lib/plugin';
336 if (defined('PHPWIKI_DIR'))
337 $plugin_dir = PHPWIKI_DIR . "/$plugin_dir";
338 $pd = new fileSet($plugin_dir, '*.php');
339 $plugins = $pd->getFiles();
342 if (!empty($plugins)) {
344 require_once("lib/WikiPlugin.php");
345 $w = new WikiPluginLoader;
346 foreach ($plugins as $plugin) {
347 $pluginName = str_replace(".php", "", $plugin);
348 $p = $w->getPlugin($pluginName, false); // second arg?
349 // trap php files which aren't WikiPlugin~s
350 if (strtolower(substr(get_parent_class($p), 0, 10)) == 'wikiplugin') {
352 $desc = $p->getArgumentsDescription();
353 $src = array("\n",'"',"'",'|','[',']','\\');
354 $replace = array('%0A','%22','%27','%7C','%5B','%5D','%5C');
355 $desc = str_replace("<br />",' ',$desc->asXML());
357 $plugin_args = ' '.str_replace($src, $replace, $desc);
358 $toinsert = "%0A<<".$pluginName.$plugin_args.">>"; // args?
359 $plugin_js .= ",['$pluginName','$toinsert']";
362 $plugin_js = substr($plugin_js, 1);
363 $more_buttons = HTML::img(array('class'=>"toolbar",
364 'id' => 'tb-plugins',
365 'src' => $WikiTheme->getImageURL("ed_plugins.png"),
366 'title'=>_("AddPlugin"),
367 'alt'=>_("AddPlugin"),
368 'onclick'=>"showPulldown('".
369 _("Insert Plugin (double-click)")
370 ."',[".$plugin_js."],'"
372 ._("Close")."','tb-plugins')"));
373 return HTML("\n", $more_buttons);
378 // result is cached. Esp. the args are expensive
379 function pagesPulldown($query, $case_exact=false, $regex='auto') {
380 require_once('lib/TextSearchQuery.php');
381 $dbi =& $GLOBALS['request']->_dbi;
382 $page_iter = $dbi->titleSearch(new TextSearchQuery($query, $case_exact, $regex));
383 if ($page_iter->count()) {
386 while ($p = $page_iter->next()) {
387 $page = $p->getName();
388 if (DISABLE_MARKUP_WIKIWORD or (!isWikiWord($page)))
389 $pages[] = "['$page', '%5B".$page."%5D']";
391 $pages[] = "['$page', '$page']";
393 return HTML("\n", HTML::img(array('class'=>"toolbar",
395 'src' => $WikiTheme->getImageURL("ed_pages.png"),
396 'title'=>_("AddPageLink"),
397 'alt'=>_("AddPageLink"),
398 'onclick'=>"showPulldown('".
399 _("Insert PageLink (double-click)")
400 ."',[".join(",",$pages)."],'"
402 ._("Close")."','tb-pages')")));
407 // result is cached. Esp. the args are expensive
408 function imagePulldown($query, $case_exact=false, $regex='auto') {
411 $image_dir = getUploadFilePath();
412 $pd = new fileSet($image_dir, '*');
413 $images = $pd->getFiles();
416 if (!empty($images)) {
418 foreach ($images as $image) {
419 // Select only files ending in ".png", ".gif", ".jpg", ".jpeg"
420 if (is_image($image)) {
421 $image_js .= ",['$image','{{".$image."}}']";
424 $image_js = substr($image_js, 1);
425 $more_buttons = HTML::img(array('class'=>"toolbar",
427 'src' => $WikiTheme->getImageURL("ed_image.png"),
428 'title'=>_("AddImage"),
429 'alt'=>_("AddImage"),
430 'onclick'=>"showPulldown('".
431 _("Insert Image (double-click)")
432 ."',[".$image_js."],'"
434 ._("Close")."','tb-images')"));
435 return HTML("\n", $more_buttons);
440 // result is cached. Esp. the args are expensive
442 function templatePulldown($query, $case_exact=false, $regex='auto') {
444 require_once('lib/TextSearchQuery.php');
445 $dbi =& $request->_dbi;
446 $page_iter = $dbi->titleSearch(new TextSearchQuery($query, $case_exact, $regex));
448 if ($page_iter->count()) {
451 while ($p = $page_iter->next()) {
452 $rev = $p->getCurrentRevision();
453 $toinsert = str_replace(array("\n",'"'), array('_nl','_quot'), $rev->_get_content());
454 //$toinsert = str_replace("\n",'\n',addslashes($rev->_get_content()));
455 $pages_js .= ",['".$p->getName()."','_nl$toinsert']";
457 $pages_js = substr($pages_js, 1);
458 if (!empty($pages_js))
459 return HTML("\n", HTML::img
460 (array('class'=>"toolbar",
461 'id' => 'tb-templates',
462 'src' => $WikiTheme->getImageURL("ed_template.png"),
463 'title'=>_("AddTemplate"),
464 'alt'=>_("AddTemplate"),
465 'onclick'=>"showPulldown('".
466 _("Insert Template (double-click)")
467 ."',[".$pages_js."],'"
469 ._("Close")."','tb-templates')")));
480 // c-hanging-comment-ender-p: nil
481 // indent-tabs-mode: nil