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    /**
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            return [ ...$states, 'restore', 'unhide', 'undelete', 'unsuppress' ];
99        }
100
101        return $states;
102    }
103
104    /**
105     * Kill the request if errors were encountered.
106     *
107     * @param AbstractBlock[] $blocks
108     */
109    protected function processError( $blocks ) {
110        $status = Status::newGood();
111        foreach ( $blocks as $block ) {
112            if ( $block->hasErrors() ) {
113                foreach ( $block->getErrors() as $key ) {
114                    $status->fatal( ApiMessage::create(
115                        $block->getErrorMessage( $key ), $key, [ $key => $block->getErrorExtra( $key ) ]
116                    ) );
117                }
118            }
119        }
120        if ( !$status->isGood() ) {
121            $this->dieStatus( $status );
122        }
123    }
124
125    /**
126     * Override prefix on CSRF token so the same code can be reused for
127     * all modules.  This is a *TEMPORARY* hack please remove as soon as
128     * unprefixed tokens are working correctly again (bug 70099).
129     *
130     * @param string $paramName
131     * @return string
132     */
133    public function encodeParamName( $paramName ) {
134        return $paramName === 'token'
135            ? $paramName
136            : parent::encodeParamName( $paramName );
137    }
138
139    /**
140     * @inheritDoc
141     */
142    public function getHelpUrls() {
143        return [
144            'https://www.mediawiki.org/wiki/Extension:Flow/API#' . $this->getAction(),
145        ];
146    }
147
148    /**
149     * @inheritDoc
150     */
151    public function needsToken() {
152        return 'csrf';
153    }
154
155    /**
156     * @inheritDoc
157     */
158    public function getParent() {
159        return $this->apiFlow;
160    }
161
162}