private function getWikitextArray($type, &$wikitext) { $section = Wikitext::getSection($wikitext, $type, true); $tips = Wikitext::splitTips($section[0]); if (!empty($tips)) { // Special case: If first tip/warning isn't a properly formed wikitext tip // ie: a tip/warning preceded with a '*', don't return a wikitext array // as the section is malformed if (preg_match('@^[^*]@', $tips[0])) { throw new Exception(self::EXCEPTION_MALFORMED_WIKITEXT . ": \"" . $tips[0] . "\""); } return $tips; } }
function revertTipOnArticle($pageId, $revId) { global $wgParser; // do not revert if no revId if ($revId <= 0 || $revId == null || $revId == "") { return false; } $undoRevision = Revision::newFromId($revId); $previousRevision = $undoRevision ? $undoRevision->getPrevious() : null; // do not revert if the page is wrong or changed.. if (is_null($undoRevision) || is_null($previousRevision) || $undoRevision->getPage() != $previousRevision->getPage() || $undoRevision->getPage() != $pageId) { return false; } $title = Title::newFromID($pageId); $article = new Article($title); $undoRevisionText = $undoRevision->getText(); $currentText = $article->getContent(); $undoTips = Wikitext::splitTips(reset(Wikitext::getSection($undoRevisionText, "Tips", true))); $prevTips = Wikitext::splitTips(reset(Wikitext::getSection($previousRevision->getText(), "Tips", true))); $currentTipsSection = Wikitext::getSection($currentText, "Tips", true); $currentTips = Wikitext::splitTips($currentTipsSection[0]); $section = $currentTipsSection[1]; $undoTipsFormatted = array(); foreach ($undoTips as $tip) { $undoTipsFormatted[] = self::cleanTip($tip); } $prevTipsFormatted = array(); foreach ($prevTips as $tip) { $prevTipsFormatted[] = self::cleanTip($tip); } $badTips = array_diff($undoTipsFormatted, $prevTipsFormatted); $resultTips = "== Tips =="; foreach ($currentTips as $currentTip) { $tip = self::cleanTip($currentTip); if (in_array($tip, $badTips)) { continue; } $resultTips .= "\n" . $currentTip; } $newText = $wgParser->replaceSection($currentText, $section, $resultTips); $success = $article->doEdit($newText, 'reverting tip from revision ' . $revId, EDIT_UPDATE | EDIT_MINOR); // mark the recent change as patrolled if ($success) { // should be ok to read from slave here because the change has been done earlier. $dbr = wfGetDB(DB_SLAVE); $rcid = $dbr->selectField('recentchanges', 'rc_id', array("rc_this_oldid={$revId}")); RecentChange::markPatrolled($rcid); PatrolLog::record($rcid, false); } return $success; }