Пример #1
0
 def pagedata(self, url_title, request=None):
     logger.debug(url_title)
     url_title = url_title.encode('utf_8', errors='xmlcharrefreplace')
     page_url = mediawiki.page_data_url(settings.MEDIAWIKI_API, url_title)
     logger.debug(page_url)
     auth = (settings.DANGO_HTPASSWD_USER, settings.DANGO_HTPASSWD_PWD)
     r = requests.get(page_url, auth=auth)
     logger.debug(r.status_code)
     pagedata = json.loads(r.text.encode('utf_8', errors='xmlcharrefreplace'))
     return pagedata
Пример #2
0
 def page(self, url_title, request=None):
     """
     @param page: Page title from URL.
     """
     logger.debug(url_title)
     url_title = url_title.encode('utf_8', errors='xmlcharrefreplace')
     page = Page()
     page.url_title = url_title
     page.uri = reverse('wikiprox-page', args=[url_title])
     page.url = mediawiki.page_data_url(settings.MEDIAWIKI_API, page.url_title)
     logger.debug(page.url)
     auth = (settings.DANGO_HTPASSWD_USER, settings.DANGO_HTPASSWD_PWD)
     r = requests.get(page.url, auth=auth)
     page.status_code = r.status_code
     logger.debug(page.status_code)
     pagedata = json.loads(r.text.encode('utf_8', errors='xmlcharrefreplace'))
     page.error = pagedata.get('error', None)
     if (page.status_code == 200) and not page.error:
         page.public = False
         ## hide unpublished pages on public systems
         #page.public = request.META.get('HTTP_X_FORWARDED_FOR',False)
         # note: header is added by Nginx, should not appear when connected directly
         # to the app server.
         page.published = mediawiki.page_is_published(pagedata)
         page.lastmod = mediawiki.page_lastmod(settings.MEDIAWIKI_API, page.url_title)
         # basic page context
         page.title = pagedata['parse']['displaytitle']
         page.title_sort = page.title
         for prop in pagedata['parse']['properties']:
             if prop.get('name',None) and prop['name'] and (prop['name'] == 'defaultsort'):
                 page.title_sort = prop['*']
         page.sources = mediawiki.find_primary_sources(
             settings.SOURCES_API,
             pagedata['parse']['images'])
         page.body = mediawiki.parse_mediawiki_text(
             pagedata['parse']['text']['*'],
             page.sources,
             page.public,
             False)
         # rewrite media URLs on stage
         # (external URLs not visible to Chrome on Android when connecting through SonicWall)
         if hasattr(settings, 'STAGE') and settings.STAGE and request:
             page.sources = sources.replace_source_urls(page.sources, request)
         page.is_article = encyclopedia.is_article(page.title)
         if page.is_article:
             # only include categories from Category:Articles
             categories_whitelist = [
                 category['title'].split(':')[1]
                 for category in encyclopedia.category_article_types()
             ]
             page.categories = [
                 c['*']
                 for c in pagedata['parse']['categories']
                 if c['*'] in categories_whitelist
             ]
             page.prev_page = encyclopedia.article_prev(page.title)
             page.next_page = encyclopedia.article_next(page.title)
             page.coordinates = mediawiki.find_databoxcamps_coordinates(pagedata['parse']['text']['*'])
             page.authors = mediawiki.find_author_info(pagedata['parse']['text']['*'])
         page.is_author = encyclopedia.is_author(page.title)
         if page.is_author:
             page.author_articles = encyclopedia.author_articles(page.title)
     return page