Example #1
0
 /**
  *	This method will try to store the data in mOptions.
  *
  * It will return true on success or an error message on failure.
  * The used form and target page will be available in mOptions after
  * execution of the method.
  *
  * This method also sets HTTP response headers according to the result.
  *
  * @param bool $prefillFromExisting If this is set, existing values in the page will be used to prefill the form.
  * @return true or an error message
  */
 public function storeSemanticData($prefillFromExisting = true)
 {
     global $wgOut, $wgRequest;
     // If the wiki is read-only we might as well stop right away
     if (wfReadOnly()) {
         return $this->reportError(wfMsg('sf_autoedit_readonly', wfReadOnlyReason()));
     }
     // ensure 'form' key exists
     if (!array_key_exists('form', $this->mOptions)) {
         $this->mOptions['form'] = null;
     }
     // ensure 'target' key exists
     if (!array_key_exists('target', $this->mOptions)) {
         $this->mOptions['target'] = null;
     }
     // If we have no target article and no form we might as well stop right away
     if (!$this->mOptions['target'] && !$this->mOptions['form']) {
         return $this->reportError(wfMsg('sf_autoedit_notargetspecified'));
     }
     // check if form was specified
     if (!$this->mOptions['form']) {
         // If no form was specified, find the default one for
         // this page.
         $title = Title::newFromText($this->mOptions['target']);
         $form_names = SFFormLinker::getDefaultFormsForPage($title);
         // if no form can be found, return
         if (count($form_names) == 0) {
             return $this->reportError(wfMsg('sf_autoedit_noformfound'));
         }
         // if more than one form found, return
         if (count($form_names) > 1) {
             return $this->reportError(wfMsg('sf_autoedit_toomanyformsfound'));
         }
         // There should now be exactly one form.
         $this->mOptions['form'] = $form_names[0];
     }
     // we only care for the form's body
     $wgOut->setArticleBodyOnly(true);
     $formedit = new SFFormEdit();
     $data = array();
     $oldRequest = $wgRequest;
     // Get the form definition and target page (if there is one),
     // as specified in the options string, then create the actual
     // HTML form from them, and call that form to modify or create
     // the page.
     if ($prefillFromExisting) {
         $wgRequest = new FauxRequest($this->mOptions, true);
         // get the Semantic Form
         if ($this->mOptions['target']) {
             $formedit->execute($this->mOptions['form'] . '/' . $this->mOptions['target']);
         } else {
             $formedit->execute($this->mOptions['form']);
         }
         // extract its data
         $form = $this->parseDataFromHTMLFrag($data, trim($wgOut->getHTML()), 'sfForm');
         if (!$form) {
             // something went wrong
             $wgRequest = $oldRequest;
             return $this->reportError(wfMsg('sf_autoedit_nosemanticform', array($this->mOptions['target'], $this->mOptions['form'])));
         }
     } else {
         self::addToArray($data, "wpSave", "Save");
     }
     // and modify as specified
     $data = SFUtils::array_merge_recursive_distinct($data, $this->mOptions);
     ////////////////////////////////////////////////////////////////////////
     // Store the modified form
     // $wgOut->clearHTML();
     $wgRequest = new FauxRequest($data, true);
     // get the MW form
     if ($this->mOptions['target']) {
         $formedit->execute($this->mOptions['form'] . '/' . $this->mOptions['target'], false);
     } else {
         $formedit->execute($this->mOptions['form'], false);
     }
     $this->mOptions['form'] = $formedit->mForm;
     $this->mOptions['target'] = $formedit->mTarget;
     $wgRequest = $oldRequest;
     if ($formedit->mError) {
         return $this->reportError($formedit->mError);
     } else {
         if (!headers_sent()) {
             header("X-Location: " . $wgOut->getRedirect());
             header("X-Form: " . $formedit->mForm);
             header("X-Target: " . $formedit->mTarget);
         }
         if ($this->isApiQuery()) {
             $this->getResult()->addValue(null, 'result', array('code' => '200', 'location' => $wgOut->getRedirect(), 'form' => $formedit->mForm, 'target' => $formedit->mTarget));
         }
     }
     return true;
 }