Delete page programmatically

Last modified by Vincent Massol on 2021/03/18 11:28

cogThis script allows to programmatically delete a page whose URL is not accessible (since the regular delete is an action, the URL to the page needs to be accessible in order to delete the page).
Type
Category
Developed by

Anca Luca

Rating
3 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Copy the following code and paste it in a page in syntax XWiki 2.1.

The responsibility to protect the access to this script is entirely yours.

Then, to use, follow the instructions in the form displayed by the script. Note that the script is not handling children pages and if you delete a page with children you're gonna either have to delete manually the children or leave some orphaned pages.

{{velocity}}
This script allows to programmatically delete a page whose URL is not accessible (since the regular delete is an action, the URL to the page needs to be accessible in order to delete the page).
The script is using regular authorization mechanism, so the page will be deleted only if the current user has the "delete" right on the page in question. Use at your own risk!

#if("$!request.action" != 'dodelete')
 {{html clean="false"}}
  <form class="xform" method='get' action="">
    <dl>
      <dt>
        <label for="spacename">Space</label>
        <div class="xHint">Fill in the name of the space of the page (all path to the page separated by dot). For nested pages, this needs to be the full name of the page, and the pagename (under) will be WebHome.</div>
      </dt>
      <dd><input name="spacename" id="spacename" type="text"></input></dd>
      <dt>
        <label for="pagename">Page</label>
        <div class="xHint">Fill in the name of the page. For nested pages, this needs to be WebHome.</div>
      </dt>
      <dd><input name="pagename" id="pagename" type="text"></input></dd>      
    </dl>
    <div class="buttonwrapper">
      <input name="action" type='hidden' value='dodelete' />
      <input type="submit" class="button primary" name="dosubmit" value="Preview delete" /> <a href="$escapetool.xml($doc.getURL())" class="button secondary">Cancel</a>
    </div>
  </form>
 {{/html}}
#else
 ## check if form is confirmed
 #set($isConfirmed = false)
 #if ("$!request.confirmed" == 'true')
   #set($isConfirmed = true)
 #end
 ## fetch the page to delete to see if it exists and it if has children
 #set($spacename = "$!request.spacename")
 #set($pagename = "$!request.pagename")
 #if ($spacename != '' && $pagename != '')
   #set($spaceToDeleteRef = $services.model.resolveSpace($spacename))
   #set($docToDeleteRef = $services.model.createDocumentReference($pagename, $spaceToDeleteRef))
   #set($docToDelete = $xwiki.getDocument($docToDeleteRef))
   #set($docExists = !($docToDelete.isNew()))
    |**Reference**|{{{$docToDeleteRef}}}
    |**Document**|{{{$docToDelete}}}
    |**Document exists**|$docExists
   ## check the children of this document and list them, if it's the case
   #if ($pagename == 'WebHome')
     #set($queryParams = [])
     #set($discard = $queryParams.add("$!{spacename}.%"))
     #set($discard = $queryParams.add("$!{spacename}.WebHome"))
     #set($query = $services.query.hql("WHERE doc.fullName LIKE ? AND doc.fullName <> ? ORDER BY doc.fullName asc").addFilter('unique').bindValues($queryParams))
     #set($docToDeleteChildren = $query.execute())
     #if ($docToDeleteChildren.size() > 0)
        |**Children**|((({{warning}}Attention, the children will not be deleted!{{/warning}}

       #foreach($docChild in $docToDeleteChildren)
         #if ($velocityCount <= 10)
            * $docChild
         #else
            * ...and $mathtool.sub($docToDeleteChildren.size(), 10) more
           #break
         #end
       #end
        )))
     #end
   #end

   ##
   ## the confirmation form or perform, once all this information was displayed
   ##
   #if ($docExists)
     #if(!$isConfirmed)
       ## delete is not confirmed, display the confirm form, hidden
       {{html clean="false"}}
          <form class="xform" method="post" action="">
            <div class="hidden">
              <input name="spacename" value="$escapetool.xml($spacename)" type='hidden'/>
              <input name="pagename" value="$escapetool.xml($pagename)" type='hidden'/>
              <input name="action" value="dodelete" type='hidden' />
              <input name="confirmed" value="true" type="hidden" />
            </div>
            <div class="buttonwrapper">
              <input type="submit" class="button primary" name="dosubmit" value="Delete" /> <a href="$escapetool.xml($doc.getURL())" class="button secondary">Cancel</a>
            </div>
          </form>
       {{/html}}
     #else
       ## delete is confirmed, delete the document and display a message
       #set($discard = $docToDelete.delete())
       {{success}}The document was successfully deleted.{{/success}}
     #end
   #else
     {{warning}}The document that you're trying to delete does not exist, nothing will be done.{{/warning}}
   #end
 #else
   {{error}}The space name or the page name are not filled in. In case of doubt for the page name (because nested page), use WebHome.{{/error}}
 #end
#end
{{/velocity}}
Tags:
     

Get Connected