Ejemplo n.º 1
0
 /**
  * Run the job
  * @return boolean success
  */
 function run()
 {
     global $wgUser, $wgCommandLineMode;
     $oldUser = $wgUser;
     $wgUser = User::newFromId($this->params['user']);
     unset($this->params['user']);
     $this->params['form'] = $this->title->getText();
     $handler = new SFAutoeditAPI(null, 'sfautoedit');
     $handler->isApiQuery(false);
     $handler->setOptions($this->params);
     $result = $handler->storeSemanticData(false);
     // wrap result in ok/error message
     if ($result === true) {
         $options = $handler->getOptions();
         $result = wfMsg('sf_autoedit_success', $options['target'], $options['form']);
     } else {
         $result = wfMsgReplaceArgs('$1', array($result));
     }
     $this->params = array('result' => $result, 'user' => $wgUser->getName());
     wfDebugLog('sps', 'Page Creation Job: ' . $result);
     $wgUser = $oldUser;
 }
Ejemplo n.º 2
0
 static function printForm(&$form_name, &$target_name, $alt_forms = array())
 {
     global $wgOut, $wgRequest;
     if (method_exists('ApiMain', 'getContext')) {
         $module = new SFAutoeditAPI(new ApiMain(), 'sfautoedit');
     } else {
         // TODO: remove else branch when raising supported version to MW 1.19
         $module = new SFAutoeditAPI(new ApiMain($wgRequest), 'sfautoedit');
     }
     $module->setOption('form', $form_name);
     $module->setOption('target', $target_name);
     // if the page was submitted, formdata should be complete => do not preload
     $module->setOption('preload', !$wgRequest->getCheck('wpSave') && !$wgRequest->getCheck('wpPreview'));
     $module->execute();
     // if action was successful and action was a Save, return
     if ($module->getStatus() === 200 && $module->getAction() === SFAutoeditAPI::ACTION_SAVE) {
         return;
     }
     // override the default title for this page if a title was specified in the form
     $result = $module->getOptions();
     $target_title = Title::newFromText($result['target']);
     if ($result['form'] !== '') {
         if ($target_name === null || $target_name === '') {
             $wgOut->setPageTitle($result['form']);
         } else {
             $wgOut->setPageTitle($result['form'] . ': ' . $target_name);
         }
     }
     $text = '';
     if (count($alt_forms) > 0) {
         $text .= '<div class="infoMessage">' . wfMessage('sf_formedit_altforms')->escaped() . ' ';
         $text .= self::printAltFormsList($alt_forms, $target_name);
         $text .= "</div>\n";
     }
     $text .= '<form name="createbox" id="sfForm" method="post" class="createbox">';
     $pre_form_html = '';
     wfRunHooks('sfHTMLBeforeForm', array(&$target_title, &$pre_form_html));
     $text .= $pre_form_html;
     $text .= $result['formHTML'];
     SFUtils::addJavascriptAndCSS();
     $javascript_text = $result['formJS'];
     if (!empty($javascript_text)) {
         $wgOut->addScript('		<script type="text/javascript">' . "\n{$javascript_text}\n" . '</script>' . "\n");
     }
     $wgOut->addHTML($text);
     return null;
 }
Ejemplo n.º 3
0
 private function evaluateForm(WebRequest &$request)
 {
     global $wgOut, $wgUser, $spsgIterators;
     $requestValues = $_POST;
     if (array_key_exists('iteratordata', $requestValues)) {
         $iteratorData = FormatJson::decode($requestValues['iteratordata'], true);
         unset($requestValues['iteratordata']);
     } else {
         throw new SPSException(SPSUtils::buildMessage('spserror-noiteratordata'));
     }
     $iteratorName = null;
     $targetFormName = null;
     $targetFieldName = null;
     $originPageId = null;
     foreach ($iteratorData as $param => $value) {
         switch ($param) {
             case 'iterator':
                 // iteratorName
                 $iteratorName = $value;
                 break;
             case 'target_form':
                 $targetFormName = $value;
                 break;
             case 'target_field':
                 $targetFieldName = $value;
                 break;
             case 'origin':
                 $originPageId = $value;
                 break;
             default:
                 $iteratorParams[$param] = $this->getAndRemoveFromArray($requestValues, $value);
         }
     }
     if (is_null($iteratorName) || $iteratorName === '') {
         throw new SPSException(SPSUtils::buildMessage('spserror-noiteratorname'));
     }
     if (!array_key_exists($iteratorName, $spsgIterators)) {
         throw new SPSException(SPSUtils::buildMessage('spserror-iteratorunknown', $iteratorName));
     }
     // iterator
     $iterator = new $spsgIterators[$iteratorName]();
     $iteratorValues = $iterator->getValues($iteratorParams);
     $iteratorValuesCount = count($iteratorValues);
     $userlimit = $this->getPageGenerationLimit();
     // check userlimit
     if ($iteratorValuesCount > $userlimit) {
         throw new SPSException(SPSUtils::buildMessage('spserror-pagegenerationlimitexeeded', $iteratorValuesCount, $userlimit));
     }
     $targetFormTitle = Title::makeTitleSafe(SF_NS_FORM, $targetFormName);
     $targetFormPageId = $targetFormTitle->getArticleID();
     $requestValues['user'] = $wgUser->getId();
     foreach ($iteratorValues as $value) {
         SFAutoeditAPI::addToArray($requestValues, $targetFieldName, $value, true);
         wfDebugLog('sps', 'Insert SPSPageCreationJob');
         $job = new SPSPageCreationJob($targetFormTitle, $requestValues);
         $job->insert();
     }
     // if given origin page does not exist use Main page
     if (Title::newFromID($originPageId) === null) {
         $originPageId = Title::newMainPage()->getArticleID();
     }
     if (isset($_SESSION)) {
         // cookies enabled
         $request->setSessionData('spsResult', $iteratorValuesCount);
         $request->setSessionData('spsForm', $targetFormPageId);
         $request->setSessionData('spsOrigin', $originPageId);
         header('Location: ' . $this->getTitle()->getFullURL());
     } else {
         // cookies disabled, write result data to URL
         header('Location: ' . $this->getTitle()->getFullURL() . "?{$targetFormPageId};{$iteratorValuesCount};{$originPageId}");
     }
     return null;
 }
 function printForm($form_name, $targetName, $alt_forms = array())
 {
     $out = $this->getOutput();
     $req = $this->getRequest();
     $module = new SFAutoeditAPI(new ApiMain(), 'sfautoedit');
     $module->setOption('form', $form_name);
     $module->setOption('target', $targetName);
     if ($req->getCheck('wpSave') || $req->getCheck('wpPreview') || $req->getCheck('wpDiff')) {
         // If the page was submitted, form data should be
         // complete => do not preload (unless it's a partial
         // form).
         if ($req->getCheck('partial')) {
             $module->setOption('preload', true);
         } else {
             $module->setOption('preload', false);
         }
     } else {
         if (!empty($targetName) && Title::newFromText($targetName)->exists()) {
             // If target page exists, do not overwrite it with
             // preload data; just preload the page's data.
             $module->setOption('preload', true);
         } else {
             if ($req->getCheck('preload')) {
                 // if page does not exist and preload parameter is set, pass that on
                 $module->setOption('preload', $req->getText('preload'));
             } else {
                 // nothing set, so do not set preload
             }
         }
     }
     $module->execute();
     $text = '';
     // if action was successful and action was a Save, return
     if ($module->getStatus() === 200) {
         if ($module->getAction() === SFAutoeditAPI::ACTION_SAVE) {
             return;
         }
     } else {
         $resultData = $module->getResultData();
         if (array_key_exists('errors', $resultData)) {
             foreach ($resultData['errors'] as $error) {
                 // FIXME: This should probably not be hard-coded to WARNING but put into a setting
                 if ($error['level'] <= SFAutoeditAPI::WARNING) {
                     $text .= Html::rawElement('p', array('class' => 'error'), $error['message']) . "\n";
                 }
             }
         }
     }
     // Override the default title for this page if a title was
     // specified in the form.
     $result = $module->getOptions();
     $targetTitle = Title::newFromText($result['target']);
     // Set page title depending on whether an explicit title was
     // specified in the form definition.
     if (array_key_exists('formtitle', $result)) {
         // set page title depending on whether the target page exists
         if (empty($targetName)) {
             $pageTitle = $result['formtitle'];
         } else {
             $pageTitle = $result['formtitle'] . ': ' . $targetName;
         }
     } elseif ($result['form'] !== '') {
         // Set page title depending on whether the target page
         // exists.
         if (empty($targetName)) {
             $pageTitle = wfMessage('sf_formedit_createtitlenotarget', $result['form'])->text();
         } elseif ($targetTitle->exists()) {
             $pageTitle = wfMessage('sf_formedit_edittitle', $result['form'], $targetName)->text();
         } else {
             $pageTitle = wfMessage('sf_formedit_createtitle', $result['form'], $targetName)->text();
         }
     } elseif (count($alt_forms) > 0) {
         // We use the 'creating' message here, instead of
         // 'sf_formedit_createtitlenotarget', to differentiate
         // between a page with no (default) form, and one with
         // no target; in English they'll show up as
         // "Creating ..." and "Create ...", respectively.
         // Does this make any difference? Who knows.
         $pageTitle = wfMessage('creating', $targetName)->text();
     } elseif ($result['form'] == '') {
         //FIXME: This looks weird; a simple else should be enough, right?
         // display error message if the form is not specified in the URL
         $pageTitle = wfMessage('formedit')->text();
         $text .= Html::element('p', array('class' => 'error'), wfMessage('sf_formedit_badurl')->text()) . "\n";
         $out->addHTML($text);
     }
     $out->setPageTitle($pageTitle);
     if (count($alt_forms) > 0) {
         $text .= '<div class="infoMessage">';
         if ($result['form'] != '') {
             $text .= wfMessage('sf_formedit_altforms')->escaped();
         } else {
             $text .= wfMessage('sf_formedit_altformsonly')->escaped();
         }
         $text .= ' ' . $this->printAltFormsList($alt_forms, $targetName);
         $text .= "</div>\n";
     }
     $text .= '<form name="createbox" id="sfForm" method="post" class="createbox">';
     $pre_form_html = '';
     Hooks::run('sfHTMLBeforeForm', array(&$targetTitle, &$pre_form_html));
     $text .= $pre_form_html;
     if (isset($result['formHTML'])) {
         $text .= $result['formHTML'];
     }
     SFUtils::addFormRLModules();
     if (isset($result['formJS'])) {
         if (method_exists('ResourceLoader', 'makeInlineScript')) {
             // MW 1.25+
             $out->addScript(ResourceLoader::makeInlineScript($result['formJS']));
         } else {
             $out->addScript('		<script type="text/javascript">' . "\n{$result['formJS']}\n" . '</script>' . "\n");
         }
     }
     $out->addHTML($text);
     return null;
 }