Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 132
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoryEditPage
0.00% covered (danger)
0.00%
0 / 132
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 showContentForm
0.00% covered (danger)
0.00%
0 / 99
0.00% covered (danger)
0.00%
0 / 1
12
 importContentFormData
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace MediaWiki\Extension\Wikistories;
4
5use MediaWiki\EditPage\EditPage;
6use MediaWiki\Html\Html;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Request\WebRequest;
9use OOUI\FieldLayout;
10use OOUI\HiddenInputWidget;
11use OOUI\MultilineTextInputWidget;
12use OOUI\TextInputWidget;
13
14class StoryEditPage extends EditPage {
15
16    /** @var StoryContent */
17    private $wipStory = null;
18
19    protected function showContentForm() {
20        $maxFrames = $this->context->getConfig()->get( 'WikistoriesMaxFrames' );
21        $maxTextLength = $this->context->getConfig()->get( 'WikistoriesMaxTextLength' );
22        $out = $this->context->getOutput();
23        /** @var StoryConverter $storyConverter */
24        $storyConverter = MediaWikiServices::getInstance()
25            ->get( 'Wikistories.StoryConverter' );
26
27        if ( $this->wipStory === null ) {
28            /** @var StoryContent $originalStory */
29            $originalStory = $this->getContentObject();
30            '@phan-var StoryContent $originalStory';
31
32            $story = $storyConverter->toLatest( $originalStory );
33        } else {
34            $story = $this->wipStory;
35        }
36
37        $currentFrames = $story->getFrames();
38        $emptyFrame = (object)[
39            'image' => (object)[ 'filename' => '' ],
40            'text' => (object)[
41                'value' => '',
42                'fromArticle' => (object)[
43                    'articleTitle' => '',
44                    'originalText' => '',
45                ],
46            ],
47        ];
48
49        $form = '<div class="ext-wikistories-editform">';
50        $form .= new FieldLayout(
51            new TextInputWidget(
52                [
53                    'name' => "story_from_article",
54                    'value' => $story->getFromArticle(), 'disabled' => true
55                ]
56            ),
57            [
58                'label' => $this->context->msg( 'wikistories-nojs-form-label-related-article' )->text(),
59                'align' => 'left',
60            ]
61        );
62        $form .= new HiddenInputWidget(
63            [
64                'name' => "story_article_id",
65                'value' => $story->getArticleId(),
66            ]
67
68        );
69        for ( $i = 0; $i < $maxFrames; $i++ ) {
70            $frame = $currentFrames[ $i ] ?? $emptyFrame;
71            $form .= Html::element( 'h3', [],
72                $this->context->msg( 'wikistories-nojs-form-label-frame' )->params( $i + 1 )->text()
73            );
74            $form .= new FieldLayout(
75                new TextInputWidget(
76                    [ 'name' => "story_frame_{$i}_image_filename", 'value' => $frame->image->filename ]
77                ),
78                [
79                    'label' => $this->context->msg( 'wikistories-nojs-form-label-image' )->text(),
80                    'align' => 'left'
81                ]
82            );
83            $form .= new FieldLayout(
84                new TextInputWidget(
85                    [
86                        'name' => "story_frame_{$i}_text_value",
87                        'value' => $frame->text->value,
88                        'maxlength' => $maxTextLength,
89                    ]
90
91                ),
92                [
93                    'label' => $this->context->msg( 'wikistories-nojs-form-label-text' )->text(),
94                    'align' => 'left'
95                ]
96            );
97            $form .= new HiddenInputWidget(
98                [
99                    'name' => "story_frame_{$i}_text_fromArticle_articleTitle",
100                    'value' => $frame->text->fromArticle->articleTitle ?? '',
101                ]
102
103            );
104            $form .= new HiddenInputWidget(
105                [
106                    'name' => "story_frame_{$i}_text_fromArticle_originalText",
107                    'value' => $frame->text->fromArticle->originalText ?? '',
108                ]
109
110            );
111        }
112
113        // Categories
114        $form .= Html::element( 'h3', [],
115            $this->context->msg( 'wikistories-nojs-form-categories-title' )->text()
116        );
117        $categories = $story->getCategories();
118        $form .= new FieldLayout(
119            new MultilineTextInputWidget(
120                [
121                    'name' => "story_categories",
122                    'value' => implode( "\n", $categories ),
123                    'rows' => 5,
124                ]
125
126            ),
127            [
128                'label' => $this->context->msg( 'wikistories-nojs-form-categories-label' )->text(),
129                'align' => 'left',
130            ]
131        );
132
133        $form .= '</div>';
134        $out->enableOOUI();
135        $out->addHTML( $form );
136    }
137
138    /**
139     * @param WebRequest &$request
140     * @return false|string|null
141     */
142    protected function importContentFormData( &$request ) {
143        $story = [
144            'articleId' => intval( $request->getText( 'story_article_id' ) ),
145            'categories' => array_values(
146                array_unique( array_filter( array_map( 'trim',
147                    explode( "\n", trim( $request->getText( 'story_categories' ) ) )
148                ) ) )
149            ),
150            'frames' => []
151        ];
152
153        $i = 0;
154        while ( true ) {
155            $filename = $request->getText( "story_frame_{$i}_image_filename" );
156            $text = $request->getText( "story_frame_{$i}_text_value" );
157            $articleTitle = $request->getText( "story_frame_{$i}_text_fromArticle_articleTitle" );
158            $originalText = $request->getText( "story_frame_{$i}_text_fromArticle_originalText" );
159            if ( $filename === '' && $text === '' ) {
160                // stop reading as soon as all fields are empty
161                break;
162            }
163            $story['frames'][] = [
164                'image' => [
165                    'filename' => $filename,
166                ],
167                'text' => [
168                    'value' => $text,
169                    'fromArticle' => [
170                        'articleTitle' => $articleTitle,
171                        'originalText' => $originalText,
172                    ],
173                ],
174            ];
175            $i++;
176        }
177
178        $stringContent = json_encode( $story, JSON_PRETTY_PRINT );
179
180        $this->wipStory = new StoryContent( $stringContent );
181
182        return $stringContent;
183    }
184
185}