aboutsummaryrefslogtreecommitdiffstats
path: root/data/lib/collectingMetadata.js
blob: efd9754a0f21b7e144c320ef7f99315f28158c3e (plain) (blame)
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
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
"use strict";

function Comment(comment) {
    var nameSpan = comment.querySelector(".bz_comment_user a.email");
    var timeSpan = comment.getElementsByClassName("bz_comment_time")[0];

    this.author = parseMailto(nameSpan).trim();
    this.element = comment;
    this.date = parseBZCommentDate(timeSpan.textContent.trim());
    this.timeSpan = timeSpan;
}


function CommentList(doc) {
  var comments = document.getElementById("comments").
    getElementsByClassName("bz_comment");
  comments = [];
  Array.forEach(comments, function(item) {
    var com = new Comment(item);
    if (com.element) {
      comments[ISODateString(com.date)] = com;
    }
  });
  this.comments = comments;
}

/**
 * Set background color of all comments made by reporter in ReporterColor color
 *
 */
CommentList.prototype.colorComments = function colorComments() {
  var reporter = getReporter();
  var reporterComments = this.comments.filter(function (com) {
    return com.author === reporter;
  });
  reporterComments.forEach(function (com) {
    com.element.style.backgroundColor = ReporterColor.toString();
  });
}

CommentList.prototype.getAllCommentsText =  function getAllCommentsText() {
  return this.comments.reduce(function (outStr, com) {
    outStr += com.getElementsByTagName("pre")[0].textContent + "\n";
  }).trim();
}

// -----------------------------------------------------------

/**
 * Parse the row with the attachment and create new Attachment object
 *
 * @param DOM
 *          element to be parsed
 *
 * TODO error handling is missing ... it's bleee
 *
 * [ attName, id, MIMEtype, size, inElem ];
 */
function Attachment(inElem) {
  // Skip over obsolete attachments
  if (inElem.getElementsByClassName("bz_obsolete").length > 0) {
    return; // FIXME how NOT to create an object?
  }

  // getting name of the attachment
  this.name = inElem.getElementsByTagName("b")[0].textContent.trim();

  // TODO probably could use url.URL object
  var aHrefsArr = inElem.getElementsByTagName("a");
  var aHref = Array.filter(aHrefsArr, function(x) {
    return x.textContent.trim() === "Details";
  })[0];
  this.id = parseURL(aHref.getAttribute("href")).params.id;

  // getting MIME type and size
  var stringArray = inElem.getElementsByClassName("bz_attach_extra_info")[0].
    textContent.replace(/[\n ()]+/g, " ").trim().split(", ");
  this.size = parseInt(stringArray[0], 10);
  this.mimeType = stringArray[1].split(" ")[0];
  this.element = inElem;
};

Attachment.prototype.isBadMIME = function isBadMIME() {
  var badMIMEArray = [ "application/octet-stream", "text/x-log", "undefined" ];
  return isInList(this.mimeType, badMIMEArray);
};

Attachment.prototype.checkXorgLink = function checkXorgLink() {
  var elemS = this.element.getElementsByTagName("td");
  var elem = elemS[elemS.length - 1];
  createDeadLink("xorgLogAnalyzeLink", "check", elem,
      analyzeXorgLog, [this.id, "AnalyzeXorgLogBacktrace"], "br");
};

Attachment.prototype.isParsed = function isParsed() {
  var titleParsedAttachment = "Part of the thread where crash happened";
  return (new RegExp(titleParsedAttachment).test(this.name));
};

// ----------------------------------------------------------------------------
function AttachList(doc) {
  this.attachments = [];
  var attach = {};
  var attElements = doc.getElementById("attachment_table").
    getElementsByTagName("tr");
  // FIXME change into list of objects and both comments and
  // attachments (and something else?) should be properties of one
  // huge object
  for ( var i = 1, ii = attElements.length - 1; i < ii; i++) {
    attach = new Attachment(attElements[i]);
    if (attach.id) {
      this.attachments.push(attach);
    }
  }
}

AttachList.prototype.getBadAttachments = function getBadAttachments() {
  return this.attachments.filter(function(att) {
    return (att.isBadMIME());
  });
}

/**
 * Add a link opening selected lines of Xorg.0.log
 */
AttachList.prototype.addCheckXorgLogLink = function addCheckXorgLogLink() {
  if (config.XorgLogAnalysis) {
    this.attachments.forEach(function (att) {
      att.checkXorgLink();
    });
  }
}

/**
 * Make it sailent that the some attachments with bad MIME type are present
 *
 * @param atts
 *          Array of attachments subarrays
 * @return none
 */
AttachList.prototype.markBadAttachments = function markBadAttachments() {
  if (!constantData.passwordState.passAvailable) {
    console.log("markBadAttachments : No password, no XML-RPC calls; sorry");
    return null;
  }

  var badAttachments = this.getBadAttachments();

  if (badAttachments.length > 0) {
    var titleElement = document.
      getElementsByClassName("bz_alias_short_desc_container")[0];
    titleElement.style.backgroundColor = "olive";

    createDeadLink("fixAllButton", "Fix all", titleElement, function() {
      Array.forEach(badAttachments, function(x) {
        fixAttachById(x[1], constantData.XMLRPCData[window.location.hostname].url);
      });
    }, [], false, null, "f");
    badAttachments.forEach(function(x, i, a) {
      addTextLink(x, constantData.XMLRPCData[window.location.hostname].url);
    });
  }
};

AttachList.prototype.getParsedAttachments = function getParsedAttachments() {
  return this.attachments.filter(function (att) {
    return (att.isParsed());
  });
};

AttachList.prototype.getXorgList = function getXorgList() {
  return this.attachments.filter(function (value) {
    // Xorg.0.log must be text, otherwise we cannot parse it
    return (/[xX].*log/.test(value.name) && /text/.test(value.mimeType));
  });
};

AttachList.prototype.forEach = function forEach(fce) {
  this.attachments.forEach(fce);
};