<commit_before><?php

//this script may only be included - so its better to die if called directly.
if (strpos($_SERVER['SCRIPT_NAME'],basename(__FILE__)) !== false) {
  header('location: index.php');
  exit;
}

class StructLib extends TikiLib {
	function StructLib($db) {
		# this is probably uneeded now
		if (!$db) {
			die ('Invalid db object passed to StructLib constructor');
		}

		$this->db = $db;
	}

	function s_export_structure($structure_id) {
		global $exportlib, $tikidomain;
		global $dbTiki;

		include_once ('lib/wiki/exportlib.php');
		include_once ('lib/tar.class.php');

		$page_info = $this->s_get_structure_info($structure_id);
		$page_name = $page_info['pageName'];
		$zipname   = $page_name . '.zip';
		$tar = new tar();
		$pages = $this->s_get_structure_pages($page_info['page_ref_id']);

		foreach ($pages as $page) {
			$data = $exportlib->export_wiki_page($page['pageName'], 0);
			$tar->addData($page['pageName'], $data, date('U'));
		}
		$dump = 'dump';
		if ($tikidomain) { $dump.= "/$tikidomain"; }
		$tar->toTar("$dump/$page_name.tar", FALSE);
		header ("location: $dump/$page_name.tar");
		return '';
	}

	function s_export_structure_tree($structure_id, $level = 0) {
		$structure_tree = $this->get_subtree($structure_id);

		$level = 0;
		$first = true;
		header('Content-type: text/plain; charset=utf8');
		foreach ( $structure_tree as $node ) {
			//This special case indicates head of structure
			if ($node['first'] and $node['last']) {
				print (tra('Use this tree to copy the structure').': ' . $node['pageName'] . "\n\n");
			}
			elseif ($node['first'] or !$node['last']) {
				if ($node['first'] and !$first) {
			        $level++;
				}
				$first = false;
				for ($i = 0; $i < $level; $i++) {
					print (' ');
				}
				print ($node['pageName']);
				if (!empty($node['page_alias'])) {
					print("->" . $node['page_alias']);
				}
				print("\n");
			}
			//node is a place holder for last in level
			else {
				$level--;
			}
		}
	}

	function s_remove_page($page_ref_id, $delete) {
		// Now recursively remove
		global $user, $tiki_p_remove;

		$query = 'select `page_ref_id`, ts.`page_id`, `pageName` ';
    $query .= 'from `tiki_structures` ts, `tiki_pages` tp ';
    $query .= 'where tp.`page_id`=ts.`page_id` and `parent_id`=?';
		$result = $this->query($query,array((int)$page_ref_id));

    //Iterate down through the child nodes
		while ($res = $result->fetchRow()) {
			$this->s_remove_page($res['page_ref_id'], $delete);
		}

    //Only delete a page if other structures arent referencing it
		if ($delete && $tiki_p_remove == 'y') {
      $page_info = $this->s_get_page_info($page_ref_id);
  		$query = 'select count(*) from `tiki_structures` where `page_id`=?';
	  	$count = $this->getOne($query, array((int)$page_info['page_id']));
	global $wikilib;<commit_msg>[FIX] structures: better chances that structure tree export respect utf8<commit_after><?php

//this script may only be included - so its better to die if called directly.
if (strpos($_SERVER['SCRIPT_NAME'],basename(__FILE__)) !== false) {
  header('location: index.php');
  exit;
}

class StructLib extends TikiLib {
	function StructLib($db) {
		# this is probably uneeded now
		if (!$db) {
			die ('Invalid db object passed to StructLib constructor');
		}

		$this->db = $db;
	}

	function s_export_structure($structure_id) {
		global $exportlib, $tikidomain;
		global $dbTiki;

		include_once ('lib/wiki/exportlib.php');
		include_once ('lib/tar.class.php');

		$page_info = $this->s_get_structure_info($structure_id);
		$page_name = $page_info['pageName'];
		$zipname   = $page_name . '.zip';
		$tar = new tar();
		$pages = $this->s_get_structure_pages($page_info['page_ref_id']);

		foreach ($pages as $page) {
			$data = $exportlib->export_wiki_page($page['pageName'], 0);
			$tar->addData($page['pageName'], $data, date('U'));
		}
		$dump = 'dump';
		if ($tikidomain) { $dump.= "/$tikidomain"; }
		$tar->toTar("$dump/$page_name.tar", FALSE);
		header ("location: $dump/$page_name.tar");
		return '';
	}

	function s_export_structure_tree($structure_id, $level = 0) {
		$structure_tree = $this->get_subtree($structure_id);

		$level = 0;
		$first = true;
		header('Content-type: text/html; charset=utf8');
		echo "<html><body><pre>\n";
		foreach ( $structure_tree as $node ) {
			//This special case indicates head of structure
			if ($node['first'] and $node['last']) {
				print (tra('Use this tree to copy the structure').': ' . $node['pageName'] . "\n\n");
			}
			elseif ($node['first'] or !$node['last']) {
				if ($node['first'] and !$first) {
			        $level++;
				}
				$first = false;
				for ($i = 0; $i < $level; $i++) {
					print (' ');
				}
				print ($node['pageName']);
				if (!empty($node['page_alias'])) {
					print("->" . $node['page_alias']);
				}
				print("\n");
			}
			//node is a place holder for last in level
			else {
				$level--;
			}
		}
		echo "\n</pre></body></html>";
	}

	function s_remove_page($page_ref_id, $delete) {
		// Now recursively remove
		global $user, $tiki_p_remove;

		$query = 'select `page_ref_id`, ts.`page_id`, `pageName` ';
    $query .= 'from `tiki_structures` ts, `tiki_pages` tp ';
    $query .= 'where tp.`page_id`=ts.`page_id` and `parent_id`=?';
		$result = $this->query($query,array((int)$page_ref_id));

    //Iterate down through the child nodes
		while ($res = $result->fetchRow()) {
			$this->s_remove_page($res['page_ref_id'], $delete);
		}

    //Only delete a page if other structures arent referencing it
		if ($delete && $tiki_p_remove == 'y') {
      $page_info = $this->s_get_page_info($page_ref_id);
  		$query = 'select count(*) from `tiki_structures` where `page_id`=?';
	  	$count = $this->getOne($query, array((int)$page_info['page_id']));
	global $wikilib;
{'fraction_non_alphanumeric': 0.19088861943530228, 'fraction_numerical': 0.0019054217910964837, 'mean_word_length': 3.848026868178002, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4200075', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2061, 'n_words': 470}