aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/develop/benchmark.php
blob: c3cf90773e58195f5a6d4ef8e6e9296090019fca (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php

//
// Security message:
//
// This script is potentially dangerous.
// Remove or comment the next line (die(".... ) to enable this script.
// Do NOT FORGET to either remove this script or disable it after you have used it.
//
die("Please read the first lines of this script for instructions on how to enable it");

//
// Do not change anything below this line.
//


$phpbb_root_path = "../";
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
include($phpbb_root_path . 'includes/post.'.$phpEx);
include($phpbb_root_path . 'includes/bbcode.'.$phpEx);

srand ((double) microtime() * 1000000);
set_time_limit(240*60);

// Here's the text we stick in posts..
$bigass_text = ' 
phpBB BBCode test suite v0.0.2
auto-linkification:
http://something.com
www.something.com
nate@phpbb.com
http://something.com/foo.php?this=that&theother=some%20encoded%20string is a link.
[code]
Simple code block with some <html> <tags>.
[/code]
[b]bolded[/b], [i]italic[/i]
[email]james@totalgeek.org[/email]
[url=http://www.totalgeek.org]totalgeek.org[/url]
[url]www.totalgeek.org[/url] 
[list] 
[*] This is the first bulleted item.
[*] This is the second bulleted item.
[/list] 
[list=A] 
[*] This is the first bulleted item.
[*] This is the second bulleted item.
[/list] 
[quote]
And a quote!
[/quote]
';


// The script expects the ID's in the tables to sequential (1,2,3,4,5), 
// so no holes please (1,4,5,8)...
$nr_of_users  = nrof(USERS_TABLE);
$nr_of_cats   = nrof(CATEGORIES_TABLE);
$nr_of_forums = nrof(FORUMS_TABLE);
$nr_of_posts  = nrof(POSTS_TABLE);

$u = $users;

$starttime = microtime();

$usercreationcount = 0;
while($users > 0)
{

	$name = "testuser_" . substr(md5(uniqid(rand())), 0, 10);
	if (make_user($name))
	{
		$usercreationcount++;
		$users--;
	}
	if (($usercreationcount % 500) == 0)
	{
		echo "status: $usercreationcount <br>\n";
		flush();
	}
	
}

if ($posts > 0)
{
	filldb($posts);
}

$endtime = microtime();

if ($submit="" || !isset($submit))
{
    ?>
Hello, welcome to this little phpBB Benchmarking script :)<p>

At the moment there are:<br>

<table>
<tr><td align="right"><?php echo $nr_of_users?></td><td>Users</td></tr>
<tr><td align="right"><?php echo $nr_of_forums?></td><td>Forums</td></tr>
<tr><td align="right"><?php echo $nr_of_posts?></td><td>Posts</td></tr>
</table>
<p>
What do you want to create?<p>

<form method="get" action="<?php echo $PHP_SELF?>">
<input type="text" name="users" size="3"> Users<br>
<input type="text" name="posts" size="3"> Posts/topics (optional: post size in <input type="text" name="size" size="3"> bytes)<br>
<input type="submit" name="submit">
</form>

    <?php
} 
else 
{

	list ($starttime_msec,$starttime_sec) = explode(" ",$starttime);
	list ($endtime_msec,$endtime_sec) = explode(" ",$endtime);
	$timetaken_sec = ($endtime_sec+$endtime_msec) - ($starttime_sec+$starttime_msec);
	print "<B>TIME TAKEN : ".$timetaken_sec."s</B><BR>\n"; 

	print "<p>\n<a href=\"$PHP_SELF\">Back to the overview page</a>\n";
}


function filldb($newposts)
{
	global $nr_of_forums;
	global $nr_of_users;
  
	$forum_topic_counts = array();
  
	for ($i = 1; $i <= $nr_of_forums; $i++)
	{
  		$forum_topic_counts[$i] = get_topic_count($i);
	}
  
	for($i = 0; $i < $newposts; $i++)
	{
		$userid   = rand(2, $nr_of_users - 1);
		$forum    = rand(1,$nr_of_forums);
		
		if ((rand(0,30) < 1) || ($forum_topic_count[$forum] == 0))
		{
			// create a new topic 1 in 30 times (or when there are none);
			$topic = make_topic($userid, "Testing topic $i", $forum);
			$forum_topic_count[$forum]++;
		} 
		else 
		{
			// Otherwise create a reply(posting) somewhere.
			$topic = get_smallest_topic($forum);
			create_posting($userid, $topic, $forum, "reply");
		}
		
		if (($i % 1000) == 0)
		{
			echo "status: $i <br>";
			flush();
		}
	 
	}
}


function get_smallest_topic($forum_id)
{
	global $db;
	
	$sql = "SELECT topic_id
		FROM " . TOPICS_TABLE . "
		WHERE (forum_id = $forum_id)
		ORDER BY topic_replies ASC LIMIT 1";
	if($result = $db->sql_query($sql))
	{
		$row = $db->sql_fetchrow($result);
		$topic_id = $row['topic_id'];

		unset($result);
		unset($row);
		return $topic_id;
	}
	else
	{
		message_die(GENERAL_ERROR, "Couldn't get smallest topic.", "", __LINE__, __FILE__, $sql);
	}
	
}


function get_topic_count($forum_id)
{
	global $db;
	
	$sql = "SELECT forum_topics_approved
		FROM " . FORUMS_TABLE . "
		WHERE (forum_id = $forum_id)";
	if($result = $db->sql_query($sql))
	{
		$row = $db->sql_fetchrow($result);
		$topic_count = $row['forum_topics_approved'];

		unset($result);
		unset($row);
		return $topic_count;
	}
	else
	{
		message_die(GENERAL_ERROR, "Couldn't get topic count.", "", __LINE__, __FILE__, $sql);
	}
	
}


function make_topic($user_id, $subject, $forum_id)
{
	global $db;
	$topic_type = POST_NORMAL;
	$topic_vote = 0;
	$current_time = time();
	
	$sql  = "INSERT INTO " . TOPICS_TABLE . " (topic_title, topic_poster, topic_time, forum_id, topic_status, topic_type, topic_vote)
			VALUES ('$subject', $user_id, $current_time, $forum_id, " . TOPIC_UNLOCKED . ", $topic_type, $topic_vote)";

	if( $result = $db->sql_query($sql, BEGIN_TRANSACTION) )
	{
		$new_topic_id = $db->sql_nextid();
	}
	else
	{
		message_die(GENERAL_ERROR, "Error inserting data into topics table", "", __LINE__, __FILE__, $sql);
	}
	
	create_posting($user_id, $new_topic_id, $forum_id);
	
	return $new_topic_id;
}



function create_posting($userid, $topic_id, $forum, $mode='newtopic')
{
	$message = generatepost();

	return make_post($topic_id, $forum, $userid, "", $message, $mode);

}



function make_post($new_topic_id, $forum_id, $user_id, $post_username, $text, $mode='newtopic')
{
	global $db;
	$current_time = time();
	$user_ip = "ac100202";
	$bbcode_on = 1;
	$html_on = 1;
	$smilies_on = 1;
	$attach_sig = 1;
	$bbcode_uid = make_bbcode_uid();
	
	$post_subject = 'random subject';
	
	$post_message = prepare_message($text, $html_on, $bbcode_on, $smilies_on, $bbcode_uid);	
	
	$sql = "INSERT INTO " . POSTS_TABLE . " (topic_id, forum_id, poster_id, attach_id, icon_id, post_username, post_time, poster_ip, post_visibility, bbcode_uid, enable_bbcode, enable_html, enable_smilies, enable_sig, post_subject, post_text)
		VALUES ($new_topic_id, $forum_id, $user_id, 0, 0, '$post_username', $current_time, '$user_ip', 1, '$bbcode_uid', $bbcode_on, $html_on, $smilies_on, $attach_sig, '$post_subject', '$post_message')";
	$result = $db->sql_query($sql);
	
	if ($result)
	{
		$new_post_id = $db->sql_nextid();
	
		$sql = "UPDATE " . TOPICS_TABLE . "
			SET topic_last_post_id = $new_post_id";
		if($mode == "reply")
		{
			$sql .= ", topic_replies = topic_replies + 1 ";
		}
		$sql .= " WHERE topic_id = $new_topic_id";

		if($db->sql_query($sql))
		{
			$sql = "UPDATE " . FORUMS_TABLE . "
				SET forum_last_post_id = $new_post_id, forum_posts_approved = forum_posts_approved + 1";
			if($mode == "newtopic")
			{
				$sql .= ", forum_topics_approved = forum_topics_approved + 1";
			}
			$sql .= " WHERE forum_id = $forum_id";
	
			if($db->sql_query($sql))
			{
				$sql = "UPDATE " . USERS_TABLE . "
					SET user_posts = user_posts + 1
					WHERE user_id = " . $user_id;
	
				if($db->sql_query($sql, END_TRANSACTION))
				{
					// SUCCESS.
					return true;
				}
				else
				{
					message_die(GENERAL_ERROR, "Error updating users table", "", __LINE__, __FILE__, $sql);
				}
			}
			else
			{
				message_die(GENERAL_ERROR, "Error updating forums table", "", __LINE__, __FILE__, $sql);
			}
		}
		else
		{
			// Rollback
			if($db->get_sql_layer() == "mysql")
			{
				$sql = "DELETE FROM " . POSTS_TABLE . "
					WHERE post_id = $new_post_id";
				$db->sql_query($sql);
			}
			message_die(GENERAL_ERROR, "Error updating topics table", "", __LINE__, __FILE__, $sql);
		}
	}
	else
	{
		message_die(GENERAL_ERROR, "Error inserting data into posts table", "", __LINE__, __FILE__, $sql);
	}	
}


function generatepost($size=850)
{
   global $bigass_text;
   // Returns a string with a length between $size and $size*0.2
   $size = rand(0.2*$size, $size);
   
   $textsize = strlen($bigass_text);
   $currentsize = 0;
   // Add whole $text multiple times
   while($currentsize < $size && $size-$currentsize <= $textsize)
   {
      $message .= $bigass_text;
      $currentsize += $textsize;
   }
   // Add the remainder number of chars and return it.
   $message .= substr($bigass_text, 0, $size-$currentsize);

   return (addslashes($message));
}
   
      
function nrof($table)
{
	global $db;
	$sql = "SELECT count(*) AS counted FROM $table";
	$result = $db->sql_query($sql);
	$topics = $db->sql_fetchrow($result);
	return $topics[counted];
}


function make_user($username)
{
	global $db, $board_config;

	$password = md5("benchpass");
	$email = "nobody@localhost";
	$signature = "$username: phpBB tester.";
	$signature_bbcode_uid = "";
	$avatar_filename = "";
	$viewemail = 0;
	$attachsig = 1;
	$allowsmilies = 1;
	$allowhtml = 1;
	$allowbbcode = 1;
	$allowviewonline = 1;
	$notifyreply = 0;
	$notifypm = 0;
	$user_timezone = $board_config['board_timezone'];
	$user_dateformat = $board_config['default_dateformat'];
	$user_lang = $board_config['default_lang'];
	$user_style = $board_config['default_style'];


	$sql = "SELECT MAX(user_id) AS total
		FROM " . USERS_TABLE;
	if($result = $db->sql_query($sql))
	{
		$row = $db->sql_fetchrow($result);
		$new_user_id = $row['total'] + 1;

		unset($result);
		unset($row);
	}
	else
	{
		message_die(GENERAL_ERROR, "Couldn't obtained next user_id information.", "", __LINE__, __FILE__, $sql);
	}

	$sql = "SELECT MAX(group_id) AS total
		FROM " . GROUPS_TABLE;
	if($result = $db->sql_query($sql))
	{
		$row = $db->sql_fetchrow($result);
		$new_group_id = $row['total'] + 1;

		unset($result);
		unset($row);
	}
	else
	{
		message_die(GENERAL_ERROR, "Couldn't obtained next user_id information.", "", __LINE__, __FILE__, $sql);
	}


	$sql = "INSERT INTO " . USERS_TABLE . "	(user_id, username, user_regdate, user_password, user_email, user_sig, user_sig_bbcode_uid, user_avatar, user_viewemail, user_attachsig, user_allowsmilies, user_allowhtml, user_allowbbcode, user_allow_viewonline, user_notify, user_notify_pm, user_timezone, user_dateformat, user_lang, user_style, user_level, user_allow_pm, user_active, user_actkey)
		VALUES ($new_user_id, '$username', " . time() . ", '$password', '$email', '$signature', '$signature_bbcode_uid', '$avatar_filename', $viewemail, $attachsig, $allowsmilies, $allowhtml, $allowbbcode, $allowviewonline, $notifyreply, $notifypm, $user_timezone, '$user_dateformat', '$user_lang', $user_style, 0, 1, ";

	
	$sql .= "1, '')";
	
	if($result = $db->sql_query($sql, BEGIN_TRANSACTION))
	{
		$sql = "INSERT INTO " . GROUPS_TABLE . " (group_id, group_name, group_description, group_single_user, group_moderator)
			VALUES ($new_group_id, '', 'Personal User', 1, 0)";
		if($result = $db->sql_query($sql))
		{
			$sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
				VALUES ($new_user_id, $new_group_id, 0)";
			if($result = $db->sql_query($sql, END_TRANSACTION))
			{
				
				// SUCCESS.
				return true;
			}
			else
			{
				message_die(GENERAL_ERROR, "Couldn't insert data into user_group table", "", __LINE__, __FILE__, $sql);
			}
		}
		else
		{
			message_die(GENERAL_ERROR, "Couldn't insert data into groups table", "", __LINE__, __FILE__, $sql);
		}
	}
	else
	{
		message_die(GENERAL_ERROR, "Couldn't insert data into users table", "", __LINE__, __FILE__, $sql);
	}

}
l opt">::VBox->new(0, 5), if_($top_label, 0, Gtk2::Label->new($top_label)), 1, create_scrolled_window($packtable), 0, gtkpack__(create_hbox(), @$action_buttons))); my $preq = $packtable->size_request; my ($xpreq, $ypreq) = ($preq->width, $preq->height); my $wreq = $w->{rwindow}->size_request; my ($xwreq, $ywreq) = ($wreq->width, $wreq->height); $w->{rwindow}->set_default_size(max($typical_width, min($typical_width*2.5, $xpreq+$xwreq)), max(200, min(450, $ypreq+$ywreq))); $w->main; } sub interactive_list { my ($title, $contents, $list, $callback, %options) = @_; my $d = ugtk2->new($title, grab => 1, if_(exists $options{transient}, transient => $options{transient})); $d->{rwindow}->set_position($options{transient} ? 'center_on_parent' : 'center_always'); my @radios = gtkradio('', @$list); my $vbradios = $callback ? create_packtable( {}, mapn { my $n = $_[1]; [ $_[0], gtksignal_connect( Gtk2::Button->new(but(N("Info..."))), clicked => sub { $callback->($n) }, ) ]; } \@radios, $list, ) : gtkpack__(Gtk2::VBox->new(0, 0), @radios); my $choice; gtkadd( $d->{window}, gtkpack__( Gtk2::VBox->new(0,5), Gtk2::Label->new($contents), int(@$list) > 8 ? gtkset_size_request(create_scrolled_window($vbradios), 250, 320) : $vbradios, gtkpack__( create_hbox(), if_(!$options{nocancel}, gtksignal_connect( Gtk2::Button->new(N("Cancel")), clicked => sub { Gtk2->main_quit }), ), gtksignal_connect( Gtk2::Button->new(N("Ok")), clicked => sub { each_index { $_->get_active and $choice = $::i } @radios; Gtk2->main_quit; } ) ) ) ); $d->main; $choice; } sub interactive_list_ { interactive_list(@_, if_($::main_window, transient => $::main_window)) } sub interactive_msg_with_banner { push @_, banner => 1 if $::isEmbedded; &interactive_msg } sub fatal_msg { interactive_msg @_; myexit -1; } sub wait_msg { my ($msg, %options) = @_; gtkflush(); $options{transient} ||= $::main_window if $::main_window; local $::isEmbedded; my $mainw = ugtk2->new(N("Please wait"), grab => 1, if_(exists $options{transient}, transient => $options{transient})); $mainw->{real_window}->set_position($options{transient} ? 'center_on_parent' : 'center_always'); my $label = ref($msg) =~ /^Gtk/ ? $msg : Gtk2::WrappedLabel->new($msg); my $banner = $options{banner} ? getbanner() : undef; gtkadd( $mainw->{window}, gtkpack__( gtkset_border_width(Gtk2::VBox->new(0, 5), 6), if_($banner, $banner), $label, if_(exists $options{widgets}, @{$options{widgets}}), ) ); $mainw->sync; gtkset_mousecursor_wait($mainw->{rwindow}->window) unless $options{no_wait_cursor}; $mainw->flush; $mainw; } sub remove_wait_msg { my $w = shift; gtkset_mousecursor_normal($w->{rwindow}->window); $w->destroy; } sub but { " $_[0] " } sub but_ { " $_[0] " } sub slow_func ($&) { my ($param, $func) = @_; if (ref($param) =~ /^Gtk/) { gtkset_mousecursor_wait($param); ugtk2::flush(); $func->(); gtkset_mousecursor_normal($param); } else { my $w = wait_msg($param); $func->(); remove_wait_msg($w); } } sub statusbar_msg { unless ($::statusbar) { #- fallback if no status bar if (defined &::wait_msg_) { goto &::wait_msg_ } else { goto &wait_msg } } my ($msg, $o_timeout) = @_; #- always use the same context description for now my $cx = $::statusbar->get_context_id("foo"); $::w and $::w->{rwindow} and gtkset_mousecursor_wait($::w->{rwindow}->window); #- returns a msg_id to be passed optionnally to statusbar_msg_remove my $id = $::statusbar->push($cx, $msg); gtkflush(); Glib::Timeout->add(5000, sub { statusbar_msg_remove($id); 0 }) if $o_timeout; $id; } sub statusbar_msg_remove { my ($msg_id) = @_; if (!$::statusbar || ref $msg_id) { #- fallback if no status bar goto &remove_wait_msg; } my $cx = $::statusbar->get_context_id("foo"); if (defined $msg_id) { $::statusbar->remove($cx, $msg_id); } else { $::statusbar->pop($cx); } $::w and $::w->{rwindow} and gtkset_mousecursor_normal($::w->{rwindow}->window); } sub slow_func_statusbar ($$&) { my ($msg, $w, $func) = @_; gtkset_mousecursor_wait($w->window); my $msg_id = statusbar_msg($msg); gtkflush(); $func->(); statusbar_msg_remove($msg_id); gtkset_mousecursor_normal($w->window); } my %u2l = ( at => N_("Austria"), au => N_("Australia"), be => N_("Belgium"), br => N_("Brazil"), ca => N_("Canada"), ch => N_("Switzerland"), cr => N_("Costa Rica"), cz => N_("Czech Republic"), de => N_("Germany"), dk => N_("Danmark"), el => N_("Greece"), es => N_("Spain"), fi => N_("Finland"), fr => N_("France"), gr => N_("Greece"), hu => N_("Hungary"), il => N_("Israel"), it => N_("Italy"), jp => N_("Japan"), ko => N_("Korea"), nl => N_("Netherlands"), no => N_("Norway"), pl => N_("Poland"), pt => N_("Portugal"), ru => N_("Russia"), se => N_("Sweden"), sg => N_("Singapore"), sk => N_("Slovakia"), tw => N_("Taiwan"), uk => N_("United Kingdom"), cn => N_("China"), com => N_("United States"), org => N_("United States"), net => N_("United States"), edu => N_("United States"), ); my $us = [ qw(com org net edu) ]; my %t2l = ( 'America/\w+' => $us, 'Asia/Tel_Aviv' => [ qw(il ru it cz at de fr se) ], 'Asia/Tokyo' => [ qw(jp ko tw), @$us ], 'Asia/Seoul' => [ qw(ko jp tw), @$us ], 'Asia/Taipei' => [ qw(tw jp), @$us ], 'Asia/(Shanghai|Beijing)' => [ qw(cn tw sg), @$us ], 'Asia/Singapore' => [ qw(cn sg), @$us ], 'Atlantic/Reykjavik' => [ qw(uk no se fi dk), @$us, qw(nl de fr at cz it) ], 'Australia/\w+' => [ qw(au jp ko tw), @$us ], 'Brazil/\w+' => [ 'br', @$us ], 'Canada/\w+' => [ 'ca', @$us ], 'Europe/Amsterdam' => [ qw(nl be de at cz fr se dk it) ], 'Europe/Athens' => [ qw(gr pl cz de it nl at fr) ], 'Europe/Berlin' => [ qw(de be at nl cz it fr se) ], 'Europe/Brussels' => [ qw(be de nl fr cz at it se) ], 'Europe/Budapest' => [ qw(cz it at de fr nl se) ], 'Europe/Copenhagen' => [ qw(dk nl de be se at cz it) ], 'Europe/Dublin' => [ qw(uk fr be nl dk se cz it) ], 'Europe/Helsinki' => [ qw(fi se no nl be de fr at it) ], 'Europe/Istanbul' => [ qw(il ru it cz it at de fr nl se) ], 'Europe/Lisbon' => [ qw(pt es fr it cz at de se) ], 'Europe/London' => [ qw(uk fr be nl de at cz se it) ], 'Europe/Madrid' => [ qw(es fr pt it cz at de se) ], 'Europe/Moscow' => [ qw(ru de pl cz at se be fr it) ], 'Europe/Oslo' => [ qw(no se fi dk de be at cz it) ], 'Europe/Paris' => [ qw(fr be de at cz nl it se) ], 'Europe/Prague' => [ qw(cz it at de fr nl se) ], 'Europe/Rome' => [ qw(it fr cz de at nl se) ], 'Europe/Stockholm' => [ qw(se no dk fi nl de at cz fr it) ], 'Europe/Vienna' => [ qw(at de cz it fr nl se) ], ); #- get distrib release number (2006.0, etc) sub etc_version() { (my $v) = split / /, cat_('/etc/version'); return $v; } #- returns the keyword describing the type of the distribution. #- the parameter indicates whether we want base or update sources sub distro_type { my ($want_base_distro) = @_; return 'cooker' if $mandrake_release =~ /cooker/i; #- we can't use updates for community while official is not out (release ends in ".0") if ($want_base_distro || $mandrake_release =~ /community/i && etc_version() =~ /\.0$/) { return 'official' if $mandrake_release =~ /official|limited/i; return 'community' if $mandrake_release =~ /community/i; #- unknown: fallback to updates } return 'updates'; } sub compat_arch_for_updates($) { # FIXME: We prefer 64-bit packages to update on biarch platforms, # since the system is populated with 64-bit packages anyway. my ($arch) = @_; return $arch =~ /x86_64|amd64/ if arch() eq 'x86_64'; MDK::Common::System::compat_arch($arch); } sub mirrors { my ($urpm, $want_base_distro) = @_; my $cachedir = $urpm->{cachedir} || '/root'; require mirror; mirror::register_downloader( sub { my ($url) = @_; my $file = $url; $file =~ s!.*/!$cachedir/!; unlink $file; # prevent "partial file" errors before_leaving(sub { unlink $file }); my ($gurpm, $id, $canceled); # display a message in statusbar (if availlable): $::statusbar and $id = statusbar_msg( $branded ? N("Please wait, downloading mirror addresses.") : N("Please wait, downloading mirror addresses from the Mandriva website."), 0); my $_clean_guard = before_leaving { undef $gurpm; $id and statusbar_msg_remove($id); }; require Rpmdrake::gurpm; require Rpmdrake::pkg; my $res = urpm::download::sync($urpm, undef, [ $url ], dir => $cachedir, callback => sub { $gurpm ||= Rpmdrake::gurpm->new(N("Please wait"), transient => $::main_window); $canceled ||= !Rpmdrake::pkg::download_callback($gurpm, @_); gtkflush(); }, ); $res or do { c::set_tagged_utf8($res); die $res }; return $canceled ? () : cat_($file); }); my @mirrors = @{ mirror::list(common::parse_LDAP_namespace_structure(cat_('/etc/product.id')), 'distrib') || [] }; require timezone; my $tz = ${timezone::read()}{timezone}; foreach my $mirror (@mirrors) { my $goodness; each_index { $_ = $u2l{$_} || $_; $_ eq $mirror->{country} and $goodness ||= 100-$::i } (map { if_($tz =~ /^$_$/, @{$t2l{$_}}) } keys %t2l), @$us; $mirror->{goodness} = $goodness + rand(); $mirror->{country} = translate($mirror->{country}); } unless (-x '/usr/bin/rsync') { @mirrors = grep { $_->{url} !~ /^rsync:/ } @mirrors; } return sort { $b->{goodness} <=> $a->{goodness} } @mirrors; } sub choose_mirror { my ($urpm, %options) = @_; my $message = $options{message} ? $options{message} : $branded ? N("I need to access internet to get the mirror list. Please check that your network is currently running. Is it ok to continue?") : N("I need to contact the Mandriva website to get the mirror list. Please check that your network is currently running. Is it ok to continue?"); delete $options{message}; my @transient_options = exists $options{transient} ? (transient => $options{transient}) : (); interactive_msg(N("Mirror choice"), $message, yesno => 1, %options) or return ''; my @mirrors = eval { mirrors($urpm, $options{want_base_distro}) }; my $error = $@; if ($error) { interactive_msg(N("Error during download"), ($branded ? N("There was an error downloading the mirror list: %s The network, or the website, may be unavailable. Please try again later.", $error) : N("There was an error downloading the mirror list: %s The network, or the Mandriva website, may be unavailable. Please try again later.", $error)), %options ); return ''; } !@mirrors and interactive_msg(N("No mirror"), ($branded ? N("I can't find any suitable mirror.") : N("I can't find any suitable mirror. There can be many reasons for this problem; the most frequent is the case when the architecture of your processor is not supported by Mandriva Linux Official Updates.")), %options ), return ''; my $w = ugtk2->new(N("Mirror choice"), grab => 1, @transient_options); $w->{rwindow}->set_position($options{transient} ? 'center_on_parent' : 'center_always'); my $tree_model = Gtk2::TreeStore->new("Glib::String"); my $tree = Gtk2::TreeView->new_with_model($tree_model); $tree->get_selection->set_mode('browse'); $tree->append_column(Gtk2::TreeViewColumn->new_with_attributes(undef, Gtk2::CellRendererText->new, text => 0)); $tree->set_headers_visible(0); gtkadd( $w->{window}, gtkpack_( Gtk2::VBox->new(0,5), 0, N("Please choose the desired mirror."), 1, create_scrolled_window($tree), 0, gtkpack( create_hbox('edge'), map { my $retv = $_->[1]; gtksignal_connect( Gtk2::Button->new(but($_->[0])), clicked => sub { if ($retv) { my ($model, $iter) = $tree->get_selection->get_selected; $model and $w->{retval} = { sel => $model->get($iter, 0) }; } Gtk2->main_quit; }, ); } [ N("Cancel"), 0 ], [ N("Ok"), 1 ] ), ) ); my %roots; $tree_model->append_set($roots{$_->{country}} ||= $tree_model->append_set(undef, [ 0 => $_->{country} ]), [ 0 => $_->{url} ]) foreach @mirrors; $w->{window}->set_size_request(500, 400); $w->{rwindow}->show_all; my $path = Gtk2::TreePath->new_first; $tree->expand_row($path, 0); $path->down; $tree->get_selection->select_path($path); $w->main && return grep { $w->{retval}{sel} eq $_->{url} } @mirrors; } sub show_urpm_progress { my ($label, $pb, $mode, $file, $percent, $total, $eta, $speed) = @_; $file =~ s|([^:]*://[^/:\@]*:)[^/:\@]*(\@.*)|$1xxxx$2|; #- if needed... state $medium; if ($mode eq 'copy') { $pb->set_fraction(0); $label->set_label(N("Copying file for medium `%s'...", $file)); } elsif ($mode eq 'parse') { $pb->set_fraction(0); $label->set_label(N("Examining file of medium `%s'...", $file)); } elsif ($mode eq 'retrieve') { $pb->set_fraction(0); $label->set_label(N("Examining remote file of medium `%s'...", $file)); $medium = $file; } elsif ($mode eq 'done') { $pb->set_fraction(1.0); $label->set_label($label->get_label . N(" done.")); $medium = undef; } elsif ($mode eq 'failed') { $pb->set_fraction(1.0); $label->set_label($label->get_label . N(" failed!")); $medium = undef; } else { length($file) > 60 and $file = $medium ? #-PO: We're downloading the said file from the said medium N("%s from medium %s", basename($file), $medium) : basename($file); if ($mode eq 'start') { $pb->set_fraction(0); $label->set_label(N("Starting download of `%s'...", $file)); } elsif ($mode eq 'progress') { if (defined $total && defined $eta) { $pb->set_fraction($percent/100); $label->set_label(N("Download of `%s', time to go:%s, speed:%s", $file, $eta, $speed)); } else { $pb->set_fraction($percent/100); $label->set_label(N("Download of `%s', speed:%s", $file, $speed)); } } } Gtk2->main_iteration while Gtk2->events_pending; } sub update_sources { my ($urpm, %options) = @_; my $cancel = 0; my $w; my $label; $w = wait_msg( $label = Gtk2::Label->new(N("Please wait, updating media...")), no_wait_cursor => 1, banner => $options{banner}, widgets => [ my $pb = gtkset_size_request(Gtk2::ProgressBar->new, 300, -1), gtkpack( create_hbox(), gtksignal_connect( Gtk2::Button->new(N("Cancel")), clicked => sub { $cancel = 1; $w->destroy; }, ), ), ], ); my @media; @media = @{$options{medialist}} if ref $options{medialist}; my $outerfatal = $urpm->{fatal}; local $urpm->{fatal} = sub { $w->destroy; $outerfatal->(@_) }; urpm::media::update_media($urpm, %options, callback => sub { $cancel and goto cancel_update; my ($type, $media) = @_; return if $type !~ /^(?:start|progress|end)$/ && @media && !member($media, @media); if ($type eq 'failed') { $urpm->{fatal}->(N("Error retrieving packages"), N("It's impossible to retrieve the list of new packages from the media `%s'. Either this update media is misconfigured, and in this case you should use the Software Media Manager to remove it and re-add it in order to reconfigure it, either it is currently unreachable and you should retry later.", $media)); } else { show_urpm_progress($label, $pb, @_); } }, ); $w->destroy; cancel_update: } sub update_sources_check { my ($urpm, $options, $error_msg, @media) = @_; my @error_msgs; local $urpm->{fatal} = sub { push @error_msgs, $_[1]; goto fatal_error }; local $urpm->{error} = sub { push @error_msgs, $_[0] }; update_sources($urpm, %$options, noclean => 1, medialist => \@media); fatal_error: if (@error_msgs) { interactive_msg(N("Error"), sprintf(translate($error_msg), join("\n", map { formatAlaTeX($_) } @error_msgs)), scroll => 1); return 0; } return 1; } sub update_sources_interactive { my ($urpm, %options) = @_; my $w = ugtk2->new(N("Update media"), grab => 1, center => 1, %options); $w->{rwindow}->set_position($options{transient} ? 'center_on_parent' : 'center_always'); my @buttons; my @media = grep { ! $_->{ignore} } @{$urpm->{media}}; unless (@media) { interactive_msg(N("Warning"), N("No active medium found. You must enable some media to be able to update them.")); return 0; } gtkadd( $w->{window}, gtkpack__( Gtk2::VBox->new(0,5), Gtk2::Label->new(N("Select the media you wish to update:")), ( @buttons = map { Gtk2::CheckButton->new_with_label($_->{name}); } @media ), Gtk2::HSeparator->new, gtkpack( create_hbox(), gtksignal_connect( Gtk2::Button->new(N("Cancel")), clicked => sub { $w->{retval} = 0; Gtk2->main_quit }, ), gtksignal_connect( Gtk2::Button->new(N("Select all")), clicked => sub { $_->set_active(1) foreach @buttons }, ), gtksignal_connect( Gtk2::Button->new(N("Update")), clicked => sub { $w->{retval} = any { $_->get_active } @buttons; # list of media listed in the checkbox panel my @buttonmedia = grep { !$_->{ignore} } @{$urpm->{media}}; @media = map_index { if_($_->get_active, $buttonmedia[$::i]{name}) } @buttons; Gtk2->main_quit; }, ), ) ) ); if ($w->main) { return update_sources_noninteractive($urpm, \@media, %options); } return 0; } sub update_sources_noninteractive { my ($urpm, $media, %options) = @_; #- force ignored media to be returned alive (forked from urpmi.update...) foreach (@{$urpm->{media}}) { $_->{modified} and delete $_->{ignore}; } urpm::media::select_media($urpm, @$media); update_sources_check( $urpm, {}, N_("Unable to update medium; it will be automatically disabled.\n\nErrors:\n%s"), @$media, ); return 1; } sub add_medium_and_check { my ($urpm, $options) = splice @_, 0, 2; my @newnames = ($_[0]); #- names of added media my $fatal_msg; my @error_msgs; local $urpm->{fatal} = sub { printf STDERR "Fatal: %s\n", $_[1]; $fatal_msg = $_[1]; goto fatal_error };