Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ThreadActionPage
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getPageName
n/a
0 / 0
n/a
0 / 0
0
 getFormFields
n/a
0 / 0
n/a
0 / 0
0
 getRightRequirement
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 checkParameters
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 getSubmitText
n/a
0 / 0
n/a
0 / 0
0
 buildForm
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 trySubmit
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3use MediaWiki\HTMLForm\HTMLForm;
4use MediaWiki\MediaWikiServices;
5use MediaWiki\Output\OutputPage;
6use MediaWiki\Request\WebRequest;
7use MediaWiki\SpecialPage\UnlistedSpecialPage;
8use MediaWiki\Title\Title;
9use MediaWiki\User\User;
10
11abstract class ThreadActionPage extends UnlistedSpecialPage {
12    /** @var User */
13    protected $user;
14    /** @var OutputPage */
15    protected $output;
16    /** @var WebRequest */
17    protected $request;
18    /** @var Title|null */
19    protected $title;
20    /** @var Thread|null */
21    protected $mThread;
22    /** @var string|null */
23    protected $mTarget;
24
25    public function __construct() {
26        parent::__construct( $this->getPageName(), $this->getRightRequirement() );
27        $this->mIncludable = false;
28
29        $this->output = $this->getOutput();
30        $this->user = $this->getUser();
31        $this->request = $this->getRequest();
32    }
33
34    abstract public function getPageName();
35
36    abstract public function getFormFields();
37
38    protected function getRightRequirement() {
39        return '';
40    }
41
42    public function execute( $par ) {
43        if ( !$this->userCanExecute( $this->getUser() ) ) {
44            $this->displayRestrictionError();
45        }
46
47        // Page title
48        $this->getOutput()->setPageTitle( $this->getDescription() );
49
50        if ( !$this->checkParameters( $par ) ) {
51            return;
52        }
53
54        $form = $this->buildForm();
55        $form->show();
56    }
57
58    /**
59     * Loads stuff like the thread and so on
60     *
61     * @param string|null $par
62     * @return bool
63     */
64    public function checkParameters( $par ) {
65        // Handle parameter
66        $this->mTarget = $par;
67        if ( $par === null || $par === "" ) {
68            $this->output->addHTML( $this->msg( 'lqt_threadrequired' )->escaped() );
69            return false;
70        }
71
72        $thread = Threads::withRoot(
73            MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle(
74                Title::newFromText( $par )
75            )
76        );
77        if ( !$thread ) {
78            $this->output->addHTML( $this->msg( 'lqt_nosuchthread' )->escaped() );
79            return false;
80        }
81
82        $this->mThread = $thread;
83
84        return true;
85    }
86
87    abstract public function getSubmitText();
88
89    public function buildForm() {
90        $form = new HTMLForm(
91            $this->getFormFields(),
92            $this->getContext(),
93            'lqt-' . $this->getPageName()
94        );
95
96        $form->setSubmitText( $this->getSubmitText() );
97        $form->setSubmitCallback( [ $this, 'trySubmit' ] );
98
99        return $form;
100    }
101
102    abstract public function trySubmit( $data );
103}