Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
41.67% covered (danger)
41.67%
15 / 36
50.00% covered (danger)
50.00%
5 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiFlowBase
41.67% covered (danger)
41.67%
15 / 36
50.00% covered (danger)
50.00%
5 / 10
74.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getBlockParams
n/a
0 / 0
n/a
0 / 0
0
 needsPage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAction
n/a
0 / 0
n/a
0 / 0
0
 getLoader
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getModerationStates
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 processError
44.44% covered (danger)
44.44%
4 / 9
0.00% covered (danger)
0.00%
0 / 1
9.29
 encodeParamName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Api;
4
5use ApiBase;
6use ApiMessage;
7use Flow\Block\AbstractBlock;
8use Flow\Container;
9use Flow\Model\AbstractRevision;
10use Flow\WorkflowLoader;
11use Flow\WorkflowLoaderFactory;
12use MediaWiki\Status\Status;
13use MediaWiki\Title\Title;
14
15abstract class ApiFlowBase extends ApiBase {
16
17    /**
18     * @var WorkflowLoader
19     */
20    protected $loader;
21
22    /**
23     * @var Title
24     */
25    protected $page;
26
27    /**
28     * @var ApiFlow
29     */
30    protected $apiFlow;
31
32    /**
33     * @param ApiFlow $api
34     * @param string $modName
35     * @param string $prefix
36     */
37    public function __construct( $api, $modName, $prefix = '' ) {
38        $this->apiFlow = $api;
39        parent::__construct( $api->getMain(), $modName, $prefix );
40    }
41
42    /**
43     * @return array
44     */
45    abstract protected function getBlockParams();
46
47    /**
48     * Returns true if the submodule required the page parameter to be set.
49     * Most submodules will need to be passed the page the API request is for.
50     * For some, this is not needed at all.
51     *
52     * @return bool
53     */
54    public function needsPage() {
55        return true;
56    }
57
58    /**
59     * @param Title $page
60     */
61    public function setPage( Title $page ) {
62        $this->page = $page;
63    }
64
65    /**
66     * Return the name of the flow action
67     * @return string
68     */
69    abstract protected function getAction();
70
71    /**
72     * @return WorkflowLoader
73     */
74    protected function getLoader() {
75        if ( $this->loader === null ) {
76            /** @var WorkflowLoaderFactory $factory */
77            $factory = Container::get( 'factory.loader.workflow' );
78            $this->loader = $factory->createWorkflowLoader( $this->page );
79        }
80
81        return $this->loader;
82    }
83
84    /**
85     * @param bool $addAliases
86     * @return string[]
87     */
88    protected function getModerationStates( $addAliases = true ) {
89        $states = [
90            AbstractRevision::MODERATED_NONE,
91            AbstractRevision::MODERATED_DELETED,
92            AbstractRevision::MODERATED_HIDDEN,
93            AbstractRevision::MODERATED_SUPPRESSED,
94        ];
95
96        if ( $addAliases ) {
97            // aliases for AbstractRevision::MODERATED_NONE
98            $states = array_merge( $states, [
99                'restore', 'unhide', 'undelete', 'unsuppress',
100            ] );
101        }
102
103        return $states;
104    }
105
106    /**
107     * Kill the request if errors were encountered.
108     *
109     * @param AbstractBlock[] $blocks
110     */
111    protected function processError( $blocks ) {
112        $status = Status::newGood();
113        foreach ( $blocks as $block ) {
114            if ( $block->hasErrors() ) {
115                foreach ( $block->getErrors() as $key ) {
116                    $status->fatal( ApiMessage::create(
117                        $block->getErrorMessage( $key ), $key, [ $key => $block->getErrorExtra( $key ) ]
118                    ) );
119                }
120            }
121        }
122        if ( !$status->isGood() ) {
123            $this->dieStatus( $status );
124        }
125    }
126
127    /**
128     * Override prefix on CSRF token so the same code can be reused for
129     * all modules.  This is a *TEMPORARY* hack please remove as soon as
130     * unprefixed tokens are working correctly again (bug 70099).
131     *
132     * @param string $paramName
133     * @return string
134     */
135    public function encodeParamName( $paramName ) {
136        return $paramName === 'token'
137            ? $paramName
138            : parent::encodeParamName( $paramName );
139    }
140
141    /**
142     * @inheritDoc
143     */
144    public function getHelpUrls() {
145        return [
146            'https://www.mediawiki.org/wiki/Extension:Flow/API#' . $this->getAction(),
147        ];
148    }
149
150    /**
151     * @inheritDoc
152     */
153    public function needsToken() {
154        return 'csrf';
155    }
156
157    /**
158     * @inheritDoc
159     */
160    public function getParent() {
161        return $this->apiFlow;
162    }
163
164}