"""
Portal configuration functions.

Perform cleaning of portal's page configuration
Usage
admin = portaladmin.PortalAdmin(Content)
admin.deletePagesByCname(cleanPagesPattern, excludePagesPattern)
where patterns are the substrings that will be matched with 
unique names of pages and labels. 
A page is deleted if its unique name contains cleanPagesPattern substring and 
if it DOES NOT contain excludePagesPattern.
"""

import sys
import util


class PortalAdmin:


    def __init__(self, content):
        self.content = content

    pagesToDelete = []
    cnameSubstrToExclude=None;

    def deletePagesByCname(self, cnameSubstrToDelete, cnameSubstrToExclude):
        print "Attempting to delete pages with title/unique name containing '%s'. This was specified by the property 'portal.clean.pattern'" % cnameSubstrToDelete
        # make sure that we search the entire tree
        self.content.select("content", "root")
        pagesToDeleteS = self.content.search("All", "cname", cnameSubstrToDelete)
        if pagesToDeleteS==None:
            print "No matching pages"
            return
        if not util.isEmpty(cnameSubstrToExclude):
            self.cnameSubstrToExclude=cnameSubstrToExclude
            print "Based on the specified exclusion pattern, pages with unique names containing '%s' will not be deleted" % cnameSubstrToExclude

        self.pagesToDelete = pagesToDeleteS.split()
        for pageId in self.pagesToDelete:
            if pageId:
                self.deleteHierarchy(pageId)



    def deleteHierarchy( self, pageId ):
        self.content.select(pageId)
        # delete children first since "delete" does not handle hierarchies
        children = self.content.children()
        if children:
            for  child in children.split():
                self.deleteHierarchy( child )

        self.deletePage(pageId)


    def deletePage (self, pageId ):
        uname=self.content.get(pageId, "uname")
        # delete only if the page is not part of the exclude pattern
        if self.cnameSubstrToExclude==None or self.cnameSubstrToExclude=="" or uname==None or uname.find(self.cnameSubstrToExclude)<0:
            # make sure there are no children
            if self.content.children(pageId)!=None:
                print "Page with uname %s will not be deleted because it has descendants that were not deleted because they matched the exclusion pattern" % uname
            else:
                print "Deleting the page "+self.pageToString(pageId)
                self.content.delete(pageId)

        else:
            print "Page with uname %s will not be deleted because it contains the substring specified in the exclusion pattern %s" % (uname, self.cnameSubstrToExclude)

        # remove the deleted item if it is among the candidates for deletion
        #print self.pagesToDelete
        try:
            pageI=self.pagesToDelete.index(pageId)
            # update the page id so we don't delete this page twice
            self.pagesToDelete[pageI]=None
        except ValueError:
            pass



    def pageToString(self, pageId):
        pageS = self.content.get(pageId, "cname")
        return pageS





