r112646 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112645‎ | r112646 | r112647 >
Date:21:17, 28 February 2012
Author:foxtrott
Status:deferred
Tags:
Comment:
use $wgActions for formedit action for MW 1.18 onwards
Modified paths:
  • /trunk/extensions/SemanticForms/SemanticForms.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_FormEditAction.php (added) (history)
  • /trunk/extensions/SemanticForms/includes/SF_FormEditTab.php (deleted) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/SemanticForms.php
@@ -79,10 +79,9 @@
8080 $wgExtensionFunctions[] = 'sffSetupExtension';
8181
8282 $wgHooks['LinkEnd'][] = 'SFFormLinker::setBrokenLink';
83 -$wgHooks['UnknownAction'][] = 'SFFormEditTab::displayForm';
8483 // 'SkinTemplateNavigation' replaced 'SkinTemplateTabs' in the Vector skin
85 -$wgHooks['SkinTemplateTabs'][] = 'SFFormEditTab::displayTab';
86 -$wgHooks['SkinTemplateNavigation'][] = 'SFFormEditTab::displayTab2';
 84+$wgHooks['SkinTemplateTabs'][] = 'FormeditAction::displayTab';
 85+$wgHooks['SkinTemplateNavigation'][] = 'FormeditAction::displayTab2';
8786 $wgHooks['smwInitProperties'][] = 'SFUtils::initProperties';
8887 $wgHooks['AdminLinks'][] = 'SFUtils::addToAdminLinks';
8988 $wgHooks['ArticlePurge'][] = 'SFFormUtils::purgeCache';
@@ -93,6 +92,14 @@
9493 $wgHooks['EditPage::importFormData'][] = 'SFUtils::showFormPreview';
9594 $wgHooks['CanonicalNamespaces'][] = 'SFUtils::registerNamespaces';
9695
 96+if ( version_compare( $wgVersion, '1.18', '<' ) ) {
 97+ // TODO: Using UnknownAction is deprecated from MW 1.18 onwards.
 98+ $wgHooks['UnknownAction'][] = 'FormeditAction::displayForm';
 99+} else {
 100+ // Introduced in MW 1.18.
 101+ $wgActions['formedit'] = true;
 102+}
 103+
97104 $wgAPIModules['sfautocomplete'] = 'SFAutocompleteAPI';
98105 $wgAPIModules['sfautoedit'] = 'SFAutoeditAPI';
99106
@@ -143,6 +150,7 @@
144151 $wgAutoloadClasses['SFParserFunctions'] = $sfgIP . '/includes/SF_ParserFunctions.php';
145152 $wgAutoloadClasses['SFAutocompleteAPI'] = $sfgIP . '/includes/SF_AutocompleteAPI.php';
146153 $wgAutoloadClasses['SFAutoeditAPI'] = $sfgIP . '/includes/SF_AutoeditAPI.php';
 154+$wgAutoloadClasses['FormeditAction'] = $sfgIP . '/includes/SF_FormEditAction.php';
147155
148156 // FormInputs
149157 $wgAutoloadClasses['SFFormInput'] = $sfgIP . '/includes/forminputs/SF_FormInput.php';
Index: trunk/extensions/SemanticForms/includes/SF_FormEditTab.php
@@ -1,178 +0,0 @@
2 -<?php
3 -/**
4 - * Utility class for 'edit with form' tab and page
5 - *
6 - * @author Yaron Koren
7 - * @file
8 - * @ingroup SF
9 - */
10 -class SFFormEditTab {
11 -
12 - /**
13 - * Adds an "action" (i.e., a tab) to edit the current article with
14 - * a form
15 - */
16 - static function displayTab( $obj, &$content_actions ) {
17 - if ( method_exists ( $obj, 'getTitle' ) ) {
18 - $title = $obj->getTitle();
19 - } else {
20 - $title = $obj->mTitle;
21 - }
22 - // Make sure that this is not a special page, and
23 - // that the user is allowed to edit it
24 - // - this function is almost never called on special pages,
25 - // but before SMW is fully initialized, it's called on
26 - // Special:SMWAdmin for some reason, which is why the
27 - // special-page check is there.
28 - if ( !isset( $title ) ||
29 - ( $title->getNamespace() == NS_SPECIAL ) ) {
30 - return true;
31 - }
32 -
33 - $form_names = SFFormLinker::getDefaultFormsForPage( $title );
34 - if ( count( $form_names ) == 0 ) {
35 - return true;
36 - }
37 -
38 - global $wgRequest, $wgUser;
39 - global $sfgRenameEditTabs, $sfgRenameMainEditTab;
40 -
41 - $user_can_edit = $wgUser->isAllowed( 'edit' ) && $title->userCan( 'edit' );
42 - // Create the form edit tab, and apply whatever changes are
43 - // specified by the edit-tab global variables.
44 - if ( $sfgRenameEditTabs ) {
45 - $form_edit_tab_text = $user_can_edit ? wfMsg( 'edit' ) : wfMsg( 'sf_viewform' );
46 - if ( array_key_exists( 'edit', $content_actions ) ) {
47 - $content_actions['edit']['text'] = $user_can_edit ? wfMsg( 'sf_editsource' ) : wfMsg( 'viewsource' );
48 - }
49 - } else {
50 - if ( $user_can_edit ) {
51 - $form_edit_tab_text = $title->exists() ? wfMsg( 'formedit' ) : wfMsg( 'sf_formcreate' );
52 - } else {
53 - $form_edit_tab_text = wfMsg( 'sf_viewform' );
54 - }
55 - // Check for renaming of main edit tab only if
56 - // $sfgRenameEditTabs is off.
57 - if ( $sfgRenameMainEditTab ) {
58 - if ( array_key_exists( 'edit', $content_actions ) ) {
59 - $content_actions['edit']['text'] = $user_can_edit ? wfMsg( 'sf_editsource' ) : wfMsg( 'viewsource' );
60 - }
61 - }
62 - }
63 -
64 - $class_name = ( $wgRequest->getVal( 'action' ) == 'formedit' ) ? 'selected' : '';
65 - $form_edit_tab = array(
66 - 'class' => $class_name,
67 - 'text' => $form_edit_tab_text,
68 - 'href' => $title->getLocalURL( 'action=formedit' )
69 - );
70 -
71 - // Find the location of the 'edit' tab, and add 'edit
72 - // with form' right before it.
73 - // This is a "key-safe" splice - it preserves both the keys
74 - // and the values of the array, by editing them separately
75 - // and then rebuilding the array. Based on the example at
76 - // http://us2.php.net/manual/en/function.array-splice.php#31234
77 - $tab_keys = array_keys( $content_actions );
78 - $tab_values = array_values( $content_actions );
79 - $edit_tab_location = array_search( 'edit', $tab_keys );
80 -
81 - // If there's no 'edit' tab, look for the 'view source' tab
82 - // instead.
83 - if ( $edit_tab_location == null ) {
84 - $edit_tab_location = array_search( 'viewsource', $tab_keys );
85 - }
86 -
87 - // This should rarely happen, but if there was no edit *or*
88 - // view source tab, set the location index to -1, so the
89 - // tab shows up near the end.
90 - if ( $edit_tab_location == null ) {
91 - $edit_tab_location = - 1;
92 - }
93 - array_splice( $tab_keys, $edit_tab_location, 0, 'form_edit' );
94 - array_splice( $tab_values, $edit_tab_location, 0, array( $form_edit_tab ) );
95 - $content_actions = array();
96 - for ( $i = 0; $i < count( $tab_keys ); $i++ ) {
97 - $content_actions[$tab_keys[$i]] = $tab_values[$i];
98 - }
99 -
100 - global $wgUser;
101 - if ( ! $wgUser->isAllowed( 'viewedittab' ) ) {
102 - // The tab can have either of these two actions.
103 - unset( $content_actions['edit'] );
104 - unset( $content_actions['viewsource'] );
105 - }
106 -
107 - return true; // always return true, in order not to stop MW's hook processing!
108 - }
109 -
110 - /**
111 - * Like displayTab(), but called with a different hook - this one is
112 - * called for the 'Vector' skin, and others.
113 - */
114 - static function displayTab2( $obj, &$links ) {
115 - // the old '$content_actions' array is thankfully just a
116 - // sub-array of this one
117 - $views_links = $links['views'];
118 - self::displayTab( $obj, $views_links );
119 - $links['views'] = $views_links;
120 - return true;
121 - }
122 -
123 - /**
124 - * The function called if we're in index.php (as opposed to one of the
125 - * special pages)
126 - */
127 - static function displayForm( $action, $article ) {
128 - global $wgOut, $sfgUseFormEditPage;
129 -
130 - // return "true" if the call failed (meaning, pass on handling
131 - // of the hook to others), and "false" otherwise
132 - if ( $action != 'formedit' ) {
133 - return true;
134 - }
135 -
136 - // @todo: This looks like bad code. If we can't find a form, we
137 - // should be showing an informative error page rather than
138 - // making it look like an edit form page does not exist.
139 - $title = $article->getTitle();
140 - $form_names = SFFormLinker::getDefaultFormsForPage( $title );
141 - if ( count( $form_names ) == 0 ) {
142 - return true;
143 - }
144 - if ( count( $form_names ) > 1 ) {
145 - $warning_text = "\t" . '<div class="warningMessage">' . wfMsg( 'sf_formedit_morethanoneform' ) . "</div>\n";
146 - $wgOut->addHTML( $warning_text );
147 - }
148 - $form_name = $form_names[0];
149 -
150 - if ( $sfgUseFormEditPage ) {
151 - # Experimental new feature extending from the internal
152 - # EditPage class
153 - $editor = new SFFormEditPage( $article, $form_name );
154 - $editor->edit();
155 - return false;
156 - }
157 -
158 - $page_name = SFUtils::titleString( $title );
159 -
160 - $msg = SFFormEdit::printForm( $form_name, $page_name );
161 -
162 - if ( $msg ) {
163 - // Some error occurred - display it.
164 - $msgdata = null;
165 - if ( is_array( $msg ) ) {
166 - if ( count( $msg ) > 1 ) {
167 - $msgdata = $msg[1];
168 - }
169 - $msg = $msg[0];
170 - }
171 -
172 - $wgOut->addHTML( Html::element( 'p', array( 'class' => 'error' ), wfMsg( $msg, $msgdata ) ) );
173 -
174 - }
175 -
176 - return false;
177 - }
178 -
179 -}
Index: trunk/extensions/SemanticForms/includes/SF_FormEditAction.php
@@ -0,0 +1,214 @@
 2+<?php
 3+/*
 4+ * To change this template, choose Tools | Templates
 5+ * and open the template in the editor.
 6+ */
 7+
 8+// TODO: Action class did not exist until MW 1.18
 9+if ( ! class_exists( 'Action') ) {
 10+ class Action{}
 11+}
 12+
 13+/**
 14+ * Description of FormeditAction
 15+ *
 16+ * @author Stephan Gambke
 17+ */
 18+class FormeditAction extends Action
 19+{
 20+ /**
 21+ * Return the name of the action this object responds to
 22+ * @return String lowercase
 23+ */
 24+ public function getName(){
 25+ return 'formedit';
 26+ }
 27+
 28+ /**
 29+ * The main action entry point. Do all output for display and send it to the context
 30+ * output. Do not use globals $wgOut, $wgRequest, etc, in implementations; use
 31+ * $this->getOutput(), etc.
 32+ * @throws ErrorPageError
 33+ */
 34+ public function show(){
 35+ return self::displayForm($this, $this->page);
 36+ }
 37+
 38+ /**
 39+ * Execute the action in a silent fashion: do not display anything or release any errors.
 40+ * @return Bool whether execution was successful
 41+ */
 42+ public function execute(){
 43+ return true;
 44+ }
 45+
 46+ /**
 47+ * Adds an "action" (i.e., a tab) to edit the current article with
 48+ * a form
 49+ */
 50+ static function displayTab( $obj, &$content_actions ) {
 51+ if ( method_exists ( $obj, 'getTitle' ) ) {
 52+ $title = $obj->getTitle();
 53+ } else {
 54+ $title = $obj->mTitle;
 55+ }
 56+ // Make sure that this is not a special page, and
 57+ // that the user is allowed to edit it
 58+ // - this function is almost never called on special pages,
 59+ // but before SMW is fully initialized, it's called on
 60+ // Special:SMWAdmin for some reason, which is why the
 61+ // special-page check is there.
 62+ if ( !isset( $title ) ||
 63+ ( $title->getNamespace() == NS_SPECIAL ) ) {
 64+ return true;
 65+ }
 66+
 67+ $form_names = SFFormLinker::getDefaultFormsForPage( $title );
 68+ if ( count( $form_names ) == 0 ) {
 69+ return true;
 70+ }
 71+
 72+ global $wgRequest, $wgUser;
 73+ global $sfgRenameEditTabs, $sfgRenameMainEditTab;
 74+
 75+ $user_can_edit = $wgUser->isAllowed( 'edit' ) && $title->userCan( 'edit' );
 76+ // Create the form edit tab, and apply whatever changes are
 77+ // specified by the edit-tab global variables.
 78+ if ( $sfgRenameEditTabs ) {
 79+ $form_edit_tab_text = $user_can_edit ? wfMsg( 'edit' ) : wfMsg( 'sf_viewform' );
 80+ if ( array_key_exists( 'edit', $content_actions ) ) {
 81+ $content_actions['edit']['text'] = $user_can_edit ? wfMsg( 'sf_editsource' ) : wfMsg( 'viewsource' );
 82+ }
 83+ } else {
 84+ if ( $user_can_edit ) {
 85+ $form_edit_tab_text = $title->exists() ? wfMsg( 'formedit' ) : wfMsg( 'sf_formcreate' );
 86+ } else {
 87+ $form_edit_tab_text = wfMsg( 'sf_viewform' );
 88+ }
 89+ // Check for renaming of main edit tab only if
 90+ // $sfgRenameEditTabs is off.
 91+ if ( $sfgRenameMainEditTab ) {
 92+ if ( array_key_exists( 'edit', $content_actions ) ) {
 93+ $content_actions['edit']['text'] = $user_can_edit ? wfMsg( 'sf_editsource' ) : wfMsg( 'viewsource' );
 94+ }
 95+ }
 96+ }
 97+
 98+ $class_name = ( $wgRequest->getVal( 'action' ) == 'formedit' ) ? 'selected' : '';
 99+ $form_edit_tab = array(
 100+ 'class' => $class_name,
 101+ 'text' => $form_edit_tab_text,
 102+ 'href' => $title->getLocalURL( 'action=formedit' )
 103+ );
 104+
 105+ // Find the location of the 'edit' tab, and add 'edit
 106+ // with form' right before it.
 107+ // This is a "key-safe" splice - it preserves both the keys
 108+ // and the values of the array, by editing them separately
 109+ // and then rebuilding the array. Based on the example at
 110+ // http://us2.php.net/manual/en/function.array-splice.php#31234
 111+ $tab_keys = array_keys( $content_actions );
 112+ $tab_values = array_values( $content_actions );
 113+ $edit_tab_location = array_search( 'edit', $tab_keys );
 114+
 115+ // If there's no 'edit' tab, look for the 'view source' tab
 116+ // instead.
 117+ if ( $edit_tab_location == null ) {
 118+ $edit_tab_location = array_search( 'viewsource', $tab_keys );
 119+ }
 120+
 121+ // This should rarely happen, but if there was no edit *or*
 122+ // view source tab, set the location index to -1, so the
 123+ // tab shows up near the end.
 124+ if ( $edit_tab_location == null ) {
 125+ $edit_tab_location = - 1;
 126+ }
 127+ array_splice( $tab_keys, $edit_tab_location, 0, 'form_edit' );
 128+ array_splice( $tab_values, $edit_tab_location, 0, array( $form_edit_tab ) );
 129+ $content_actions = array();
 130+ for ( $i = 0; $i < count( $tab_keys ); $i++ ) {
 131+ $content_actions[$tab_keys[$i]] = $tab_values[$i];
 132+ }
 133+
 134+ global $wgUser;
 135+ if ( ! $wgUser->isAllowed( 'viewedittab' ) ) {
 136+ // The tab can have either of these two actions.
 137+ unset( $content_actions['edit'] );
 138+ unset( $content_actions['viewsource'] );
 139+ }
 140+
 141+ return true; // always return true, in order not to stop MW's hook processing!
 142+ }
 143+
 144+ /**
 145+ * Like displayTab(), but called with a different hook - this one is
 146+ * called for the 'Vector' skin, and others.
 147+ */
 148+ static function displayTab2( $obj, &$links ) {
 149+ // the old '$content_actions' array is thankfully just a
 150+ // sub-array of this one
 151+ return self::displayTab( $obj, $links['views'] );
 152+ }
 153+
 154+ /**
 155+ * The function called if we're in index.php (as opposed to one of the
 156+ * special pages)
 157+ */
 158+ static function displayForm( $action, $article ) {
 159+ global $sfgUseFormEditPage;
 160+
 161+ // TODO: This function will be called as a hook handler and $action will
 162+ // be a string before MW 1.18. From 1.18 onwards this function will#
 163+ // only be called for formedit actions, i.e. the if statement can be
 164+ // removed then.
 165+
 166+ // return "true" if the call failed (meaning, pass on handling
 167+ // of the hook to others), and "false" otherwise
 168+ if ( is_string( $action ) && $action !== 'formedit' ) {
 169+ return true;
 170+ }
 171+
 172+ // @todo: This looks like bad code. If we can't find a form, we
 173+ // should be showing an informative error page rather than
 174+ // making it look like an edit form page does not exist.
 175+ $title = $article->getTitle();
 176+ $form_names = SFFormLinker::getDefaultFormsForPage( $title );
 177+ if ( count( $form_names ) == 0 ) {
 178+ return true;
 179+ }
 180+ if ( count( $form_names ) > 1 ) {
 181+ $warning_text = "\t" . '<div class="warningMessage">' . wfMsg( 'sf_formedit_morethanoneform' ) . "</div>\n";
 182+ $action->getOutput()->addHTML( $warning_text );
 183+ }
 184+ $form_name = $form_names[0];
 185+
 186+ if ( $sfgUseFormEditPage ) {
 187+ # Experimental new feature extending from the internal
 188+ # EditPage class
 189+ $editor = new SFFormEditPage( $article, $form_name );
 190+ $editor->edit();
 191+ return false;
 192+ }
 193+
 194+ $page_name = SFUtils::titleString( $title );
 195+
 196+ $msg = SFFormEdit::printForm( $form_name, $page_name );
 197+
 198+ if ( $msg ) {
 199+ // Some error occurred - display it.
 200+ $msgdata = null;
 201+ if ( is_array( $msg ) ) {
 202+ if ( count( $msg ) > 1 ) {
 203+ $msgdata = $msg[1];
 204+ }
 205+ $msg = $msg[0];
 206+ }
 207+
 208+ $action->getOutput()->addHTML( Html::element( 'p', array( 'class' => 'error' ), wfMsg( $msg, $msgdata ) ) );
 209+
 210+ }
 211+
 212+ return false;
 213+ }
 214+
 215+}
Property changes on: trunk/extensions/SemanticForms/includes/SF_FormEditAction.php
___________________________________________________________________
Added: svn:eol-style
1216 + native