Ejemplo n.º 1
0
 public function execute()
 {
     $action = $this->getMain()->getVal('type');
     $page = $this->getMain()->getVal('pageid');
     try {
         // If post is set, get the post object by id
         // By fetching the post object, we also validate the id
         $postList = $this->getMain()->getVal('postid');
         $postList = $this->parsePostList($postList);
         switch ($action) {
             case 'list':
                 if (!$page) {
                     $this->dieNoParam('pageid');
                 }
                 $this->getResult()->addValue(null, $this->getModuleName(), $this->fetchPosts($page));
                 break;
             case 'like':
                 if (!$postList) {
                     $this->dieNoParam('postid');
                 }
                 foreach ($postList as $post) {
                     $post->setUserAttitude($this->getUser(), Post::ATTITUDE_LIKE);
                 }
                 $this->getResult()->addValue(null, $this->getModuleName(), '');
                 break;
             case 'dislike':
                 if (!$postList) {
                     $this->dieNoParam('postid');
                 }
                 foreach ($postList as $post) {
                     $post->setUserAttitude($this->getUser(), Post::ATTITUDE_NORMAL);
                 }
                 $this->getResult()->addValue(null, $this->getModuleName(), '');
                 break;
             case 'report':
                 if (!$postList) {
                     $this->dieNoParam('postid');
                 }
                 foreach ($postList as $post) {
                     $post->setUserAttitude($this->getUser(), Post::ATTITUDE_REPORT);
                 }
                 $this->getResult()->addValue(null, $this->getModuleName(), '');
                 break;
             case 'delete':
                 if (!$postList) {
                     $this->dieNoParam('postid');
                 }
                 foreach ($postList as $post) {
                     $post->delete($this->getUser());
                 }
                 $this->getResult()->addValue(null, $this->getModuleName(), '');
                 break;
             case 'recover':
                 if (!$postList) {
                     $this->dieNoParam('postid');
                 }
                 foreach ($postList as $post) {
                     $post->recover($this->getUser());
                 }
                 $this->getResult()->addValue(null, $this->getModuleName(), '');
                 break;
             case 'erase':
                 if (!$postList) {
                     $this->dieNoParam('postid');
                 }
                 foreach ($postList as $post) {
                     $post->erase($this->getUser());
                 }
                 $this->getResult()->addValue(null, $this->getModuleName(), '');
                 break;
             case 'post':
                 if (!$page) {
                     $this->dieNoParam('pageid');
                 }
                 $text = $this->getMain()->getVal('content');
                 if (!$text) {
                     $this->dieNoParam('content');
                 }
                 // Permission check
                 Post::checkIfCanPost($this->getUser());
                 $spam = !SpamFilter::validate($text);
                 // Parse as wikitext if specified
                 if ($this->getMain()->getCheck('wikitext')) {
                     $parser = new \Parser();
                     $opt = new \ParserOptions($this->getUser());
                     $opt->setEditSection(false);
                     $output = $parser->parse($text, \Title::newFromId($page), $opt);
                     $text = $output->getText();
                     unset($parser);
                     unset($opt);
                     unset($output);
                 }
                 $data = array('id' => null, 'pageid' => $page, 'userid' => $this->getUser()->getId(), 'username' => $this->getUser()->getName(), 'text' => $text, 'parentid' => count($postList) ? $postList[0]->id : null, 'status' => $spam ? Post::STATUS_SPAM : Post::STATUS_NORMAL, 'like' => 0, 'report' => 0);
                 $postObject = new Post($data);
                 global $wgMaxNestLevel;
                 // Restrict max nest level
                 if ($postObject->getNestLevel() > $wgMaxNestLevel) {
                     $postObject->parentid = $postObject->getParent()->parentid;
                     $postObject->parent = $postObject->getParent()->parent;
                 }
                 $postObject->post();
                 if ($spam) {
                     global $wgTriggerFlowThreadHooks;
                     if ($wgTriggerFlowThreadHooks) {
                         \Hooks::run('FlowThreadSpammed', array($postObject));
                     }
                 }
                 $this->getResult()->addValue(null, $this->getModuleName(), '');
                 break;
             default:
                 $this->dieUsage("Unrecognized value for parameter 'type': {$action}", 'unknown_type');
         }
     } catch (\UsageException $e) {
         throw $e;
     } catch (\Exception $e) {
         $this->getResult()->addValue("error", 'code', 'unknown_error');
         $this->getResult()->addValue("error", 'info', $e->getMessage());
     }
     return true;
 }