Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialEnableStructuredDiscussions
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 8
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getFormFields
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 getDisplayFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMessagePrefix
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
156
 onSuccess
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Specials;
4
5use ExtensionRegistry;
6use Flow\Container;
7use Flow\Import\Converter;
8use Flow\Import\EnableFlow\EnableFlowWikitextConversionStrategy;
9use Flow\Import\SourceStore\NullImportSourceStore;
10use IDBAccessObject;
11use MediaWiki\MediaWikiServices;
12use MediaWiki\SpecialPage\FormSpecialPage;
13use MediaWiki\Status\Status;
14use MediaWiki\Title\Title;
15
16/**
17 * A special page that allows users with the flow-create-board right to create
18 * boards where there no page exists
19 */
20class SpecialEnableStructuredDiscussions extends FormSpecialPage {
21    /**
22     * @var \Flow\WorkflowLoaderFactory
23     */
24    protected $loaderFactory;
25
26    /**
27     * @var \Flow\TalkpageManager
28     */
29    protected $occupationController;
30
31    /**
32     * @var string Full page name that was converted to a board
33     */
34    protected $page = '';
35
36    public function __construct() {
37        parent::__construct( 'EnableStructuredDiscussions', 'flow-create-board' );
38
39        $this->loaderFactory = Container::get( 'factory.loader.workflow' );
40        $this->occupationController = Container::get( 'occupation_controller' );
41    }
42
43    public function execute( $par ) {
44        $this->addHelpLink( 'Help:Structured Discussions/Activation' );
45
46        if ( $par !== null ) {
47            $title = Title::newFromText( $par );
48            if ( $title ) {
49                $this->page = $title->getPrefixedText();
50            }
51        }
52
53        parent::execute( $par );
54    }
55
56    protected function getFormFields() {
57        return [
58            'page' => [
59                'type' => 'text',
60                'label-message' => 'flow-special-enableflow-page',
61                'default' => $this->page,
62            ],
63            'header' => [
64                'type' => 'textarea',
65                'label-message' => 'flow-special-enableflow-header'
66            ],
67        ];
68    }
69
70    protected function getDisplayFormat() {
71        return 'ooui';
72    }
73
74    protected function getMessagePrefix() {
75        return 'flow-special-enableflow';
76    }
77
78    /**
79     * Creates a flow board.
80     * Archives any pre-existing wikitext talk page.
81     *
82     * @param array $data Form data
83     * @return Status
84     */
85    public function onSubmit( array $data ) {
86        $page = $data['page'];
87        $title = Title::newFromText( $page );
88        if ( !$title ) {
89            return Status::newFatal( 'flow-special-enableflow-invalid-title', $page );
90        }
91
92        // Canonicalize so the error or confirmation message looks nicer (no underscores).
93        $page = $title->getPrefixedText();
94
95        if ( $title->getContentModel() === CONTENT_MODEL_FLOW_BOARD ) {
96            return Status::newFatal( 'flow-special-enableflow-board-already-exists', $page );
97        }
98
99        $status = Status::newGood();
100
101        if ( $title->exists( IDBAccessObject::READ_LATEST ) ) {
102            if ( ExtensionRegistry::getInstance()->isLoaded( 'Liquid Threads' ) && \LqtDispatch::isLqtPage( $title ) ) {
103                return Status::newFatal( 'flow-special-enableflow-page-is-liquidthreads', $page );
104            }
105
106            $logger = Container::get( 'default_logger' );
107
108            $converter = new Converter(
109                MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(),
110                Container::get( 'importer' ),
111                $logger,
112                $this->getUser(),
113                new EnableFlowWikitextConversionStrategy(
114                    MediaWikiServices::getInstance()->getParser(),
115                    new NullImportSourceStore(),
116                    $logger,
117                    $this->getUser(),
118                    [],
119                    $data['header']
120                )
121            );
122
123            try {
124                $converter->convert( $title );
125            } catch ( \Exception $e ) {
126                \MWExceptionHandler::logException( $e );
127                $status->fatal( 'flow-error-external', $e->getMessage() );
128            }
129
130        } else {
131            $creationStatus = $this->occupationController->safeAllowCreation( $title, $this->getUser(), false );
132            if ( !$creationStatus->isGood() ) {
133                return Status::newFatal( 'flow-special-enableflow-board-creation-not-allowed', $page );
134            }
135
136            $loader = $this->loaderFactory->createWorkflowLoader( $title );
137            $blocks = $loader->getBlocks();
138
139            $action = 'edit-header';
140            $params = [
141                'header' => [
142                    'content' => $data['header'],
143                    'format' => 'wikitext',
144                ],
145            ];
146
147            $blocksToCommit = $loader->handleSubmit(
148                $this->getContext(),
149                $action,
150                $params
151            );
152
153            foreach ( $blocks as $block ) {
154                if ( $block->hasErrors() ) {
155                    $errors = $block->getErrors();
156
157                    foreach ( $errors as $errorKey ) {
158                        $status->fatal( $block->getErrorMessage( $errorKey ) );
159                    }
160                }
161            }
162
163            if ( $status->isOK() ) {
164                $loader->commit( $blocksToCommit );
165            }
166        }
167
168        $this->page = $data['page'];
169        return $status;
170    }
171
172    public function onSuccess() {
173        $confirmationMessage = $this->msg( 'flow-special-enableflow-confirmation', wfEscapeWikiText( $this->page ) )->parse();
174        $this->getOutput()->addHTML( $confirmationMessage );
175    }
176
177    protected function getGroupName() {
178        return 'wiki';
179    }
180}