mtrack /web/wiki.php

Language PHP Lines 384
MD5 Hash e0805916fea760dc66b85ddadfe48f49 Estimated Cost $8,730 (why?)
Repository https://bitbucket.org/yoander/mtrack View Raw File View Project SPDX
  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
<?php # vim:ts=2:sw=2:et:
/* For licensing and copyright terms, see the file named LICENSE */

include '../inc/common.php';

$pi = mtrack_get_pathinfo();
if (empty($pi)) {
  header("Location: {$ABSWEB}wiki.php/WikiStart");
  exit;
}

$rev = isset($_GET['rev']) ? $_GET['rev'] : null;
if (!$rev || !preg_match("/^[a-fA-F0-9]+$/", $rev)) {
  $rev = null;
}

$hide_outline = false;

try {
  if ($rev) {
    $W = MTrackAPI::invoke('GET', '/wiki/page/' . $pi, null, array(
      'rev' => $rev
    ))->result;
  } else {
    $W = MTrackAPI::invoke('GET', '/wiki/page/' . $pi)->result;
  }
  $ATTACH = json_encode(MTrackAPI::invoke(
                'GET', "/wiki/attach/" . $pi)->result);
  $edit = ($rev == null) && MTrackACL::hasAnyRights("wiki:$pi", 'modify');
} catch (MTrackAPI_Exception $e) {
  if ($e->getCode() != 404) {
    throw $e;
  }

  /* if they're looking at a directory, give them a list of candidate
   * pages to look at instead */
  $tree = MTrackWikiItem::get_wiki_tree();
  $path_bits = explode('/', $pi);
  $dir = $tree;
  foreach ($path_bits as $name) {
    if (!isset($tree->{$name})) {
      $tree = null;
      break;
    }
    $tree = $tree->{$name};
  }

  if ($tree) {
    $edit = false;

    /* fake an empty summary page listing out the elements on this one */
    $W = new stdclass;
    $W->id = $pi;
    $W->version = null;
    $W->content = "\n\n";

    foreach ($tree as $k => $name) {
      $W->content .= " * [wiki:$pi/$k $k]\n";
    }
    $W->content .= "\n\n[help: Complete List of Wiki Pages]\n";

    $W->content_html = MTrackWiki::format_to_html($W->content);
    $W->changelog_html = null;
    $W->changelog = null;
    $W->when = null;
    $W->who = null;
    $ATTACH = json_encode(array());
    $hide_outline = true;

  } else {

    /* fake an empty one so that we can give the user the option of creating
     * a new page */
    $W = new stdclass;
    $W->id = $pi;
    $W->version = null;
    $W->content = null;
    $W->content_html = '';
    $W->changelog_html = null;
    $W->changelog = null;
    $W->when = null;
    $W->who = null;
    $ATTACH = json_encode(array());
    $edit = MTrackACL::hasAnyRights("wiki:$pi", 'create');

    if (!$edit) {
      header("HTTP/1.0 404 Not Found");
      mtrack_head($pi);
      echo "<h1>Page Not Found</h1>";
      mtrack_foot();
      exit;
    }
  }
}
mtrack_head($pi);

if ($hide_outline) {
  echo <<<HTML
<script>
$(document).ready(function() {
  $('#outlinetoggle').hide();
  $('#wikiinfo').hide();
});
</script>
HTML;
}


$edit = json_encode($edit);
$latest = json_encode($rev == null);
$J = json_encode($W);
$crumbs = mtrack_breadcrumb($pi, $ABSWEB . 'wiki.php');
echo <<<HTML
<div id='wikiview'>
  $crumbs
  <div id='wiki' class='wikipage'></div>
  <div id='attachments'>
    <h2>Attachments</h2>
    <div id="attachlist"></div>
  </div>
</div>
<button class='btn' id='outlinetoggle'>Toggle Outline</button>
<div id='wikiinfo'>
  <div id='wikiinfotmpl'></div>
  <label>Page Outline</label>
  <ol id='outline'></ul>
</div>
<div id="attachment-form" class="popupForm" style="display:none">
  <form action="${ABSWEB}post-attachment.php" method="POST"
      id="upload-form" enctype="multipart/form-data" target="upload_target">
    <input type="hidden" name="object" value="wiki:$pi">
    <label for='attachments[]'>Select file(s) to be attached</label>
    <input name="attachments[]" class='btn multi' type="file">
    <iframe id="upload_target" name="upload_target" src="${ABSWEB}/mtrack.css">
    </iframe>
    <input type="submit" class="btn btn-primary"
      id="confirm-upload" value="Upload">
    <button class='btn' id="cancel-upload">Cancel</button>
  </form>
</div>
<script type="text/template" id='attach-template'>
  <a class='attachment' href='<%= ABSWEB %>attachment.php/<%- object %>/<%- cid %>/<%- filename %>'><%- filename %></a> (<%- size %>) added by <%- who %>
  <abbr class='timeinterval' title='<%- changedate %>'><%- changedate %></abbr>
  <button class='btn'>x</button>
  <% if (image && 0) { %>
  <br><a href='<%= ABSWEB %>attachment.php/<%- object %>/<%- cid %>/<%- filename %>'><img src='<%= ABSWEB %>attachment.php/<%- object %>/<%- cid %>/<%- filename %>' width='<%- width %>' height='<%- height %>' border='0'></a>
  <% } %>
</script>


<script type='text/template' id='wiki-info-template'>
<label><%- id %></label><br>
<% if (!latest) { %>
<br>
<div class='ui-state-highlight ui-corner-all'>
  <span class='ui-icon ui-icon-info'></span>
  This is not the most recent version of this page!<br>
  <a href="${ABSWEB}wiki.php/<%- id %>"><b>Show latest version</b></a>
</div>
<br>
<% } %>
<div id="wiki-buttons">
<button id="save-wiki" class="btn btn-success hide-until-change"><%
if (version) {
%>Save<% } else { %>Create this page<% } %></button>
  <button class='btn btn-primary' id="edit-wiki"
      ><i class="icon-edit icon-white"></i> Edit this page</button>
  <button id="cancel-wiki"
      class="btn hide-until-change">Cancel</button>
  <button class="btn" id="attach-wiki"><i class="icon-upload"></i> Add Attachment</button>
  <button class='btn'
    onclick="document.location.href = '${ABSWEB}help.php'; return false;"
    ><i class='icon-book'></i> Help &amp; Page List</button>
  <div class='alert alert-danger' id='wiki-error' style="display:none">
    <a class='close' data-dismiss='alert'>&times;</a>
    <span id="wiki-error-text">Whoops</span>
  </div>
  <br>
</div>
<% if (who) { %>
<div id="wiki-change-info">
<img class='gravatar' src="${ABSWEB}avatar.php?u=<%- who %>&amp;s=24">
<div class="wiki-change-desc">
<%= changelog_html %>
</div>
<abbr class='timeinterval' title='<%- when %>'><%- when %></abbr>
by <%- who %><br>
<a href="${ABSWEB}log.php/default/wiki/<%- filename %>">Page History</a>
</div>
<% } %>
</script>
<script>
$(document).ready(function() {
  var WIKIPAGE = $J;
  // Fake up a property so we can tell if we are looking at a historical
  // version of the wiki page
  WIKIPAGE.latest = $latest;

  var attachments = $ATTACH;
  WIKI = new MTrackWiki(WIKIPAGE);
  WIKI.getAttachments().reset(attachments);
  var editable = $edit;
  var editor = null;
  var attach_view = null;
  var wasNew = WIKI.isNew();

  $('#outlinetoggle').click(function () {
    $('#wikiinfo').toggle();
  });

  function update_attachments() {
    if (WIKI.getAttachments().length) {
      $('#attachments').show();
    } else {
      $('#attachments').hide();
    }
  }

  function save() {
    var overlay = $('<div class="overlay"><div class="progress progress-striped progress-success active"><div class="bar" style="width: 100%"></div></div></div>');
    overlay.appendTo('body').fadeIn('fast', function () {
      WIKI.save(WIKI.toJSON(), {
        success: function(model, response) {
          $('button.hide-until-change').fadeOut('fast');
          overlay.fadeOut('fast', function () {
            WIKIPAGE = WIKI.toJSON();
            WIKI.unset('comment', {silent: true});
            if (wasNew || !WIKI.get('content') ||
                WIKI.get('content').length == 0) {
              // created/deleted, easier just to reload
              window.onbeforeunload = null;
              window.location.href = window.location.href;
              return;
            }
            editor.render();
            overlay.remove();
            WIKI.changed = false;
            attach_view.editable = true;
            attach_view.render();
            mtrack_wiki_outline($('#wiki'), $('#outline'));
          });
        },
        error: function(model, response) {
          var err;
          var conflict = null;
          if (!_.isObject(response)) {
            err = response;
          } else {
            err = response.statusText;
            try {
              var r = JSON.parse(response.responseText);
              err = r.message;
              if (r.code == 409) {
                conflict = r.extra;
              }
            } catch (e) {
              err = response.statusText;
            }
          }

          overlay.fadeOut('fast', function () {
            overlay.remove();
            if (conflict) {
              WIKI.set(conflict);
            }
            $('#wiki-error-text').text(err);
            $('#wiki-error').fadeIn('fast')
          });
        }
      });
    });
  }

  var comment_explanation =
    "\\n\\n> Please enter a reason for this change. Your changes will not be applied until you click Save. (this line will be automatically removed)\\n";
  WIKI.bind('change:comment', function () {
    var c = WIKI.get('comment').replace(comment_explanation, "");
    WIKI.set({comment: c},{silent: true});
    save();
  });

  function update_info() {
    update_attachments();

    if (WIKI.hasChanged()) {
      WIKI.changed = true;
    }
    $('#wikiinfotmpl').html(_.template(
      $('#wiki-info-template').html(), WIKI.toJSON()
    ));
    if (!editable) {
      $('#wiki-buttons').hide();
    }
    if (WIKI.changed) {
      $('button.hide-until-change').fadeIn('fast');
    }
    $('#wiki-error').click(function () {
      $(this).fadeOut('fast');
      return false;
    });
    $('#wikiinfotmpl .timeinterval').timeago();

    $('#edit-wiki').click(function () {
      editor.edit();
      return false;
    });
    $('#cancel-wiki').click(function () {
      $('#wiki-error').fadeOut('fast');
      WIKI.set(WIKIPAGE);
      WIKI.changed = false;
      $('button.hide-until-change').fadeOut('fast');
    });

    $('#save-wiki').click(function () {
      $('#wiki-error').fadeOut('fast');
      if (!WIKI.get('comment')) {
        WIKI.set({comment: comment_explanation}, {silent:true});
      }
      var reason = new MTrackWikiTextAreaView({
        model: WIKI,
        srcattr: 'comment',
        use_overlay: true,
        Caption: "Comment on this wiki edit",
        OKLabel: "Save Wiki Page",
        CancelLabel: "Return to Wiki Editor"
      });
      reason.edit();
    });
    mtrack_wiki_outline($('#wiki'), $('#outline'));
  }

  WIKI.bind('change', update_info);
  WIKI.bind('error', function (model, err) {
    $('#wiki-error-text').text(err);
    $('#wiki-error').fadeIn('fast');
  });
  WIKI.getAttachments().bind('all', update_attachments);

  editor = new MTrackWikiTextAreaView({
    model: WIKI,
    wikiContext: "wiki:",
    use_overlay: true,
    srcattr: "content",
    readonly: !editable,
    renderedattr: "content_html",
    doubleclick: false,
    placeholder:
      WIKI.isNew() ?
        "This page doesn't exist; click the \"Edit this page\" button to start editing!"
        : "Saving this page now (with no content) will delete it",
    el: "#wiki"
  });
  /* hide the outline while editing, in case the user wants to search
   * for text in the page */
  editor.bind('editstart', function () {
    $('#wikiinfo').hide();
  });
  editor.bind('editend', function () {
    $('#wikiinfo').show();
  });

  update_info();

  attach_view = new MTrackTicketAttachmentsView({
    model: WIKI,
    collection: WIKI.getAttachments(),
    type: 'wiki',
    button: '#attach-wiki',
    editable: editable && !WIKI.isNew(),
    el: '#attachlist'
  });

  window.onbeforeunload = function() {
    if (WIKI.changed) {
      return "You haven't saved your changes!";
    }
  };

});
</script>
HTML;

mtrack_foot();
Back to Top