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