Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
WikitextEditor
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 getHtml
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 runEditFormInitialHook
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 buildEditor
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 addNewLineAtEnd
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace FileImporter\Html;
4
5use FileImporter\HookRunner;
6use MediaWiki\Context\MutableContext;
7use MediaWiki\EditPage\EditPage;
8use MediaWiki\Html\Html;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\Page\Article;
11use MediaWiki\Title\Title;
12
13/**
14 * @license GPL-2.0-or-later
15 * @author Christoph Jauera <christoph.jauera@wikimedia.de>
16 */
17class WikitextEditor extends SpecialPageHtmlFragment {
18
19    public function getHtml( Title $filePage, string $wikitext ): string {
20        $outputPage = $this->getOutput();
21        $outputPage->addModules( 'mediawiki.action.edit' );
22        $outputPage->addModuleStyles( 'mediawiki.action.edit.styles' );
23        $this->runEditFormInitialHook( $filePage );
24
25        return EditPage::getEditToolbar() .
26            $this->buildEditor( $wikitext );
27    }
28
29    /**
30     * Run EditPage::showEditForm:initial hook mainly for the WikiEditor toolbar
31     * @see \MediaWiki\Extension\WikiEditor\Hooks::onEditPage__showEditForm_initial
32     * Triggering the hook means we don't have special handling for any extensions.
33     */
34    private function runEditFormInitialHook( Title $filePage ): void {
35        // We need to fake the context to make extensions like CodeMirror believe they are editing
36        // the actual file page.
37        $context = $this->getContext();
38        $context->getRequest()->setVal( 'action', 'edit' );
39        if ( $context instanceof MutableContext ) {
40            $context->setTitle( $filePage );
41        }
42
43        $editPage = new EditPage(
44            Article::newFromTitle( $filePage, $context )
45        );
46        $editPage->setContextTitle( $filePage );
47
48        ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onEditPage__showEditForm_initial(
49            $editPage, $this->getOutput()
50        );
51    }
52
53    /**
54     * @see EditPage::showTextbox
55     *
56     * @return string HTML
57     */
58    private function buildEditor( string $wikitext ): string {
59        $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
60        $class = 'mw-editfont-' . $userOptionsLookup->getOption( $this->getUser(), 'editfont' );
61        $pageLang = $this->getLanguage();
62
63        $wikitext = $this->addNewLineAtEnd( $wikitext );
64
65        $attributes = [
66            'aria-label' => $this->msg( 'edit-textarea-aria-label' )->text(),
67            'id' => 'wpTextbox1',
68            'class' => $class,
69            'cols' => 80,
70            'rows' => 25,
71            'accesskey' => ',',
72            'tabindex' => 1,
73            'lang' => $pageLang->getHtmlCode(),
74            'dir' => $pageLang->getDir(),
75            'autofocus' => 'autofocus',
76        ];
77
78        return Html::textarea( 'intendedWikitext', $wikitext, $attributes );
79    }
80
81    /**
82     * @see EditPage::addNewLineAtEnd
83     */
84    private function addNewLineAtEnd( string $wikitext ): string {
85        return $wikitext === '' ? '' : $wikitext . "\n";
86    }
87
88}