r36667 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r36666‎ | r36667 | r36668 >
Date:12:14, 26 June 2008
Author:jojo
Status:old
Tags:
Comment:
Updated pdfserver for the new mwlib API. Interface has changed slighty, extension code adjusted.
Modified paths:
  • /trunk/extensions/Collection/Collection.body.php (modified) (history)
  • /trunk/extensions/Collection/pdf-server/README.txt (modified) (history)
  • /trunk/extensions/Collection/pdf-server/pdfserver.py (modified) (history)

Diff [purge]

Index: trunk/extensions/Collection/Collection.body.php
@@ -518,10 +518,12 @@
519519 global $wgServer;
520520 global $wgScriptPath;
521521
522 - $response = self::pdfServerCommand( 'pdf_generate', array(
 522+ $response = self::pdfServerCommand( 'render', array(
523523 'metabook' => $this->buildJSONCollection( $collection ),
524524 'base_url' => $wgServer . $wgScriptPath,
525525 'template_blacklist' => $wgPDFTemplateBlacklist,
 526+ 'writer' => 'rl',
 527+ 'content_type' => 'application/pdf',
526528 ) );
527529
528530 if ( !$response ) {
@@ -544,7 +546,7 @@
545547 $collection_id = $wgRequest->getVal( 'collection_id' );
546548 $return_to = $wgRequest->getVal( 'return_to' );
547549
548 - $response = self::pdfServerCommand( 'pdf_status', array(
 550+ $response = self::pdfServerCommand( 'render_status', array(
549551 'collection_id' => $collection_id,
550552 ) );
551553 if ( !$response ) {
@@ -563,7 +565,7 @@
564566 );
565567 $wgOut->addMeta( 'http:refresh', '2; URL=' . $url );
566568 $wgOut->setPageTitle( wfMsg( 'coll-generating_pdf_title' ) );
567 - $wgOut->addWikiText( wfMsgNoTrans( 'coll-generating_pdf_text', $response->progress ) );
 569+ $wgOut->addWikiText( wfMsgNoTrans( 'coll-generating_pdf_text', $response->status->progress ) );
568570 break;
569571 case 'finished':
570572 $wgOut->setPageTitle( wfMsg( 'coll-pdf_finished_title' ) );
@@ -592,7 +594,7 @@
593595 global $wgOut;
594596 global $wgRequest;
595597
596 - $response = self::pdfServerCommand( 'pdf_download', array(
 598+ $response = self::pdfServerCommand( 'download', array(
597599 'collection_id' => $wgRequest->getVal( 'collection_id' ),
598600 ), $decode=false );
599601
Index: trunk/extensions/Collection/pdf-server/pdfserver.py
@@ -14,11 +14,11 @@
1515 # accompanied script clean-cache.py.
1616 cache_dir = '/var/cache/pdfserver/'
1717
18 -# (Path to) mw-pdf executable.
19 -mwpdf_cmd = 'mw-pdf'
 18+# (Path to) mw-render executable.
 19+mwrender_cmd = 'mw-render'
2020
21 -# Logfile for mw-pdf.
22 -mwpdf_logfile = '/var/log/mw-pdf.log'
 21+# Logfile for mw-render.
 22+mwrender_logfile = '/var/log/mw-render.log'
2323
2424 # (Path to) mw-zip executable.
2525 mwzip_cmd = 'mw-zip'
@@ -48,7 +48,6 @@
4949 import StringIO
5050 import subprocess
5151 import time
52 -import urllib
5352
5453 def uid(max_length=10):
5554 f = StringIO.StringIO()
@@ -60,9 +59,10 @@
6160
6261 class PDFServer(object):
6362 metabook_filename = 'metabook.json'
64 - pdf_filename = 'collection.pdf'
 63+ contenttype_filename = 'content_type.txt'
6564 error_filename = 'errors.txt'
66 - progress_filename = 'progress.txt'
 65+ status_filename = 'status.txt'
 66+ output_filename = 'output'
6767
6868 def __init__(self, form):
6969 self.form = form
@@ -133,14 +133,20 @@
134134 print # end of headers
135135 print content,
136136
137 - def do_pdf_generate(self):
 137+ def do_render(self):
138138 metabook_data = self.form.getvalue('metabook')
139139 if not metabook_data:
140140 return self.error_response('metabook argument required')
141141 base_url = self.form.getvalue('base_url')
142142 if not base_url:
143143 return self.error_response('base_url argument required')
144 -
 144+ writer = self.form.getvalue('writer')
 145+ if not writer:
 146+ return self.error_response('writer argument required')
 147+ content_type = self.form.getvalue('content_type')
 148+ if not content_type:
 149+ return self.error_response('content_type argument required')
 150+ writer_options = self.form.getvalue('writer_options')
145151 template_blacklist = self.form.getvalue('template_blacklist')
146152
147153 collection_id = self.new_collection()
@@ -150,34 +156,42 @@
151157 f.write(metabook_data)
152158 f.close()
153159
 160+ contenttype_path = self.get_path(collection_id, self.contenttype_filename)
 161+ f = open(contenttype_path, 'wb')
 162+ f.write(content_type)
 163+ f.close()
 164+
154165 args=[
155 - mwpdf_cmd,
 166+ mwrender_cmd,
156167 '--daemonize',
157 - '--logfile', mwpdf_logfile,
158 - '--errorfile', self.get_path(collection_id, self.error_filename),
 168+ '--logfile', mwrender_logfile,
 169+ '--error-file', self.get_path(collection_id, self.error_filename),
 170+ '--status-file', self.get_path(collection_id, self.status_filename),
159171 '--metabook', metabook_path,
160172 '--conf', base_url,
161 - '--progress', self.get_path(collection_id, self.progress_filename),
162 - '--output', self.get_path(collection_id, self.pdf_filename),
 173+ '--writer', writer,
 174+ '--output', self.get_path(collection_id, self.output_filename),
163175 ]
 176+ if writer_options:
 177+ args.extend(['--writer-options', writer_options])
164178 if template_blacklist:
165179 args.extend(['--template-blacklist', template_blacklist])
166180
167181 try:
168 - rc = subprocess.call(executable=mwpdf_cmd, args=args)
169 - except IOError, e:
170 - raise RuntimeError('Could not execute command %r: %s' % (mwpdf_cmd, e))
 182+ rc = subprocess.call(executable=mwrender_cmd, args=args)
 183+ except OSError, e:
 184+ raise RuntimeError('Could not execute command %r: %s' % (mwrender_cmd, e))
171185 if rc != 0:
172 - return self.error_response('command %r failed: rc = %d' % (mwpdf_cmd, rc))
 186+ return self.error_response('command %r failed: rc = %d' % (mwrender_cmd, rc))
173187
174188 return self.json_response({
175189 'collection_id': collection_id,
176190 })
177191
178 - def do_pdf_status(self):
 192+ def do_render_status(self):
179193 collection_id = self.get_collection()
180194
181 - if os.path.exists(self.get_path(collection_id, self.pdf_filename)):
 195+ if os.path.exists(self.get_path(collection_id, self.output_filename)):
182196 return self.json_response({
183197 'collection_id': collection_id,
184198 'state': 'finished',
@@ -193,19 +207,24 @@
194208 })
195209
196210 try:
197 - progress = int(open(self.get_path(collection_id, self.progress_filename), 'rb').read())
198 - except IOError:
199 - progress = 0
 211+ f = open(self.get_path(collection_id, self.status_filename), 'rb')
 212+ status = simplejson.loads(f.read())
 213+ f.close()
 214+ except (IOError, ValueError):
 215+ status = {'progress': 0}
200216 return self.json_response({
201217 'collection_id': collection_id,
202218 'state': 'progress',
203 - 'progress': progress,
 219+ 'status': status,
204220 })
205221
206 - def do_pdf_download(self):
 222+ def do_download(self):
 223+ collection_id = self.get_collection()
 224+ content_type = open(self.get_path(collection_id, self.contenttype_filename), 'rb').read()
 225+ content = open(self.get_path(collection_id, self.output_filename), 'rb').read()
207226 return {
208 - 'content_type': 'application/pdf',
209 - 'content': open(self.get_path(self.get_collection(), self.pdf_filename)).read()
 227+ 'content_type': content_type,
 228+ 'content': content,
210229 }
211230
212231 def do_zip_post(self):
@@ -237,7 +256,7 @@
238257 args.extend(['--template-blacklist', template_blacklist])
239258 try:
240259 rc = subprocess.call(executable=mwzip_cmd, args=args)
241 - except IOError, e:
 260+ except OSError, e:
242261 raise RuntimeError('Could not execute command %r: %s' % (mwzip_cmd, e))
243262 if rc != 0:
244263 return self.error_response('command %r failed: rc = %d' % (mwzip_cmd, rc))
Index: trunk/extensions/Collection/pdf-server/README.txt
@@ -37,7 +37,7 @@
3838 cd reportlab/reportlab
3939 python setup.py install
4040
41 -*mwlib*
 41+*mwlib, version >= 0.7*
4242 You need to have setuptools/easy_install installed.
4343 Installation should be as easy as typing::
4444
@@ -48,7 +48,7 @@
4949
5050 python setup.py install
5151
52 -*mwlib.rl*
 52+*mwlib.rl, version >= 0.7*
5353 Install *mwlib.rl* with ``easy_install``::
5454
5555 easy_install mwlib.rl

Status & tagging log