r63492 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63491‎ | r63492 | r63493 >
Date:19:42, 9 March 2010
Author:yaron
Status:deferred
Tags:
Comment:
Renamed class and page; added parsing for "page name="; moved Javascript to below HTML, so that "show on select=" parameter can work
Modified paths:
  • /trunk/extensions/SemanticForms/specials/SF_FormEdit.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/specials/SF_FormEdit.php
@@ -1,18 +1,19 @@
22 <?php
33 /**
4 - * Displays a pre-defined form for adding data.
 4+ * Displays a pre-defined form for either creating a new page or editing an
 5+ * existing one.
56 *
67 * @author Yaron Koren
78 */
89 if (!defined('MEDIAWIKI')) die();
910
10 -class SFAddData extends SpecialPage {
 11+class SFFormEdit extends SpecialPage {
1112
1213 /**
1314 * Constructor
1415 */
15 - function SFAddData() {
16 - SpecialPage::SpecialPage('AddData');
 16+ function SFFormEdit() {
 17+ SpecialPage::SpecialPage('FormEdit');
1718 wfLoadExtensionMessages('SemanticForms');
1819 }
1920
@@ -32,27 +33,27 @@
3334
3435 $alt_forms = $wgRequest->getArray('alt_form');
3536
36 - self::printAddForm($form_name, $target_name, $alt_forms);
 37+ self::printForm($form_name, $target_name, $alt_forms);
3738 }
3839
3940 static function printAltFormsList($alt_forms, $target_name) {
4041 $text = "";
41 - $ad = SpecialPage::getPage('AddData');
 42+ $fe = SpecialPage::getPage('FormEdit');
 43+ $fe_url = $fe->getTitle()->getFullURL();
4244 $i = 0;
4345 foreach ($alt_forms as $alt_form) {
4446 if ($i++ > 0) { $text .= ", "; }
45 - $text .= '<a href="' . $ad->getTitle()->getFullURL() . "/" . $alt_form . "/" . $target_name . '">' . str_replace('_', ' ', $alt_form) . "</a>";
 47+ $text .= "<a href=\"$fe_url/$alt_form/$target_name\">" . str_replace('_', ' ', $alt_form) . "</a>";
4648 }
4749 return $text;
4850 }
4951
50 -static function printAddForm($form_name, $target_name, $alt_forms) {
 52+static function printForm($form_name, $target_name, $alt_forms = array()) {
5153 global $wgOut, $wgRequest, $wgScriptPath, $sfgScriptPath, $sfgFormPrinter, $sfgYUIBase;
5254
5355 wfLoadExtensionMessages('SemanticForms');
5456
5557 // initialize some variables
56 - $page_title = null;
5758 $target_title = null;
5859 $page_name_formula = null;
5960
@@ -68,8 +69,13 @@
6970 $form_definition = $form_article->getContent();
7071 $form_definition = StringUtils::delimiterReplace('<noinclude>', '</noinclude>', '', $form_definition);
7172 $matches;
72 - if (preg_match('/{{{info.*page name=([^\|}]*)/m', $form_definition, $matches)) {
 73+ if (preg_match('/{{{info.*page name=([^\|]*)/m', $form_definition, $matches)) {
7374 $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);
 79+ }
7480 } elseif (count($alt_forms) == 0) {
7581 $wgOut->addWikiText( "<p class='error'>" . wfMsg('sf_adddata_badurl') . '</p>');
7682 return;
@@ -80,17 +86,30 @@
8187
8288 if ($target_name != '') {
8389 $target_title = Title::newFromText($target_name);
84 - $s = wfMsg('sf_adddata_title', $form_title->getText(), $target_title->getPrefixedText());
 90+ if ($target_title->exists()) {
 91+ $s = wfMsg('sf_editdata_title', $form_title->getText(), $target_title->getPrefixedText());
 92+ } else {
 93+ $s = wfMsg('sf_adddata_title', $form_title->getText(), $target_title->getPrefixedText());
 94+ }
8595 $wgOut->setPageTitle($s);
8696 }
8797
88 - // target_title should be null - we shouldn't be adding a page that
89 - // already exists
 98+ // handling is different depending on whether page already exists
 99+ // or not
90100 if ($target_title && $target_title->exists()) {
91 - $wgOut->addWikiText( "<p class='error'>" . wfMsg('articleexists') . '</p>');
92 - return;
 101+ if ($wgRequest->getVal('query') == 'true') {
 102+ $page_contents = null;
 103+ $page_is_source = false;
 104+ } elseif ($content != null) {
 105+ $page_contents = $content;
 106+ $page_is_source = true;
 107+ } else {
 108+ $target_article = new Article($target_title);
 109+ $page_contents = $target_article->getContent();
 110+ $page_is_source = true;
 111+ }
93112 } elseif ($target_name != '') {
94 - $page_title = str_replace('_', ' ', $target_name);
 113+ $target_name = str_replace('_', ' ', $target_name);
95114 }
96115
97116 if (! $form_title || ! $form_title->exists()) {
@@ -129,7 +148,7 @@
130149 $page_contents = null;
131150 }
132151 list ($form_text, $javascript_text, $data_text, $form_page_title, $generated_page_name) =
133 - $sfgFormPrinter->formHTML($form_definition, $form_article->getID(), $form_submitted, $page_is_source, $page_contents, $page_title, $page_name_formula);
 152+ $sfgFormPrinter->formHTML($form_definition, $form_submitted, $page_is_source, $form_article->getID(), $page_contents, $target_name, $page_name_formula);
134153 if ($form_submitted) {
135154 if ($page_name_formula != '') {
136155 $target_name = $generated_page_name;
@@ -147,6 +166,11 @@
148167 // if any formula stuff is still in the name
149168 // after the parsing, just remove it
150169 $target_name = StringUtils::delimiterReplace('<', '>', '', $target_name);
 170+
 171+ // now run the parser on it
 172+ global $wgParser;
 173+ $target_name = $wgParser->recursiveTagParse($target_name);
 174+
151175 if (strpos($target_name, '{num')) {
152176 // get unique number start value from
153177 // target name; if it's not there, or
@@ -177,6 +201,9 @@
178202 $target_title = Title::newFromText($target_name);
179203 }
180204 }
 205+ if (is_null($target_title)) {
 206+ die (wfMsg('img-auth-badtitle', $target_name));
 207+ }
181208 $wgOut->setArticleBodyOnly( true );
182209 $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'));
183210 } else {
@@ -203,9 +230,18 @@
204231 }
205232 }
206233 SFUtils::addJavascriptAndCSS();
 234+ // instead of adding the Javascript using addScript(), which is the
 235+ // standard approach, we add it using addHTML(), below the form text -
 236+ // that's so the Javascript created for fields with a 'show on select'
 237+ // parameter, if there are any, get placed below the form HTML, so
 238+ // that they can affect (i.e., hide) the relevant form fields.
 239+ // if there's a less hacky way to do this, the code should switch to
 240+ // that.
 241+ //if (! empty($javascript_text))
 242+ // $wgOut->addScript(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n");
 243+ $wgOut->addHTML($text);
207244 if (! empty($javascript_text))
208 - $wgOut->addScript(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n");
209 - $wgOut->addHTML($text);
 245+ $wgOut->addHTML(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n");
210246 }
211247
212248 }

Status & tagging log