tiki-import_structuredtext.php 4.07 KB
Newer Older
mose's avatar
mose committed
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
changi67's avatar
changi67 committed
6
// 
mose's avatar
mose committed
7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
changi67's avatar
changi67 committed
9
// $Id$
mose's avatar
mose committed
10 11

require_once ('tiki-setup.php');
12 13
$access->check_feature('feature_wiki');
$access->check_permission('tiki_p_admin');
mose's avatar
mose committed
14

15 16 17 18
/**
 * @param $dump
 * @return array
 */
changi67's avatar
changi67 committed
19 20
function parse_st($dump)
{
mose's avatar
mose committed
21 22
	$bodysep  = '>>>>>>>>>>>>>>>>>>>>>>>>';
	$titlesep = '>>>>>>>>>>--------------';
changi67's avatar
changi67 committed
23
	$pages = preg_split("/$bodysep/", $dump);
mose's avatar
mose committed
24 25 26
	$res = array();
	array_shift($pages);
	foreach ($pages as $p) {
changi67's avatar
changi67 committed
27 28 29
		$ret['pagename'] = trim(substr($p, 0, strpos($p, ">")));
		$ret['pagename'] = str_replace(' ', '', ucwords(str_replace('_', ' ', $ret['pagename'])));
		$ret['body'] = substr($p, strpos($p, $titlesep)+strlen($titlesep));
mose's avatar
mose committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
		$res[] = $ret;
	}
	return $res;
}

$smarty->assign('result', 'n');

if (isset($_REQUEST["import"])) {
	check_ticket('import-st');

	$path = 'dump/'.$tikidomain.'/'.$_REQUEST["path"];

	if (is_file($path)) {
		$fp = fopen($path, "r");
		$full = fread($fp, filesize($path));
		//$full = fread($fp, 16000);
changi67's avatar
changi67 committed
46
		fclose($fp);
mose's avatar
mose committed
47 48 49 50 51

		$parts = parse_st($full);

		foreach ($parts as $part) {

52
			$part["body"] = preg_replace("/\[([^\]]*)\]/e", "str_replace(' ','',ucwords('(($1))'))", $part["body"]);
mose's avatar
mose committed
53 54
			$part["body"] = preg_replace("/(\(\([^\)]*\)\))/e", "str_replace(' ','',ucwords('$1'))", $part["body"]);
			
55
			$part["body"] = preg_replace("/( |\n|^)(http:\/\/[^ ]+)( |\n)/", "$1[$2]$3", $part["body"]);
mose's avatar
mose committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
			
			// "A link to Google":http://google.com
			$part["body"] = preg_replace("~\"([^\"]*)\":(((ht|f)tps?://|mailto:)[^\s]*)~", "[$2|$1]", $part["body"]);

			// internal labelled links
			$part["body"] = preg_replace("~\"([^\"]+)\":([^\s]+)~", "(($2|$1))", $part["body"]);
			
			// html links
			$part["body"] = preg_replace("~<a href=\"([^\"]*)\">([^<]*)</a>~", "[$1|$2]", $part["body"]);

			// remove <br>
			$part["body"] = preg_replace("/<br(\s*\/)?>(\r?\n)?/", "\n", $part["body"]);

		 // manage lists
changi67's avatar
changi67 committed
70 71
			$part["body"] = preg_replace("/\n \*/", "\n**", $part["body"]);
			$part["body"] = preg_replace("/\n  \*/", "\n***", $part["body"]);
mose's avatar
mose committed
72 73 74 75 76 77 78 79 80 81 82 83
		 
			// change <b>..</b>
			$part["body"] = preg_replace("/ _([^_]*)_ /", " ===$1=== ", $part["body"]);
			$part["body"] = preg_replace("/\*\*([^\*\n]+)\*\*/", "__$1__", $part["body"]);
			$part["body"] = preg_replace("~<b>([^<]*)</b>~", "__$1__", $part["body"]);
			$part["body"] = preg_replace("/\*([^\*\n]+)\*/", "''$1''", $part["body"]);
			$part["body"] = preg_replace("~<i>([^<]*)</i>~", "''$1''", $part["body"]);
			
			// change <hr>
			$part["body"] = preg_replace("/<hr(\s*\/)?>(\r?\n)?/", "---\n", $part["body"]);

     // manage formatting
changi67's avatar
changi67 committed
84 85 86
			$part["body"] = preg_replace("/^(\n*)([^\n]+)(\n\n) /", "!$2$3", $part["body"]);
			$part["body"] = preg_replace("/(\n\n)([^\n]{1,200})(\n\n) /", "$1!!$2$3", $part["body"]);
			$part["body"] = preg_replace("/\n +/", "\n", $part["body"]);
mose's avatar
mose committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

			$pagename = urldecode($part["pagename"]);

			$msg = '';

			if (isset($_REQUEST["remo"]) and $_REQUEST["remo"] == 'y') {
				$tikilib->remove_all_versions($pagename, '');
			}

			if ($tikilib->page_exists($pagename)) {
				if (isset($_REQUEST["crunch"]) and $_REQUEST["crunch"] == 'y') {
					$msg = '<b>' . tra('overwriting old page'). '</b>';
					$tikilib->update_page($pagename, $part["body"], tra('updated from structured text import'), 'System', '0.0.0.0', '');
				} else {
					$msg = '<b>' . tra('page not added (Exists)'). '</b>';
				}
			} else {
				$msg = tra('page created');
				$tikilib->create_page($pagename, 0, $part["body"], time(), tra('created from structured text import'), 'System', 0, '');
			}

			$aux["page"] = $pagename;
changi67's avatar
changi67 committed
109
			$aux["ex"] = substr($part['body'], 0, 42);
mose's avatar
mose committed
110 111 112 113 114 115 116 117 118 119
			$aux["msg"] = $msg;
			$lines[] = $aux;
		}
	}

	$smarty->assign('lines', $lines);
	$smarty->assign('result', 'y');
}
ask_ticket('import-st');

120 121 122
// disallow robots to index page:
$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');

mose's avatar
mose committed
123 124
$smarty->assign('mid', 'tiki-import_structuredtext.tpl');
$smarty->display("tiki.tpl");