Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
44.12% covered (danger)
44.12%
15 / 34
50.00% covered (danger)
50.00%
5 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiFlowBase
44.12% covered (danger)
44.12%
15 / 34
50.00% covered (danger)
50.00%
5 / 10
67.43
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 / 9
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 Flow\Block\AbstractBlock;
6use Flow\Container;
7use Flow\Model\AbstractRevision;
8use Flow\WorkflowLoader;
9use Flow\WorkflowLoaderFactory;
10use MediaWiki\Api\ApiBase;
11use MediaWiki\Api\ApiMessage;
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    public function setPage( Title $page ) {
59        $this->page = $page;
60    }
61
62    /**
63     * Return the name of the flow action
64     * @return string
65     */
66    abstract protected function getAction();
67
68    /**
69     * @return WorkflowLoader
70     */
71    protected function getLoader() {
72        if ( $this->loader === null ) {
73            /** @var WorkflowLoaderFactory $factory */
74            $factory = Container::get( 'factory.loader.workflow' );
75            $this->loader = $factory->createWorkflowLoader( $this->page );
76        }
77
78        return $this->loader;
79    }
80
81    /**
82     * @param bool $addAliases
83     * @return string[]
84     */
85    protected function getModerationStates( $addAliases = true ) {
86        $states = [
87            AbstractRevision::MODERATED_NONE,
88            AbstractRevision::MODERATED_DELETED,
89            AbstractRevision::MODERATED_HIDDEN,
90            AbstractRevision::MODERATED_SUPPRESSED,
91        ];
92
93        if ( $addAliases ) {
94            // aliases for AbstractRevision::MODERATED_NONE
95            return [ ...$states, 'restore', 'unhide', 'undelete', 'unsuppress' ];
96        }
97
98        return $states;
99    }
100
101    /**
102     * Kill the request if errors were encountered.
103     *
104     * @param AbstractBlock[] $blocks
105     */
106    protected function processError( $blocks ) {
107        $status = Status::newGood();
108        foreach ( $blocks as $block ) {
109            if ( $block->hasErrors() ) {
110                foreach ( $block->getErrors() as $key ) {
111                    $status->fatal( ApiMessage::create(
112                        $block->getErrorMessage( $key ), $key, [ $key => $block->getErrorExtra( $key ) ]
113                    ) );
114                }
115            }
116        }
117        if ( !$status->isGood() ) {
118            $this->dieStatus( $status );
119        }
120    }
121
122    /**
123     * Override prefix on CSRF token so the same code can be reused for
124     * all modules.  This is a *TEMPORARY* hack please remove as soon as
125     * unprefixed tokens are working correctly again (bug 70099).
126     *
127     * @param string $paramName
128     * @return string
129     */
130    public function encodeParamName( $paramName ) {
131        return $paramName === 'token'
132            ? $paramName
133            : parent::encodeParamName( $paramName );
134    }
135
136    /**
137     * @inheritDoc
138     */
139    public function getHelpUrls() {
140        return [
141            'https://www.mediawiki.org/wiki/Extension:Flow/API#' . $this->getAction(),
142        ];
143    }
144
145    /**
146     * @inheritDoc
147     */
148    public function needsToken() {
149        return 'csrf';
150    }
151
152    /**
153     * @inheritDoc
154     */
155    public function getParent() {
156        return $this->apiFlow;
157    }
158
159}