1# This Source Code Form is subject to the terms of the Mozilla Public 2# License, v. 2.0. If a copy of the MPL was not distributed with this 3# file, You can obtain one at http://mozilla.org/MPL/2.0/. 4# 5# This Source Code Form is "Incompatible With Secondary Licenses", as 6# defined by the Mozilla Public License, v. 2.0. 7 8package Bugzilla::Extension::Example; 9use strict; 10use base qw(Bugzilla::Extension); 11 12use Bugzilla::Constants; 13use Bugzilla::Error; 14use Bugzilla::Group; 15use Bugzilla::User; 16use Bugzilla::User::Setting; 17use Bugzilla::Util qw(diff_arrays html_quote); 18use Bugzilla::Status qw(is_open_state); 19use Bugzilla::Install::Filesystem; 20 21# This is extensions/Example/lib/Util.pm. I can load this here in my 22# Extension.pm only because I have a Config.pm. 23use Bugzilla::Extension::Example::Util; 24 25use Data::Dumper; 26 27# See bugmail_relationships. 28use constant REL_EXAMPLE => -127; 29 30our $VERSION = '1.0'; 31 32sub admin_editusers_action { 33 my ($self, $args) = @_; 34 my ($vars, $action, $user) = @$args{qw(vars action user)}; 35 my $template = Bugzilla->template; 36 37 if ($action eq 'my_action') { 38 # Allow to restrict the search to any group the user is allowed to bless. 39 $vars->{'restrictablegroups'} = $user->bless_groups(); 40 $template->process('admin/users/search.html.tmpl', $vars) 41 || ThrowTemplateError($template->error()); 42 exit; 43 } 44} 45 46sub attachment_process_data { 47 my ($self, $args) = @_; 48 my $type = $args->{attributes}->{mimetype}; 49 my $filename = $args->{attributes}->{filename}; 50 51 # Make sure images have the correct extension. 52 # Uncomment the two lines below to make this check effective. 53 if ($type =~ /^image\/(\w+)$/) { 54 my $format = $1; 55 if ($filename =~ /^(.+)(:?\.[^\.]+)$/) { 56 my $name = $1; 57 #$args->{attributes}->{filename} = "${name}.$format"; 58 } 59 else { 60 # The file has no extension. We append it. 61 #$args->{attributes}->{filename} .= ".$format"; 62 } 63 } 64} 65 66sub auth_login_methods { 67 my ($self, $args) = @_; 68 my $modules = $args->{modules}; 69 if (exists $modules->{Example}) { 70 $modules->{Example} = 'Bugzilla/Extension/Example/Auth/Login.pm'; 71 } 72} 73 74sub auth_verify_methods { 75 my ($self, $args) = @_; 76 my $modules = $args->{modules}; 77 if (exists $modules->{Example}) { 78 $modules->{Example} = 'Bugzilla/Extension/Example/Auth/Verify.pm'; 79 } 80} 81 82sub bug_check_can_change_field { 83 my ($self, $args) = @_; 84 85 my ($bug, $field, $new_value, $old_value, $priv_results) 86 = @$args{qw(bug field new_value old_value priv_results)}; 87 88 my $user = Bugzilla->user; 89 90 # Disallow a bug from being reopened if currently closed unless user 91 # is in 'admin' group 92 if ($field eq 'bug_status' && $bug->product_obj->name eq 'Example') { 93 if (!is_open_state($old_value) && is_open_state($new_value) 94 && !$user->in_group('admin')) 95 { 96 push(@$priv_results, PRIVILEGES_REQUIRED_EMPOWERED); 97 return; 98 } 99 } 100 101 # Disallow a bug's keywords from being edited unless user is the 102 # reporter of the bug 103 if ($field eq 'keywords' && $bug->product_obj->name eq 'Example' 104 && $user->login ne $bug->reporter->login) 105 { 106 push(@$priv_results, PRIVILEGES_REQUIRED_REPORTER); 107 return; 108 } 109 110 # Allow updating of priority even if user cannot normally edit the bug 111 # and they are in group 'engineering' 112 if ($field eq 'priority' && $bug->product_obj->name eq 'Example' 113 && $user->in_group('engineering')) 114 { 115 push(@$priv_results, PRIVILEGES_REQUIRED_NONE); 116 return; 117 } 118} 119 120sub bug_columns { 121 my ($self, $args) = @_; 122 my $columns = $args->{'columns'}; 123 push (@$columns, "delta_ts AS example") 124} 125 126sub bug_end_of_create { 127 my ($self, $args) = @_; 128 129 # This code doesn't actually *do* anything, it's just here to show you 130 # how to use this hook. 131 my $bug = $args->{'bug'}; 132 my $timestamp = $args->{'timestamp'}; 133 134 my $bug_id = $bug->id; 135 # Uncomment this line to see a line in your webserver's error log whenever 136 # you file a bug. 137 # warn "Bug $bug_id has been filed!"; 138} 139 140sub bug_end_of_create_validators { 141 my ($self, $args) = @_; 142 143 # This code doesn't actually *do* anything, it's just here to show you 144 # how to use this hook. 145 my $bug_params = $args->{'params'}; 146 147 # Uncomment this line below to see a line in your webserver's error log 148 # containing all validated bug field values every time you file a bug. 149 # warn Dumper($bug_params); 150 151 # This would remove all ccs from the bug, preventing ANY ccs from being 152 # added on bug creation. 153 # $bug_params->{cc} = []; 154} 155 156sub bug_start_of_update { 157 my ($self, $args) = @_; 158 159 # This code doesn't actually *do* anything, it's just here to show you 160 # how to use this hook. 161 my ($bug, $old_bug, $timestamp, $changes) = 162 @$args{qw(bug old_bug timestamp changes)}; 163 164 foreach my $field (keys %$changes) { 165 my $used_to_be = $changes->{$field}->[0]; 166 my $now_it_is = $changes->{$field}->[1]; 167 } 168 169 my $old_summary = $old_bug->short_desc; 170 171 my $status_message; 172 if (my $status_change = $changes->{'bug_status'}) { 173 my $old_status = new Bugzilla::Status({ name => $status_change->[0] }); 174 my $new_status = new Bugzilla::Status({ name => $status_change->[1] }); 175 if ($new_status->is_open && !$old_status->is_open) { 176 $status_message = "Bug re-opened!"; 177 } 178 if (!$new_status->is_open && $old_status->is_open) { 179 $status_message = "Bug closed!"; 180 } 181 } 182 183 my $bug_id = $bug->id; 184 my $num_changes = scalar keys %$changes; 185 my $result = "There were $num_changes changes to fields on bug $bug_id" 186 . " at $timestamp."; 187 # Uncomment this line to see $result in your webserver's error log whenever 188 # you update a bug. 189 # warn $result; 190} 191 192sub bug_end_of_update { 193 my ($self, $args) = @_; 194 195 # This code doesn't actually *do* anything, it's just here to show you 196 # how to use this hook. 197 my ($bug, $old_bug, $timestamp, $changes) = 198 @$args{qw(bug old_bug timestamp changes)}; 199 200 foreach my $field (keys %$changes) { 201 my $used_to_be = $changes->{$field}->[0]; 202 my $now_it_is = $changes->{$field}->[1]; 203 } 204 205 my $old_summary = $old_bug->short_desc; 206 207 my $status_message; 208 if (my $status_change = $changes->{'bug_status'}) { 209 my $old_status = new Bugzilla::Status({ name => $status_change->[0] }); 210 my $new_status = new Bugzilla::Status({ name => $status_change->[1] }); 211 if ($new_status->is_open && !$old_status->is_open) { 212 $status_message = "Bug re-opened!"; 213 } 214 if (!$new_status->is_open && $old_status->is_open) { 215 $status_message = "Bug closed!"; 216 } 217 } 218 219 my $bug_id = $bug->id; 220 my $num_changes = scalar keys %$changes; 221 my $result = "There were $num_changes changes to fields on bug $bug_id" 222 . " at $timestamp."; 223 # Uncomment this line to see $result in your webserver's error log whenever 224 # you update a bug. 225 # warn $result; 226} 227 228sub bug_fields { 229 my ($self, $args) = @_; 230 231 my $fields = $args->{'fields'}; 232 push (@$fields, "example") 233} 234 235sub bug_format_comment { 236 my ($self, $args) = @_; 237 238 # This replaces every occurrence of the word "foo" with the word 239 # "bar" 240 241 my $regexes = $args->{'regexes'}; 242 push(@$regexes, { match => qr/\bfoo\b/, replace => 'bar' }); 243 244 # And this links every occurrence of the word "bar" to example.com, 245 # but it won't affect "foo"s that have already been turned into "bar" 246 # above (because each regex is run in order, and later regexes don't modify 247 # earlier matches, due to some cleverness in Bugzilla's internals). 248 # 249 # For example, the phrase "foo bar" would become: 250 # bar <a href="http://example.com/bar">bar</a> 251 my $bar_match = qr/\b(bar)\b/; 252 push(@$regexes, { match => $bar_match, replace => \&_replace_bar }); 253} 254 255# Used by bug_format_comment--see its code for an explanation. 256sub _replace_bar { 257 my $args = shift; 258 # $match is the first parentheses match in the $bar_match regex 259 # in bug-format_comment.pl. We get up to 10 regex matches as 260 # arguments to this function. 261 my $match = $args->{matches}->[0]; 262 # Remember, you have to HTML-escape any data that you are returning! 263 $match = html_quote($match); 264 return qq{<a href="http://example.com/">$match</a>}; 265}; 266 267sub buglist_columns { 268 my ($self, $args) = @_; 269 270 my $columns = $args->{'columns'}; 271 $columns->{'example'} = { 'name' => 'bugs.delta_ts' , 'title' => 'Example' }; 272 $columns->{'product_desc'} = { 'name' => 'prod_desc.description', 273 'title' => 'Product Description' }; 274} 275 276sub buglist_column_joins { 277 my ($self, $args) = @_; 278 my $joins = $args->{'column_joins'}; 279 280 # This column is added using the "buglist_columns" hook 281 $joins->{'product_desc'} = { 282 from => 'product_id', 283 to => 'id', 284 table => 'products', 285 as => 'prod_desc', 286 join => 'INNER', 287 }; 288} 289 290sub search_operator_field_override { 291 my ($self, $args) = @_; 292 293 my $operators = $args->{'operators'}; 294 295 my $original = $operators->{component}->{_non_changed}; 296 $operators->{component} = { 297 _non_changed => sub { _component_nonchanged($original, @_) } 298 }; 299} 300 301sub _component_nonchanged { 302 my $original = shift; 303 my ($invocant, $args) = @_; 304 305 $invocant->$original($args); 306 # Actually, it does not change anything in the result, 307 # just an example. 308 $args->{term} = $args->{term} . " OR 1=2"; 309} 310 311sub bugmail_recipients { 312 my ($self, $args) = @_; 313 my $recipients = $args->{recipients}; 314 my $bug = $args->{bug}; 315 316 my $user = 317 new Bugzilla::User({ name => Bugzilla->params->{'maintainer'} }); 318 319 if ($bug->id == 1) { 320 # Uncomment the line below to add the maintainer to the recipients 321 # list of every bugmail from bug 1 as though that the maintainer 322 # were on the CC list. 323 #$recipients->{$user->id}->{+REL_CC} = 1; 324 325 # And this line adds the maintainer as though he had the "REL_EXAMPLE" 326 # relationship from the bugmail_relationships hook below. 327 #$recipients->{$user->id}->{+REL_EXAMPLE} = 1; 328 } 329} 330 331sub bugmail_relationships { 332 my ($self, $args) = @_; 333 my $relationships = $args->{relationships}; 334 $relationships->{+REL_EXAMPLE} = 'Example'; 335} 336 337sub config_add_panels { 338 my ($self, $args) = @_; 339 340 my $modules = $args->{panel_modules}; 341 $modules->{Example} = "Bugzilla::Extension::Example::Config"; 342} 343 344sub config_modify_panels { 345 my ($self, $args) = @_; 346 347 my $panels = $args->{panels}; 348 349 # Add the "Example" auth methods. 350 my $auth_params = $panels->{'auth'}->{params}; 351 my ($info_class) = grep($_->{name} eq 'user_info_class', @$auth_params); 352 my ($verify_class) = grep($_->{name} eq 'user_verify_class', @$auth_params); 353 354 push(@{ $info_class->{choices} }, 'CGI,Example'); 355 push(@{ $verify_class->{choices} }, 'Example'); 356 357 push(@$auth_params, { name => 'param_example', 358 type => 't', 359 default => 0, 360 checker => \&check_numeric }); 361} 362 363sub db_schema_abstract_schema { 364 my ($self, $args) = @_; 365# $args->{'schema'}->{'example_table'} = { 366# FIELDS => [ 367# id => {TYPE => 'SMALLSERIAL', NOTNULL => 1, 368# PRIMARYKEY => 1}, 369# for_key => {TYPE => 'INT3', NOTNULL => 1, 370# REFERENCES => {TABLE => 'example_table2', 371# COLUMN => 'id', 372# DELETE => 'CASCADE'}}, 373# col_3 => {TYPE => 'varchar(64)', NOTNULL => 1}, 374# ], 375# INDEXES => [ 376# id_index_idx => {FIELDS => ['col_3'], TYPE => 'UNIQUE'}, 377# for_id_idx => ['for_key'], 378# ], 379# }; 380} 381 382sub email_in_before_parse { 383 my ($self, $args) = @_; 384 385 my $subject = $args->{mail}->header('Subject'); 386 # Correctly extract the bug ID from email subjects of the form [Bug comp/NNN]. 387 if ($subject =~ /\[.*(\d+)\].*/) { 388 $args->{fields}->{bug_id} = $1; 389 } 390} 391 392sub email_in_after_parse { 393 my ($self, $args) = @_; 394 my $reporter = $args->{fields}->{reporter}; 395 my $dbh = Bugzilla->dbh; 396 397 # No other check needed if this is a valid regular user. 398 return if login_to_id($reporter); 399 400 # The reporter is not a regular user. We create an account for him, 401 # but he can only comment on existing bugs. 402 # This is useful for people who reply by email to bugmails received 403 # in mailing-lists. 404 if ($args->{fields}->{bug_id}) { 405 # WARNING: we return now to skip the remaining code below. 406 # You must understand that removing this line would make the code 407 # below effective! Do it only if you are OK with the behavior 408 # described here. 409 return; 410 411 Bugzilla::User->create({ login_name => $reporter, cryptpassword => '*' }); 412 413 # For security reasons, delete all fields unrelated to comments. 414 foreach my $field (keys %{$args->{fields}}) { 415 next if $field =~ /^(?:bug_id|comment|reporter)$/; 416 delete $args->{fields}->{$field}; 417 } 418 } 419 else { 420 ThrowUserError('invalid_username', { name => $reporter }); 421 } 422} 423 424sub enter_bug_entrydefaultvars { 425 my ($self, $args) = @_; 426 427 my $vars = $args->{vars}; 428 $vars->{'example'} = 1; 429} 430 431sub error_catch { 432 my ($self, $args) = @_; 433 # Customize the error message displayed when someone tries to access 434 # page.cgi with an invalid page ID, and keep track of this attempt 435 # in the web server log. 436 return unless Bugzilla->error_mode == ERROR_MODE_WEBPAGE; 437 return unless $args->{error} eq 'bad_page_cgi_id'; 438 439 my $page_id = $args->{vars}->{page_id}; 440 my $login = Bugzilla->user->identity || "Someone"; 441 warn "$login attempted to access page.cgi with id = $page_id"; 442 443 my $page = $args->{message}; 444 my $new_error_msg = "Ah ah, you tried to access $page_id? Good try!"; 445 $new_error_msg = html_quote($new_error_msg); 446 # There are better tools to parse an HTML page, but it's just an example. 447 # Since Perl 5.16, we can no longer write "class" inside look-behind 448 # assertions, because "ss" is also seen as the german ß character, which 449 # makes Perl 5.16 complain. The right fix is to use the /aa modifier, 450 # but it's only understood since Perl 5.14. So the workaround is to write 451 # "clas[s]" instead of "class". Stupid and ugly hack, but it works with 452 # all Perl versions. 453 $$page =~ s/(?<=<td id="error_msg" clas[s]="throw_error">).*(?=<\/td>)/$new_error_msg/si; 454} 455 456sub flag_end_of_update { 457 my ($self, $args) = @_; 458 459 # This code doesn't actually *do* anything, it's just here to show you 460 # how to use this hook. 461 my $flag_params = $args; 462 my ($object, $timestamp, $old_flags, $new_flags) = 463 @$flag_params{qw(object timestamp old_flags new_flags)}; 464 my ($removed, $added) = diff_arrays($old_flags, $new_flags); 465 my ($granted, $denied) = (0, 0); 466 foreach my $new_flag (@$added) { 467 $granted++ if $new_flag =~ /\+$/; 468 $denied++ if $new_flag =~ /-$/; 469 } 470 my $bug_id = $object->isa('Bugzilla::Bug') ? $object->id 471 : $object->bug_id; 472 my $result = "$granted flags were granted and $denied flags were denied" 473 . " on bug $bug_id at $timestamp."; 474 # Uncomment this line to see $result in your webserver's error log whenever 475 # you update flags. 476 # warn $result; 477} 478 479sub group_before_delete { 480 my ($self, $args) = @_; 481 # This code doesn't actually *do* anything, it's just here to show you 482 # how to use this hook. 483 484 my $group = $args->{'group'}; 485 my $group_id = $group->id; 486 # Uncomment this line to see a line in your webserver's error log whenever 487 # you file a bug. 488 # warn "Group $group_id is about to be deleted!"; 489} 490 491sub group_end_of_create { 492 my ($self, $args) = @_; 493 # This code doesn't actually *do* anything, it's just here to show you 494 # how to use this hook. 495 my $group = $args->{'group'}; 496 497 my $group_id = $group->id; 498 # Uncomment this line to see a line in your webserver's error log whenever 499 # you create a new group. 500 #warn "Group $group_id has been created!"; 501} 502 503sub group_end_of_update { 504 my ($self, $args) = @_; 505 # This code doesn't actually *do* anything, it's just here to show you 506 # how to use this hook. 507 508 my ($group, $changes) = @$args{qw(group changes)}; 509 510 foreach my $field (keys %$changes) { 511 my $used_to_be = $changes->{$field}->[0]; 512 my $now_it_is = $changes->{$field}->[1]; 513 } 514 515 my $group_id = $group->id; 516 my $num_changes = scalar keys %$changes; 517 my $result = 518 "There were $num_changes changes to fields on group $group_id."; 519 # Uncomment this line to see $result in your webserver's error log whenever 520 # you update a group. 521 #warn $result; 522} 523 524sub install_before_final_checks { 525 my ($self, $args) = @_; 526 print "Install-before_final_checks hook\n" unless $args->{silent}; 527 528 # Add a new user setting like this: 529 # 530 # add_setting('product_chooser', # setting name 531 # ['pretty', 'full', 'small'], # options 532 # 'pretty'); # default 533 # 534 # To add descriptions for the setting and choices, add extra values to 535 # the hash defined in global/setting-descs.none.tmpl. Do this in a hook: 536 # hook/global/setting-descs-settings.none.tmpl . 537} 538 539sub install_filesystem { 540 my ($self, $args) = @_; 541 my $create_dirs = $args->{'create_dirs'}; 542 my $recurse_dirs = $args->{'recurse_dirs'}; 543 my $htaccess = $args->{'htaccess'}; 544 545 # Create a new directory in datadir specifically for this extension. 546 # The directory will need to allow files to be created by the extension 547 # code as well as allow the webserver to server content from it. 548 # my $data_path = bz_locations->{'datadir'} . "/" . __PACKAGE__->NAME; 549 # $create_dirs->{$data_path} = Bugzilla::Install::Filesystem::DIR_CGI_WRITE; 550 551 # Update the permissions of any files and directories that currently reside 552 # in the extension's directory. 553 # $recurse_dirs->{$data_path} = { 554 # files => Bugzilla::Install::Filesystem::CGI_READ, 555 # dirs => Bugzilla::Install::Filesystem::DIR_CGI_WRITE 556 # }; 557 558 # Create a htaccess file that allows specific content to be served from the 559 # extension's directory. 560 # $htaccess->{"$data_path/.htaccess"} = { 561 # perms => Bugzilla::Install::Filesystem::WS_SERVE, 562 # contents => Bugzilla::Install::Filesystem::HT_DEFAULT_DENY 563 # }; 564} 565 566sub install_update_db { 567 my $dbh = Bugzilla->dbh; 568# $dbh->bz_add_column('example', 'new_column', 569# {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0}); 570# $dbh->bz_add_index('example', 'example_new_column_idx', [qw(value)]); 571} 572 573sub install_update_db_fielddefs { 574 my $dbh = Bugzilla->dbh; 575# $dbh->bz_add_column('fielddefs', 'example_column', 576# {TYPE => 'MEDIUMTEXT', NOTNULL => 1, DEFAULT => ''}); 577} 578 579sub job_map { 580 my ($self, $args) = @_; 581 582 my $job_map = $args->{job_map}; 583 584 # This adds the named class (an instance of TheSchwartz::Worker) as a 585 # handler for when a job is added with the name "some_task". 586 $job_map->{'some_task'} = 'Bugzilla::Extension::Example::Job::SomeClass'; 587 588 # Schedule a job like this: 589 # my $queue = Bugzilla->job_queue(); 590 # $queue->insert('some_task', { some_parameter => $some_variable }); 591} 592 593sub mailer_before_send { 594 my ($self, $args) = @_; 595 596 my $email = $args->{email}; 597 # If you add a header to an email, it's best to start it with 598 # 'X-Bugzilla-<Extension>' so that you don't conflict with 599 # other extensions. 600 $email->header_set('X-Bugzilla-Example-Header', 'Example'); 601} 602 603sub object_before_create { 604 my ($self, $args) = @_; 605 606 my $class = $args->{'class'}; 607 my $object_params = $args->{'params'}; 608 609 # Note that this is a made-up class, for this example. 610 if ($class->isa('Bugzilla::ExampleObject')) { 611 warn "About to create an ExampleObject!"; 612 warn "Got the following parameters: " 613 . join(', ', keys(%$object_params)); 614 } 615} 616 617sub object_before_delete { 618 my ($self, $args) = @_; 619 620 my $object = $args->{'object'}; 621 622 # Note that this is a made-up class, for this example. 623 if ($object->isa('Bugzilla::ExampleObject')) { 624 my $id = $object->id; 625 warn "An object with id $id is about to be deleted!"; 626 } 627} 628 629sub object_before_set { 630 my ($self, $args) = @_; 631 632 my ($object, $field, $value) = @$args{qw(object field value)}; 633 634 # Note that this is a made-up class, for this example. 635 if ($object->isa('Bugzilla::ExampleObject')) { 636 warn "The field $field is changing from " . $object->{$field} 637 . " to $value!"; 638 } 639} 640 641sub object_columns { 642 my ($self, $args) = @_; 643 my ($class, $columns) = @$args{qw(class columns)}; 644 645 if ($class->isa('Bugzilla::ExampleObject')) { 646 push(@$columns, 'example'); 647 } 648} 649 650sub object_end_of_create { 651 my ($self, $args) = @_; 652 653 my $class = $args->{'class'}; 654 my $object = $args->{'object'}; 655 656 warn "Created a new $class object!"; 657} 658 659sub object_end_of_create_validators { 660 my ($self, $args) = @_; 661 662 my $class = $args->{'class'}; 663 my $object_params = $args->{'params'}; 664 665 # Note that this is a made-up class, for this example. 666 if ($class->isa('Bugzilla::ExampleObject')) { 667 # Always set example_field to 1, even if the validators said otherwise. 668 $object_params->{example_field} = 1; 669 } 670 671} 672 673sub object_end_of_set { 674 my ($self, $args) = @_; 675 676 my ($object, $field) = @$args{qw(object field)}; 677 678 # Note that this is a made-up class, for this example. 679 if ($object->isa('Bugzilla::ExampleObject')) { 680 warn "The field $field has changed to " . $object->{$field}; 681 } 682} 683 684sub object_end_of_set_all { 685 my ($self, $args) = @_; 686 687 my $object = $args->{'object'}; 688 my $object_params = $args->{'params'}; 689 690 # Note that this is a made-up class, for this example. 691 if ($object->isa('Bugzilla::ExampleObject')) { 692 if ($object_params->{example_field} == 1) { 693 $object->{example_field} = 1; 694 } 695 } 696 697} 698 699sub object_end_of_update { 700 my ($self, $args) = @_; 701 702 my ($object, $old_object, $changes) = 703 @$args{qw(object old_object changes)}; 704 705 # Note that this is a made-up class, for this example. 706 if ($object->isa('Bugzilla::ExampleObject')) { 707 if (defined $changes->{'name'}) { 708 my ($old, $new) = @{ $changes->{'name'} }; 709 print "The name field changed from $old to $new!"; 710 } 711 } 712} 713 714sub object_update_columns { 715 my ($self, $args) = @_; 716 my ($object, $columns) = @$args{qw(object columns)}; 717 718 if ($object->isa('Bugzilla::ExampleObject')) { 719 push(@$columns, 'example'); 720 } 721} 722 723sub object_validators { 724 my ($self, $args) = @_; 725 my ($class, $validators) = @$args{qw(class validators)}; 726 727 if ($class->isa('Bugzilla::Bug')) { 728 # This is an example of adding a new validator. 729 # See the _check_example subroutine below. 730 $validators->{example} = \&_check_example; 731 732 # This is an example of overriding an existing validator. 733 # See the check_short_desc validator below. 734 my $original = $validators->{short_desc}; 735 $validators->{short_desc} = sub { _check_short_desc($original, @_) }; 736 } 737} 738 739sub _check_example { 740 my ($invocant, $value, $field) = @_; 741 warn "I was called to validate the value of $field."; 742 warn "The value of $field that I was passed in is: $value"; 743 744 # Make the value always be 1. 745 my $fixed_value = 1; 746 return $fixed_value; 747} 748 749sub _check_short_desc { 750 my $original = shift; 751 my $invocant = shift; 752 my $value = $invocant->$original(@_); 753 if ($value !~ /example/i) { 754 # Use this line to make Bugzilla throw an error every time 755 # you try to file a bug or update a bug without the word "example" 756 # in the summary. 757 if (0) { 758 ThrowUserError('example_short_desc_invalid'); 759 } 760 } 761 return $value; 762} 763 764sub page_before_template { 765 my ($self, $args) = @_; 766 767 my ($vars, $page) = @$args{qw(vars page_id)}; 768 769 # You can see this hook in action by loading page.cgi?id=example.html 770 if ($page eq 'example.html') { 771 $vars->{cgi_variables} = { Bugzilla->cgi->Vars }; 772 } 773} 774 775sub path_info_whitelist { 776 my ($self, $args) = @_; 777 my $whitelist = $args->{whitelist}; 778 push(@$whitelist, "page.cgi"); 779} 780 781sub post_bug_after_creation { 782 my ($self, $args) = @_; 783 784 my $vars = $args->{vars}; 785 $vars->{'example'} = 1; 786} 787 788sub product_confirm_delete { 789 my ($self, $args) = @_; 790 791 my $vars = $args->{vars}; 792 $vars->{'example'} = 1; 793} 794 795 796sub product_end_of_create { 797 my ($self, $args) = @_; 798 799 my $product = $args->{product}; 800 801 # For this example, any lines of code that actually make changes to your 802 # database have been commented out. 803 804 # This section will take a group that exists in your installation 805 # (possible called test_group) and automatically makes the new 806 # product hidden to only members of the group. Just remove 807 # the restriction if you want the new product to be public. 808 809 my $example_group = new Bugzilla::Group({ name => 'example_group' }); 810 811 if ($example_group) { 812 $product->set_group_controls($example_group, 813 { entry => 1, 814 membercontrol => CONTROLMAPMANDATORY, 815 othercontrol => CONTROLMAPMANDATORY }); 816# $product->update(); 817 } 818 819 # This section will automatically add a default component 820 # to the new product called 'No Component'. 821 822 my $default_assignee = new Bugzilla::User( 823 { name => Bugzilla->params->{maintainer} }); 824 825 if ($default_assignee) { 826# Bugzilla::Component->create( 827# { name => 'No Component', 828# product => $product, 829# description => 'Select this component if one does not ' . 830# 'exist in the current list of components', 831# initialowner => $default_assignee }); 832 } 833} 834 835sub quicksearch_map { 836 my ($self, $args) = @_; 837 my $map = $args->{'map'}; 838 839 # This demonstrates adding a shorter alias for a long custom field name. 840 $map->{'impact'} = $map->{'cf_long_field_name_for_impact_field'}; 841} 842 843sub sanitycheck_check { 844 my ($self, $args) = @_; 845 846 my $dbh = Bugzilla->dbh; 847 my $sth; 848 849 my $status = $args->{'status'}; 850 851 # Check that all users are Australian 852 $status->('example_check_au_user'); 853 854 $sth = $dbh->prepare("SELECT userid, login_name 855 FROM profiles 856 WHERE login_name NOT LIKE '%.au'"); 857 $sth->execute; 858 859 my $seen_nonau = 0; 860 while (my ($userid, $login, $numgroups) = $sth->fetchrow_array) { 861 $status->('example_check_au_user_alert', 862 { userid => $userid, login => $login }, 863 'alert'); 864 $seen_nonau = 1; 865 } 866 867 $status->('example_check_au_user_prompt') if $seen_nonau; 868} 869 870sub sanitycheck_repair { 871 my ($self, $args) = @_; 872 873 my $cgi = Bugzilla->cgi; 874 my $dbh = Bugzilla->dbh; 875 876 my $status = $args->{'status'}; 877 878 if ($cgi->param('example_repair_au_user')) { 879 $status->('example_repair_au_user_start'); 880 881 #$dbh->do("UPDATE profiles 882 # SET login_name = CONCAT(login_name, '.au') 883 # WHERE login_name NOT LIKE '%.au'"); 884 885 $status->('example_repair_au_user_end'); 886 } 887} 888 889sub template_before_create { 890 my ($self, $args) = @_; 891 892 my $config = $args->{'config'}; 893 # This will be accessible as "example_global_variable" in every 894 # template in Bugzilla. See Bugzilla/Template.pm's create() function 895 # for more things that you can set. 896 $config->{VARIABLES}->{example_global_variable} = sub { return 'value' }; 897} 898 899sub template_before_process { 900 my ($self, $args) = @_; 901 902 my ($vars, $file, $context) = @$args{qw(vars file context)}; 903 904 if ($file eq 'bug/edit.html.tmpl') { 905 $vars->{'viewing_the_bug_form'} = 1; 906 } 907} 908 909sub user_preferences { 910 my ($self, $args) = @_; 911 my $tab = $args->{current_tab}; 912 my $save = $args->{save_changes}; 913 my $handled = $args->{handled}; 914 915 return unless $tab eq 'my_tab'; 916 917 my $value = Bugzilla->input_params->{'example_pref'}; 918 if ($save) { 919 # Validate your data and update the DB accordingly. 920 $value =~ s/\s+/:/g; 921 } 922 $args->{'vars'}->{example_pref} = $value; 923 924 # Set the 'handled' scalar reference to true so that the caller 925 # knows the panel name is valid and that an extension took care of it. 926 $$handled = 1; 927} 928 929sub webservice { 930 my ($self, $args) = @_; 931 932 my $dispatch = $args->{dispatch}; 933 $dispatch->{Example} = "Bugzilla::Extension::Example::WebService"; 934} 935 936sub webservice_error_codes { 937 my ($self, $args) = @_; 938 939 my $error_map = $args->{error_map}; 940 $error_map->{'example_my_error'} = 10001; 941} 942 943# This must be the last line of your extension. 944__PACKAGE__->NAME; 945