r65254 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65253‎ | r65254 | r65255 >
Date:21:58, 18 April 2010
Author:yaron
Status:deferred
Tags:
Comment:
Added 'sfSetTargetName' and 'sfHTMLBeforeForm' hooks; improved formatting of code
Modified paths:
  • /trunk/extensions/SemanticForms/specials/SF_FormEdit.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/specials/SF_FormEdit.php
@@ -30,6 +30,7 @@
3131 $form_name = isset( $queryparts[0] ) ? $queryparts[0] : '';
3232 $target_name = isset( $queryparts[1] ) ? $queryparts[1] : '';
3333 }
 34+ wfRunHooks( 'sfSetTargetName', array( &$target_name ) );
3435
3536 $alt_forms = $wgRequest->getArray( 'alt_form' );
3637
@@ -48,203 +49,209 @@
4950 return $text;
5051 }
5152
52 -static function printForm( $form_name, $target_name, $alt_forms = array() ) {
53 - global $wgOut, $wgRequest, $wgScriptPath, $sfgScriptPath, $sfgFormPrinter, $sfgYUIBase;
 53+ static function printForm( $form_name, $target_name, $alt_forms = array() ) {
 54+ global $wgOut, $wgRequest, $wgScriptPath, $sfgScriptPath, $sfgFormPrinter, $sfgYUIBase;
5455
55 - wfLoadExtensionMessages( 'SemanticForms' );
 56+ wfLoadExtensionMessages( 'SemanticForms' );
5657
57 - // initialize some variables
58 - $target_title = null;
59 - $page_name_formula = null;
 58+ // initialize some variables
 59+ $target_title = null;
 60+ $page_name_formula = null;
6061
61 - // get contents of form and target page - if there's only one,
62 - // it might be a target with only alternate forms
63 - if ( $form_name == '' ) {
64 - $wgOut->addHTML( "<p class='error'>" . wfMsg( 'sf_formedit_badurl' ) . '</p>' );
65 - return;
66 - } elseif ( $target_name == '' ) {
67 - // parse the form to see if it has a 'page name' value set
68 - $form_title = Title::makeTitleSafe( SF_NS_FORM, $form_name );
69 - $form_article = new Article( $form_title );
70 - $form_definition = $form_article->getContent();
71 - $form_definition = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $form_definition );
72 - $matches;
73 - if ( preg_match( '/{{{info.*page name=([^\|]*)/m', $form_definition, $matches ) ) {
74 - $page_name_formula = str_replace( '_', ' ', $matches[1] );
75 - // if the tag close ('}}}') is in here, chop off that
76 - // and everything after it
77 - if ( $pos = strpos( $page_name_formula, '}}}' ) ) {
78 - $page_name_formula = substr( $page_name_formula, 0, $pos );
 62+ // get contents of form and target page - if there's only one,
 63+ // it might be a target with only alternate forms
 64+ if ( $form_name == '' ) {
 65+ $wgOut->addHTML( "<p class='error'>" . wfMsg( 'sf_formedit_badurl' ) . '</p>' );
 66+ return;
 67+ } elseif ( $target_name == '' ) {
 68+ // parse the form to see if it has a 'page name' value set
 69+ $form_title = Title::makeTitleSafe( SF_NS_FORM, $form_name );
 70+ $form_article = new Article( $form_title );
 71+ $form_definition = $form_article->getContent();
 72+ $form_definition = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $form_definition );
 73+ $matches;
 74+ if ( preg_match( '/{{{info.*page name=([^\|]*)/m', $form_definition, $matches ) ) {
 75+ $page_name_formula = str_replace( '_', ' ', $matches[1] );
 76+ // if the tag close ('}}}') is in here, chop off
 77+ // that and everything after it
 78+ if ( $pos = strpos( $page_name_formula, '}}}' ) ) {
 79+ $page_name_formula = substr( $page_name_formula, 0, $pos );
 80+ }
 81+ } elseif ( count( $alt_forms ) == 0 ) {
 82+ $wgOut->addWikiText( "<p class='error'>" . wfMsg( 'sf_formedit_badurl' ) . '</p>' );
 83+ return;
7984 }
80 - } elseif ( count( $alt_forms ) == 0 ) {
81 - $wgOut->addWikiText( "<p class='error'>" . wfMsg( 'sf_formedit_badurl' ) . '</p>' );
82 - return;
8385 }
84 - }
8586
86 - $form_title = Title::makeTitleSafe( SF_NS_FORM, $form_name );
 87+ $form_title = Title::makeTitleSafe( SF_NS_FORM, $form_name );
8788
88 - if ( $target_name != '' ) {
89 - $target_title = Title::newFromText( $target_name );
90 - if ( $target_title->exists() ) {
91 - $s = wfMsg( 'sf_formedit_edittitle', $form_title->getText(), $target_title->getPrefixedText() );
92 - } else {
93 - $s = wfMsg( 'sf_formedit_createtitle', $form_title->getText(), $target_title->getPrefixedText() );
 89+ if ( $target_name != '' ) {
 90+ $target_title = Title::newFromText( $target_name );
 91+ if ( $target_title->exists() ) {
 92+ $s = wfMsg( 'sf_formedit_edittitle', $form_title->getText(), $target_title->getPrefixedText() );
 93+ } else {
 94+ $s = wfMsg( 'sf_formedit_createtitle', $form_title->getText(), $target_title->getPrefixedText() );
 95+ }
 96+ $wgOut->setPageTitle( $s );
9497 }
95 - $wgOut->setPageTitle( $s );
96 - }
9798
98 - // handling is different depending on whether page already exists
99 - // or not
100 - if ( $target_title && $target_title->exists() ) {
101 - if ( $wgRequest->getVal( 'query' ) == 'true' ) {
102 - $page_contents = null;
103 - $page_is_source = false;
104 - } else {
105 - $target_article = new Article( $target_title );
106 - $page_contents = $target_article->getContent();
107 - $page_is_source = true;
108 - }
109 - } elseif ( $target_name != '' ) {
110 - $target_name = str_replace( '_', ' ', $target_name );
111 - }
112 -
113 - if ( ! $form_title || ! $form_title->exists() ) {
114 - if ( $form_name == '' )
115 - $text = '<p class="error">' . wfMsg( 'sf_formedit_badurl' ) . "</p>\n";
116 - else {
117 - if ( count( $alt_forms ) > 0 ) {
118 - $text .= '<div class="infoMessage">' . wfMsg( 'sf_formedit_altformsonly' ) . ' ';
119 - $text .= self::printAltFormsList( $alt_forms, $form_name );
120 - $text .= "</div>\n";
121 - } else
122 - $text = '<p class="error">' . wfMsg( 'sf_formstart_badform', SFUtils::linkText( SF_NS_FORM, $form_name ) ) . ".</p>\n";
 99+ // handling is different depending on whether or not page
 100+ // already exists
 101+ if ( $target_title && $target_title->exists() ) {
 102+ if ( $wgRequest->getVal( 'query' ) == 'true' ) {
 103+ $page_contents = null;
 104+ $page_is_source = false;
 105+ } else {
 106+ $target_article = new Article( $target_title );
 107+ $page_contents = $target_article->getContent();
 108+ $page_is_source = true;
 109+ }
 110+ } elseif ( $target_name != '' ) {
 111+ $target_name = str_replace( '_', ' ', $target_name );
123112 }
124 - } elseif ( $target_name == '' && $page_name_formula == '' ) {
125 - $text = '<p class="error">' . wfMsg( 'sf_formedit_badurl' ) . "</p>\n";
126 - } else {
127 - $form_article = new Article( $form_title );
128 - $form_definition = $form_article->getContent();
129113
130 - $save_page = $wgRequest->getCheck( 'wpSave' );
131 - $preview_page = $wgRequest->getCheck( 'wpPreview' );
132 - $diff_page = $wgRequest->getCheck( 'wpDiff' );
133 - $form_submitted = ( $save_page || $preview_page || $diff_page );
134 - // get 'preload' query value, if it exists
135 - if ( ! $form_submitted ) {
136 - if ( $wgRequest->getCheck( 'preload' ) ) {
137 - $page_is_source = true;
138 - $page_contents = SFFormUtils::getPreloadedText( $wgRequest->getVal( 'preload' ) );
 114+ if ( ! $form_title || ! $form_title->exists() ) {
 115+ if ( $form_name == '' ) {
 116+ $text = '<p class="error">' . wfMsg( 'sf_formedit_badurl' ) . "</p>\n";
139117 } else {
140 - // let other extensions preload the page, if they want
141 - wfRunHooks( 'sfEditFormPreloadText', array( &$page_contents, $target_title, $form_title ) );
142 - $page_is_source = ( $page_contents != null );
 118+ if ( count( $alt_forms ) > 0 ) {
 119+ $text .= '<div class="infoMessage">' . wfMsg( 'sf_formedit_altformsonly' ) . ' ';
 120+ $text .= self::printAltFormsList( $alt_forms, $form_name );
 121+ $text .= "</div>\n";
 122+ } else
 123+ $text = '<p class="error">' . wfMsg( 'sf_formstart_badform', SFUtils::linkText( SF_NS_FORM, $form_name ) ) . ".</p>\n";
143124 }
 125+ } elseif ( $target_name == '' && $page_name_formula == '' ) {
 126+ $text = '<p class="error">' . wfMsg( 'sf_formedit_badurl' ) . "</p>\n";
144127 } else {
145 - $page_is_source = false;
146 - $page_contents = null;
147 - }
148 - list ( $form_text, $javascript_text, $data_text, $form_page_title, $generated_page_name ) =
149 - $sfgFormPrinter->formHTML( $form_definition, $form_submitted, $page_is_source, $form_article->getID(), $page_contents, $target_name, $page_name_formula );
150 - if ( $form_submitted ) {
151 - if ( $page_name_formula != '' ) {
152 - $target_name = $generated_page_name;
153 - // prepend a super-page, if one was specified
154 - if ( $wgRequest->getCheck( 'super_page' ) ) {
155 - $target_name = $wgRequest->getVal( 'super_page' ) . '/' . $target_name;
 128+ $form_article = new Article( $form_title );
 129+ $form_definition = $form_article->getContent();
 130+
 131+ $save_page = $wgRequest->getCheck( 'wpSave' );
 132+ $preview_page = $wgRequest->getCheck( 'wpPreview' );
 133+ $diff_page = $wgRequest->getCheck( 'wpDiff' );
 134+ $form_submitted = ( $save_page || $preview_page || $diff_page );
 135+ // get 'preload' query value, if it exists
 136+ if ( ! $form_submitted ) {
 137+ if ( $wgRequest->getCheck( 'preload' ) ) {
 138+ $page_is_source = true;
 139+ $page_contents = SFFormUtils::getPreloadedText( $wgRequest->getVal( 'preload' ) );
 140+ } else {
 141+ // let other extensions preload the
 142+ // page, if they want
 143+ wfRunHooks( 'sfEditFormPreloadText', array( &$page_contents, $target_title, $form_title ) );
 144+ $page_is_source = ( $page_contents != null );
156145 }
157 - // prepend a namespace, if one was specified
158 - if ( $wgRequest->getCheck( 'namespace' ) ) {
159 - $target_name = $wgRequest->getVal( 'namespace' ) . ':' . $target_name;
160 - }
161 - // replace "unique number" tag with one that
162 - // won't get erased by the next line
163 - $target_name = preg_replace( '/<unique number(.*)>/', '{num\1}', $target_name, 1 );
164 - // if any formula stuff is still in the name
165 - // after the parsing, just remove it
166 - $target_name = StringUtils::delimiterReplace( '<', '>', '', $target_name );
 146+ } else {
 147+ $page_is_source = false;
 148+ $page_contents = null;
 149+ }
 150+ list ( $form_text, $javascript_text, $data_text, $form_page_title, $generated_page_name ) =
 151+ $sfgFormPrinter->formHTML( $form_definition, $form_submitted, $page_is_source, $form_article->getID(), $page_contents, $target_name, $page_name_formula );
 152+ if ( $form_submitted ) {
 153+ if ( $page_name_formula != '' ) {
 154+ $target_name = $generated_page_name;
 155+ // prepend a super-page, if one was specified
 156+ if ( $wgRequest->getCheck( 'super_page' ) ) {
 157+ $target_name = $wgRequest->getVal( 'super_page' ) . '/' . $target_name;
 158+ }
 159+ // prepend a namespace, if one was specified
 160+ if ( $wgRequest->getCheck( 'namespace' ) ) {
 161+ $target_name = $wgRequest->getVal( 'namespace' ) . ':' . $target_name;
 162+ }
 163+ // replace "unique number" tag with one
 164+ // that won't get erased by the next line
 165+ $target_name = preg_replace( '/<unique number(.*)>/', '{num\1}', $target_name, 1 );
 166+ // if any formula stuff is still in the
 167+ // name after the parsing, just remove it
 168+ $target_name = StringUtils::delimiterReplace( '<', '>', '', $target_name );
167169
168 - // now run the parser on it
169 - global $wgParser;
170 - // ...but first, replace spaces back with
171 - // underlines, in case a magic word or parser
172 - // function name contains underlines -
173 - // hopefully this won't cause problems of
174 - // its own
175 - $target_name = str_replace( ' ', '_', $target_name );
176 - $target_name = $wgParser->recursiveTagParse( $target_name );
 170+ // now run the parser on it
 171+ global $wgParser;
 172+ // ...but first, replace spaces back
 173+ // with underlines, in case a magic word
 174+ // or parser function name contains
 175+ // underlines - hopefully this won't
 176+ // cause problems of its own
 177+ $target_name = str_replace( ' ', '_', $target_name );
 178+ $target_name = $wgParser->recursiveTagParse( $target_name );
177179
178 - if ( strpos( $target_name, '{num' ) ) {
179 - // get unique number start value from
180 - // target name; if it's not there, or
181 - // it's not a positive number,
182 - // start it out as blank
183 - preg_match( '/{num.*start=([^;]*).*}/', $target_name, $matches );
184 - if ( count( $matches ) == 2 && is_numeric( $matches[1] ) && $matches[1] >= 0 ) {
185 - $title_number = $matches[1];
 180+ if ( strpos( $target_name, '{num' ) ) {
 181+ // get unique number start value
 182+ // from target name; if it's not
 183+ // there, or it's not a positive
 184+ // number, start it out as blank
 185+ preg_match( '/{num.*start=([^;]*).*}/', $target_name, $matches );
 186+ if ( count( $matches ) == 2 && is_numeric( $matches[1] ) && $matches[1] >= 0 ) {
 187+ $title_number = $matches[1];
 188+ } else {
 189+ $title_number = "";
 190+ }
 191+ // cycle through numbers for
 192+ // this tag until we find one
 193+ // that gives a nonexistent page
 194+ // title
 195+ do {
 196+ $target_title = Title::newFromText( preg_replace( '/{num.*}/', $title_number, $target_name ) );
 197+ // if title number is blank,
 198+ // change it to 2; otherwise,
 199+ // increment it, and if necessary
 200+ // pad it with leading 0s as well
 201+ if ( $title_number == "" ) {
 202+ $title_number = 2;
 203+ } else {
 204+ $title_number = str_pad( $title_number + 1, strlen( $title_number ), '0', STR_PAD_LEFT );
 205+ }
 206+ } while ( $target_title->exists() );
186207 } else {
187 - $title_number = "";
 208+ $target_title = Title::newFromText( $target_name );
188209 }
189 - // cycle through numbers for this tag
190 - // until we find one that gives a
191 - // nonexistent page title
192 - do {
193 - $target_title = Title::newFromText( preg_replace( '/{num.*}/', $title_number, $target_name ) );
194 - // if title number is blank,
195 - // change it to 2; otherwise,
196 - // increment it, and if necessary
197 - // pad it with leading 0s as well
198 - if ( $title_number == "" ) {
199 - $title_number = 2;
200 - } else {
201 - $title_number = str_pad( $title_number + 1, strlen( $title_number ), '0', STR_PAD_LEFT );
202 - }
203 - } while ( $target_title->exists() );
204 - } else {
205 - $target_title = Title::newFromText( $target_name );
206210 }
207 - }
208 - if ( is_null( $target_title ) ) {
209 - die ( wfMsg( 'badtitle' ) . ": $target_name" );
210 - }
211 - $wgOut->setArticleBodyOnly( true );
212 - $text = SFUtils::printRedirectForm( $target_title, $data_text, $wgRequest->getVal( 'wpSummary' ), $save_page, $preview_page, $diff_page, $wgRequest->getCheck( 'wpMinoredit' ), $wgRequest->getCheck( 'wpWatchthis' ), $wgRequest->getVal( 'wpStarttime' ), $wgRequest->getVal( 'wpEdittime' ) );
213 - } else {
214 - // override the default title for this page if
215 - // a title was specified in the form
216 - if ( $form_page_title != null ) {
217 - if ( $target_name == '' ) {
218 - $wgOut->setPageTitle( $form_page_title );
219 - } else {
220 - $wgOut->setPageTitle( "$form_page_title: {$target_title->getPrefixedText()}" );
 211+ if ( is_null( $target_title ) ) {
 212+ die ( wfMsg( 'badtitle' ) . ": $target_name" );
221213 }
222 - }
223 - $text = "";
224 - if ( count( $alt_forms ) > 0 ) {
225 - $text .= '<div class="infoMessage">' . wfMsg( 'sf_formedit_altforms' ) . ' ';
226 - $text .= self::printAltFormsList( $alt_forms, $target_name );
227 - $text .= "</div>\n";
228 - }
229 - $text .= <<<END
 214+ $wgOut->setArticleBodyOnly( true );
 215+ $text = SFUtils::printRedirectForm( $target_title, $data_text, $wgRequest->getVal( 'wpSummary' ), $save_page, $preview_page, $diff_page, $wgRequest->getCheck( 'wpMinoredit' ), $wgRequest->getCheck( 'wpWatchthis' ), $wgRequest->getVal( 'wpStarttime' ), $wgRequest->getVal( 'wpEdittime' ) );
 216+ } else {
 217+ // override the default title for this page if
 218+ // a title was specified in the form
 219+ if ( $form_page_title != null ) {
 220+ if ( $target_name == '' ) {
 221+ $wgOut->setPageTitle( $form_page_title );
 222+ } else {
 223+ $wgOut->setPageTitle( "$form_page_title: {$target_title->getPrefixedText()}" );
 224+ }
 225+ }
 226+ $text = "";
 227+ if ( count( $alt_forms ) > 0 ) {
 228+ $text .= '<div class="infoMessage">' . wfMsg( 'sf_formedit_altforms' ) . ' ';
 229+ $text .= self::printAltFormsList( $alt_forms, $target_name );
 230+ $text .= "</div>\n";
 231+ }
 232+ $text .= <<<END
230233 <form name="createbox" onsubmit="return validate_all()" action="" method="post" class="createbox">
231234
232235 END;
233 - $text .= $form_text;
 236+ $pre_form_html = '';
 237+ wfRunHooks( 'sfHTMLBeforeForm', array( &$page_title, &$pre_form_html ) );
 238+ $text .= $pre_form_html;
 239+ $text .= $form_text;
 240+ }
234241 }
 242+ SFUtils::addJavascriptAndCSS();
 243+ // instead of adding the Javascript using addScript(), which is
 244+ // the standard approach, we add it using addHTML(), below the
 245+ // form text - that's so the Javascript created for fields with
 246+ // a 'show on select' parameter, if there are any, get placed
 247+ // below the form HTML, so that they can affect (i.e., hide) the
 248+ // relevant form fields. if there's a less hacky way to do this,
 249+ // the code should switch to that.
 250+ // if (! empty($javascript_text))
 251+ // $wgOut->addScript(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n");
 252+ $wgOut->addHTML( $text );
 253+ if ( ! empty( $javascript_text ) ) {
 254+ $wgOut->addHTML( ' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n" );
 255+ }
235256 }
236 - SFUtils::addJavascriptAndCSS();
237 - // instead of adding the Javascript using addScript(), which is the
238 - // standard approach, we add it using addHTML(), below the form text -
239 - // that's so the Javascript created for fields with a 'show on select'
240 - // parameter, if there are any, get placed below the form HTML, so
241 - // that they can affect (i.e., hide) the relevant form fields.
242 - // if there's a less hacky way to do this, the code should switch to
243 - // that.
244 - // if (! empty($javascript_text))
245 - // $wgOut->addScript(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n");
246 - $wgOut->addHTML( $text );
247 - if ( ! empty( $javascript_text ) )
248 - $wgOut->addHTML( ' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n" );
249 -}
250257
251258 }

Status & tagging log