tiki-create_multilang_pages.php 5.61 KB
Newer Older
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
// 
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$
10 11

include_once('tiki-setup.php');
lphuberdeau's avatar
lphuberdeau committed
12 13 14
$tikilib = TikiLib::lib('tiki');
$wikilib = TikiLib::lib('wiki');
$semanticlib = TikiLib::lib('semantic');
15
$multilinguallib = TikiLib::lib('multilingual');
16

changi67's avatar
changi67 committed
17 18
$access->check_feature(array( 'feature_wiki', 'feature_multilingual' ));
$access->check_permission('tiki_p_edit');
19

20 21 22 23
compute_relevant_languages();
create_pages_if_necessary();
display();

changi67's avatar
changi67 committed
24 25
function create_pages_if_necessary()
{
26
	$smarty = TikiLib::lib('smarty');
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	$template_name = null;
	if (isset($_REQUEST['template_name'])) {
		$template_name = $_REQUEST['template_name'];	
	}
	$pages_to_create = get_pages_to_create();
	list($inexistant_page, $existing_pages)
	   = check_for_existence_of_pages($pages_to_create);
	$smarty->assign('pages_created', $inexistant_page);
	$smarty->assign('pages_not_created', $existing_pages);
	if (count($inexistant_page) > 0 || count($existing_pages) > 0) {
		$smarty->assign('display_creation_result', 'y');
	} else {
		$smarty->assign('display_creation_result', 'n');
	}
	foreach ($inexistant_page as $lang => $page_name) {
		create_page($page_name, $lang, $template_name);
	}
	make_pages_translations_of_each_other($inexistant_page);
}

47 48 49 50 51
/**
 * @param $page_name
 * @param $lang
 * @param null $template_name
 */
changi67's avatar
changi67 committed
52 53
function create_page($page_name, $lang, $template_name=null)
{
54 55 56
	global $user;
	$multilinguallib = TikiLib::lib('multilingual');
	$tikilib = TikiLib::lib('tiki');
57 58 59 60 61 62 63 64 65
	$content = '';
	if ($template_name != null) {
		$template_id = $multilinguallib->getTemplateIDInLanguage('wiki', $template_name, $lang);
		$template_data = $templateslib->get_template($template_id, $lang);
		$content = $template_data['content'];
	}
	$tikilib->create_page($page_name, 0, $content, null, '', null, $user, '', $lang);
}

66 67 68
/**
 * @param $pages
 */
changi67's avatar
changi67 committed
69 70
function make_pages_translations_of_each_other($pages)
{
71 72
	$multilinguallib = TikiLib::lib('multilingual');
	$tikilib = TikiLib::lib('tiki');
73 74 75 76 77 78 79
	if (count($pages) == 0) return;
	$first_page_id = null;
	foreach ($pages as $this_page_lang => $this_page_name) {
		if ($first_page_id == null) {
			$first_page_id = $tikilib->get_page_id_from_name($this_page_name);
			$first_page_lang = $this_page_lang;
		} else {
changi67's avatar
changi67 committed
80
			$this_page_info = $tikilib->get_page_info($this_page_name);
81
			$this_page_id = $this_page_info['page_id'];
changi67's avatar
changi67 committed
82
			$multilinguallib->insertTranslation('wiki page', $first_page_id, $first_page_lang, $this_page_id, $this_page_lang);
83 84 85 86
		}
	}
}

87 88 89
/**
 * @return array
 */
changi67's avatar
changi67 committed
90 91
function compute_relevant_languages()
{
92 93 94
	global $prefs;
	$multilinguallib = TikiLib::lib('multilingual');
	$smarty = TikiLib::lib('smarty');
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	
	$all_languages_with_country_codes = $prefs['available_languages'];
	$all_languages = strip_country_code_from_lang_ids($all_languages_with_country_codes);	
	$user_languages_with_country_codes = $multilinguallib->preferredLangs();
	$user_languages = strip_country_code_from_lang_ids($user_languages_with_country_codes);
	
	$other_languages = array();
	foreach ($all_languages as $index => $lang) {
		if (!in_array($lang, $user_languages)) {
			$other_languages[] = $lang;
		}
	}
	$smarty->assign('user_languages', $user_languages);
	$smarty->assign('other_languages', $other_languages);
	
	$result = array($user_languages, $other_languages, $all_languages);
	return $result;
}

114 115 116
/**
 * @return array
 */
changi67's avatar
changi67 committed
117 118
function get_pages_to_create()
{
119 120 121 122 123 124 125 126 127 128 129 130 131
	$pages_to_create = array();
	foreach ($_REQUEST as $arg_name => $arg_val) {
		if (preg_match('/page_name_([\s\S]*)/', $arg_name, $matches)) {
			if ($arg_val != '') {
				$lang = $matches[1];
				$pages_to_create[$lang] = $arg_val;
			}			
		}
	}
	set_smarty_page_links($pages_to_create);
	return $pages_to_create;
}

132 133 134 135
/**
 * @param $pages_to_create
 * @return array
 */
changi67's avatar
changi67 committed
136 137
function check_for_existence_of_pages($pages_to_create)
{
138 139
	$tikilib = TikiLib::lib('tiki');
	$semantic = TikiLib::lib('semantic');
140 141 142 143 144 145 146
	$non_existant_pages = array();
	$existing_pages = array();
	
	foreach ($pages_to_create as $lang => $page_name) {		
		$exists = false;
		if ($tikilib->page_exists($page_name)) {
			$exists = true;
changi67's avatar
changi67 committed
147
		} else {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
			$aliases = $semanticlib->getAliasContaining($page_name, true);
			if (count($aliases) > 0) {
				$exists = true;
			}
		}
		
		if ($exists) {
			$existing_pages[$lang] = $page_name;
		} else {
			$non_existant_pages[$lang] = $page_name;
		}		
	}
	
	return array($non_existant_pages, $existing_pages);
}

164 165 166
/**
 * @param $page_names
 */
changi67's avatar
changi67 committed
167 168
function set_smarty_page_links($page_names)
{
169 170
	$smarty = TikiLib::lib('smarty');
	$wikilib = TikiLib::lib('wiki');
171 172 173 174 175 176 177 178 179 180 181
	
	$page_links = array();
	foreach ($page_names as $a_page_name) {
		$page_links[$a_page_name] =
			"<a href=\"".
			$wikilib->url_for_operation_on_a_page('tiki-index.php', $a_page_name, false).
			"\">$a_page_name</a>";
	}
	$smarty->assign('page_links', $page_links);
}	

changi67's avatar
changi67 committed
182 183
function display()
{
184
	$smarty = TikiLib::lib('smarty');
185 186 187 188 189 190
	// disallow robots to index page:
	$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
	$smarty->assign('mid', 'tiki-create_multilang_pages.tpl');
	$smarty->display('tiki.tpl');
}

191 192 193 194
/**
 * @param $lang_ids_with_country_code
 * @return array
 */
changi67's avatar
changi67 committed
195 196
function strip_country_code_from_lang_ids($lang_ids_with_country_code)
{
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	$lang_ids = array();
	foreach ($lang_ids_with_country_code as $index => $this_lang_id) {
		if (strlen($this_lang_id) > 2) {
			preg_match('/^([^\-]*)-/', $this_lang_id, $matches);
			$this_lang_id = $matches[1];

		}
		if (!in_array($this_lang_id, $lang_ids)) {
			$lang_ids[] = $this_lang_id;
		}
	}
	
	return $lang_ids;
}