This file is indexed.

/usr/share/lua/5.1/versium/mysql.lua is in sputnik 12.06.27-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
-----------------------------------------------------------------------------
-- Implements Versium API using using the LuaSQL driver for MySQL.
--
-- (c) 2008  James Whitehead II (jnwhiteh@gmail.com)
-- License: MIT/X, see http://sputnik.freewisdom.org/en/License
-----------------------------------------------------------------------------

module(..., package.seeall)
local util = require("versium.util")
local errors = require("versium.errors")
require("luasql.mysql")

-----------------------------------------------------------------------------
-- A table that describes what this versium implementation can and cannot do.
-----------------------------------------------------------------------------
capabilities = {
   can_save = true,
   has_history = true,
   is_persistent = true,
   get_nodes_prefix = true,
}

local schemas = {}
schemas.node = [[
CREATE TABLE IF NOT EXISTS %s ( 
   id VARCHAR(255) NOT NULL,
   version INT UNSIGNED NOT NULL AUTO_INCREMENT,
   author VARCHAR(255) NOT NULL,
   comment TEXT,
   timestamp CHAR(19) NOT NULL,
   data MEDIUMBLOB NOT NULL,
   PRIMARY KEY(id, version)
);]]
schemas.node_index = [[
CREATE TABLE IF NOT EXISTS %s (
   id VARCHAR(255) NOT NULL,
   version INT UNSIGNED NOT NULL,
   PRIMARY KEY (id)
);]]

-----------------------------------------------------------------------------
-- A table representing the class.
-----------------------------------------------------------------------------
local MySQLVersium = {}
local MySQLVersium_mt = {__metatable = {}, __index = MySQLVersium}

-----------------------------------------------------------------------------
-- Instantiates a new MySQLVersium object that represents a connection to
-- a storage system.  This is the only function that this module exports.
-- 
-- @param params         a table of parameters
-- @return               a new versium object.
-----------------------------------------------------------------------------
function new(params)
   -- Params table accepts the following:
   -- prefix - A string that will be prepended to table names
   -- connect - A list that is passed to the luasql connection function

   -- Try to connect to the given database
   local env = luasql.mysql()
   local con = env:connect(unpack(params))

   if not con then
      error(errors.could_not_initialize("Could not connect to MySQL database"))
   end

   -- Create the new object
   local obj = {env=env, con=con, versium=versium, params=params}
   setmetatable(obj, MySQLVersium_mt)

   obj.tables = {}
   obj.capabilities = capabilities

   -- Create the two data tables, if they don't already exist
   local tables = {"node", "node_index"}

   for idx,tbl in ipairs(tables) do
      obj.tables.node = string.format("%snode", params.prefix or "")
      obj.tables.node_index = string.format("%snode_index", params.prefix or "")

      local cmd = obj:prepare(schemas[tbl]:format(obj.tables[tbl]))
      assert(con:execute(cmd))
   end

   -- Pre-build our queries
   obj.queries = {
      GET_NODE_VERSION = string.format("SELECT id,data,version from %s WHERE id = %%s and version = %%s;", obj.tables.node),
      GET_NODE_LATEST = string.format("SELECT n.id,n.data,n.version FROM %s as n NATURAL JOIN %s WHERE n.id = %%s;", obj.tables.node, obj.tables.node_index),
      GET_VERSION = string.format("SELECT max(version) as version FROM %s WHERE id = %%s;", obj.tables.node),
      GET_NODES_PREFIX = string.format("SELECT n.* FROM %s as n NATURAL JOIN %s WHERE n.id LIKE %%s;", obj.tables.node, obj.tables.node_index),
      GET_NODE_IDS = string.format("SELECT id FROM %s ORDER BY id;", obj.tables.node_index),
      GET_NODE_IDS_PREFIX_LIMIT = string.format("SELECT id FROM %s WHERE id LIKE %%s ORDER BY id LIMIT %%s;", obj.tables.node_index),
      GET_NODE_IDS_PREFIX = string.format("SELECT id FROM %s WHERE id LIKE %%s ORDER BY id;", obj.tables.node_index),
      GET_NODE_IDS_LIMIT = string.format("SELECT id FROM %s ORDER BY id LIMIT %%s;", obj.tables.node_index),
      NODE_EXISTS = string.format("SELECT DISTINCT id FROM %s WHERE id = %%s;", obj.tables.node),
      INSERT_NODE = string.format("INSERT INTO %s (id,author,comment,timestamp,data) VALUES (%%s, %%s, %%s, %%s, %%s);", obj.tables.node),
      INSERT_INDEX = string.format("INSERT INTO %s (id, version) VALUES (%%s, %%s);", obj.tables.node_index),
      UPDATE_INDEX = string.format("UPDATE %s SET version=%%s WHERE id = %%s;", obj.tables.node_index),
      GET_METADATA_ALL = string.format("SELECT id,version,timestamp,author,comment FROM %s WHERE id = %%s ORDER BY timestamp, version;", obj.tables.node),
      GET_METADATA_VERSION = string.format("SELECT id,version,timestamp,author,comment FROM %s WHERE id = %%s and version = %%s;", obj.tables.node),
      GET_METADATA_LATEST = string.format("SELECT n.id,n.version,timestamp,author,comment FROM %s as n NATURAL JOIN %s WHERE n.id = %%s ORDER BY timestamp, version;", obj.tables.node, obj.tables.node_index),
      GET_COMPLETE_HISTORY = string.format("SELECT id,version,timestamp,author,comment FROM %s WHERE id like %%s ORDER BY timestamp DESC, version LIMIT %%s, %%s;", obj.tables.node)
   }

   return obj 
end

-----------------------------------------------------------------------------
-- Prepares a SQL statement using placeholders.
-- 
-- @param statement      the statement to be prepared
-- @param ...            a list of parameters  
-- @return               the prepared statement.
-----------------------------------------------------------------------------
function MySQLVersium:prepare(statement, ...)
   local count = select('#', ...)

   if count > 0 then
      local someBindings = {}

      for index = 1, count do
         local value = select(index, ...)
         local type = type(value)

         if type == 'string' then
            value = "'" .. self.con:escape(value) .. "'"
         elseif type == 'nil' then
            value = 'null'
         else
            value = tostring(value)
         end 

         someBindings[index] = value
      end

      statement = statement:format(unpack(someBindings))
   end

   return statement
end

-----------------------------------------------------------------------------
-- Executes a query using the MySQL connection.  This function is exposed to
-- allow other code to hook it if necessary, since we cannot hook con.execute
--
-- @param query          the query to be executed
-- @param noreconnect    do not attempt to reconnect to server if it has died
-----------------------------------------------------------------------------
function MySQLVersium:execute(query, noreconnect)
   local cur, err = self.con:execute(query)
   if cur then
      return cur
   elseif cur == nil and err and err:match("gone away") and not noreconnect then
      -- Reopen the connection to the MySQL server
      local con = self.env:connect(unpack(self.params))
      if con then
         self.con = con
         return self:execute(query, true)
      else
         return nil, err
      end
   end
   return cur, err
end

-----------------------------------------------------------------------------
-- Returns the data stored in the node as a string and a table representing
-- the node's metadata.  Returns nil if the node doesn't exist.  Throws an
-- error if anything else goes wrong.
--
-- @param id             a node id.
-- @param version        [optional] the desired version of the node (defaults
--                       to current).
-- @return               a byte-string representing the data stored in the
--                       node or nil if the node could not be loaded or nil.
-- @return               a table representing the metadata, including the
--                       following fields (see get_node_history()) or nil.
-- @see get_node_history
-----------------------------------------------------------------------------
function MySQLVersium:get_node(id, version)
   assert(id)

   -- Get the most recent version of the node
   local cmd
   if version then
      cmd = self:prepare(self.queries.GET_NODE_VERSION, id, version)
   else
      cmd = self:prepare(self.queries.GET_NODE_LATEST, id)
   end

   -- Run the query to get the node
   local cur = assert(self:execute(cmd))
   local row = cur:fetch({}, "a")
   cur:close()

   if not row then
      return nil
   end

   -- Query the metadata
   local cmd
   if version then
      cmd = self:prepare(self.queries.GET_METADATA_VERSION, id, version)
   else
      cmd = self:prepare(self.queries.GET_METADATA_LATEST, id)
   end

   local cur = assert(self:execute(cmd))
   local metadata = cur:fetch({}, "a")
   cur:close()

   assert(row.data)
   return row.data, metadata
end

-----------------------------------------------------------------------------
-- Returns true if the node with this id exists and false otherwise.
-- 
-- @param id             an id of an node.
-- @return               true or false.
-----------------------------------------------------------------------------
function MySQLVersium:node_exists(id)
   assert(id)

   local cmd = self:prepare(self.queries.NODE_EXISTS, id)
   local cur = assert(self:execute(cmd))
   local row = cur:fetch({}, "*a")
   cur:close()

   return row ~= nil
end

-----------------------------------------------------------------------------
-- Returns a table with the metadata for the latest version of the node. Same
-- as get_node_history(id)[1] in case of this implementation.
-- 
-- @param id             an id of an node.
-- @return               the metadata for the latest version or nil.
-----------------------------------------------------------------------------
function MySQLVersium:get_node_info(id)
   assert(id)

   -- Fetch the latest version number
   local cmd = self:prepare(self.queries.GET_METADATA_LATEST, id)
   local cur = assert(self:execute(cmd))
   local row = cur:fetch({}, "a")
   cur:close()

   return row
end

-----------------------------------------------------------------------------
-- Returns a list of existing node ids, up to a certain limit.  (If no limit
-- is specified, all ids are returned.)  The ids can be optionally filtered
-- by prefix.  (In this case, the limit applies to the number of ids that
-- are being _returned_.)  The ids can be returned in any order.
--
-- @param prefix         [optional] a prefix to filter the ids (defaults to
--                       "").
-- @param limit          [optional] the maximum number of ids to return.
-- @return               a list of node ids.
-- @return               true if there are more ids left.
-----------------------------------------------------------------------------
function MySQLVersium:get_node_ids(prefix, limit)
   local nodes = {}
   local cmd
   if prefix and limit then
      prefix = prefix .. "%"
      cmd = self:prepare(self.queries.GET_NODE_IDS_PREFIX_LIMIT, prefix, limit)
   elseif prefix then
      prefix = prefix .. "%"
      cmd = self:prepare(self.queries.GET_NODE_IDS_PREFIX, prefix)
   elseif limit then
      cmd = self:prepare(self.queries.GET_NODE_IDS_LIMIT, limit)
   else
      cmd = self:prepare(self.queries.GET_NODE_IDS)
   end

   local cur = assert(self:execute(cmd))
   local row = cur:fetch({}, "a")

   while row do
      nodes[#nodes+1] = row.id
      row = cur:fetch(row, "a")
   end

   cur:close()
   return nodes
end

-----------------------------------------------------------------------------
-- Saves a new version of the node.
--
-- @param id             the id of the node (required).
-- @param data           the value to save (required, but "" is ok).
-- @param author         the user name to be associated with the change (required).
-- @param comment        the change comment (optional).
-- @param extra          any extra metadata (optional).
-- @return               the version id of the new node.
-----------------------------------------------------------------------------
function MySQLVersium:save_version(id, data, author, comment, extra, timestamp)
   assert (id)
   assert(data)
   assert(author)

   if not timestamp then
      local t = os.date("*t")
      timestamp = string.format("%02d-%02d-%02d %02d:%02d:%02d", t.year, t.month, t.day, t.hour, t.min, t.sec)
   end

   -- Store the new node in the 'nodes' table
   local cmd = self:prepare(self.queries.INSERT_NODE, id, author, comment, timestamp, data)
   assert(self:execute(cmd), out)

   local cur,err = assert(self:execute("SELECT LAST_INSERT_ID();"))
   local version = tonumber(assert(cur:fetch("*a")))
   cur:close()

   -- Update the index table to the newest revision
   if version == 1 then
      local cmd = self:prepare(self.queries.INSERT_INDEX, id, version)
      local cur = assert(self:execute(cmd))
      assert(cur, err)
   else
      local cmd = self:prepare(self.queries.UPDATE_INDEX, version, id) 
      local cur = assert(self:execute(cmd))
   end

   -- Return the new version number
   return version
end

-----------------------------------------------------------------------------
-- Returns the history of the node as a list of tables, each representing a 
-- revision of the node (with fields like version, author, comment, and extra).
-- The version can be filtered by a time prefix. Returns an empty table if 
-- the node does not exist.
--
-- @param id        the id of the node.
-- @param prefix    time prefix.
-- @return history  a list of tables representing the versions (the list will
-- be empty if the node doesn't exist)
-----------------------------------------------------------------------------
function MySQLVersium:get_node_history(id, prefix)
   assert(id)

   local history = {}

   -- Pull the history of the given node
   local cmd = self:prepare(self.queries.GET_METADATA_ALL, id)
   local cur = self:execute(cmd)
   local row = cur:fetch({}, "a")

   while row do
      table.insert(history, 1, row)
      row = cur:fetch({}, "a")
   end

   cur:close()

   return history
end

-----------------------------------------------------------------------------
-- Returns the data and metadata for multiple nodes, using a single database
-- query.  This is used to optimize specific types of pages where the system
-- may need to pull many nodes at once.  Returns an empty table if there are
-- no nodes matching the prefix.
--
-- @param prefix          the prefix to query
-- @return                (1) a table containing the data for each node,
--                        indexed by node name.
--                        (2) a table containing the metadata for each node,
--                        indexed by node name.
-----------------------------------------------------------------------------
function MySQLVersium:get_nodes_by_prefix(prefix)
   prefix = prefix or ""
   local data,metadata = {}, {}

   local cmd = self:prepare(self.queries.GET_NODES_PREFIX, prefix .. "%")
   local cur = self:execute(cmd)
   local row = cur:fetch({}, "a")

   while row do
      data[row.id] = row.data
      row.data = nil
      metadata[row.id] = row
      row = cur:fetch({}, "a")
   end

   cur:close()

   return data, metadata
end

local LARGE_LIMIT = "18446744073709551615"
function MySQLVersium:get_complete_history(id_prefix, date_prefix, limit)
   local history = {}

   -- Pull the history of the given node
   limit = limit or LARGE_LIMIT
   id_prefix = id_prefix and (id_prefix .. "%") or "%" 
   local cmd = self:prepare(self.queries.GET_COMPLETE_HISTORY, id_prefix, 0, limit)

   local cur = self:execute(cmd)
   local row = cur:fetch({}, "a")

   while row do
      table.insert(history, 1, row)
      row = cur:fetch({}, "a")
   end

   cur:close()

   return history
end

-- vim:ts=3 ss=3 sw=3 expandtab