-
Notifications
You must be signed in to change notification settings - Fork 8
/
BugzillaQuery.php
1113 lines (1064 loc) · 37.8 KB
/
BugzillaQuery.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* A bugzilla query
*/
/**
* Copyright (C) 2008 - Ian Homer & bemoko
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses>.
*/
class BugzillaQuery extends BSQLQuery {
var $supportedParameters=array (
'alias' => 'field-id',
'assigned' => 'field-date',
'attachments' => 'field-number',
'bar' => 'column',
'bzalternateconfig' => 'free',
'bzurl' => 'value', # Show the url to the BZ query
'blocks' => 'field-depends',
'cc' => 'field',
'closed' => 'field-date',
'columns' => 'columns',
'created' => 'field-date',
'createdformat' => 'value', # date or relativedate
'component' => 'field',
'customfields' => 'value', # Comma separated list of custom fields
'customprefix' => 'cf_', # Prefix that all custom fields should start with
'deadline' => 'field-date',
'depends' => 'field-depends',
'dependsstatus' => 'field-join', # Status values to include in depends (and blocks) tasks
'detailsrow' => 'columns',
'detailsrowprepend' => 'free',
'disablecache' => 'boolean',
'estimated' => 'field-number',
'filters' => 'filters', # Generic filter setting which can be used for custom fields
'flag' => 'field-special',
'format' => 'value', # table (default), list, inline or count
'from' => 'field',
'group' => 'sort',
'groupformat' => 'value',
'grouporder' => 'value', # asc or desc
'hardware' => 'field',
'heading' => 'free',
'headers' => 'value',
'hide' => 'value',
'id' => 'field-id',
'implicitcustom' => 'boolean', # true => allow custom fields not explicitly defined and not
# starting with the custom field prefix
'instance' => 'value', # Alternative bugzilla instance as defined in
# LocalSettings configuration
'keywords' => 'field-keywords',
'link' => 'columns', # Define rules for linking headings and values
# through to wiki pages
'lastcomment' => 'boolean',
'maxrows' => 'value',
'maxrowsbar' => 'value',
'milestone' => 'field',
'modified' => 'field-date',
'modifiedformat' => 'value', # date or relativedate
'nameformat' => 'value', # real (default),tla or login
'order' => 'value',
'os' => 'field',
'priority' => 'field',
'product' => 'field',
'qa' => 'field',
'quickflag' => 'value',
'noresultsmessage' => 'free',
'remaining' => 'field-number',
'reopened' => 'field-date',
'resolution' => 'field',
'resolved' => 'field-date',
'search' => 'field-text',
'severity' => 'field',
'sort' => 'sort',
'sortable' => 'boolean', # Whether the table is sortable or not
'status' => 'field',
'status_whiteboard' => 'field',
'style' => 'free', # Define the CSS style to be applied to the table
'to' => 'field',
'total' => 'columns',
'url' => 'field',
'version' => 'field',
'verified' => 'field-date',
'votes' => 'field-number',
'work' => 'field-number',
'zeroasblank' => 'boolean' # Render "0" as blank, if false rendered as "0" (default=true)
);
var $defaultParameters=array (
'bzurl' => 'show',
'columns' => 'id,priority,status,severity,version,product,summary,url',
'customprefix' => 'cf_',
'dependsstatus' => '!(CLOSED,VERIFIED,RESOLVED)',
'format' => 'table',
'implicitcustom' => 'false', # Default to false since Bugzilla nowadays enforces custom
# fields to start with "cf_"
'noresultsmessage' => 'no bugzilla tickets were found',
'order' => 'asc',
'status' => '!CLOSED',
'sort' => 'priority,status',
'sortable' => '1',
'zeroasblank' => 'true'
);
public $columnName=array (
'alias' => 'Alias',
'assigned' => 'Assigned',
'attachments' => '@',
'blocks' => 'Blocks',
'closed' => 'Closed',
'component' => 'Component',
'cc' => 'CC',
'created' => 'Created',
'deadline' => 'Deadline',
'depends' => 'Depends',
'estimated' => 'E',
'flag' => 'Flagged For',
'flagdate' => 'Flag Date',
'flagfrom' => 'Flagged By',
'flagname' => 'Flag',
'from' => 'Requester',
'hardware' => 'Hardware',
'keyworddefs.name' => 'Keywords',
'id' => 'ID',
'milestone' => 'Milestone',
'modified' => 'Modified',
'os' => 'OS',
'product' => 'Product',
'priority' => 'P',
'qa' => 'QA',
'remaining' => 'R' ,
'reopened' => 'Reopened',
'resolution' => 'Resolution',
'resolved' => 'Resolved',
'severity' => 'Severity',
'status' => 'Status',
'summary' => 'Summary',
'to' => 'Assignee',
'url' => ' ',
'version' => 'Version',
'verified' => 'Verified',
'votes' => 'Votes',
'work' => 'W'
);
public $columnLabelName=array (
'estimated' => 'Estimated',
'priority' => 'Priority',
'remaining' => 'Remaining',
'work' => 'Work'
);
# Fields and their mapping to the value in the results sets
public $fieldMapping=array (
'cc' => 'cc',
'from' => 'raisedby',
'to' => 'assignedto',
);
var $fieldSQLColumn=array (
'assigned' => 'assignedactivity.bug_when',
'attachments' => 'attachments.nattachments',
'cc' => 'ccprofiles.login_name',
'component' => 'components.name',
'closed' => 'closedactivity.bug_when',
'created' => 'creation_ts',
'estimated' => 'estimated_time',
'hardware' => 'rep_platform',
'id' => 'bugs.bug_id',
'from' => 'reporterprofiles.login_name',
'keywords' => 'keyworddefs.name',
'milestone' => 'target_milestone',
'modified' => 'lastdiffed',
'product' => 'products.name',
'os' => 'op_sys',
'qa' => 'qaprofiles.login_name',
'remaining' => 'remaining_time',
'reopened' => 'reopenedactivity.bug_when',
'resolved' => 'resolvedactivity.bug_when',
'severity' => 'bug_severity',
'status' => 'bug_status',
'to' => 'profiles.login_name',
'url' => 'bug_file_loc',
'verified' => 'verifiedactivity.bug_when',
'work' => 'work_time'
);
# Bugzilla Query field names
var $fieldBZQuery=array (
'blocks' => 'blocked',
'hardware' => 'rep_platform',
'id' => 'bug_id',
'milestone' => 'target_milestone',
'os' => 'op_sys',
'qa' => 'qa_contact',
'severity' => 'bug_severity',
'status' => 'bug_status',
'to' => 'assigned_to'
);
var $fieldDefaultOrder=array (
'modified' => 'desc',
'votes' => 'desc'
);
public $formats=array (
'alias' => 'id',
'assigned' => 'date',
'blocks' => 'id',
'cc' => 'name',
'created' => 'date',
'closed' => 'date',
'deadline' => 'date',
'depends' => 'id',
'estimated' => 'number',
'flagdate' => 'date',
'flagfrom' => 'name',
'from' => 'name',
'id' => 'id',
'modified' => 'relativedate',
'qa' => 'name',
'remaining' => 'number',
'reopened' => 'date',
'resolved' => 'date',
'to' => 'name',
'url' => 'url',
'votes' => 'number',
'work' => 'number'
);
var $fieldValues=array (
'priority' => 'P1,P2,P3,P4,P5',
'status' => 'ASSIGNED,NEW,REOPENED,RESOLVED,VERIFIED,CLOSED',
'severity' => 'blocker,critical,major,normal,minor,trivial,enhancement'
);
var $sortMapping=array (
'deadline' => "COALESCE(deadline, '2100-01-01')",
'milestone' => "COALESCE(NULLIF(milestone,'---'),'XXXXX')",
'id' => "bugs.bug_id"
);
public $dependsRowColumns=array (
'depends' => "block",
'dependsto' => "title", # Output in the title
'dependsstatus' => "extra", # Output as greyed
'dependssummary' => "block",
);
public $blocksRowColumns=array (
'blocks' => "block",
'blocksto' => "title", # Output in the title
'blocksstatus' => "extra",
'blockssummary' => "block"
);
#
# Title for a given value rendering
#
public $valueTitle=array (
'alias' => "id,alias",
'blocks' => "blocks,blocksalias",
'depends' => "depends,dependsalias",
'id' => "id,alias"
);
private $supportedCustomFields=array(); # Supported custom fields
private $requiredCustomFields=array(); # Custom fields that are required for the report
private $bzFieldCount=0; # Field counter for BZ URL
private $customPrefixLength; # Cached length of custom prefix length
public $bzURL=""; # Bugzilla URL to run query
public $explitlyOneValue; # Set if report is explictly one value
public static $fieldIds=array();
public static $buglistServerRelativeUri="/buglist.cgi?";
#
# Parse in a context object which implements the following
#
# Public Variables
# - debug, bzserver, interwiki,
# - database, host, dbuser, password;
#
# Functions
# - debug
# - warn,
# - getErrorMessage
#
function BugzillaQuery( $connector ) {
$this->setConnector($connector);
$this->setContext($connector->getContext());
}
#
# Get rendering formats
#
function getFormats() {
return $this->formats;
}
#
# Get default priority
#
function getDefaultSort() {
return $this->defaultSort;
}
#
# Render the results
#
function render() {
$this->bzURL=$this->context->bzserver . BugzillaQuery::$buglistServerRelativeUri;
#
# Register supported custom fields
#
if ($this->get("customfields")) {
$this->supportedCustomFields=explode(",",$this->get("customfields"));
}
#
# Calculate the customPrefixLength once so that we reuse below
#
$this->customPrefixLength=strlen($this->get('customprefix'));
#
# Extract rules on how to link through headings and values to wiki pages
#
if ($this->get("link")) {
foreach (explode(",",$this->get("link")) as $linkedColumn) {
$parts=explode("~",$linkedColumn);
$format="link";
if (sizeof($parts) > 1) {
$format.="~".$parts[1];
}
$this->formats[$parts[0]]=$format;
$this->implictlyAddColumn($parts[0]);
}
}
#
# If lastcomment mode is selected then we require the keywords field and we
# will set those to link through to the appropriate wiki page
#
if ($this->get("lastcomment")) {
$this->requireField("keywords");
if (!array_key_exists("keywords",$this->formats)) {
$this->formats["keywords"]="link~keyword";
}
}
#
# Sorting does not work when grouping is enabled so we disable it
#
if ($this->get("group")) {
$this->set("sortable","0");
}
$db = $this->connector->connect();
$this->initFieldIds($db);
$sql = $this->getSQL();
if (!$db)
return $this->connector->getError();
$result = $this->connector->execute($sql,$db);
#
# Check that the record set is open
#
if ($result) {
if ($this->get('format') == "count") {
while ($line = $this->connector->fetch($result)) {
$output=$line["count"];
}
} else {
$this->overrideFormats();
$renderer=new BugzillaQueryRenderer($this);
if ($this->connector->getRowCount($result) > 0) {
$this->context->debug &&
$this->context->debug("Rendering results");
$output=$renderer->renderHTML($result);
} else {
$this->context->debug &&
$this->context->debug("No results to render");
#
# If total is set then we still want to render the result
# because we want the zero totals to show
#
if ($this->get('total')) {
$output=$renderer->renderHTML($result);
} else {
$output=$renderer->renderNoResultsHTML();
}
}
}
$this->context->debug &&
$this->context->debug("Freeing up db result");
$this->connector->free($result);
} else {
return $this->context->getErrorMessage('bReport_sqlerror',
$sql." ".$this->connector->getDbError($db));
}
$this->connector->close($db);
if ($this->context->debug) {
$output.="<div>SQL = ".$sql."</div>";
}
$this->context->debug &&
$this->context->debug(
"All done and returning page output : Number of characters in output = "
.strlen($output));
$this->context->debug==2 && $this->context->debug("Report HTML output is ".$output);
return $output;
}
/**
* Build the SQL for the query
*/
public function getSQL() {
$this->context->debug &&
$this->context->debug("Rendering BugzillaQuery");
$where="";
#
# Process fields and make sure we have SQL and implicit usage built up
#
foreach (array_keys($this->supportedParameters) as $column) {
$fieldValue=$this->get($column);
if ($fieldValue) {
$this->context->debug &&
$this->context
->debug("Handling field : $column : $fieldValue");
$type=$this->supportedParameters[$column];
#
# Support generic argument syntax
#
if ($type == 'filters') {
$args=split("%26",$fieldValue);
foreach ($args as $arg) {
$this->context
->debug("Processing filter : $arg");
#
# Match for encoded =
#
if (preg_match("/%3D/",$arg)) {
$parts=split("%3D",$arg);
$argColumn=$parts[0];
$argFieldValue=$parts[1];
if ($this->isCustomField($argColumn)) {
$argType="field";
$this->addCustomField($argColumn);
} else {
$argType=$this->supportedParameters[$argColumn];
}
if (substr($argType,0,5)!='field') {
$this->context
->warn("$argColumn is not of type field so ignoring");
} else {
$where.=$this->processField($argColumn,$argFieldValue,$argType);
}
} else {
$this->context
->warn("arg field not a valid format so ignoring " .
", it should include '%3D', it was ".$arg);
}
}
} else if ($type == 'field-keywords') {
$where.="and EXISTS (SELECT keywordid,keywords.bug_id from ".
$this->connector->getTable("keywords")." as keywords ".
" LEFT JOIN ".
$this->connector->getTable("keyworddefs")." as keyworddefs ".
" on keywords.keywordid=keyworddefs.id ".
" where keywords.bug_id=bugs.bug_id ".
$this->processField($column,$fieldValue,"field").
")";
} else {
$where.=$this->processField($column,$fieldValue,$type);
}
}
}
if ($this->get('format') == "list") {
$this->requireField("to");
$this->requireField("deadline");
}
if ($this->get('flag')) {
$this->requireField("flag");
$this->implictlyAddColumn("flagfrom");
$this->implictlyAddColumn("flagname");
$this->implictlyAddColumn("flagdate");
}
if ($this->get('lastcomment')) {
$this->requireField("lastcomment");
}
if ($this->get('search')) {
$where.=" and short_desc like '%".$this->get('search')."%'";
}
#
# Set implicit group order
#
if ($this->get('group') && array_key_exists($this->get('group'),
$this->fieldDefaultOrder)) {
$this->setImplicit('grouporder',$this->fieldDefaultOrder[
$this->get('group')]);
}
#
# Quick flag enabled by default
#
$this->requireField("quickflag");
#
# Alias enabled by default
#
$this->requireField("alias");
#
# Prepare the query;
#
$this->preSQLGenerate();
$this->context->debug &&
$this->context->debug("Columns required are "
.join(",",array_keys($this->fieldsRequired)));
$sql="";
if ($this->get('format') == "count") {
$sql.="SELECT count(distinct(id)) as count from (";
}
$sql.="SELECT DISTINCT bugs.bug_id as id";
if ($this->isRequired("alias")) {
$sql.=", aliases.alias as alias";
}
if ($this->isRequired("assigned")) {
$sql.=", assignedactivity.bug_when as assigned";
}
if ($this->isRequired("attachments")) {
$sql.=", attachments.nattachments as attachments ";
}
if ($this->isRequired("blocks")) {
$sql.=", blockstab.blocks as blocks, blockstab.blocksalias as blocksalias, blockstab.blockssummary as blockssummary,blockstab.blocksstatus as blocksstatus, blockstab.blockspriority as blockspriority, blockstab.realname as blocksto";
}
if ($this->isRequired("cc")) {
if ($this->get('nameformat')=='login') {
$sql.=", ccprofiles.login_name as cc";
} else {
$sql.=", ccprofiles.realname as cc";
}
}
if ($this->isRequired("closed")) {
$sql.=", closedactivity.bug_when as closed";
}
if ($this->isRequired("component")) {
$sql.=", components.name as component";
}
if ($this->isRequired("created")) {
$sql.=", creation_ts as created";
}
if ($this->isRequired("deadline")) {
$sql.=", deadline";
}
if ($this->isRequired("depends")) {
$sql.=", dependstab.depends as depends, dependstab.dependsalias as dependsalias, dependstab.dependssummary as dependssummary,dependstab.dependsstatus as dependsstatus, dependstab.dependspriority as dependspriority, dependstab.realname as dependsto";
}
if ($this->isRequired("flag")) {
if ($this->get('nameformat')=='login') {
$sql.=", flagprofiles.flagfrom_login as flagfrom";
$sql.=", flagprofiles.flag_login as flag";
} else {
$sql.=", flagprofiles.flagfrom_realname as flagfrom";
$sql.=", flagprofiles.flag_realname as flag";
}
$sql.=", flagprofiles.flagname as flagname";
$sql.=", flagprofiles.flagdate as flagdate";
} else if ($this->isRequired("quickflag")) {
$sql.=", quickflag.flagdate as flagdate";
}
if ($this->isRequired("estimated")) {
$sql.=", estimated_time as estimated";
}
if ($this->isRequired("from")) {
if ($this->get('nameformat')=='login') {
$sql.=", reporterprofiles.login_name as raisedby";
} else {
$sql.=", reporterprofiles.realname as raisedby";
}
}
if ($this->isRequired("hardware")) {
$sql.=", rep_platform as hardware";
}
if ($this->isRequired("keywords")) {
$sql.=", (SELECT group_concat(keyworddefs.name) FROM bugs.keyworddefs WHERE keyworddefs.id in (SELECT keywords.keywordid FROM bugs.keywords WHERE keywords.bug_id=bugs.bug_id )) as keywords";
}
if ($this->isRequired("milestone")) {
$sql.=", target_milestone as milestone";
}
if ($this->isRequired("lastcomment")) {
$sql.=", longdescslastcomment.thetext";
}
if ($this->isRequired("modified")) {
$sql.=", lastdiffed as modified";
}
if ($this->isRequired("os")) {
$sql.=", op_sys as os";
}
#
# Priority always required because it used as class name for bug row
#
$sql.=", priority";
if ($this->isRequired("product")) {
$sql.=", products.name as product";
}
if ($this->isRequired("qa")) {
if ($this->get('nameformat')=='login') {
$sql.=", qaprofiles.login_name as qa";
} else {
$sql.=", qaprofiles.realname as qa";
}
}
if ($this->isRequired("remaining")) {
$sql.=", remaining_time as remaining";
}
if ($this->isRequired("reopened")) {
$sql.=", reopenedactivity.bug_when as reopened";
}
if ($this->isRequired("resolution")) {
$sql.=", resolution";
}
if ($this->isRequired("resolved")) {
$sql.=", resolvedactivity.bug_when as resolved";
}
#
# Severity always required because it used as class name for bug row
#
$sql.=", bug_severity as severity";
if ($this->isRequired("status")) {
$sql.=", bug_status as status";
}
if ($this->isRequired("summary")) {
$sql.=", short_desc as summary";
}
if ($this->isRequired("to")) {
if ($this->get('nameformat')=='login') {
$sql.=", profiles.login_name as assignedto";
} else {
$sql.=", profiles.realname as assignedto";
}
}
if ($this->isRequired("url")) {
$sql.=", bug_file_loc as url";
}
if ($this->isRequired("version")) {
$sql.=", version";
}
if ($this->isRequired("votes")) {
$sql.=", votes";
}
if ($this->isRequired("work")) {
$sql.=", SUM(longdescswork.work_time) as work";
}
#
# Now add custom fields
#
$this->context->debug &&
$this->context->debug(sizeof($this->requiredCustomFields)." custom fields");
if (sizeof($this->requiredCustomFields) > 0) {
foreach($this->requiredCustomFields as $column) {
$sql.=", $column";
}
}
$sql.=" FROM ".$this->connector->getTable("bugs");
if ($this->isRequired("assigned")) {
$sql.=" LEFT JOIN ".
" (SELECT bug_id, MAX(bug_when) as bug_when from ".
$this->connector->getTable("bugs_activity").
" where fieldid=".BugzillaQuery::$fieldIds["bug_status"].
" and added='ASSIGNED' GROUP BY bug_id) as assignedactivity on bugs.bug_id=assignedactivity.bug_id";
}
if ($this->isRequired("attachments")) {
$sql.=" LEFT JOIN (SELECT bug_id as attachmentbugid, COUNT(attach_id) as nattachments from ".
$this->connector->getTable("attachments").
" group by attachmentbugid) as ".
"attachments on attachments.attachmentbugid=bugs.bug_id";
}
if ($this->isRequired("blocks")) {
$sql.=" LEFT JOIN (SELECT dependson,blocked as blocks, blockedalias.alias as blocksalias, blockedbugs.short_desc as blockssummary, blockedbugs.bug_status as blocksstatus, blockedbugs.priority as blockspriority,login_name,realname from ".
$this->connector->getTable("dependencies")
." INNER JOIN ".
$this->connector->getTable("bugs").
" as blockedbugs ON dependencies.blocked=blockedbugs.bug_id".
" LEFT JOIN ".
$this->connector->getTable("bugs_aliases").
" as blockedalias ON blockedalias.bug_id=blockedbugs.bug_id".
" INNER JOIN ".
$this->connector->getTable("profiles").
" ON blockedbugs.assigned_to=profiles.userid".
" where 1=1 ".$this->getWhereClause($this->get('dependsstatus'),"blockedbugs.bug_status").
" order by blockedbugs.priority".
") as blockstab ON blockstab.dependson=bugs.bug_id";
}
if ($this->isRequired("component")) {
$sql.=" LEFT JOIN ".
$this->connector->getTable("components").
" on bugs.component_id=components.id";
}
if ($this->isRequired("cc")) {
$sql.=" INNER JOIN (SELECT bug_id,login_name,realname from ".
$this->connector->getTable("cc").
" INNER JOIN ".
$this->connector->getTable("profiles").
" ON cc.who=profiles.userid";
if ($this->get('cc')) {
$sql.=$this
->getWhereClause($this->get('cc'),"profiles.login_name");
}
$sql.=") as ".
"ccprofiles on ccprofiles.bug_id=bugs.bug_id";
}
if ($this->isRequired("closed")) {
$sql.=" LEFT JOIN ".
" (SELECT bug_id, MAX(bug_when) as bug_when from ".
$this->connector->getTable("bugs_activity").
" where fieldid=".BugzillaQuery::$fieldIds["bug_status"].
" and added='CLOSED' GROUP BY bug_id) as closedactivity on bugs.bug_id=closedactivity.bug_id";
}
if ($this->isRequired("depends")) {
$sql.=" LEFT JOIN (SELECT blocked,dependson as depends, dependsonalias.alias as dependsalias, dependsonbugs.short_desc as dependssummary, dependsonbugs.bug_status as dependsstatus, dependsonbugs.priority as dependspriority, login_name, realname from ".
$this->connector->getTable("dependencies")
." INNER JOIN ".
$this->connector->getTable("bugs").
" as dependsonbugs ON dependencies.dependson=dependsonbugs.bug_id".
" LEFT JOIN ".
$this->connector->getTable("bugs_aliases").
" as dependsonalias ON dependsonalias.bug_id=dependsonbugs.bug_id".
" INNER JOIN ".
$this->connector->getTable("profiles").
" ON dependsonbugs.assigned_to=profiles.userid".
" where 1=1 ".$this->getWhereClause($this->get('dependsstatus'),"dependsonbugs.bug_status").
" order by dependsonbugs.priority".
") as dependstab ON dependstab.blocked=bugs.bug_id";
}
if ($this->isRequired("flag")) {
$sql.=" INNER JOIN (SELECT bug_id,creation_date as flagdate,flagsto.login_name as flag_login,flagsto.realname as flag_realname,flagsfrom.login_name as flagfrom_login, flagsfrom.realname as flagfrom_realname,flagtypes.name as flagname from ".
$this->connector->getTable("flags").
" INNER JOIN ".
$this->connector->getTable("flagtypes").
" ON flags.type_id=flagtypes.id INNER JOIN ".
$this->connector->getTable("profiles").
" as flagsto ON flags.requestee_id=flagsto.userid INNER JOIN ".
$this->connector->getTable("profiles").
" as flagsfrom ON flags.setter_id=flagsfrom.userid where status='?'";
if ($this->get('flag')) {
$sql.=$this
->getWhereClause($this->get('flag'),
"flagsto.login_name");
}
$sql.=") as ".
"flagprofiles on flagprofiles.bug_id=bugs.bug_id";
} else if ($this->isRequired("quickflag")) {
$sql.=" LEFT JOIN (SELECT bug_id as quickflagbugid, MAX(creation_date) as flagdate from ".
$this->connector->getTable("flags").
" where status='?' group by quickflagbugid) as ".
"quickflag on quickflag.quickflagbugid=bugs.bug_id";
}
if ($this->isRequired("from")) {
$sql.=" LEFT JOIN ".
$this->connector->getTable("profiles").
" as reporterprofiles on bugs.reporter=reporterprofiles.userid";
}
if ($this->isRequired("lastcomment")) {
$sql.=" LEFT JOIN (SELECT MAX(longdescs.bug_when) as sub_comment_when, ".
"longdescs.bug_id as sub_bug_id FROM ".
$this->connector->getTable("longdescs").
" GROUP BY longdescs.bug_id) ".
"descs ON bugs.bug_id=descs.sub_bug_id LEFT JOIN ".
$this->connector->getTable("longdescs")." as longdescslastcomment ON ".
"longdescslastcomment.bug_when=sub_comment_when";
}
if ($this->isRequired("product")) {
$sql.=" LEFT JOIN ".
$this->connector->getTable("products").
" on bugs.product_id=products.id";
}
if ($this->isRequired("qa")) {
$sql.=" LEFT JOIN ".
$this->connector->getTable("profiles").
" as qaprofiles on bugs.qa_contact=qaprofiles.userid";
}
if ($this->isRequired("reopened")) {
$sql.=" LEFT JOIN ".
" (SELECT bug_id, MAX(bug_when) as bug_when from ".
$this->connector->getTable("bugs_activity").
" where fieldid=".BugzillaQuery::$fieldIds["bug_status"].
" and added='REOPENED' GROUP BY bug_id) as reopenedactivity on bugs.bug_id=reopenedactivity.bug_id";
}
if ($this->isRequired("resolved")) {
$sql.=" LEFT JOIN ".
" (SELECT bug_id, MAX(bug_when) as bug_when from ".
$this->connector->getTable("bugs_activity").
" where fieldid=".BugzillaQuery::$fieldIds["bug_status"].
" and added='RESOLVED' GROUP BY bug_id) as resolvedactivity on bugs.bug_id=resolvedactivity.bug_id";
}
if ($this->isRequired("to")) {
$sql.=" LEFT JOIN ".
$this->connector->getTable("profiles").
" on bugs.assigned_to=profiles.userid";
}
if ($this->isRequired("verified")) {
$sql.=" LEFT JOIN ".
" (SELECT bug_id, MAX(bug_when) as bug_when from ".
$this->connector->getTable("bugs_activity").
" where fieldid=".BugzillaQuery::$fieldIds["bug_status"].
" and added='VERIFIED' GROUP BY bug_id) as verifiedactivity on bugs.bug_id=verifiedactivity.bug_id";
}
if ($this->isRequired("alias")) {
$sql.=" LEFT JOIN ".
$this->connector->getTable("bugs_aliases").
" as aliases ON aliases.bug_id=bugs.bug_id";
}
if ($this->isRequired("work")) {
$sql.=", ".$this->connector->getTable("longdescs")." as longdescswork";
}
$sql.=" where 1=1 ".$where;
if ($this->isRequired("work")) {
$sql.=" and longdescswork.bug_id=bugs.bug_id GROUP BY bugs.bug_id";
}
$sql.=" order by ".
$this->getMappedSort()." ".$this->getOrder();
if ($this->get('format') == "count") {
$sql.=") as b";
}
$sql.=";";
$this->context->debug && $this->context->debug("SQL : ".$sql);
return $sql;
}
#
# Process a field
#
public function processField($column,$fieldValue,$type) {
$where="";
switch ($type) {
case "field-id" :
if (!strpos($fieldValue,",")) {
$this->explitlyOneValue=true;
$this->context->debug &&
$this->context
->debug("Explicitly one value");
}
case "field" :
case "field-filter" :
case "field-date" :
case "field-number" :
if ($type!="field-id") {
#
# If field is multiple values ",", not a value
# "!", any value "*", a non-null (or
# positive value) "+", then add column and
# sort, otherwise remove it.
#
if (preg_match("/[,!+\*%<>]/",$fieldValue)) {
$this->implictlyAddColumn($column);
if (array_key_exists($column,
$this->fieldDefaultOrder)) {
$this->setImplicit('sort',"$column");
$this->setImplicit('order',
$this->fieldDefaultOrder[$column]);
}
} else {
$this->implictlyRemoveColumn($column);
}
}
case "field-depends" :
$sqlColumn=$column;
if (array_key_exists($column,
$this->fieldSQLColumn)) {
$sqlColumn=$this->fieldSQLColumn[$column];
}
switch ($type) {
case "field-id" :
case "field" :
case "field-depends" :
$where=$this->
getWhereClause($fieldValue,
$sqlColumn);
break;
case "field-number" :
$where=$this->
getIntWhereClause($fieldValue,
$sqlColumn);
break;
case "field-date" :
$where=$this->
getDateWhereClause($fieldValue,
$sqlColumn);
}
$this->requireField($column);
# Create the bugzilla query URL
$bzFieldName=$column;
if (array_key_exists($column,
$this->fieldBZQuery)) {
$bzFieldName=$this->fieldBZQuery[$column];
}
$this->bzURL.=
$this->getBZQuery($fieldValue,$bzFieldName);
break;
}
return $where;
}
public function getMatchExpression($match,$name,$negate) {
$trimmedMatch=trim($match);
$controlCharacter=substr($trimmedMatch,0,1);
$localNegate=$negate;
$range=0;
$this->context->debug &&
$this->context
->debug("Control character is ".$controlCharacter);
if (preg_match("/[!><]/",$controlCharacter)) {
$trimmedMatch=substr($trimmedMatch,1);
switch ($controlCharacter) {
case "!":
$localNegate=!$negate;
break;
case ">":
case "<":
$range=1;
break;
}
}
$decodedMatch=$this->safeSQLdecode($trimmedMatch);
if (preg_match("/%/",$match)) {
# We have a like clause
if ($localNegate) {
return $name." not like '".$decodedMatch."'";
} else {
return $name." like '".$decodedMatch."'";
}
} elseif ($range) {
if ($controlCharacter=="<"){
if ($localNegate) {
return $name." > '".$decodedMatch."'";
} else {
return $name." <'".$decodedMatch."'";
}
} else {
if ($localNegate) {
return $name." < '".$decodedMatch."'";
} else {
return $name." > '".$decodedMatch."'";
}
}
} else {
if ($localNegate) {
return $name."<>'".$decodedMatch."'";
} else {
return $name."='".$decodedMatch."'";
}
}
}
#
# Get the BZ Query URL
#
private function getBZQuery($value,$name) {
if (preg_match("/^[\*+-]/",$value)) {
#
# *,+ and - (i.e. any value, not null/not zero and null/zero)
# not supported in Bugzilla queries
#
return "";
}
#
# Replace spaces with "%20" to make it a safe URL
#
$safeValue=str_replace(" ","%20",$value);
$query="";
$bzFieldGroupCount=0;
$pos=strpos($safeValue,"!(");
$operator;
$negate;
if ($pos===false) {
$pos=strpos($safeValue,"!");
if ($pos===FALSE) {
$operator="OR";
$negate=false;
} else {