Skip to content

Commit

Permalink
Merge pull request #696 from Pchelolo/pdf
Browse files Browse the repository at this point in the history
Added PDF endpoint
  • Loading branch information
gwicke committed Nov 29, 2016
2 parents 64fc109 + 7cdee92 commit 812c4dd
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config.example.wikimedia.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ default_project: &default_project
cache_control: s-maxage=86400, max-age=86400
# 10 days Varnish caching, one day client-side
purged_cache_control: s-maxage=864000, max-age=86400
pdf:
# Cache PDF for 5 minutes since it's not purged
cache_control: s-maxage=600, max-age=600
uri: https://pdf-electron.wmflabs.org
secret: secret
skip_updates: false

# A different project template, sharing configuration options.
Expand Down
5 changes: 5 additions & 0 deletions config.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ default_project: &default_project
host: http://appservice.wmflabs.org
events: {}
purged_cache_control: test_purged_cache_control
pdf:
# Cache PDF for 5 minutes since it's not purged
cache_control: s-maxage=600, max-age=600
uri: https://pdf-electron.wmflabs.org
secret: secret
skip_updates: false

labs_project: &labs_project
Expand Down
2 changes: 2 additions & 0 deletions projects/wmf_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ paths:
- path: v1/random.yaml
options: '{{merge({"random_cache_control": "s-maxage=2, max-age=1"},
options.mobileapps)}}'
- path: v1/pdf.yaml
options: '{{options.pdf}}'
/feed:
x-modules:
- path: v1/feed.js
Expand Down
2 changes: 2 additions & 0 deletions projects/wmf_wiktionary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ paths:
options:
response_cache_control: '{{options.purged_cache_control}}'
host: '{{options.mobileapps.host}}'
- path: v1/pdf.yaml
options: '{{options.pdf}}'
/transform:
x-modules:
- path: v1/transform.yaml
Expand Down
1 change: 0 additions & 1 deletion test/features/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const assert = require('../utils/assert.js');
const server = require('../utils/server.js');
const preq = require('preq');
const P = require('bluebird');

function assertStorageRequest(requests, method, bucket, expected) {
const storageRequests = requests.filter((log) =>
Expand Down
25 changes: 25 additions & 0 deletions test/features/pdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const assert = require('../utils/assert.js');
const server = require('../utils/server.js');
const preq = require('preq');

describe('Feed', () => {

before(() => server.start());

it('Should get PDF for a page', () => {
return preq.get({
uri: `${server.config.hostPort}/ru.wikipedia.org/v1/page/pdf/%D0%94%D0%B0%D1%80%D1%82_%D0%92%D0%B5%D0%B9%D0%B4%D0%B5%D1%80`
})
.then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.headers['content-disposition'],
'attachment; filename=%D0%94%D0%B0%D1%80%D1%82_%D0%92%D0%B5%D0%B9%D0%B4%D0%B5%D1%80.pdf;'
+ ' filename*=%D0%94%D0%B0%D1%80%D1%82_%D0%92%D0%B5%D0%B9%D0%B4%D0%B5%D1%80.pdf');
assert.deepEqual(res.headers['content-type'], 'application/pdf');
assert.ok(/"\d+\/[\d\w-]+"/.test(res.headers.etag));
assert.ok(res.body.length !== 0);
});
});
});
89 changes: 89 additions & 0 deletions v1/pdf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
swagger: '2.0'
info:
version: '1.0.0-beta'
title: MediaWiki PDF API
description: Page PDF Render API
termsOfService: https://github.com/wikimedia/restbase#restbase
contact:
name: Services
email: services@lists.wikimedia.org
url: https://www.mediawiki.org/wiki/Services
license:
name: Apache licence, v2
url: https://www.apache.org/licenses/LICENSE-2.0
paths:
/pdf/{title}:
x-route-filters:
- path: ./lib/revision_table_access_check_filter.js
options:
redirect_cache_control: '{{options.cache_control}}'
get:
tags:
- Page content
summary: Get a page as PDF
description: |
Renders the page content as PDF.
Stability: [experimental](https://www.mediawiki.org/wiki/API_versioning#Experimental)
produces:
- application/pdf
parameters:
- name: title
in: path
description: "Page title. Use underscores instead of spaces. Example: `Main_Page`."
type: string
required: true
responses:
'200':
description: The PDF render of an article
schema:
type: file
headers:
ETag:
description: >
Syntax: "{revision}/{tid}".
Example: "701384379/154d7bca-c264-11e5-8c2f-1b51b33b59fc"
'404':
description: Unknown page title
schema:
$ref: '#/definitions/problem'
default:
description: Error
schema:
$ref: '#/definitions/problem'

x-request-handler:
- get_latest_revision:
request:
method: get
uri: /{domain}/sys/page_revisions/page/{title}
- get_pdf_from_backend:
request:
method: get
uri: '{{options.uri}}/pdf?accessKey={options.secret}&url=https://{{domain}}/wiki/{title}'
return:
status: 200
headers:
# Firefox supports filename*= syntax while Chrome respect filename=. Safari is stupid and understands neither
content-disposition: 'attachment; filename={request.params.title}.pdf; filename*={request.params.title}.pdf'
content-type: '{{get_pdf_from_backend.headers.content-type}}'
content-length: '{{get_pdf_from_backend.headers.content-length}}'
cache-control: '{{default(options.cache_control, "s-maxage=600, max-age=600")}}'
etag: '{{get_latest_revision.headers.etag}}'
body: '{{get_pdf_from_backend.body}}'
x-monitor: false # PDF generation is expensive and it's not stored, so don't run checker script

definitions:
# A https://tools.ietf.org/html/draft-nottingham-http-problem
problem:
required:
- type
properties:
type:
type: string
title:
type: string
detail:
type: string
instance:
type: string

0 comments on commit 812c4dd

Please sign in to comment.