/**
  *   The main function
  */
 function execute($par)
 {
     global $wgRequest, $wgUser, $wgOut, $wgImportVideoSources;
     if ($wgUser->isBlocked()) {
         $wgOut->blockedPage();
         return;
     }
     wfLoadExtensionMessages('Importvideo');
     if ($wgRequest->getVal('popup') == 'true') {
         $wgOut->setArticleBodyOnly(true);
     }
     $this->setHeaders();
     $source = $this->mSource = $wgRequest->getVal('source', 'youtube');
     $target = isset($par) ? $par : $wgRequest->getVal('target');
     $query = $wgRequest->getVal('q');
     $me = Title::makeTitle(NS_SPECIAL, "Importvideo");
     $wasnew = $this->getRequest()->getVal('wasnew');
     // some sanity checks on the target
     if ($target && !$wasnew) {
         $title = Title::newFromURL($target);
         if (!$title || !$title->exists()) {
             $wgOut->addHTML("Error: target article does not exist.");
             return;
         } else {
             $article = new Article($title);
             $article->loadPageData();
             if ($article->mIsRedirect) {
                 $wgOut->addHTML("Error: target article is a redirect.");
                 return;
             }
         }
     }
     $wgOut->addHTML("<div id='importvideo'>");
     $wgOut->addHTML("<h2>" . wfMsg('add_a_video') . "</h2>");
     # changing target article feature
     $search = $wgRequest->getVal("dosearch", null);
     if ($search != null) {
         $this->doSearch($target, $orderby, $query, $search);
         return;
     }
     $sp = null;
     switch ($source) {
         case 'howcast':
             $sp = new ImportvideoHowcast($source);
             break;
         case 'youtube':
         default:
             $sp = new ImportvideoYoutube($source);
             break;
     }
     // handle special cases where user is adding a video to a new article or by category
     if ($wgRequest->getVal('new') || $wgRequest->getVal('wasnew')) {
         if ($wgRequest->getVal('new')) {
             $t = $this->getNewArticleWithoutVideo();
             $target = $t->getText();
         } else {
             $t = Title::newFromText($target);
         }
         $wgRequest->setVal('target', $target);
     } else {
         if ($wgRequest->getVal('category') && $target == '') {
             $t = $this->getTitleFromCategory($wgRequest->getVal('category'));
             $target = $t->getText();
             $wgRequest->setVal('target', $target);
         }
     }
     // construct base url to switch between sources
     $url = $me->getFullURL() . "?target=" . urlencode($target) . "&q=" . urlencode($query) . $this->getURLExtras() . "&source=";
     $title = Title::newFromText($target);
     if (!trim($target)) {
         $wgOut->addHTML("Error: no target specified.");
         return;
     }
     $target = $title->getText();
     //get the steps and intro to show to the user
     $r = Revision::newFromTitle($title);
     $text = "";
     if ($r) {
         $text = $r->getText();
     }
     $article = new Article($title);
     $extra = $article->getSection($text, 0);
     $steps = "";
     for ($i = 1; $i < 3; $i++) {
         $xx = $article->getSection($text, $i);
         if (preg_match("/^==[ ]+" . wfMsg('steps') . "/", $xx)) {
             $steps = $xx;
             break;
         }
     }
     $extra = preg_replace("/{{[^}]*}}/", "", $extra);
     $extra = $wgOut->parse($extra);
     $steps = $wgOut->parse($steps);
     $cancel = "";
     $nextlink = "/Special:Importvideo?new=1&skip={$title->getArticleID()}";
     if ($wgRequest->getVal('category')) {
         $nextlink = "/Special:Importvideo?category=" . urlencode($wgRequest->getVal('category'));
     }
     if ($wgRequest->getVal('popup') != 'true') {
         $wgOut->addHTML("<div class='article_title'>\n\t\t\t\t" . wfMsg('importvideo_article') . "- <a href='{$title->getFullURL()}' target='new'>" . wfMsg('howto', $title->getText()) . "</a>");
         $wgOut->addHTML("<spanid='showhide' style='font-size: 80%; text-align:right; font-weight: normal;'>\n\t\t\t\t\t(<a href='{$nextlink}' accesskey='s'>next article</a> |\n\t\t\t\t\t<a href='{$url}&dosearch=1' accesskey='s'>" . wfMsg('importvideo_searchforarticle') . "</a> {$cancel} )\n\t\t\t\t</span>");
         if ($wgRequest->getVal('category')) {
             $wgOut->addHTML("You are adding videos to articles from the \"{$wgRequest->getVal('category')}\" category.\n\t\t\t\t\t(<a href='#'>change</a>)");
         }
         $wgOut->addHTML("</div>");
         $wgOut->addHTML("<div class='video_related wh_block'>\n\t\t\t\t\t<h2>Introduction</h2>\n\t\t\t\t\t{$extra}\n\t\t\t\t\t<br clear='all'/>\n\t\t\t\t\t<div id='showhide' style='font-size: 80%; text-align:right;'>\n\t\t\t\t\t\t<span id='showsteps'><a href='#' onclick='javascript:showhidesteps(); return false;'>" . wfMsg('importvideo_showsteps') . "</a></span>\n\t\t\t\t\t\t<span id='hidesteps' style='display: none;'><a href='#' onclick='javascript:showhidesteps(); return false;'>" . wfMsg('importvideo_hidesteps') . "</a></span>\n\t\t\t\t\t</div>\n\t\t\t\t\t<div id='stepsarea' style='display: none;'>\n\t\t\t\t\t{$steps}\n\t\t\t\t\t</div>\n\t\t\t\t\t<br clear='all'/>\n\t\t\t\t</div>\n\t\t\t");
     }
     $wgOut->addHTML("<script type='text/javascript' src='" . wfGetPad('/extensions/min/f/extensions/wikihow/video/importvideo.js?rev=') . WH_SITEREV . "'> </script>\t");
     $wgOut->addHTML("<link rel='stylesheet' type='text/css' href='" . wfGetPad('/extensions/min/f/extensions/wikihow/video/importvideo.css?rev=') . WH_SITEREV . "' />");
     $wgOut->addHTML("<script type='text/javascript'>\n\t\t\tvar isPopUp = " . ($wgRequest->getVal('popup') ? "true" : "false") . ";\n\t\t\t</script>");
     if (!$wgRequest->wasPosted()) {
         $wgOut->addHTML(wfMsgWikiHtml('add_video_info'));
         # HEADER for import page
         $url = $me->getFullURL() . "?target=" . urlencode($target) . "&q=" . urlencode($query) . $this->getURLExtras() . "&source=";
         // refine form
         $orderby = $wgRequest->getVal('orderby', 'relevance');
         $wgOut->addHTML($this->refineForm($me, $target, $wgRequest->getVal('popup') == 'true', $query, $orderby));
         // sources tab
         $wgOut->addHTML("<ul id='importvideo_search_tabs'>");
         foreach ($wgImportVideoSources as $s) {
             $selected = $s == $source ? ' class="iv_selected"' : '';
             $wgOut->addHTML("<li{$selected}><a href='{$url}{$s}'>" . wfMsg('importvideo_source_' . $s) . "</a></li>");
         }
         $wgOut->addHTML("</ul>");
         $vt = Title::makeTitle(NS_VIDEO, $target);
         if ($vt->getArticleID() > 0 && $wgRequest->getVal('popup') != 'true') {
             $wgOut->addHTML("<div class='wh_block importvideo_main'>" . wfMsgExt('importvideo_videoexists', 'parse', $vt->getFullText()) . "</div>");
         }
     }
     //special class just for pop-ups
     if ($wgRequest->getVal('popup')) {
         $pop_class = 'importvideo_pop';
     }
     $wgOut->addHTML("<div class='wh_block importvideo_main {$pop_class}'>");
     $sp->execute($par);
     $wgOut->addHTML("</div>");
     //Bebeth: took out extra closing div
     $wgOut->addHTML("</div>");
     //Scott: put a brand new extra closing div in (take that, Bebeth!)
 }
 function execute($par)
 {
     global $wgOut, $wgRequest, $wgUser, $wgParser;
     if ($wgUser->isBlocked()) {
         $wgOut->blockedPage();
         return;
     }
     if ($wgUser->getID() == 0) {
         $wgOut->setRobotpolicy('noindex,nofollow');
         $wgOut->showErrorPage('nosuchspecialpage', 'nospecialpagetext');
         return;
     }
     if (class_exists('WikihowCSSDisplay')) {
         WikihowCSSDisplay::setSpecialBackground(true);
     }
     if ($wgRequest->getVal('fetchReviewersTable')) {
         $wgOut->setArticleBodyOnly(true);
         echo $this->getReviewersTable();
         return;
     }
     wfLoadExtensionMessages('Importvideo');
     wfLoadExtensionMessages("Videoadder");
     $target = isset($par) ? $par : $wgRequest->getVal('target');
     $cat = htmlspecialchars($wgRequest->getVal('cat'));
     // get just the HTML for the Ajax call for the next video
     // used even on the initial page load
     if ($target == 'getnext') {
         $wgOut->setArticleBodyOnly(true);
         // process any skipped videos
         if ($wgRequest->getVal('va_page_id') && !preg_match("@[^0-9]@", $wgRequest->getVal('va_page_id'))) {
             $dbw = wfGetDB(DB_MASTER);
             $vals = array('va_vid_id' => $wgRequest->getVal('va_vid_id'), 'va_user' => $wgUser->getID(), 'va_user_text' => $wgUser->getName(), 'va_timestamp' => wfTimestampNow(), 'va_inuse' => null, 'va_src' => 'youtube');
             $va_skip = $wgRequest->getVal('va_skip');
             if ($va_skip < 2) {
                 $vals['va_skipped_accepted'] = $va_skip;
             }
             $dbw->update('videoadder', $vals, array('va_page' => $wgRequest->getVal('va_page_id')));
             if ($wgRequest->getVal('va_skip') == 0) {
                 // import the video
                 $tx = Title::newFromID($wgRequest->getVal('va_page_id'));
                 $ipv = new ImportvideoYoutube();
                 $text = $ipv->loadVideoText($wgRequest->getVal('va_vid_id'));
                 $vid = Title::makeTitle(NS_VIDEO, $tx->getText());
                 Importvideo::updateVideoArticle($vid, $text, wfMessage('va_addingvideo')->text());
                 Importvideo::updateMainArticle($tx, wfMessage('va_addingvideo')->text());
                 wfRunHooks("VAdone", array());
                 $wgOut->redirect('');
             } else {
                 if ($wgRequest->getVal('va_skip') == 2) {
                     // the user has skipped it and not rejected this one, don't show it to them again
                     self::skipArticle($wgRequest->getVal('va_page_id'));
                     wfRunHooks("VAskipped", array());
                 }
             }
         }
         $results = self::getNext();
         if (empty($results)) {
             $wgOut->addHTML("Something went wrong. Refresh.");
             return;
         }
         $title = $results[0];
         $vid = $results[1];
         $id = str_replace("http://gdata.youtube.com/feeds/api/videos/", "", $vid['ID']);
         if (!$title) {
             $wgOut->addHTML("Something went wrong. Refresh.");
             return;
         }
         $revision = Revision::newFromTitle($title);
         $popts = $wgOut->parserOptions();
         $popts->setTidy(true);
         $parserOutput = $wgOut->parse($revision->getText(), $title, $popts);
         $magic = WikihowArticleHTML::grabTheMagic($revision->getText());
         $html = WikihowArticleHTML::processArticleHTML($parserOutput, array('no-ads' => true, 'ns' => NS_MAIN, 'magic-word' => $magic));
         $result['guts'] = "<div id='va_buttons'><a href='#' onclick='va_skip(); return false;' class='button secondary ' style='float:left;'>Skip</a>\n<a id='va_yes' href='#' class='button primary disabled buttonright'>Yes, it does</a>\n<a id='va_no' href='#' class='button secondary buttonright'>No, it doesn't</a><div class='clearall'></div></div>\n<div id='va_title'><a href='{$title->getFullURL()}' target='new'>" . wfMsg("howto", $title->getText()) . "</a></div>\n\t\t\t\t\t<div id='va_video' class='section_text'>\n\t<p id='va_notice'>" . wfMsg('va_notice') . "</p>\n<object width='480' height='385'><param name='movie' value='http://www.youtube.com/v/{$id}&amp;hl=en_US&amp;fs=1'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/{$id}&amp;hl=en_US&amp;fs=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='480' height='385'></embed></object>\n\t\t\t\t\t<input type='hidden' id='va_vid_id' value='{$id}'/>\n\t\t\t\t\t<input type='hidden' id='va_page_id' value='{$title->getArticleID()}'/>\n\t\t\t\t\t<input type='hidden' id='va_page_title' value='" . htmlspecialchars($title->getText()) . "'/>\n\t\t\t\t\t<input type='hidden' id='va_page_url' value='" . htmlspecialchars($title->getFullURL()) . "'/>\n\t\t\t\t\t<input type='hidden' id='va_skip' value='0'/>\n\t\t\t\t\t<input type='hidden' id='va_src' value='youtube'/>\n\t\t\t\t\t</div>\n\n\t\t\t</div>\n\t\t\t";
         $result['article'] = $html;
         $dropdown = wfMsg('va_browsemsg') . " " . self::getCategoryDropDown();
         $result['options'] = "<div id='va_browsecat'>" . $dropdown . "</div>";
         $result['cat'] = $cat;
         print_r(json_encode($result));
         return;
     }
     // add the layer of the page
     $this->setHeaders();
     $this->setSideWidgets();
     $wgOut->addJScode('vcooj');
     $wgOut->addJScode('vaddj');
     $wgOut->addCSScode('vaddc');
     $wgOut->addHTML("<div class='tool_header tool'><h1>" . wfMessage('va_question') . "<span id='va_instructions'>" . wfMessage('va_instructions') . "</span></h1>");
     $wgOut->addHTML("<div id='va_guts'>\n\t\t\t\t\t<center><img src='/extensions/wikihow/rotate.gif'/></center>\n\t\t\t\t</div>");
     $wgOut->addHTML("</div>");
     $wgOut->addHTML("<input type='hidden' id='va_cat' value='{$cat}' />");
     $wgOut->addHTML("<div id='va_article'></div>");
     $langKeys = array('va_congrats', 'va_check');
     $js = Wikihow_i18n::genJSMsgs($langKeys);
     $wgOut->addHTML($js);
     return;
 }