aboutsummaryrefslogtreecommitdiffstats
path: root/quicksearch.js
blob: e4c21868b03271be21d4a7d3a6602993bc356af5 (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
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
//
// This is the main JS file for QuickSearch.
//
// Derived from:
//
//   * C. Begle's SimpleSearch tool:
//     http://www.mozilla.org/quality/help/simplesearch.html
//     http://www.mozilla.org/quality/help/bugreport.js 
//
//   * Jesse Ruderman's bugzilla search page:
//     http://www.cs.hmc.edu/~jruderma/s/bugz.html
//
// Created by
//     Andreas Franke <afranke@mathweb.org>

function go_to (url) {
  document.location.href = url;
  //window.open(url, "other" );
}

function map(l, f) {
    l1 = new Array();
    for (var i=0; i<l.length; i++) {
        l1[i] = f(l[i]);
    }
    return l1;
}

function isPrefix(s1, s2) {
    return (s1.length <= s2.length) &&
           (s1 == s2.substring(0,s1.length))
}

function member(s, l) {
    return (l.length > 0)
    && ((s == l[0]) || member(s, l.slice(1)));
}

function add(s, l) {
    if (! member(s, l)) {
        l.unshift(s);
    }
}

function addAll(l1, l2) {
    for (var i=0; i<l1.length; i++) {
        add(l1[i],l2);
    }        
}

function isSubset (l1, l2) {
    return (l1.length == 0)
    || (member(l1[0],l2) && subset(l1.slice(1),l2));
}

// fields

var f1 = new Array();
var f2 = new Array();

function add_mapping(from,to) {
    f1[f1.length] = from;
    f2[f2.length] = to;
}

// Status, Resolution, Platform, OS, Priority, Severity
add_mapping("status",             "bug_status");
add_mapping("resolution",         "resolution");  // no change
add_mapping("platform",           "rep_platform");
add_mapping("os",                 "op_sys");
add_mapping("opsys",              "op_sys");
add_mapping("priority",           "priority");    // no change
add_mapping("pri",                "priority");
add_mapping("severity",           "bug_severity");
add_mapping("sev",                "bug_severity");
// People: AssignedTo, Reporter, QA Contact, CC, Added comment (?)
add_mapping("owner",              "assigned_to");
add_mapping("assignee",           "assigned_to");
add_mapping("assignedto",         "assigned_to");
add_mapping("reporter",           "reporter");    // no change
add_mapping("rep",                "reporter");
add_mapping("qa",                 "qa_contact"); 
add_mapping("qacontact",          "qa_contact");
add_mapping("cc",                 "cc");          // no change
// Product, Version, Component, Target Milestone
add_mapping("product",            "product");     // no change
add_mapping("prod",               "product");
add_mapping("version",            "version");     // no change
add_mapping("ver",                "version");
add_mapping("component",          "component");   // no change
add_mapping("comp",               "component");
add_mapping("milestone",          "target_milestone");
add_mapping("target",             "target_milestone");
add_mapping("targetmilestone",    "target_milestone");
// Summary, Description, URL, Status whiteboard, Keywords
add_mapping("summary",            "short_desc");
add_mapping("shortdesc",          "short_desc");
add_mapping("desc",               "longdesc");
add_mapping("description",        "longdesc");
//add_mapping("comment",          "longdesc");    // ???
          // reserve "comment" for "added comment" email search?
add_mapping("longdesc",           "longdesc");
add_mapping("url",                "bug_file_loc");
add_mapping("whiteboard",         "status_whiteboard");
add_mapping("statuswhiteboard",   "status_whiteboard");
add_mapping("sw",                 "status_whiteboard");
add_mapping("keywords",           "keywords");    // no change
add_mapping("kw",                 "keywords");
// Attachments
add_mapping("attachment",         "attachments.description");
add_mapping("attachmentdesc",     "attachments.description");
add_mapping("attachdesc",         "attachments.description");
add_mapping("attachmentdata",     "attachments.thedata");
add_mapping("attachdata",         "attachments.thedata");
add_mapping("attachmentmimetype", "attachments.mimetype");
add_mapping("attachmimetype",     "attachments.mimetype");

// disabled because of bug 30823:
// "BugsThisDependsOn"       --> "dependson"
// "OtherBugsDependingOnThis"--> "blocked"
//add_mapping("dependson",        "dependson"); 
//add_mapping("blocked",          "blocked");

// Substring search doesn't make much sense for the following fields: 
// "Attachment is patch"    --> "attachments.ispatch"
// "Last changed date"      --> "delta_ts"
// "Days since bug changed" --> "(to_days(now()) - to_days(bugs.delta_ts))"
//"groupset"
//"everconfirmed"
//"bug","bugid","bugno"     --> "bug_id"
// "votes"                  --> "votes"
//     "votes>5", "votes>=5", "votes=>5" works now, see below
//     "votes:5" is interpreted as "votes>=5"

function findIndex(array,value) {
    for (var i=0; i<array.length; i++)
        if (array[i] == value) return i;
    return -1;
}

function mapField(fieldname) {
    var i = findIndex(f1,fieldname);
    if (i >= 0) return f2[i];
    return undefined;
} 

// `keywords' is defined externally
 
function is_keyword(s) {
    return member(s, keywords);
}

// `platforms' is defined externally

function is_platform(str) {
    return member (str.toLowerCase(),platforms);
}

// `severities' is defined externally

function is_severity(str) {
    return member(str.toLowerCase(),severities);
}

// `product_exceptions' is defined externally

function match_product(str) {
    s = str.toLowerCase();
    return (s.length > 2) && (! member(s,product_exceptions));
}

// `component_exceptions are defined externally

function match_component(str) {
    s = str.toLowerCase();
    return (s.length > 2) && (! member(s,component_exceptions));
}

var status_and_resolution = ""; // for pretty debug output only; these vars
var charts = "";                // always hold the data from the last query

// derived from http://www.mozilla.org/quality/help/bugreport.js

function make_chart(expr, field, type, value) {
    charts += "<tr>" +
              "<td><tt>" + expr + "</tt></td>" + 
              "<td><tt>" + field + "</tt></td>" + 
              "<td><tt>" + type + "</tt></td>" + 
              "<td><tt>" + value + "</tt></td>" +
              "</tr>";
    return "&field" + expr + "=" + field +
           "&type"  + expr + "=" + type  +
           "&value" + expr + "=" + escape(value).replace(/[+]/g,"%2B");
}

// returns true if at least one of comparelist had the prefix, false otherwise
function addPrefixMatches(prefix, comparelist, resultlist) {
    var foundMatch = false;
    for (var i=0; i<comparelist.length; i++) {
        if (isPrefix(prefix,comparelist[i])) {
            foundMatch = true;
            add(comparelist[i],resultlist);
        }
    }
    return foundMatch;
}

function prefixesNotFoundError(prefixes,statusValues,resolutionValues) {
    var txt;
    if (prefixes.length == 1) {
        txt = "is not a prefix ";
    } else {
        txt = "are not prefixes ";
    }
    alert(prefixes + "\n" + txt + 
          "of one of these status or resolution values:\n" +
          statusValues + "\n" + resolutionValues + "\n");
}

function make_query_URL(url, input, searchLong) {

    status_and_resolution = "";
    charts = "";

    var searchURL = url; //bugzilla + "buglist.cgi";
    var abort = false;
    
    // escape everything between quotes: "foo bar" --> "foo%20bar"
    var parts = input.split('"');
    if ((parts.length % 2) != 1) {
        alert('Unterminated quote');
        abort = true;
        return undefined;      
    }
    for (var i=1; i<parts.length; i+=2) {
        parts[i] = escape(parts[i]);
    }
    var input2 = parts.join('"');

    // abort if there are still brackets
    if (input2.match(/[(]|[\)]/)) {
        alert('Brackets (...) are not supported.\n' + 
              'Use quotes "..." for values that contain special characters.');
        abort = true;
        return undefined;
    }

    // translate " AND "," OR "," NOT " to space,comma,dash
    input2 = input2.replace(/[\s]+AND[\s]+/g," ");
    input2 = input2.replace(/[\s]+OR[\s]+/g,"|");
    input2 = input2.replace(/[\s]+NOT[\s]+/g," -");

    // now split into words at space positions
    var word = input2.split(/[\s]+/);

    // determine bug_status and resolution 
    // the first word may contain relevant info

    // `statuses_open' and `statuses_resolved' are defined externally
    var statusOpen     = statuses_open;
    var statusResolved = statuses_resolved;
    var statusAll      = statusOpen.concat(statusResolved);

    // `resolutions' is defined externally
    var bug_status = statusOpen.slice().reverse(); //reverse is just cosmetic
    var resolution = new Array();

    // This function matches the given prefixes against the given statuses and
    // resolutions. Matched statuses are added to bug_status, matched 
    // resolutions are added to resolution. Returns true iff some matches 
    // were found for at least one of the given prefixes.
    function matchPrefixes(prefixes,statuses,resolutions) {
        var failedPrefixes = new Array();
        var foundMatch = false;
        for (var j=0; j<prefixes.length; j++) {
            var ok1 = addPrefixMatches(prefixes[j],statuses,bug_status);
            var ok2 = addPrefixMatches(prefixes[j],resolutions,resolution);
            if ((! ok1) && (! ok2)) {
                add(prefixes[j],failedPrefixes);
            } else {
                foundMatch = true;
            }
        }
        //report an error if some (but not all) prefixes didn't match anything
        if (foundMatch && (failedPrefixes.length > 0)) {
            prefixesNotFoundError(failedPrefixes,statuses,resolutions);
            abort = true;
        }
        return foundMatch;
    }
    
    if (word[0] == "ALL") {
        // special case: search for bugs regardless of status
        addAll(statusResolved,bug_status);
        word.shift();
    } else if (word[0] == "OPEN") {
        // special case: search for open bugs only
        word.shift();
    } else if (word[0].match("^[+][A-Z]+(,[A-Z]+)*$")) {
        // e.g. +DUP,FIX 
        w0 = word.shift();
        prefixes = w0.substring(1).split(",");
        if (! matchPrefixes(prefixes,statusResolved,resolutions)) {
            word.unshift(w0);
        }
    } else if (word[0].match("^[A-Z]+(,[A-Z]+)*$")) {
        // e.g. NEW,ASSI,REOP,FIX
        bug_status = new Array(); // reset
        w0 = word.shift();        
        prefixes = w0.split(",");
        if (! matchPrefixes(prefixes,statusAll,resolutions)) {
            word.unshift(w0);
            bug_status = statusOpen.reverse(); //reset to default bug_status
        }
    } else {
        // default case: 
        // search for unresolved bugs only
        // uncomment this to include duplicate bugs in the search
        // add("DUPLICATE",resolution);
    }
    if (resolution.length > 0) {
        resolution = resolution.reverse();
        resolution.unshift("---");
        addAll(statusResolved,bug_status);
    }
    bug_status = bug_status.reverse();
    bug_status = map(bug_status,escape);
    searchURL += "?bug_status=" +  bug_status.join("&bug_status=");
    status_and_resolution += 'Status: <tt>'+bug_status+'</tt>';

    if (resolution.length > 0) {
        resolution = map(resolution,escape);
        searchURL += "&resolution=" + resolution.join("&resolution=");
        status_and_resolution += '<br>'+'Resolution: <tt>'+resolution+'</tt>';
    }
                              
    // end of bug_status & resolution stuff

    var chart = 0;
    var and   = 0;
    var or    = 0;

    var negation = false;

    function negate_comparison_type(type) {
        switch(type) {
            case "substring": return "notsubstring";
            case "anywords":  return "nowords";
            case "regexp":    return "notregexp";
            default:
                // e.g. "greaterthan" 
                alert("Can't negate comparison type: `" + type + "'");
                abort = true;
        }
    }

    function add_chart(field,type,value) {
        // undo escaping for value: '"foo%20bar"' --> 'foo bar'
        var parts = value.split('"');
        if ((parts.length % 2) != 1) {
            alert('Internal error: unescaping failure');
            abort = true;
        }
        for (var i=1; i<parts.length; i+=2) {
            parts[i] = unescape(parts[i]);
        }
        var value2 = parts.join('');

        // negate type if negation is set
        var type2 = type;
        if (negation) {
            type2 = negate_comparison_type(type2);
        }
        searchURL += make_chart(chart+"-"+and+"-"+or,field,type2,value2);
        or++;
        if (negation) {
            and++;
            or=0;
        }
    }

    for (var i=0; i<word.length; i++, chart++) {

        w = word[i];
        
        negation = false;
        if (w[0] == "-") {
            negation = true;
            w = w.substring(1);
        }

        switch (w[0]) {
            case "+":
                alternative = w.substring(1).split(/[|,]/);
                for (var j=0; j<alternative.length; j++)
                    add_chart("short_desc","substring",alternative[j]);
                break;
            case "#":
                alternative = w.substring(1).replace(/[|,]/g," ");
                add_chart("short_desc","anywords",alternative);
                if (searchLong)
                    add_chart("longdesc","anywords",alternative);
                break;
            case ":":
                alternative = w.substring(1).split(",");
                for ( var j=0; j<alternative.length; j++) {
                    add_chart("product","substring",alternative[j]);
                    add_chart("component","substring",alternative[j]);
                }
                break;
            case "@":
                alternative = w.substring(1).split(",");
                for ( var j=0; j<alternative.length; j++)
                    add_chart("assigned_to","substring",alternative[j]);
                break;
            case "[":
                add_chart("short_desc","substring",w);
                add_chart("status_whiteboard","substring",w);
                break;
            case "!":
                add_chart("keywords","anywords",w.substring(1));
                break;
            default:
                alternative=w.split("|");
                for (var j=0; j<alternative.length; j++) {

                    w=alternative[j];

                    // votes:xx ("at least xx votes")
                    if (w.match("^votes[:][0-9]+$")) {
                        n = w.split(/[:]/)[1];
                        add_chart("votes","greaterthan",String(n-1));
                        continue;
                    }
                    // generic field1,field2,field3:value1,value2 notation
                    if (w.match("^[^:]+[:][^:\/][^:]*$")) {
                        parts = w.split(":");
                        fields = parts[0].split(/[,]+/);
                        values = parts[1].split(/[,]+/);
                        for (var k=0; k<fields.length; k++) {
                            field = mapField(fields[k]);
                            if (field == undefined) {
                                alert("`"+fields[k]+"'"+
                                      " is not a valid field name.");
                                abort = true;
                                return undefined;
                            } else {
                                 for (var l=0; l<values.length; l++) {
                                     add_chart(field,"substring",values[l]);
                                 }
                            }  
                        }
                        continue;
                    }
                    comma_separated_words=w.split(/[,]+/);
                    for (var k=0; k<comma_separated_words.length; k++) {
                        w=comma_separated_words[k];

                        // platform
                        if (is_platform(w)) {
                            add_chart("rep_platform","substring",w);
                            continue;
                        }
                        // priority
                        if (w.match("^[pP][1-5](,[pP]?[1-5])*$")) {
                            expr = "["+w.replace(/[p,]/g,"")+"]";
                            add_chart("priority","regexp",expr);
                            continue;
                        }
                        if (w.match("^[pP][1-5]-[1-5]$")) {
                            expr = "["+w.substring(1)+"]";
                            add_chart("priority","regexp",expr);
                            continue;
                        }
                        // severity
                        if (is_severity(w)) {
                            add_chart("bug_severity","substring",w);
                            continue;
                        }
                        // votes>xx
                        if (w.match("^votes>[0-9]+$")) {
                            n = w.split(">")[1];
                            add_chart("votes","greaterthan",n);
                            continue;
                        }
                        // votes>=xx, votes=>xx
                        if (w.match("^votes(>=|=>)[0-9]+$")) {
                            separator = w.match("^votes(>=|=>)[0-9]+$")[1];
                            n = w.split(separator)[1];
                            add_chart("votes","greaterthan",String(n-1));
                            continue;
                        }
                        // really default case
                        if (match_product(w)) {
                            add_chart("product","substring",w);
                        }
                        if (match_component(w)) {
                            add_chart("component","substring",w);
                        }
                        if (is_keyword(w)) {
                            add_chart("keywords","substring",w);
                            if (w.length > 2) {
                                add_chart("short_desc","substring",w);
                                add_chart("status_whiteboard","substring",w);
                            }
                        } else {
                            add_chart("short_desc","substring",w);
                            add_chart("status_whiteboard","substring",w);
                        }
                        if (searchLong)
                            add_chart("longdesc","substring",w);
                 
                        // URL field (for IP addrs, host.names, scheme://urls)
                        if (w.match(/[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+/)
                           || w.match(/^[A-Za-z]+([.][A-Za-z]+)+/)
                           || w.match(/[:][\/][\/]/)
                           || w.match(/localhost/)
                           || w.match(/mailto[:]?/)
                           // || w.match(/[A-Za-z]+[:][0-9]+/) //host:port
                           )
                            add_chart("bug_file_loc","substring",w);
                    }
                }
        }
        and = 0;
        or = 0;
    }

    //searchURL += "&cmdtype=doit";

    if (abort == false) {
        return searchURL;
    } else {
        return undefined;
    }
}

function unique_id () {
    return (new Date()).getTime();
}

function ShowURL(mode) {
    var input = document.f.id.value;
    var searchURL = make_query_URL(bugzilla+"buglist.cgi", input, false);
    if (searchURL != undefined) {
        var pieces = searchURL.replace(/[\?]/g,"\n?").replace(/[\&]/g,"\n&");
        if (mode == "alert") {
            alert(pieces);
        } else {
            var table = "<table border=1>" + 
                          "<thead>" + 
                            "<tr>" + 
                              "<th>Chart-And-Or</th>" + 
                              "<th>Field</th>" + 
                              "<th>Type</th>" + 
                              "<th>Value</th>" + 
                            "</tr>" + 
                          "</thead>" + 
                          "<tbody>" + charts + "</tbody>" +
                        "</table>";
            var html = '<html>' + 
                         '<head>' + 
                           '<title>' + input + '</title>' +
                         '</head>' +
                         '<body>' + 
                           '<a href="' + searchURL + '">' +
                             'Submit Query' +
                           '</a>' +
                           '<p>' + status_and_resolution + 
                           '<p>' + table + 
                           '<pre>' +
                             pieces.replace(/[\n]/g,"<br>") +
                           '</pre>' +  
                         '</body>' +
                       '</html>';
            var w = window.open("","preview_"+unique_id());
            w.document.write(html);
            w.document.close();
        }
    }
}

//
// new interface: 
// searchLong is a boolean now (not a checkbox/radiobutton)
//
function Search(url, input, searchLong) {
    var inputstring = new String(input);
    var word = inputstring.split(/[\s]+/);
  
    // Check for empty input
    if ( word.length == 1 && word[0] == "" )
        return;
    
    // Check for potential Bugzilla-busting intensive queries
    if ((searchLong!=false) && word.length > 4) {  
        var message = "Searching Descriptions for more than four words " +
                      "will take a very long time indeed. Please choose " +
                      "no more than four keywords for your query.";
        alert(message);
        return;
    }
    var searchURL = make_query_URL(url, inputstring, searchLong);
    if (searchURL != undefined) {
        go_to(searchURL);
         //window.open(searchURL, "other" );
    } else {
        return;
    }
}

//
// original interface, untested
//
//function SearchForBugs (input, searchRadio) {
//    if (searchRadio[0].checked) {
//        return Search(bugzilla + "buglist.cgi", input, false);
//    } else {
//        return Search(bugzilla + "buglist.cgi", input, true);
//    }
//}

// derived from http://www.cs.hmc.edu/~jruderma/s/bugz.html

// QuickSearch combines lookup-by-bug-number and search
// in a single textbox. It's name must be document.f.id .
//
// type nothing:
//    --> go to bugzilla front page
// type a number:
//    --> go to that bug number
// type several numbers, separated by commas:
//    --> go to a buglist of just those bug numbers
// type anything else:
//    --> search summary, product, component, keywords, status whiteboard
//        (and URL if it's an IP address, a host.name, or an absolute://URL)

function QuickSearch ()
{
    var input = document.f.id.value;

    if (input == "") 
    {
        //once this _is_ on http://bugzilla.mozilla.org, it should just return;
        go_to(bugzilla);
    } 
    else if (input.match(/^[0-9, ]*$/)) 
    {
        if (input.indexOf(",") == -1) {
            // only _one_ bug number --> show_bug
            go_to(bugzilla+"show_bug.cgi?id="+escape(input));
        } else {
            // comma-separated bug numbers --> buglist
            go_to(bugzilla+"buglist.cgi?bug_id="+escape(input)
                  + "&bugidtype=include&order=bugs.bug_id");
        }
    }
    else
    {
        Search(bugzilla+"buglist.cgi",input,false);
    }
    return;
}

function LoadQuery() {
    var input = document.f.id.value;
    Search(bugzilla+"query.cgi",input,false);
    return;
}