# copyright 2007 ETH Zurich, DMATL
# author lorenz textor

def process_version(version):
    if not version:
        return True
    docElement = version.content.firstChild
    nodes = docElement.getElementsByTagName('source')
    for node in nodes:
        id = str(node.getAttribute('id').strip())
        try:
            checked.append(version.restrictedTraverse(id))
        except:
            missing.append(id)
            return False
    return True

def unique(s):
    u = {}
    for x in s:
        u[x] = 1
    return u.keys()


CONTAINERS = ['Silva Folder','Silva Publication', 'Folder']
CONTENT = ['Silva Document', 'Silva Personal Information', 'Silva VVZ Page', 'IMAP Calendar List']

checked = []
missing = []
ok = []
bad = []

def scan_objs(this_container):
    for obj in this_container.objectValues(CONTENT):
        try:
            viewable = obj.get_viewable()
        except:
            viewable = None
        try:
            editable = obj.get_editable()
        except:
            editable = None
        if viewable and not process_version(viewable):
            bad.append(obj)
        elif viewable:
            ok.append(obj)
        if editable and not process_version(editable):
            bad.append(obj)
        elif editable:
            ok.append(obj)
    
def scan_objs_in(this_container):
    scan_objs(this_container)
    for folder in this_container.objectValues(CONTAINERS):
        scan_objs_in(folder)

scan_objs_in(context)

print '<p>%d pages with missing code sources:</p>' % len(bad)
print '<br />'.join(unique([x.absolute_url_path() for x in bad]))
print '<p>%d missing code sources:</p>' % len(missing)
print '<br />'.join(unique(missing))
print '<p>%d pages with good code sources</p>' % len(unique(ok))
# print '<br />'.join([x.absolute_url_path() for x in ok])
print '<p>%d good code sources:</p>' % len(checked)
print '<br />'.join(unique([x.absolute_url_path() for x in checked]))
return printed
