Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PFHelperFormAction
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 5
506
0.00% covered (danger)
0.00%
0 / 1
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 show
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 displayTab
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
182
 displayForm
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3use MediaWiki\MediaWikiServices;
4
5/**
6 * Handles the formcreate action - used for helper forms for creating
7 * properties, forms, etc..
8 *
9 * @author Yaron Koren
10 * @file
11 * @ingroup PF
12 */
13
14class PFHelperFormAction extends Action {
15
16    /**
17     * Return the name of the action this object responds to
18     * @return string lowercase
19     */
20    public function getName() {
21        return 'formcreate';
22    }
23
24    /**
25     * The main action entry point.  Do all output for display and send it to the context
26     * output.  Do not use globals $wgOut, $wgRequest, etc, in implementations; use
27     * $this->getOutput(), etc.
28     * @throws ErrorPageError
29     * @return false
30     */
31    public function show() {
32        return self::displayForm( $this->getArticle() );
33    }
34
35    /**
36     * Execute the action in a silent fashion: do not display anything or release any errors.
37     * @return bool whether execution was successful
38     */
39    public function execute() {
40        return true;
41    }
42
43    /**
44     * Handler for the SkinTemplateNavigation hook.
45     *
46     * Adds an "action" (i.e., a tab) to edit the current article with
47     * a form
48     * @param SkinTemplate $obj
49     * @param array &$links
50     */
51    static function displayTab( $obj, &$links ) {
52        $title = $obj->getTitle();
53        $user = $obj->getUser();
54
55        // Make sure that this page is in one of the relevant
56        // namespaces, and that it doesn't exist yet.
57        $namespacesWithHelperForms = [ NS_TEMPLATE, PF_NS_FORM, NS_CATEGORY ];
58        if ( defined( 'SMW_NS_PROPERTY' ) ) {
59            $namespacesWithHelperForms[] = SMW_NS_PROPERTY;
60        }
61        if ( !isset( $title ) ||
62            ( !in_array( $title->getNamespace(), $namespacesWithHelperForms ) ) ) {
63            return;
64        }
65        if ( $title->exists() ) {
66            return;
67        }
68
69        // The tab should show up automatically for properties and
70        // forms, but not necessarily for templates and categories,
71        // since some of them might be outside of the SMW/PF system.
72        if ( in_array( $title->getNamespace(), [ NS_TEMPLATE, NS_CATEGORY ] ) ) {
73            global $wgPageFormsShowTabsForAllHelperForms;
74            if ( !$wgPageFormsShowTabsForAllHelperForms ) {
75                return;
76            }
77        }
78
79        $content_actions = &$links['views'];
80
81        $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
82        $userCanEdit = $permissionManager->userCan( 'edit', $user, $title, $permissionManager::RIGOR_QUICK );
83        $form_create_tab_text = ( $userCanEdit ) ? 'pf_formcreate' : 'pf_viewform';
84
85        $class_name = ( $obj->getRequest()->getVal( 'action' ) == 'formcreate' ) ? 'selected' : '';
86        $form_create_tab = [
87            'class' => $class_name,
88            'text' => wfMessage( $form_create_tab_text )->text(),
89            'href' => $title->getLocalURL( 'action=formcreate' )
90        ];
91
92        // Find the location of the 'create' tab, and add 'create
93        // with form' right before it.
94        // This is a "key-safe" splice - it preserves both the keys
95        // and the values of the array, by editing them separately
96        // and then rebuilding the array. Based on the example at
97        // http://us2.php.net/manual/en/function.array-splice.php#31234
98        $tab_keys = array_keys( $content_actions );
99        $tab_values = array_values( $content_actions );
100        $edit_tab_location = array_search( 'edit', $tab_keys );
101
102        // If there's no 'edit' tab, look for the 'view source' tab
103        // instead.
104        if ( $edit_tab_location == null ) {
105            $edit_tab_location = array_search( 'viewsource', $tab_keys );
106        }
107
108        // This should rarely happen, but if there was no edit *or*
109        // view source tab, set the location index to -1, so the
110        // tab shows up near the end.
111        if ( $edit_tab_location == null ) {
112            $edit_tab_location = -1;
113        }
114        array_splice( $tab_keys, $edit_tab_location, 0, 'formedit' );
115        array_splice( $tab_values, $edit_tab_location, 0, [ $form_create_tab ] );
116        $content_actions = [];
117        foreach ( $tab_keys as $i => $key ) {
118            $content_actions[$key] = $tab_values[$i];
119        }
120
121        if ( !$obj->getUser()->isAllowed( 'viewedittab' ) ) {
122            // The tab can have either of these two actions.
123            unset( $content_actions['edit'] );
124            unset( $content_actions['viewsource'] );
125        }
126    }
127
128    /**
129     * The function called if we're in index.php (as opposed to one of the
130     * special pages).
131     * @param Article $article
132     * @return false
133     */
134    private static function displayForm( $article ) {
135        $title = $article->getTitle();
136        if ( defined( 'SMW_NS_PROPERTY' ) && $title->getNamespace() == SMW_NS_PROPERTY ) {
137            $createPropertyPage = new PFCreateProperty();
138            $createPropertyPage->execute( $title->getText() );
139        } elseif ( $title->getNamespace() == NS_TEMPLATE ) {
140            $createTemplatePage = new PFCreateTemplate();
141            $createTemplatePage->execute( $title->getText() );
142        } elseif ( $title->getNamespace() == PF_NS_FORM ) {
143            $createFormPage = new PFCreateForm();
144            $createFormPage->execute( $title->getText() );
145        } elseif ( $title->getNamespace() == NS_CATEGORY ) {
146            $createCategoryPage = new PFCreateCategory();
147            $createCategoryPage->execute( $title->getText() );
148        }
149
150        return false;
151    }
152}