コード例 #1
0
	static function processVideo( $raw_video_name ) {
		global $wgExternalDatawareDB, $wgCityId, $providers;
		$dbw_dataware = wfGetDB( DB_MASTER, array(), $wgExternalDatawareDB );

		$status			= STATUS_UNKNOWN;
		$statusMsg		= '';
		$videoName		= substr($raw_video_name, 1);
		$isNameTaken	= 0;
		$meta			= '';
		$fullResponse	= '';
		$provider		= false;
		$thumbnail		= '';
		$apiUrl			= '';
		$videoOrgId		= '';
		$bl				= 0;


		// start processing video
		$titleOrg = Title::newFromText( $videoName, NS_VIDEO );
		$titleNew = Title::newFromText( $videoName, NS_FILE );

		if($titleNew && $titleNew->exists() ) {
			echo ("[CONFLICT] Article in NS_FILE namespace already exists\n");
			$isNameTaken = 1;
		}
		$videoOrg = new VideoPage( $titleOrg );
		$videoOrg->load();
		$videoOrgId = $videoOrg->getVideoId();
		$videoOrgProviderId = $videoOrg->getProvider();

		// ugly fix for ugly MW logic
		if ( empty( $videoOrgId ) || empty( $videoOrgProviderId ) ) {

			$titleOrg = Title::newFromText( 'Video:'.$videoName );
			$titleNew = Title::newFromText( 'File:'.$videoName );

			if($titleNew && $titleNew->exists() ) {
				echo ("[CONFLICT] Article in NS_FILE namespace already exists\n");
				$isNameTaken = 1;
			}
			$videoOrg = new VideoPage( $titleOrg );
			$videoOrg->load();
			$videoOrgId = $videoOrg->getVideoId();
			$videoOrgProviderId = $videoOrg->getProvider();
		}

		if(isset($backlinks[$raw_video_name])) {
			$bl = $backlinks[$raw_video_name];
		}

		if( !isset( $providers[ $videoOrgProviderId ] ) ) {
			echo ("Provider: unsupported ($videoOrgProviderId)\n");
			$status = STATUS_NOT_SUPPORTED;
		} else {
			$provider = $providers[ $videoOrgProviderId ];
			echo ("Provider: $provider\n");

			$retries = 0;
			$retry = true;
			while($retries < 20 && $retry) {
				try {
					$className = ucfirst( $provider ) . 'ApiWrapper';
					if(is_subclass_of($className, 'PseudoApiWrapper')) {
						$apiWrapper = F::build( $className, array( $videoName ) );
					} else {
						$apiWrapper = F::build( $className, array( $videoOrgId ) );
					}
					$meta = $apiWrapper->getMetadata();
					if( $retries == 0 ) {
						echo ("Got data\n");
					} else {
						echo ("Got data after $retries failed tries\n");
					}
					$status = STATUS_OK;
					$thumbnail = $apiWrapper->getThumbnailUrl();
					break;
				}

				catch( VideoNotFoundException $e ) {
					echo ("[ERROR] video not found\n");
					$retry = false;
					$status = STATUS_KNOWN_ERROR;
					$statusMsg = 'NOT_FOUND';
					if(!is_subclass_of($className, 'PseudoApiWrapper')) {
						// if a video failed that used regular provider
						// just get fake metadata (from old entry)
						$apiWrapper = F::build( 'FakeApiWrapper', array( $videoName ) );
						$meta = $apiWrapper->getMetadata();
					}
					break;
				}
				catch( VideoIsPrivateException $e ) {
					echo ("[ERROR] video is private\n");
					$retry = false;
					$status = STATUS_KNOWN_ERROR;
					$statusMsg = 'PRIVATE';
					if(!is_subclass_of($className, 'PseudoApiWrapper')) {
						// if a video failed that used regular provider
						// just get fake metadata (from old entry)
						$apiWrapper = F::build( 'FakeApiWrapper', array( $videoName ) );
						$meta = $apiWrapper->getMetadata();
					}
					break;
				}
				catch( VideoQuotaExceededException $e ) {
					echo ("[ERROR] quota exceeded, will retry\n");
					$retries += 1;
					sleep(2);
					continue;
				}
				catch( NegativeResponseException $e ) {
					echo ("[ERROR] negative response\n");
					$retry = false;
					$status = STATUS_UNKNOWN_ERROR;
					$fullResponse = $e->content;
					if(!is_subclass_of($className, 'PseudoApiWrapper')) {
						// if a video failed that used regular provider
						// just get fake metadata (from old entry)
						$apiWrapper = F::build( 'FakeApiWrapper', array( $videoName ) );
						$meta = $apiWrapper->getMetadata();
					}
					break;
				}
				catch( Exception $e ) {
					echo ("[ERROR] Unknown error: ". $e->getMessage() . "\n");
					// unknown error
					$retry = false;
					$status = STATUS_UNKNOWN_ERROR;
					$fullResponse = $e->getMessage();
					if( isset($e->apiUrl) ) {
						$apiUrl = $e->apiUrl;
					}
					if(!is_subclass_of($className, 'PseudoApiWrapper')) {
						// if a video failed that used regular provider
						// just get fake metadata (from old entry)
						$apiWrapper = F::build( 'FakeApiWrapper', array( $videoName ) );
						$meta = $apiWrapper->getMetadata();
					}
					break;
				}
			}
			if( $retries == 20 ) {
				echo ("[ERROR] unable to fetch metadata\n");
				$status = STATUS_UNKNOWN_ERROR;
				$statusMsg = 'TOO_MANY_RETRIES';
			}
		}



		$dbprovider = $provider ? $provider : $videoOrgProviderId;
		if(empty($dbprovider)) {
			$dbprovider = 'EMPTY';
		}

		$video = array(
			'img_name'		=> $videoName,
			'wiki_id'		=> $wgCityId,
			'provider'		=> $dbprovider,
			'new_metadata'	=> serialize($meta),
			'is_name_taken'	=> $isNameTaken,
			'status'		=> $status,
			'status_msg'	=> $statusMsg,
			'full_response'	=> serialize($fullResponse),
			'thumbnail_url'	=> $thumbnail,
			'api_url'		=> $apiUrl,
			'video_id'		=> $videoOrgId,
			'backlinks'		=> $bl,
		);

		if(isset(static::$previouslyProcessed[$videoName])) {
			// update

			// if status was unsupported and still is
			// no need to update
			if( static::$previouslyProcessed[$videoName]['status'] == STATUS_NOT_SUPPORTED && $status == STATUS_NOT_SUPPORTED ) {
				return (object)$video;
			}

			$dbw_dataware->update('video_premigrate',
				array(
					'provider' 		=> $dbprovider,
					'new_metadata'	=> serialize($meta),
					'is_name_taken'	=> $isNameTaken,
					'status'		=> $status,
					'status_msg'	=> $statusMsg,
					'full_response'	=> serialize($fullResponse),
					'thumbnail_url'	=> $thumbnail,
					'api_url'		=> $apiUrl,
					'video_id'		=> $videoOrgId,
					'backlinks'		=> $bl,
				),
				array(
					'img_name'		=> $videoName,
					'wiki_id'		=> $wgCityId,
				)
			);
		} else {
			// insert
			$dbw_dataware->insert('video_premigrate', $video );
		}

		return (object)$video;
	}