~davewalker/etherpad/ubuntu-unlimited-max-users-and-revisions

« back to all changes in this revision

Viewing changes to etherpad/src/plugins/twitterStyleTags/hooks.js

  • Committer: James Page
  • Date: 2011-04-13 08:00:43 UTC
  • Revision ID: james.page@canonical.com-20110413080043-eee2nq7y1v7cv2mp
* Refactoring to use native Ubuntu Java libraries. 
* debian/control:
  - use openjdk instead of sun's java
  - update maintainer
* debian/etherpad.init.orig, debian/etherpad.upstart:
  - move the init script out of the way
  - create a basic upstart script
  - note that the open office document conversion daemon was dropped
    from the upstart configuration; if this behavior is desired, please
    create a separate upstart job for it
* debian/rules:
  - just use basic dh_installinit, as it will pick up the new upstart job
* New release
* Changed maintainer to Packaging
* Fixed installation scripts
* Initial Release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import("etherpad.log");
 
2
import("dispatch.{Dispatcher,PrefixMatcher,forward}");
 
3
import("plugins.twitterStyleTags.models.tagQuery");
 
4
import("sqlbase.sqlobj");
 
5
import("etherpad.collab.server_utils");
 
6
import("etherpad.utils");
 
7
import("etherpad.pad.padutils");
 
8
import("fastJSON");
 
9
 
 
10
function padModelWriteToDB(args) {
 
11
  /* Update tags for the pad */
 
12
 
 
13
  var new_tags = args.pad.text().match(new RegExp("#[^,#=!\\s][^,#=!\\s]*", "g"));
 
14
  if (new_tags == null) new_tags = new Array();
 
15
  for (i = 0; i < new_tags.length; i++)
 
16
    new_tags[i] = new_tags[i].substring(1);
 
17
  var new_tags_str = new_tags.join('#')
 
18
 
 
19
  var old_tags_row = sqlobj.selectSingle("PAD_TAG_CACHE", { PAD_ID: args.padId });
 
20
  var old_tags_str;
 
21
  if (old_tags_row !== null)
 
22
    old_tags_str = old_tags_row['TAGS'];
 
23
  else
 
24
    old_tags_str = '';
 
25
 
 
26
  // var old_tags = old_tags_str != '' ? old_tags_str.split('#') : new Array();
 
27
 
 
28
  if (new_tags_str != old_tags_str) {
 
29
    // log.info({message: 'Updating tags', new_tags:new_tags, old_tags:old_tags});
 
30
 
 
31
    if (old_tags_row)
 
32
      sqlobj.update("PAD_TAG_CACHE", {PAD_ID: args.padId }, {TAGS: new_tags.join('#')});
 
33
    else
 
34
      sqlobj.insert("PAD_TAG_CACHE", {PAD_ID: args.padId, TAGS: new_tags.join('#')});
 
35
 
 
36
    sqlobj.deleteRows("PAD_TAG", {PAD_ID: args.padId});
 
37
 
 
38
    for (i = 0; i < new_tags.length; i++) {
 
39
      var tag_row = sqlobj.selectSingle("TAG", { NAME: new_tags[i] });
 
40
      if (tag_row === null) {
 
41
        sqlobj.insert("TAG", {NAME: new_tags[i]});
 
42
        tag_row = sqlobj.selectSingle("TAG", { NAME: new_tags[i] });
 
43
      }
 
44
      sqlobj.insert("PAD_TAG", {PAD_ID: args.padId, TAG_ID: tag_row['ID']});
 
45
    }
 
46
  }
 
47
}
 
48
 
 
49
function queryAccessSql(args) {
 
50
  return [function (querySql) {
 
51
    return tagQuery.getQueryToSql(['public'], [], querySql);
 
52
  }];
 
53
}
 
54
 
 
55
function queryToSql(args) {
 
56
  return [function (querySql) {
 
57
    if (request.params.query == undefined || request.params.query == '') {
 
58
      return querySql;
 
59
    } else  {
 
60
      var tags = tagQuery.queryToTags(request.params.query);
 
61
 
 
62
      return tagQuery.getQueryToSql(tags.tags, tags.antiTags, querySql);
 
63
    }
 
64
  }];
 
65
}
 
66
 
 
67
function queryExtra() {
 
68
  return [function (querySql, info, clientVars) {
 
69
    var tags = tagQuery.queryToTags(request.params.query);
 
70
 
 
71
    var queryNewTagsSql = tagQuery.newTagsSql(querySql);
 
72
    var newTags = sqlobj.executeRaw(queryNewTagsSql.sql, queryNewTagsSql.params);
 
73
 
 
74
    info.tagQuery = tagQuery;
 
75
    info.tags = tags.tags;
 
76
    info.antiTags = tags.antiTags;
 
77
    info.newTags = newTags;
 
78
    info.padIdToReadonly = server_utils.padIdToReadonly;
 
79
  }];
 
80
}
 
81
 
 
82
function queryFormat() {
 
83
  function createFormat(format) {
 
84
    return function (querySql, info, clientVars) {
 
85
      var tags = tagQuery.queryToTags(request.params.query);
 
86
 
 
87
      var limit = 10;
 
88
      if (format == 'sitemap')
 
89
        limit = undefined;
 
90
 
 
91
      padSql = tagQuery.padInfoSql(querySql, limit);
 
92
      var matchingPads = sqlobj.executeRaw(padSql.sql, padSql.params);
 
93
 
 
94
      for (i = 0; i < matchingPads.length; i++) {
 
95
        matchingPads[i].TAGS = matchingPads[i].TAGS.split('#');
 
96
      }
 
97
      
 
98
      info.matchingPads = matchingPads;
 
99
 
 
100
      if (format == "html") {
 
101
        utils.renderHtml("tagBrowser.ejs", info, ['twitterStyleTags', 'search']);
 
102
      } else if (format == "rss") {
 
103
        response.setContentType("application/xml; charset=utf-8");
 
104
        response.write(utils.renderTemplateAsString("tagRss.ejs", info, ['twitterStyleTags', 'search']));
 
105
        if (request.acceptsGzip) {
 
106
          response.setGzip(true);
 
107
        }
 
108
      } else if (format == "sitemap") {
 
109
        response.setContentType("application/xml; charset=utf-8");
 
110
        response.write(utils.renderTemplateAsString("tagSitemap.ejs", info, ['twitterStyleTags', 'search']));
 
111
        if (request.acceptsGzip) {
 
112
          response.setGzip(true);
 
113
        }
 
114
      } else if (format == "json") {
 
115
        response.setContentType("application/json; charset=utf-8");
 
116
        response.write(fastJSON.stringify(info));
 
117
        if (request.acceptsGzip) {
 
118
          response.setGzip(true);
 
119
        }
 
120
      } else {
 
121
        throw new Error("Unknown format " + format);
 
122
      }
 
123
      return true;
 
124
    };
 
125
  }
 
126
 
 
127
  return [{'pads.html': createFormat('html'),
 
128
           'pads.rss': createFormat('rss'),
 
129
           'pads.sitemap': createFormat('sitemap'),
 
130
           'pads.json': createFormat('json')
 
131
         }];
 
132
}
 
133
 
 
134
function querySummary(args) {
 
135
  var res = args.template.include("twitterStyleTagsQuerySummary.ejs", {}, ['twitterStyleTags']);
 
136
  if (res.replace(new RegExp("^[ \n]*"), "").replace(new RegExp("[ \n]*$"), "") == '')
 
137
    return [];
 
138
  return [res];
 
139
}
 
140
 
 
141
function queryRefiner(args) {
 
142
 return [args.template.include("twitterStyleTagsQueryRefiner.ejs", {}, ['twitterStyleTags'])];
 
143
}
 
144
 
 
145
function docbarItemsSearch() {
 
146
 return ["<td class='docbarbutton'><a href='/ep/search?type=pads'>Pads</a></td>"];
 
147
}
 
148
 
 
149
function editBarItemsLeftPad(arg) {
 
150
  return [arg.template.include('twitterStyleTagsEditbarButtons.ejs', undefined, ['twitterStyleTags'])];
 
151
}