Example #1
0
def search_pages(**kwargs):
    """
    return a list of pages tagged with all `tags` (boolean AND)
    like [{"absolute_pagename": name, "path": path}]

    :param **kwargs: the search parameters (which are $and'ed together, so a page must match all criteria to be returned). Format:
        {
        tags: a list of tag values like: ['tag1', 'target-language:en']. This is assumed to be valid tags, no checking is performed in this function!
        pagename: a string to (partially) match in the page urls/names
        notags: a special argument that searches for pages with no tags.
        }
    """

    if not len(kwargs):
        # fixme: we should prompt the user for what they want to search
        raise IndexingError('your search query cannot be empty')

    indexing_db = get_indexing_mongo_database()
    if indexing_db is None:
        raise IndexingError("indexing database is not available")
    collection = indexing_db.urn_index

    # construct the mongodb query
    query = {}
    query["current_wikipages"] = {"$not": {"$size": 0}}
    for key in kwargs:
        if key == 'tags' and kwargs[key]:
            query["tags"] = {"$all": kwargs[key]}
        if key == 'pagename':
            query['current_wikipages']['$regex'] = kwargs[key]
            query['current_wikipages']['$options'] = 'i'
        if key == 'notags':
            # special search feature to report all pages without tags
            query["tags"] = {"$size": 0}
            break

    if len(query) > 1:
        query = {'$and': [query]}

    # perform the search
    pages = collection.find(query, {"current_wikipages": 1, "tags": 1}).sort("current_wikipages")
    results = []
    for page in pages:
        absolute_pagename = page["current_wikipages"][0]
        prefix, pagename = split_pagename(absolute_pagename)
        try:
            wns = registered_namespaces[prefix]
        except KeyError:
            # for some reason there's a prefix we don't know about.  move
            # along.
            pass
        else:
            path = "/%s/%s" % (prefix, wns.path_func(pagename))
            results.append({
                "absolute_pagename": absolute_pagename,
                "path": path,
                "tags": page["tags"],
            })

    return results
Example #2
0
def creation_view(request, page_type):
    try:
        view_func = registered_creation_views[page_type]
    except KeyError:
        raise Http404

    response = view_func(request)
    if "target" in request.GET:
        try:
            target = split_pagename(request.GET["target"])
        except WikiPrefixNotProvided:
            raise Http404
        if not is_legal_wiki_pagename(*target):
            raise Http404
        check_edit_permission(request, *target)
    if "target" in request.GET and isinstance(response, SuccessfulEditRedirect):
        page, page_created = WikiPage.objects.get_or_create(name=join_pagename(*target))
        return _handle_successful_wikiedit(request, response, page)
    return response
Example #3
0
def search_pages(**kwargs):
    """
    return a list of pages tagged with all `tags` (boolean AND)
    like [{"absolute_pagename": name, "path": path}]

    :param **kwargs: the search parameters (which are $and'ed together, so a page must match all criteria to be returned). Format:
        {
        tags: a list of tag values like: ['tag1', 'target-language:en']. This is assumed to be valid tags, no checking is performed in this function!
        pagename: a string to (partially) match in the page urls/names
        notags: a special argument that searches for pages with no tags.
        }
    """

    if not len(kwargs):
        # fixme: we should prompt the user for what they want to search
        raise IndexingError('your search query cannot be empty')

    indexing_db = get_indexing_mongo_database()
    if indexing_db is None:
        raise IndexingError("indexing database is not available")
    collection = indexing_db.urn_index

    # construct the mongodb query
    query = {}
    query["current_wikipages"] = {"$not": {"$size": 0}}
    for key in kwargs:
        if key == 'tags' and kwargs[key]:
            query["tags"] = {"$all": kwargs[key]}
        if key == 'pagename':
            query['current_wikipages']['$regex'] = kwargs[key]
            query['current_wikipages']['$options'] = 'i'
        if key == 'notags':
            # special search feature to report all pages without tags
            query["tags"] = {"$size": 0}
            break

    if len(query) > 1:
        query = {'$and': [query]}

    # perform the search
    pages = collection.find(query, {
        "current_wikipages": 1,
        "tags": 1
    }).sort("current_wikipages")
    results = []
    for page in pages:
        absolute_pagename = page["current_wikipages"][0]
        prefix, pagename = split_pagename(absolute_pagename)
        try:
            wns = registered_namespaces[prefix]
        except KeyError:
            # for some reason there's a prefix we don't know about.  move
            # along.
            pass
        else:
            path = "/%s/%s" % (prefix, wns.path_func(pagename))
            results.append({
                "absolute_pagename": absolute_pagename,
                "path": path,
                "tags": page["tags"],
            })

    return results
Example #4
0
 def split_pagename(self):
     return split_pagename(self.name)