Fossil

Check-in [1df648ab]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Implemented /json/wiki/create.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | json
Files: files | file ages | folders
SHA1: 1df648abfeb97d303ea94069a92fbcf236ae74fa
User & Date: stephan 2011-09-22 19:14:12
Context
2011-09-22
20:07
Implemented /json/timeline/wiki. Fixed --limit/-n handling of timeline/ci. check-in: 12545465 user: stephan tags: json
19:14
Implemented /json/wiki/create. check-in: 1df648ab user: stephan tags: json
17:56
Implemented /json/wiki/save. check-in: 2162c86d user: stephan tags: json
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ajax/wiki-editor.html.

172
173
174
175
176
177
178






179
180
181
182
183
184
185
...
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
...
301
302
303
304
305
306
307

308
309
310
311
312
313
314
        // FIXME: this.url is hosed for login because of how i overload onResponse()
        if( this.url ) TheApp.jqe.textPath.val(this.url.replace(WhAjaj.Connector.options.ajax.url,''));
        TheApp.jqe.taResponse.val( val );
    };
    opt.onError = function(req,opt) {
        TheApp.jqe.taResponse.val( "ERROR SENDING REQUEST:\n"+WhAjaj.stringify(opt) );
    };







    TheApp.cgi.onLogin = function(){
      TheApp.jqe.taResponse.val( "Logged in. Auth token =\n"+this.authToken );
      TheApp.jqe.currentAuthToken.text("Auth token: "+(this.authToken || "not logged in"));
    };
    TheApp.cgi.onLogout = function(){
      TheApp.jqe.taResponse.val( "Logged out!" );
................................................................................
    TheApp.showPage = function(name){
        function doShow(page){
            TheApp.currentPage = page;
            TheApp.jqe.spanPageName.text('('+page.name+')');
            TheApp.jqe.taPageContent.val(page.content);
        }
        var p = ('object' === typeof name) ? name : TheApp.pages[name];
        if('object' === typeof p) {
            doShow(p);
            return;
        }
        TheApp.cgi.sendCommand('/json/wiki/get',{
            page:name
        },{
            onResponse:function(resp,req){
................................................................................
                TheApp.onResponse(resp,req);
                if(resp.resultCode) return;
                var p = resp.payload;
                doShow( TheApp.pages[p.name] = p );
            }
        });
    };
    TheApp.updatePageList = function(list){
        var tgt = TheApp.jqe.pageListArea;






        tgt.empty();
        var i, p, a;

        function makeLink(name){
            var a = jQuery('<span class="wikiPageLink"></span>');

            a.text(name).

            click(function(e){
                TheApp.showPage(name);
                e.preventDefault();
                return false;
            });
            return a;
        }

        for( i = 0; i < list.length; ++i ){
            tgt.append(makeLink(list[i])).append('<br/>');

        }
    };

    TheApp.loadPageList = function(){
        TheApp.cgi.sendCommand('/json/wiki/list',null,{
            onResponse:function(resp,req){
                TheApp.onResponse(resp,req);
                if(resp.resultCode) return;
                else TheApp.updatePageList(resp.payload);






            }
        });
        return false /*for click handlers*/;
    }

    TheApp.savePage = function(p){
        p = p || TheApp.currentPage || TheApp.pages[TheApp.currentPage];
        if( 'object' !== typeof p ){
            p = TheApp.pages[p];
        }
        if('object' !== typeof p){
            alert("savePage() argument is not a page object or known page name.");
        }
        TheApp.pages[p.name] = p;
        p.content = TheApp.jqe.taPageContent.val();
        var req = {
            name:p.name,
            content:p.content
        };
        if(! confirm("Really save wiki page ["+p.name+"]?") ) return;
























        TheApp.cgi.sendCommand('/json/wiki/save',req);








    };

});

</script>

</head>
................................................................................
<table>
    <tr>
        <th>Page List</th>
        <th>Content <span id='spanPageName'></span></th>
    </tr>
    <tr>
        <td width='25%' valign='top'>

            <div id='pageListArea'></div>
        </td>
        <td width='75%' valign='top'>
            <input type='button' value='Save' onclick='TheApp.savePage()' /><br/>
            <textarea id='taPageContent' rows='20' cols='60'></textarea>
        </td>
    </tr>







>
>
>
>
>
>







 







|







 







|
|
>
>
>
>
>
>
|
|
>

<
>
|
>
|




|

>

|
>








|
>
>
>
>
>
>






|













>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>







 







>







172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
...
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
...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
        // FIXME: this.url is hosed for login because of how i overload onResponse()
        if( this.url ) TheApp.jqe.textPath.val(this.url.replace(WhAjaj.Connector.options.ajax.url,''));
        TheApp.jqe.taResponse.val( val );
    };
    opt.onError = function(req,opt) {
        TheApp.jqe.taResponse.val( "ERROR SENDING REQUEST:\n"+WhAjaj.stringify(opt) );
    };

    TheApp.jqe.taPageContent.blur(function(){
        var p = TheApp.currentPage;
        if(! p ) return;
        p.content = TheApp.jqe.taPageContent.val();
    });

    TheApp.cgi.onLogin = function(){
      TheApp.jqe.taResponse.val( "Logged in. Auth token =\n"+this.authToken );
      TheApp.jqe.currentAuthToken.text("Auth token: "+(this.authToken || "not logged in"));
    };
    TheApp.cgi.onLogout = function(){
      TheApp.jqe.taResponse.val( "Logged out!" );
................................................................................
    TheApp.showPage = function(name){
        function doShow(page){
            TheApp.currentPage = page;
            TheApp.jqe.spanPageName.text('('+page.name+')');
            TheApp.jqe.taPageContent.val(page.content);
        }
        var p = ('object' === typeof name) ? name : TheApp.pages[name];
        if(('object' === typeof p) && p.content) {
            doShow(p);
            return;
        }
        TheApp.cgi.sendCommand('/json/wiki/get',{
            page:name
        },{
            onResponse:function(resp,req){
................................................................................
                TheApp.onResponse(resp,req);
                if(resp.resultCode) return;
                var p = resp.payload;
                doShow( TheApp.pages[p.name] = p );
            }
        });
    };
    TheApp.refreshPageListView = function(){
        var list = (function(){
            var k, v, li = [];
            for( k in TheApp.pages ){
                if(!TheApp.pages.hasOwnProperty(k)) continue;
                li.push(k);
            }
            return li;                
        })();
        var i, p, a, tgt = TheApp.jqe.pageListArea;
        tgt.text('');
        function makeLink(name){

            var link = jQuery('<span></span>');
            link.text(name);
            link.addClass('wikiPageLink');
            link.click(function(e){
                TheApp.showPage(name);
                e.preventDefault();
                return false;
            });
            return link;
        }
        list.sort();
        for( i = 0; i < list.length; ++i ){
            tgt.append(makeLink(list[i]));
            tgt.append('<br/>');
        }
    };

    TheApp.loadPageList = function(){
        TheApp.cgi.sendCommand('/json/wiki/list',null,{
            onResponse:function(resp,req){
                TheApp.onResponse(resp,req);
                if(resp.resultCode) return;
                var i, v, p, ar = resp.payload;
                for( i = 0; i < ar.length; ++i ){
                    v = ar[i];
                    p = TheApp.pages[v];
                    if( !p ) TheApp.pages[v] = {name:v};
                }
                TheApp.refreshPageListView();
            }
        });
        return false /*for click handlers*/;
    }

    TheApp.savePage = function(p){
        p = p || TheApp.currentPage;
        if( 'object' !== typeof p ){
            p = TheApp.pages[p];
        }
        if('object' !== typeof p){
            alert("savePage() argument is not a page object or known page name.");
        }
        TheApp.pages[p.name] = p;
        p.content = TheApp.jqe.taPageContent.val();
        var req = {
            name:p.name,
            content:p.content
        };
        if(! confirm("Really save wiki page ["+p.name+"]?") ) return;
        TheApp.cgi.sendCommand('/json/wiki/'+(p.isNew?'create':'save'),req,{
            onResponse:function(resp,req){
                TheApp.onResponse(resp,req);
                if(resp.resultCode) return;
                delete p.isNew;
                p.timestamp = resp.payload.timestamp;
            }
        });
        
    };

    TheApp.createNewPage = function(){
        var name = prompt("New page name?");
        if(!name) return;
        var p = {
            name:name,
            content:"New, empty page.",
            isNew:true
        };
        TheApp.pages[name] = p;
        TheApp.refreshPageListView();
        TheApp.showPage(p);
/*
        if(! confirm("Really create new wiki page ["+name+"]?") ) return;
        TheApp.cgi.sendCommand('/json/wiki/create',req,{
            onResponse:function(resp,req){
                TheApp.onResponse(resp,req);
                if(resp.resultCode) return;
                TheApp.pages[p.name] = p;
                TheApp.refreshPageListView();
            }
        });
*/
    };

});

</script>

</head>
................................................................................
<table>
    <tr>
        <th>Page List</th>
        <th>Content <span id='spanPageName'></span></th>
    </tr>
    <tr>
        <td width='25%' valign='top'>
            <input type='button' value='Create new...' onclick='TheApp.createNewPage()' /><br/>
            <div id='pageListArea'></div>
        </td>
        <td width='75%' valign='top'>
            <input type='button' value='Save' onclick='TheApp.savePage()' /><br/>
            <textarea id='taPageContent' rows='20' cols='60'></textarea>
        </td>
    </tr>

Changes to src/json.c.

1479
1480
1481
1482
1483
1484
1485


1486
1487
1488
1489

1490
1491
1492
1493
1494
1495
1496
....
1591
1592
1593
1594
1595
1596
1597
1598











1599
1600
1601
1602
1603

1604
1605
1606
1607
1608
1609


1610
1611
1612
1613
1614
























1615
1616



1617
1618

1619

1620
1621
1622
1623

1624
1625
1626


1627

1628
1629
1630
1631
1632
1633
1634
1635













1636
















1637
1638
1639
1640
1641
1642
1643
#undef SETBUF
}


static cson_value * json_wiki_list(unsigned int depth);
static cson_value * json_wiki_get(unsigned int depth);
static cson_value * json_wiki_save(unsigned int depth);


/*
** Mapping of /json/wiki/XXX commands/paths to callbacks.
*/
static const JsonPageDef JsonPageDefs_Wiki[] = {

{"get", json_wiki_get, 0},
{"list", json_wiki_list, 0},
{"save", json_wiki_save, 1},
/* Last entry MUST have a NULL name. */
{NULL,NULL,0}
};

................................................................................
    /*TODO: add the 'A' card (file attachment) entries?*/
    manifest_destroy(pWiki);
    return payV;
  }
}

/*
** Implementation of /json/wiki/save.











*/
static cson_value * json_wiki_save(unsigned int depth){
  Blob content = empty_blob;
  cson_value * nameV;
  cson_value * contentV;

  cson_value * payV = NULL;
  cson_object * pay = NULL;
  cson_string const * jstr = NULL;
  char const * zContent;
  char const * zBody = NULL;
  char const * zPageName;


  if( !g.perm.WrWiki ){
    g.json.resultCode = FSL_JSON_E_DENIED;
    return NULL;
  }
  nameV = json_req_payload_get("name");
























  contentV = nameV ? json_req_payload_get("content") : NULL;
  if(!nameV || !contentV){



    g.json.resultCode = FSL_JSON_E_MISSING_ARGS;
    return NULL;

  }

  if( !cson_value_is_string(nameV)
      || !cson_value_is_string(contentV)){
    g.json.resultCode = FSL_JSON_E_INVALID_ARGS;
    return NULL;

  }
  zPageName = cson_string_cstr(cson_value_get_string(nameV));
  jstr = cson_value_get_string(contentV);


  blob_append(&content, cson_string_cstr(jstr),(int)cson_string_length_bytes(jstr));

  wiki_cmd_commit(zPageName, 0, &content);
  blob_reset(&content);

  payV = cson_value_new_object();
  pay = cson_value_get_object(payV);
  cson_object_set( pay, "name", nameV );
  cson_object_set( pay, FossilJsonKeys.timestamp,
                   json_new_timestamp(-1) );













  return payV;
















}

/*
** Implementation of /json/wiki/list.
*/
static cson_value * json_wiki_list(unsigned int depth){
  cson_value * listV = NULL;







>
>




>







 







|
>
>
>
>
>
>
>
>
>
>
>

|



>






>
>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
|
<
>
|
>



<
>

<

>
>
|
>
|







>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
....
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661

1662
1663
1664
1665
1666
1667

1668
1669

1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
#undef SETBUF
}


static cson_value * json_wiki_list(unsigned int depth);
static cson_value * json_wiki_get(unsigned int depth);
static cson_value * json_wiki_save(unsigned int depth);
static cson_value * json_wiki_create(unsigned int depth);

/*
** Mapping of /json/wiki/XXX commands/paths to callbacks.
*/
static const JsonPageDef JsonPageDefs_Wiki[] = {
{"create", json_wiki_create, 1},
{"get", json_wiki_get, 0},
{"list", json_wiki_list, 0},
{"save", json_wiki_save, 1},
/* Last entry MUST have a NULL name. */
{NULL,NULL,0}
};

................................................................................
    /*TODO: add the 'A' card (file attachment) entries?*/
    manifest_destroy(pWiki);
    return payV;
  }
}

/*
** Internal impl of /wiki/save and /wiki/create. If createMode is 0
** and the page already exists then a
** FSL_JSON_E_RESOURCE_ALREADY_EXISTS error is triggered.  If
** createMode is false then the FSL_JSON_E_RESOURCE_NOT_FOUND is
** triggered if the page does not already exists.
**
** Note that the error triggered when createMode==0 and no such page
** exists is rather arbitrary - we could just as well create the entry
** here if it doesn't already exist. With that, save/create would
** become one operation. That said, i expect there are people who
** would categorize such behaviour as "being too clever" or "doing too
** much automatically" (and i would likely agree with them).
*/
static cson_value * json_wiki_create_or_save(char createMode){
  Blob content = empty_blob;
  cson_value * nameV;
  cson_value * contentV;
  cson_value * emptyContent = NULL;
  cson_value * payV = NULL;
  cson_object * pay = NULL;
  cson_string const * jstr = NULL;
  char const * zContent;
  char const * zBody = NULL;
  char const * zPageName;
  unsigned int contentLen = 0;
  int rid;
  if( !g.perm.WrWiki ){
    g.json.resultCode = FSL_JSON_E_DENIED;
    return NULL;
  }
  nameV = json_req_payload_get("name");
  if(!nameV){
    g.json.resultCode = FSL_JSON_E_MISSING_ARGS;
    goto error;
  }
  zPageName = cson_string_cstr(cson_value_get_string(nameV));
  rid = db_int(0,
     "SELECT x.rid FROM tag t, tagxref x"
     " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
     " ORDER BY x.mtime DESC LIMIT 1",
     zPageName
  );

  if(rid){
    if(createMode){
      g.json.resultCode = FSL_JSON_E_RESOURCE_ALREADY_EXISTS;
      goto error;
    }
  }else{
    if(!createMode){
      g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
      goto error;
    }
  }

  contentV = json_req_payload_get("content");
  if( !contentV ){
    if( createMode ){
      contentV = emptyContent = cson_value_new_string("",0);
    }else{
      g.json.resultCode = FSL_JSON_E_MISSING_ARGS;

      goto error;
    }
  }
  if( !cson_value_is_string(nameV)
      || !cson_value_is_string(contentV)){
    g.json.resultCode = FSL_JSON_E_INVALID_ARGS;

    goto error;
  }

  jstr = cson_value_get_string(contentV);
  contentLen = (int)cson_string_length_bytes(jstr);
  if(contentLen){
    blob_append(&content, cson_string_cstr(jstr),contentLen);
  }
  wiki_cmd_commit(zPageName, 0==rid, &content);
  blob_reset(&content);

  payV = cson_value_new_object();
  pay = cson_value_get_object(payV);
  cson_object_set( pay, "name", nameV );
  cson_object_set( pay, FossilJsonKeys.timestamp,
                   json_new_timestamp(-1) );

  goto ok;
  error:
  assert( 0 != g.json.resultCode );
  cson_value_free(payV);
  payV = NULL;
  ok:
  if( emptyContent ){
    /* We have some potentially tricky memory ownership
       here, which is why we handle emptyContent separately.
    */
    cson_value_free(emptyContent);
  }
  return payV;

}

/*
** Implementation of /json/wiki/create.
*/
static cson_value * json_wiki_create(unsigned int depth){
  return json_wiki_create_or_save(1);
}

/*
** Implementation of /json/wiki/save.
*/
static cson_value * json_wiki_save(unsigned int depth){
  /* FIXME: add GET/POST.payload bool option createIfNotExists. */
  return json_wiki_create_or_save(0);
}

/*
** Implementation of /json/wiki/list.
*/
static cson_value * json_wiki_list(unsigned int depth){
  cson_value * listV = NULL;

Changes to src/json_detail.h.

35
36
37
38
39
40
41

42
43
44
45
46
47
48
FSL_JSON_E_NYI = FSL_JSON_E_GENERIC_SUB1 + 8,
FSL_JSON_E_PANIC = FSL_JSON_E_GENERIC_SUB1 + 9,

FSL_JSON_E_AUTH = 2000,
FSL_JSON_E_MISSING_AUTH = FSL_JSON_E_AUTH + 1,
FSL_JSON_E_DENIED = FSL_JSON_E_AUTH + 2,
FSL_JSON_E_WRONG_MODE = FSL_JSON_E_AUTH + 3,


FSL_JSON_E_LOGIN_FAILED = FSL_JSON_E_AUTH + 100,
FSL_JSON_E_LOGIN_FAILED_NOSEED = FSL_JSON_E_LOGIN_FAILED + 1,
FSL_JSON_E_LOGIN_FAILED_NONAME = FSL_JSON_E_LOGIN_FAILED + 2,
FSL_JSON_E_LOGIN_FAILED_NOPW = FSL_JSON_E_LOGIN_FAILED + 3,
FSL_JSON_E_LOGIN_FAILED_NOTFOUND = FSL_JSON_E_LOGIN_FAILED + 4,








>







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
FSL_JSON_E_NYI = FSL_JSON_E_GENERIC_SUB1 + 8,
FSL_JSON_E_PANIC = FSL_JSON_E_GENERIC_SUB1 + 9,

FSL_JSON_E_AUTH = 2000,
FSL_JSON_E_MISSING_AUTH = FSL_JSON_E_AUTH + 1,
FSL_JSON_E_DENIED = FSL_JSON_E_AUTH + 2,
FSL_JSON_E_WRONG_MODE = FSL_JSON_E_AUTH + 3,
FSL_JSON_E_RESOURCE_ALREADY_EXISTS = FSL_JSON_E_AUTH + 4,

FSL_JSON_E_LOGIN_FAILED = FSL_JSON_E_AUTH + 100,
FSL_JSON_E_LOGIN_FAILED_NOSEED = FSL_JSON_E_LOGIN_FAILED + 1,
FSL_JSON_E_LOGIN_FAILED_NONAME = FSL_JSON_E_LOGIN_FAILED + 2,
FSL_JSON_E_LOGIN_FAILED_NOPW = FSL_JSON_E_LOGIN_FAILED + 3,
FSL_JSON_E_LOGIN_FAILED_NOTFOUND = FSL_JSON_E_LOGIN_FAILED + 4,

Changes to src/wiki.c.

803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
     zPageName
  );
  if( rid==0 && !isNew ){
    g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
    fossil_fatal("no such wiki page: %s", zPageName);
  }
  if( rid!=0 && isNew ){
    g.json.resultCode = FSL_JSON_E_DENIED/*need a better code for this*/;
    fossil_fatal("wiki page %s already exists", zPageName);
  }

  blob_zero(&wiki);
  zDate = date_in_standard_format("now");
  blob_appendf(&wiki, "D %s\n", zDate);
  free(zDate);







|







803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
     zPageName
  );
  if( rid==0 && !isNew ){
    g.json.resultCode = FSL_JSON_E_RESOURCE_NOT_FOUND;
    fossil_fatal("no such wiki page: %s", zPageName);
  }
  if( rid!=0 && isNew ){
    g.json.resultCode = FSL_JSON_E_RESOURCE_ALREADY_EXISTS;
    fossil_fatal("wiki page %s already exists", zPageName);
  }

  blob_zero(&wiki);
  zDate = date_in_standard_format("now");
  blob_appendf(&wiki, "D %s\n", zDate);
  free(zDate);