public static function listDrafts(&$title = null, $user = null) { global $wgOut, $wgRequest, $wgUser, $wgLang; //added twice? $wgOut->addScript('<style type="text/css" media="all">/*<![CDATA[*/ @import "' . wfGetPad('/extensions/min/f/extensions/Drafts/Drafts.css?rev=') . WH_SITEREV . '"; /*]]>*/</style>'); // Get draftID $currentDraft = Draft::newFromID($wgRequest->getIntOrNull('draft')); //based on user preference $adv = $wgUser->getOption('defaulteditor', '') == 'advanced' ? '&advanced=true' : ''; // Output HTML for list of drafts $drafts = Draft::getDrafts($title, $user); if (count($drafts) > 0) { global $egDraftsLifeSpan; // Internationalization wfLoadExtensionMessages('Drafts'); // Add a summary, on Special:Drafts only if (!$title || $title->getNamespace() == NS_SPECIAL) { $wgOut->wrapWikiMsg('<div class="mw-drafts-summary">$1</div>', array('drafts-view-summary', $wgLang->formatNum($egDraftsLifeSpan))); } // Build XML $wgOut->addHTML(Xml::openElement('table', array('cellpadding' => 5, 'cellspacing' => 0, 'border' => 0, 'id' => 'drafts-list-table', 'class' => 'section_text', 'style' => 'margin-top:15px'))); $wgOut->addHTML(Xml::openElement('tr')); $wgOut->addHTML(Xml::element('th', array('align' => 'left', 'nowrap' => 'nowrap', 'style' => 'text-align:center;'), wfMsg('drafts-view-article'))); $wgOut->addHTML(Xml::element('th', array('align' => 'left', 'nowrap' => 'nowrap', 'style' => 'text-align:center;'), wfMsg('drafts-view-saved'))); $wgOut->addHTML(Xml::element('th', array(), wfMsg('edit'))); $wgOut->addHTML(Xml::element('th', array(), wfMsg('discard'))); $wgOut->addHTML(Xml::closeElement('tr')); // Add existing drafts for this page and user $index = 0; foreach ($drafts as $draft) { // Get article title text $htmlTitle = $draft->getTitle()->getEscapedText(); // Build Article Load link if ($draft->getHTML5()) { $params = 'h5e=true&draft=' . urlencode($draft->getID()); if ($draft->getTitle()->getArticleID() == 0) { $params .= "&create-new-article=true"; } $urlLoad = $draft->getTitle()->getFullUrl($params); } else { $urlLoad = $draft->getTitle()->getFullUrl('action=edit' . $adv . '&draft=' . urlencode($draft->getID())); } // Build discard link $urlDiscard = sprintf('%s?discard=%s&token=%s', SpecialPage::getTitleFor('Drafts')->getFullUrl(), urlencode($draft->getID()), urlencode($wgUser->editToken())); // If in edit mode, return to editor if ($wgRequest->getText('action') == 'edit' || $wgRequest->getText('action') == 'submit') { $urlDiscard .= '&returnto=' . urlencode('edit'); } // Append section to titles and links if ($draft->getSection() !== null) { // Detect section name $lines = explode("\n", $draft->getText()); // If there is any content in the section if (count($lines) > 0) { $htmlTitle .= '#' . htmlspecialchars(trim(trim(substr($lines[0], 0, 255), '='))); } // Modify article link and title $urlLoad .= '§ion=' . urlencode($draft->getSection()); $urlDiscard .= '§ion=' . urlencode($draft->getSection()); } // Build XML if ($index % 2 == 1) { $wgOut->addHTML(Xml::openElement('tr', array('style' => 'background: #eee;'))); } else { $wgOut->addHTML(Xml::openElement('tr', array('style' => ''))); } $wgOut->addHTML(Xml::openElement('td', array('align' => 'left', 'class' => 'draftpage'))); $wgOut->addHTML(Xml::element('a', array('href' => $urlLoad, 'style' => 'font-weight:' . ($currentDraft->getID() == $draft->getID() ? 'bold' : 'normal')), $htmlTitle)); $wgOut->addHTML(Xml::closeElement('td')); $wgOut->addHTML(Xml::element('td', array('align' => 'left', 'nowrap' => 'nowrap', 'class' => 'draftsaved'), $wgLang->timeanddate($draft->getSaveTime(), true))); // edit link $wgOut->addHTML(Xml::openElement('td', array('align' => 'left', 'nowrap' => 'nowrap', 'class' => 'draftedit'))); $wgOut->addHTML(Xml::element('a', array('href' => $urlLoad), wfMsg('edit'))); $wgOut->addHTML(Xml::closeElement('td')); $wgOut->addHTML(Xml::openElement('td', array('align' => 'left', 'nowrap' => 'nowrap', 'class' => 'draftdiscard'))); $wgOut->addHTML(Xml::element('a', array('href' => $urlDiscard, 'onclick' => "if( true || !wgAjaxSaveDraft.insync ) return confirm('" . Xml::escapeJsString(wfMsgHTML('drafts-discard-warn', $draft->getTitle()->getText())) . "')"), wfMsg('drafts-view-discard'))); $wgOut->addHTML(Xml::closeElement('td')); $wgOut->addHTML(Xml::closeElement('tr')); $index++; } $wgOut->addHTML(Xml::closeElement('table')); // Return number of drafts return count($drafts); } return 0; }
function execute($par) { global $wgOut, $wgRequest, $wgParser, $wgUser, $wgFilterCallback, $wgCookiePath, $wgCookieDomain, $wgCookieSecure; $wgOut->disable(); // build the article which we are about to save $t = Title::newFromUrl($wgRequest->getVal('target')); $a = new Article($t); $action = $wgRequest->getVal('eaction'); wfDebug("Html5Editor::execute called with {$action}\n"); // process the edit update if ($action == 'get-vars') { $wgOut->disable(); $response = array('edittoken' => $wgUser->editToken(), 'edittime' => $a->getTimestamp(true), 'drafttoken' => wfGenerateToken(), 'olddraftid' => 0); // do they already have a draft saved? $drafts = Draft::getDrafts($t, $wgUser->getID()); if ($drafts) { // do we only select an html5 draft? probably not. // for loop here in case we want to display multiple drafts of same article $response['olddraftid'] = $drafts[0]->getID(); } print json_encode($response); return; } else { if ($action == 'load-draft') { $draftid = $wgRequest->getVal('draftid'); $draft = new Draft($draftid); if (!$draft->exists()) { wfLoadExtensionMessages("Html5editor"); $response = array('error' => wfMsg('h5e-draft-does-not-exist', $draftid), 'html' => ''); wfDebug("DRAFT: {$draftid} does not exist \n"); } else { $text = $draft->getText(); $html = $this->parse($t, $a, $text); $response = array(error => '', 'html' => $html); } print json_encode($response); return; } else { if ($action == 'save-draft') { $token = $wgRequest->getVal('edittoken'); if ($wgUser->matchEditToken($token)) { wfDebug("Html5Editor::execute save-draft edit token ok!\n"); $oldtext = $a->getContent(); $html = $wgRequest->getVal('html'); $newtext = $this->convertHTML2Wikitext($html, $oldtext); $draftid = $wgRequest->getVal('draftid', null); $draft = null; // 'null' apparently is what javascript is giving us. doh. if (!$draftid || preg_match("@[^0-9]@", $draftid)) { wfDebug("Html5Editor::execute getting draft id from title \n"); $draftid = self::getDraftIDFromTitle($t); } if (!$draftid || $draftid == 'null') { $draft = new Draft(); } else { $draft = Draft::newFromID($draftid); } wfDebug("Html5Editor::execute got draft id {$draftid} \n"); $draft->setTitle($t); //$draft->setStartTime( $wgRequest->getText( 'wpStarttime' ) ); $draft->setEditTime($wgRequest->getText('edittime')); $draft->setSaveTime(wfTimestampNow()); $draft->setText($newtext); $draft->setSummary($wgRequest->getText('editsummary')); $draft->setHtml5(true); //$draft->setMinorEdit( $wgRequest->getInt( 'wpMinoredit', 0 ) ); // Save draft $draft->save(); wfDebug("Html5Editor::execute saved draft with id {$draft->getID()} and text {$newtext} \n"); $response = array('draftid' => $draft->getID()); print json_encode($response); return; } else { wfDebug("Html5Editor::execute save-draft edit token BAD {$token} \n"); $response = array('error' => 'edit token bad'); print json_encode($response); return; } return; } else { if ($action == 'save-summary') { // this implementation could have a few problems // 1. if a user is editing the article in separate windows, it will // only update the last edit // 2. Could be easy to fake an edit summary save, but is limited to // edits made by the user /// 3. There's no real 'paper' trail of the saved summary // grab the cookie with the rev_id global $wgCookiePrefix; if (isset($_COOKIE["{$wgCookiePrefix}RevId" . $t->getArticleID()])) { $revid = $_COOKIE["{$wgCookiePrefix}RevId" . $t->getArticleID()]; wfDebug("AXX: updating revcomment {$revid} \n"); $dbw = wfGetDB(DB_MASTER); $summary = "updating from html5 editor, " . $wgRequest->getVal('summary'); $dbw->update('revision', array('rev_comment' => $summary), array('rev_id' => $revid, 'rev_user_text' => $wgUser->getName()), "Html5Editor::saveComment", array("LIMIT" => 1)); $dbw->update('recentchanges', array('rc_comment' => $summary), array('rc_this_oldid' => $revid, 'rc_user_text' => $wgUser->getName()), "Html5Editor::saveComment", array("LIMIT" => 1)); } else { wfDebug("AXX: NOT updating revcomment, why\n"); } return; } else { if ($action == 'publish-html') { // check the edit token $token = $wgRequest->getVal('edittoken'); if (!$wgUser->matchEditToken($token)) { $response = array('error' => wfMsg('sessionfailure')); print json_encode($response); return; } // check the edit time and check for a conflict $edittime = $wgRequest->getVal('edittime'); if (!preg_match('/^\\d{14}$/', $edittime)) { $edittime = null; } if (!$edittime) { $response = array('error' => 'missing or invalid edit time'); print json_encode($response); return; } if ($response = $this->getPermissionErrors($t)) { print json_encode($response); return; } $newArticle = !$t->exists(); $a = new Article($t); // check for edit conflict // if( $this->mArticle->getTimestamp() != $this->edittime ) { // $this->isConflict = true; // } // now ... let's convert the HTML back into wikitext... holy crap, we are nuts $oldtext = $a->getContent(); $html = $wgRequest->getVal('html'); $newtext = $this->convertHTML2Wikitext($html, $oldtext); // filter callback? if ($wgFilterCallback && $wgFilterCallback($t, $newtext, null)) { # Error messages or other handling should be performed by the filter function $response = array('error' => self::$spam_message, 'html' => $html); print json_encode($response); return; } // do the save // TODO: check for conflicts (obviously) if ($a->doEdit($newtext, $wgRequest->getVal('summary') . " (HTML5) ")) { //$alerts = new MailAddress("*****@*****.**"); //UserMailer::send($alerts, $alerts, "HTML5 Ouput for {$t->getText()}", "{$t->getFullURL()}?action=history \n HTML: " . trim($html) . "\n\nwikitext:\n $newtext\n\n\nUser: "******"\n\n\n\nPOST: " . print_r($_POST, true) ); $r = Revision::newFromTitle($t); $this->setRevCookie($t, $r); #$html = WikihowArticleHTML::postProcess($wgOut->parse($newtext)); $html = $this->parse($t, $a, $newtext); // Create an anon attribution cookie if ($newArticle && $wgUser->getId() == 0) { setcookie('aen_anon_newarticleid', $a->getId(), time() + 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure); } $response = array(error => '', 'html' => $html); print json_encode($response); return; } else { $response = array(error => 'Error saving', 'html' => ''); print json_encode($response); return; } } } } } } return; }