/
tiki-create_multilang_pages.php
206 lines (183 loc) · 5.46 KB
/
tiki-create_multilang_pages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* @package tikiwiki
*/
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
//
// 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.
// $Id: tiki-create_multilang_pages.php 44444 2013-01-05 21:24:24Z changi67 $
include_once('tiki-setup.php');
include_once('lib/tikilib.php');
include_once('lib/wiki/wikilib.php');
include_once 'lib/wiki/semanticlib.php';
include_once ('lib/multilingual/multilinguallib.php');
$access->check_feature(array( 'feature_wiki', 'feature_multilingual' ));
$access->check_permission('tiki_p_edit');
compute_relevant_languages();
create_pages_if_necessary();
display();
function create_pages_if_necessary()
{
global $smarty, $_REQUEST;
$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);
}
/**
* @param $page_name
* @param $lang
* @param null $template_name
*/
function create_page($page_name, $lang, $template_name=null)
{
global $tikilib, $multilinguallib, $user;
$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);
}
/**
* @param $pages
*/
function make_pages_translations_of_each_other($pages)
{
global $tikilib, $multilinguallib;
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 {
$this_page_info = $tikilib->get_page_info($this_page_name);
$this_page_id = $this_page_info['page_id'];
$multilinguallib->insertTranslation('wiki page', $first_page_id, $first_page_lang, $this_page_id, $this_page_lang);
}
}
}
/**
* @return array
*/
function compute_relevant_languages()
{
global $multilinguallib, $smarty, $_REQUEST, $prefs;
$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;
}
/**
* @return array
*/
function get_pages_to_create()
{
global $_REQUEST;
$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;
}
/**
* @param $pages_to_create
* @return array
*/
function check_for_existence_of_pages($pages_to_create)
{
global $tikilib, $semanticlib;
$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;
} else {
$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);
}
/**
* @param $page_names
*/
function set_smarty_page_links($page_names)
{
global $wikilib, $smarty;
$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);
}
function display()
{
global $smarty;
// disallow robots to index page:
$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
$smarty->assign('mid', 'tiki-create_multilang_pages.tpl');
$smarty->display('tiki.tpl');
}
/**
* @param $lang_ids_with_country_code
* @return array
*/
function strip_country_code_from_lang_ids($lang_ids_with_country_code)
{
$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;
}