Пример #1
0
 /**
  * add video
  * @requestParam string url
  * @responseParam string html
  * @responseParam string error - error message
  */
 public function addVideo()
 {
     if (!$this->wg->User->isLoggedIn()) {
         $this->error = wfMsg('videos-error-not-logged-in');
         return;
     }
     $url = urldecode($this->getVal('url', ''));
     if (empty($url)) {
         $this->error = wfMsg('videos-error-no-video-url');
         return;
     }
     if ($this->wg->User->isBlocked()) {
         $this->error = wfMsg('videos-error-blocked-user');
         return;
     }
     $videoService = new VideoService();
     $retval = $videoService->addVideo($url);
     if (!is_array($retval)) {
         $this->error = $retval;
     } else {
         $this->videoInfo = $retval;
     }
 }
Пример #2
0
 public function doSubmit()
 {
     global $wgOut;
     $errors = array();
     $replaced = false;
     $this->mUrl = $this->getRequest()->getVal('wpWikiaVideoAddUrl', '');
     $this->mName = $this->getRequest()->getVal('wpWikiaVideoAddName', '');
     if ($this->mName == '') {
         $this->mName = $this->getRequest()->getVal('name', '');
         if ($this->mName != '') {
             $replaced = true;
         }
     }
     if ($this->mUrl == '') {
         $errors['videoUrl'] = wfMessage('wva-failure')->text();
         $this->showForm($errors);
         return;
     } else {
         if ($this->mName == '') {
             $videoService = new VideoService();
             $retval = $videoService->addVideo($this->mUrl);
             if (is_array($retval)) {
                 list($title, $videoPageId, $videoProvider) = $retval;
             } else {
                 $errors['videoUrl'] = $retval;
                 $this->showForm($errors);
                 return;
             }
         } else {
             $this->mName = ucfirst($this->mName);
             // sanitize all video titles
             $this->mName = VideoFileUploader::sanitizeTitle($this->mName);
             $title = Title::makeTitleSafe(NS_FILE, $this->mName);
             if ($title instanceof Title) {
                 $permErrors = $title->getUserPermissionsErrors('edit', $this->getUser());
                 $permErrorsUpload = $title->getUserPermissionsErrors('upload', $this->getUser());
                 $permErrorsCreate = $title->exists() ? array() : $title->getUserPermissionsErrors('create', $this->getUser());
                 if ($permErrors || $permErrorsUpload || $permErrorsCreate) {
                     header('X-screen-type: error');
                     $wgOut->addWikiMsg('wva-protected');
                     return;
                 }
                 if (empty(F::app()->wg->allowNonPremiumVideos)) {
                     $errors['videoUrl'] = wfMessage('videohandler-non-premium')->parse();
                     $this->showForm($errors);
                     return;
                 }
                 if (WikiaFileHelper::useVideoHandlersExtForEmbed()) {
                     $res = null;
                     try {
                         $res = VideoFileUploader::URLtoTitle($this->mUrl, $this->mName);
                     } catch (Exception $e) {
                     }
                     if (!$res) {
                         $errors['videoUrl'] = wfMessage('wva-failure')->text();
                         $this->showForm($errors);
                         return;
                     }
                 }
             } else {
                 //bad title returned
                 $errors['name'] = wfMessage('wva-failure')->text();
                 $this->showForm($errors);
                 return;
             }
         }
     }
     if ($replaced) {
         $successMsgKey = 'wva-success-replaced';
     } else {
         $successMsgKey = 'wva-success';
     }
     $query = array("sort" => "recent", "msg" => $successMsgKey, "msgTitle" => urlencode($title));
     $wgOut->redirect(SpecialPage::getTitleFor("Videos")->getLocalUrl($query));
 }
Пример #3
0
function upgradeYouTubeTag($editpage, $request)
{
    $app = F::app();
    // Don't convert <youtube> tags if the user is not logged in or is blocked.
    // It provides a loophole for those users to upload video.
    if (!$app->wg->User->isLoggedIn()) {
        return true;
    }
    if ($app->wg->User->isBlocked()) {
        return true;
    }
    if (!$app->wg->User->isAllowed('videoupload')) {
        return true;
    }
    $text = $editpage->textbox1;
    // Note that we match <nowiki> here to consume that text and any possible
    // <youtube> tags within it.  We don't want to convert anything within <nowiki>
    $text = preg_replace_callback('/(<nowiki>.*?<\\/nowiki>)|(<youtube([^>]*)>([^<]+)<\\/youtube>)/i', function ($matches) {
        // If we don't have a youtube match (its a nowiki tag) return as is
        if (empty($matches[2])) {
            return $matches[0];
        }
        // Separate the Youtube ID and parameters
        $paramText = trim($matches[3]);
        // Node value can look like: <youtube_id>|400px|thumb|center
        // @TODO evaluate calling parser to parse params and upload correctly styled video
        $nodeValues = explode('|', $matches[4]);
        $ytid = trim($nodeValues[0]);
        // Check to see if the whole URL is used
        if (preg_match('/(?:youtube\\.com\\/watch\\?(?:[^&]*&)*v=|youtu\\.be\\/)([^?&\\n]+)/', $ytid, $ytidMatches) === 1) {
            $ytid = $ytidMatches[1];
        }
        // Parse out the width and height parameters
        $params = parseSizeParams($paramText);
        // If height is less than 30, they probably are using this as an audio file
        // so don't bother converting it.
        if ($params['height'] <= AUDIO_ONLY_HEIGHT) {
            return $matches[0];
        }
        $url = 'http://www.youtube.com/watch?v=' . $ytid;
        $videoService = new VideoService();
        $videoFileUploader = new VideoFileUploader();
        $videoFileUploader->setExternalUrl($url);
        $apiWrapper = $videoFileUploader->getApiWrapper();
        if (!$apiWrapper->videoExists()) {
            return createRawOutput($matches[0]);
        }
        $retval = $videoService->addVideo($url);
        if (is_array($retval)) {
            list($title, $videoPageId, $videoProvider) = $retval;
            return "[[{$title}|" . $params['width'] . "px]]";
        } else {
            return $matches[0];
        }
    }, $text);
    $editpage->textbox1 = $text;
    return true;
}
Пример #4
0
 function insertVideo()
 {
     global $wgRequest, $wgUser;
     wfProfileIn(__METHOD__);
     if ($wgUser->isBlocked()) {
         header('X-screen-type: error');
         wfProfileOut(__METHOD__);
         return wfMessage('videos-error-blocked-user')->plain();
     }
     if (!$wgUser->isAllowed('videoupload')) {
         header('X-screen-type: error');
         wfProfileOut(__METHOD__);
         return wfMessage('videos-error-admin-only')->plain();
     }
     $url = $wgRequest->getVal('url');
     $tempname = 'Temp_video_' . $wgUser->getID() . '_' . rand(0, 1000);
     $title = Title::makeTitle(NS_FILE, $tempname);
     $nonPremiumException = null;
     try {
         $awf = ApiWrapperFactory::getInstance();
         /* @var $awf ApiWrapperFactory */
         $apiwrapper = $awf->getApiWrapper($url);
     } catch (Exception $e) {
         $nonPremiumException = $e;
     }
     $embedOptions = ['autoplay' => false, 'isAjax' => false];
     if (!empty($apiwrapper)) {
         // try ApiWrapper first - is it from a supported 3rd party ( non-premium ) provider?
         $provider = $apiwrapper->getMimeType();
         $file = new WikiaLocalFile($title, RepoGroup::singleton()->getLocalRepo());
         $file->forceMime($provider);
         $file->setVideoId($apiwrapper->getVideoId());
         $file->setProps(array('mime' => $provider));
         // Loading this to deal with video descriptions
         $vHelper = new VideoHandlerHelper();
         $props['id'] = $apiwrapper->getVideoId();
         $props['vname'] = $apiwrapper->getTitle();
         $props['metadata'] = '';
         $props['description'] = $vHelper->getVideoDescription($file);
         $props['provider'] = $provider;
         $embed_code = $file->getEmbedCode(VIDEO_PREVIEW, $embedOptions);
         $props['code'] = json_encode($embed_code);
     } else {
         // if not a supported 3rd party ( non-premium ) video, try to parse link for File:
         // get the video file
         $videoService = new VideoService();
         $file = $videoService->getVideoFileByUrl($url);
         if (!$file) {
             header('X-screen-type: error');
             if ($nonPremiumException) {
                 if (empty(F::app()->wg->allowNonPremiumVideos)) {
                     wfProfileOut(__METHOD__);
                     return wfMessage('videohandler-non-premium')->parse();
                 }
                 if ($nonPremiumException->getMessage() != '') {
                     wfProfileOut(__METHOD__);
                     return $nonPremiumException->getMessage();
                 }
             }
             wfProfileOut(__METHOD__);
             return wfMessage('vet-bad-url')->plain();
         }
         // Loading this to deal with video descriptions
         $vHelper = new VideoHandlerHelper();
         $embedCode = $file->getEmbedCode(VIDEO_PREVIEW, $embedOptions);
         $props['provider'] = 'FILE';
         $props['id'] = $file->getHandler()->getVideoId();
         $props['vname'] = $file->getTitle()->getText();
         $props['code'] = json_encode($embedCode);
         $props['metadata'] = '';
         $props['description'] = $vHelper->getVideoDescription($file);
         $props['premiumVideo'] = !$file->isLocal();
     }
     wfProfileOut(__METHOD__);
     return $this->detailsPage($props);
 }
Пример #5
0
 /**
  * Convert the youtube tag to a [[File:...]] wiki tag
  *
  * @param Article $page - The page on which the tag exists
  * @param $ytid - The Youtube tag ID
  * @param $userText - The user who made the edit
  * @return bool - Whether this upgrade was successful
  */
 public function upgradeTag(Article $page, $ytid, $userText)
 {
     global $wgUser;
     global $wgTestMode, $wgCityId, $wgCurPageID, $wgChangeMade;
     /*
     		// Load the user who embedded this video
     		$wgUser = User::newFromName( $userText );
     
     		// Fall back to user WikiaBot if we can't find this user
     		if ( !$wgUser ) {
     			$wgUser = User::newFromName( 'WikiaBot' );
     		}
     
     		// If we still can't load the user, bail here
     		if ( !$wgUser ) {
     			echo "WARN: Could not load user $userText\n";
     			return false;
     		}
     		if ( $wgUser->isAnon() ) {
     			$wgUser->addToDatabase();
     		}
     */
     $wgChangeMade = 0;
     $text = $page->getText();
     $text = preg_replace_callback("/(<nowiki>.*?<\\/nowiki>)|<youtube([^>]*)>(" . preg_quote($ytid, '/') . ")<\\/youtube>/i", function ($matches) {
         global $wgTestMode, $wgCityId, $wgCurPageID, $wgChangeMade;
         // Some limits and defaults
         $width_max = 640;
         $height_max = 385;
         $width_def = 425;
         $height_def = 355;
         // If this matched a <nowiki> tag (and thus no param or ytid, don't do anything
         if (empty($matches[3])) {
             return $matches[0];
         }
         // Separate the Youtube ID and parameters
         $paramText = trim($matches[2]);
         $ytid = $matches[3];
         // Parse out the width and height parameters
         $params = array();
         if (preg_match_all('/(width|height)\\s*=\\s*["\']?([0-9]+)["\']?/', $paramText, $paramMatches)) {
             $paramKeys = $paramMatches[1];
             $paramVals = $paramMatches[2];
             foreach ($paramKeys as $key) {
                 $params[$key] = array_shift($paramVals);
             }
         }
         // Fill in a default value if none was given
         if (empty($params['height'])) {
             $params['height'] = $height_def;
         }
         if (empty($params['width'])) {
             $params['width'] = $width_def;
         }
         // Constrain the max height and width
         if ($params['height'] > $height_max) {
             $params['height'] = $height_max;
         }
         if ($params['width'] > $width_max) {
             $params['width'] = $width_max;
         }
         // If height is less than 30px they probably want this for audio.  Don't convert
         if ($params['height'] < 30) {
             echo "\t[Skip:{$wgCityId},{$wgCurPageID}] Ignoring tag meant for audio (height = " . $params['height'] . ")\n";
             return $matches[0];
         }
         if (preg_match('/^http:\\/\\//', $ytid)) {
             $url = trim($ytid);
         } else {
             $url = 'http://www.youtube.com/watch?v=' . trim($ytid);
         }
         $videoService = new VideoService();
         if ($wgTestMode) {
             echo "\t[TEST] Replacing: " . $matches[0] . "\n" . "\t            with: {$url}\n";
             return $matches[0];
         } else {
             try {
                 $retval = $videoService->addVideo($url);
             } catch (Exception $e) {
                 echo "\t[Skip:{$wgCityId},{$wgCurPageID}] Call to addVideo failed: " . $e->getMessage() . "\n";
                 return $matches[0];
             }
         }
         if (is_array($retval)) {
             list($title, $videoPageId, $videoProvider) = $retval;
             $wgChangeMade++;
             return "[[{$title}|" . $params['width'] . "px]]";
         } else {
             echo "\t[Skip:{$wgCityId},{$wgCurPageID}] Unable to upload video\n";
             return $matches[0];
         }
     }, $text);
     /*
     		// Load wikia user
     		$wgUser = User::newFromName( 'WikiaBot' );
     		if ( !$wgUser ) {
     			echo "WARN: Could not load WikiaBot user\n";
     			return false;
     		}
     */
     if ($wgTestMode) {
         return true;
     }
     if ($wgChangeMade == 0) {
         echo "\tNo changes made, skipping edit\n";
         return true;
     }
     try {
         # Do the edit
         $status = $page->doEdit($text, 'Automatically converting <youtube> tags and uploading video', EDIT_MINOR | EDIT_FORCE_BOT | EDIT_AUTOSUMMARY | EDIT_SUPPRESS_RC);
     } catch (Exception $e) {
         echo "\t[Skip:{$wgCityId},{$wgCurPageID}] Edit failed: " . $e->getMessage() . "\n";
         return false;
     }
     $retval = true;
     if (!$status->isGood()) {
         echo "\t[Skip:{$wgCityId},{$wgCurPageID}] Edit is not good: " . $status->getWikiText() . "\n";
         $retval = false;
     }
     return $retval;
 }