Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
LexiconArticleEditHooks
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 onCustomEditor
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
5
 onSkinTemplateNavigation__Universal
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace MediaWiki\Wikispeech\Hooks;
4
5/**
6 * @file
7 * @ingroup Extensions
8 * @license GPL-2.0-or-later
9 */
10
11use Article;
12use MediaWiki\Hook\CustomEditorHook;
13use MediaWiki\Hook\SkinTemplateNavigation__UniversalHook;
14use SkinTemplate;
15use SpecialPage;
16use User;
17
18/**
19 * Redirects edits of lexicon wiki pages to SpecialPage:EditLexicon
20 *
21 * @since 0.1.9
22 */
23class LexiconArticleEditHooks implements CustomEditorHook, SkinTemplateNavigation__UniversalHook {
24
25    /**
26     * This hook is called when invoking the page editor.
27     *
28     * @since 0.1.9
29     * @param Article $article Article being edited
30     * @param User $user User performing the edit
31     * @return bool|void True or no return value to allow the normal editor to be used.
32     *   False if implementing a custom editor, e.g. for a special namespace, etc.
33     */
34    public function onCustomEditor(
35        $article,
36        $user
37    ) {
38        if ( !$article->getTitle()->inNamespace( NS_PRONUNCIATION_LEXICON ) ) {
39            return true;
40        }
41
42        $request = $article->getContext()->getRequest();
43        if ( $request->getText( "action" ) === "submit" ) {
44            return true;
45        }
46
47        if (
48            $request->getBool( "raw" )
49            && $user->isAllowed( 'wikispeech-edit-lexicon-raw' )
50        ) {
51            return true;
52        }
53
54        $tuple = explode( '/', $article->getTitle()->getText(), 2 );
55        $language = mb_strtolower( $tuple[0] );
56        $key = $tuple[1];
57
58        $editLexicon = SpecialPage::getTitleFor( 'EditLexicon' );
59        $article->getContext()->getOutput()->redirect(
60            $editLexicon->getFullURL( [
61                'word' => $key,
62                'language' => $language,
63            ] )
64        );
65        return false;
66    }
67
68    /**
69     * Add a tab for editing an entry as text.
70     *
71     * @since 0.1.11
72     * @param SkinTemplate $skinTemplate The skin template on which
73     *  the UI is built.
74     * @param array &$links Navigation links.
75     */
76    public function onSkinTemplateNavigation__Universal( $skinTemplate, &$links ): void {
77        if ( !$skinTemplate->getTitle()->inNamespace( NS_PRONUNCIATION_LEXICON ) ) {
78            return;
79        }
80
81        if ( !$skinTemplate->getUser()->isAllowed( 'wikispeech-edit-lexicon-raw' ) ) {
82            return;
83        }
84
85        $title = $skinTemplate->getTitle();
86        $url = $title->getLinkURL( [ 'action' => 'edit', 'raw' => 1 ] );
87        $links['views']['editlexiconraw'] = [
88            'text' => $skinTemplate->msg( 'wikispeech-edit-lexicon-raw' )->text(),
89            'href' => $url
90        ];
91    }
92}