Example #1
0
 public function getDescription($bParse = true)
 {
     /** @var $title Title */
     $title = $this->getTitle();
     $memcKey = wfmemcKey(__METHOD__, $title->getArticleID(), $title->getTouchedCached(), 'parsed');
     $res = $this->wg->memc->get($memcKey);
     if (!is_string($res)) {
         if (!$bParse) {
             $oArticle = new Article($title);
             return $oArticle->getText();
         }
         $res = $this->getDescriptionParsed(false);
         $this->wg->memc->set($memcKey, $res, self::DESCRIPTION_CACHE_TTL);
     }
     return $res;
 }
Example #2
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;
 }