Index: branches/lqt-updates/extensions/LiquidThreads/api/ApiLqtFormatter.php |
— | — | @@ -0,0 +1,89 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class ApiLqtFormatter extends ApiBase { |
| 5 | + |
| 6 | + public function execute() { |
| 7 | + $params = $this->extractRequestParams(); |
| 8 | + $result = $this->getResult(); |
| 9 | + |
| 10 | + $formatter = $this->getFormatter( $params ); |
| 11 | + |
| 12 | + $ctxClass = $formatter->getContextClass(); |
| 13 | + $context = new $ctxClass; |
| 14 | + |
| 15 | + $contextParams = array( |
| 16 | + 'base-url', |
| 17 | + 'nesting-level', |
| 18 | + 'single', |
| 19 | + 'timestamp', |
| 20 | + ); |
| 21 | + |
| 22 | + foreach( $contextParams as $param ) { |
| 23 | + if ( $params[$param] !== null ) { |
| 24 | + $context->set($param, $params[$param]); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + $object = LiquidThreadsObject::retrieve( $params['object'] ); |
| 29 | + |
| 30 | + $output = array( |
| 31 | + 'html' => $formatter->getHTML($object, $context), |
| 32 | + ); |
| 33 | + |
| 34 | + $result->addValue( null, 'form', $output ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get an array of valid forms and their corresponding classes. |
| 39 | + */ |
| 40 | + public function getFormatters() { |
| 41 | + return array( |
| 42 | + 'post' => 'LiquidThreadsPostFormatter', |
| 43 | + 'topic' => 'LiquidThreadsTopicFormatter', |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates the appropriate LiquidThreadsEditForm object |
| 49 | + * @param $params Array: The parameters passed to the API module. |
| 50 | + */ |
| 51 | + public function getFormatter( $params ) { |
| 52 | + global $wgUser; |
| 53 | + |
| 54 | + $formName = $params['formatter']; |
| 55 | + |
| 56 | + if ( $formName == 'post' ) { |
| 57 | + $formatter = LiquidThreadsPostFormatter::singleton(); |
| 58 | + } elseif ( $formName == 'topic' ) { |
| 59 | + $formatter = LiquidThreadsTopicFormatter::singleton(); |
| 60 | + } else { |
| 61 | + $this->dieUsage( "Not yet implemented", 'not-implemented' ); |
| 62 | + } |
| 63 | + |
| 64 | + return $formatter; |
| 65 | + } |
| 66 | + |
| 67 | + public function getAllowedParams() { |
| 68 | + return array( |
| 69 | + 'formatter' => array( |
| 70 | + ApiBase::PARAM_REQUIRED => true, |
| 71 | + ApiBase::PARAM_TYPE => array_keys( $this->getFormatters() ), |
| 72 | + ), |
| 73 | + |
| 74 | + ## Params for post |
| 75 | + 'base-url' => null, |
| 76 | + 'nesting-level' => null, |
| 77 | + 'single' => null, |
| 78 | + |
| 79 | + ## Shared params |
| 80 | + 'object' => array( |
| 81 | + ApiBase::PARAM_REQUIRED => true, |
| 82 | + ), |
| 83 | + 'timestamp' => null, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + public function getVersion() { |
| 88 | + return __CLASS__ . ': $Id: ApiLqtForm.php 79941 2011-01-10 17:18:57Z hartman $'; |
| 89 | + } |
| 90 | +} |