Was this page helpful?

Button to Move/Rename a page

     

    Introduction

    THIS TEMPLATE IS CURRENTLY IN BETA PLEASE REPORT ALL TROUBLES

    I created this template because someone on the forums asked how you can create a button that will force a page to move without using the Move Page Dialog.  I decided that this would be some nice functionality to automate page workflows and would also be a great example of how to use the Move Page API with jQuery.  All questions and comments should be directed to this forum thread.  

    Please note that the redirects from a moved page may get in the way of some of what you want to do.  I have not yet figured out how to stop the redirects from happening.

    History

    Version Date Author Description
    0.9b  11/18/2009 rberinger  Initial Release 

    Requirements

    • This template requires MindTouch version: 9.02 or greater
    • Dekiapi extension

    How do I install it?

    1. Create a template, call it "Template:MovePage" (or rename as you desire).  You must have UNSAFECONTENT permission for this to work.
    2. Create a "DekiScript" block on the template page (use the "Style" menu in the editor")
    3. Copy the code from the end of this page and paste it into the DekiScript block.  To copy, click "expand source", then mouse over the top right corner of the source code, and click the "view source" button.  This will pop up a window with the source code.  Select all, then copy to clipboard.
    4. Make sure there isn't an extra blank paragraph after the DekiScript block! Do this every time you edit!!!  
    5. Save.

     

    A quick note about the examples on this page

    For all the examples on this page, the code is shown before the working example.  The code is shown with the syntax extension, and looks like this:

    MovePage()

    This means that the actual code on the page should be enclosed in a DekiScript block.  If you want to copy the code from this page, then use the same procedure as described in steps 2-4 above. 

    How do I use it?

     

    Arguments

    Name
    Type
    Default
    Description
     page str?  page.path  The Path of the page to move 
     title str?  n/a  New Page Title (renameing) 
     moveto str? n/a The root path to move the page to (This should be the path only without the page title)
     action str? follow What do do after the move (refresh, follow or nothing)
     text str? Move Page Text to place on the button

    Examples

     See the following page for a working example:  Move Page Test Area.

     

    Reference(s)

     All questions and comments should be directed to this forum thread.

    Credits/Special Thanks

     Thanks to sego for challenging me to create this template

     

    Template/Extension Source Code

    // AUTHOR: rberinger
    // Version: 0.9b (Currently Beta)
    // Date:  11/18/2009
    
    dekiapi();
    
    var PageToMove = ($0 ?? $page ?? page.path);
    var NewPageTitle = ($1 ?? $title ?? '');
    var MoveTo = ($2 ?? $moveto ?? '');
    var Action = ($3 ?? $action ?? 'follow'); // refresh, follow, ''
    var ButtonText = ($4 ?? $text ?? 'Move Page');
    
    if(string.startswith(PageToMove, '/')) {
        let PageToMove = string.substr(PageToMove, 1);
    }
    
    // Get the page that we are moving
    var p = wiki.getpage(PageToMove);
    
    // The new page path 
    if(string.LastIndexOf(MoveTo, '/') != (#MoveTo - 1) ) {
        let MoveTo ..= '/';
    }
    
    // We need to get our page title whether new or old
    var ttl = ((NewPageTitle != '') ? NewPageTitle : p.title);
    
    // The pageid of the page to be moved
    var pageID = p.id;
    
    // Account for special characters
    let ttl = string.replace(ttl, ' ', '_');
    let ttl = string.replace(ttl, '/', '//');
    
    // Append the page title to the path
    var NewTitle = MoveTo ..ttl;
    
    // Lets make sure we are not trying to move the page again
    // We have to check this way due to possible page redirects that may exist
    if(p.path != NewTitle) {
        <input id=(@id) class="MoveButton" type="button" value=(ButtonText) ctor="when($this.click) { MovePage(PageToMoveID, NewLoc, MoveAction) }" />
    }
    
    <html><head>
    
        <script type="text/javascript">"
    
            var NewLoc = "..json.emit(NewTitle)..";
            var PageToMoveID = "..json.emit(pageID)..";
            var MoveAction = "..json.emit(string.ToLower(Action))..";
            
        "</script>
    
    <script type="text/javascript">"
    
        var MovePage = function(pidToMove, NewPath, ActionAfterMove) {
            var Site_API = "..json.emit(site.api)..";
            var Site_URI = "..json.emit(site.uri)..";
            
            var Url = Site_API + '/pages/' + pidToMove + '/move?to=' + NewPath;
    
            Deki.$.ajax({
                    type: 'POST',
                    url: Url,
                    complete: function(data, result){
                        if(result == 'success') {
                            if(ActionAfterMove == 'refresh') {
                                window.location = window.location;
                            } else if(ActionAfterMove == 'follow') {
                                location.replace( Site_URI + NewPath ); 
                            } else if(ActionAfterMove == '') {
                            }
                        } else {
                            alert( 'Move Failed' );
                        }
                    }    
             });
    
        }
    
    "</script>
    
    
    </head></html> 

     

    Disclaimers

    This has not been extensively tested as of yet and I'm sure I did not account for every possible scenerio so please test in your environment with some test pages prior to using in production.

    Was this page helpful?
    Tag page
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by