示例#1
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;
}