public static function uploadNews($image, $name, $url)
 {
     global $wgSitename;
     $is = new ImageServing(array(), 90);
     $thumb_url = $is->getThumbnails(array($image));
     $thumb_url = array_pop($thumb_url);
     $thumb_url = $thumb_url['url'];
     $params = array('$IMGNAME' => $name, '$ARTICLE_URL' => $url, '$WIKINAME' => $wgSitename, '$IMG_URL' => $url, '$EVENTIMG' => $thumb_url);
     self::pushEvent(self::$messageName, $params, __CLASS__);
 }
 /**
  * @author Federico "Lox" Lucignano <*****@*****.**>
  *
  * Implementation of a parser function
  */
 public static function parseTag($input, $args, $parser)
 {
     $relatedTitle = null;
     $relatedImage = null;
     $relatedUrl = null;
     if (!empty($args[TOPLIST_ATTRIBUTE_RELATED])) {
         self::$mAttributes[TOPLIST_ATTRIBUTE_RELATED] = $args[TOPLIST_ATTRIBUTE_RELATED];
         $relatedTitle = Title::newFromText($args[TOPLIST_ATTRIBUTE_RELATED]);
         $relatedUrl = $relatedTitle->getLocalUrl();
     }
     if (!empty($args[TOPLIST_ATTRIBUTE_PICTURE])) {
         self::$mAttributes[TOPLIST_ATTRIBUTE_PICTURE] = $args[TOPLIST_ATTRIBUTE_PICTURE];
         if (!empty(self::$mAttributes[TOPLIST_ATTRIBUTE_PICTURE])) {
             $source = new ImageServing(null, 200);
             $result = $source->getThumbnails(array(self::$mAttributes[TOPLIST_ATTRIBUTE_PICTURE]));
             if (!empty($result[self::$mAttributes[TOPLIST_ATTRIBUTE_PICTURE]])) {
                 $relatedImage = $result[self::$mAttributes[TOPLIST_ATTRIBUTE_PICTURE]];
                 if (empty($relatedUrl)) {
                     $title = Title::newFromText($relatedImage['name'], NS_FILE);
                     $relatedUrl = $title->getLocalURL();
                 }
             }
         }
     }
     self::$mAttributes[TOPLIST_ATTRIBUTE_DESCRIPTION] = '';
     if (!empty($args[TOPLIST_ATTRIBUTE_DESCRIPTION])) {
         self::$mAttributes[TOPLIST_ATTRIBUTE_DESCRIPTION] = $args[TOPLIST_ATTRIBUTE_DESCRIPTION];
     }
     if (!empty(self::$mList)) {
         $list = self::$mList;
         self::$mList = null;
     } else {
         $list = TopList::newFromTitle($parser->mTitle);
     }
     if (!empty($list)) {
         $template = new EasyTemplate(dirname(__FILE__) . "/templates/");
         if ($relatedTitle instanceof Title) {
             $relatedTitleData = array('localURL' => $relatedTitle->getLocalURL(), 'text' => $relatedTitle->getText());
         } else {
             $relatedTitleData = null;
         }
         $template->set_vars(array('list' => $list, 'listTitle' => $list->getTitle()->getText(), 'relatedTitleData' => $relatedTitleData, 'relatedImage' => $relatedImage, 'attribs' => self::$mAttributes, 'relatedUrl' => $relatedUrl, 'description' => self::$mAttributes[TOPLIST_ATTRIBUTE_DESCRIPTION]));
         self::$mOutput = $template->render('list');
         // remove whitespaces to avoid extra <p> tags
         self::$mOutput = preg_replace("#[\n\t]+#", '', self::$mOutput);
     } else {
         self::$mOutput = '';
     }
     return self::$mOutput;
 }
 /**
  * @author Federico "Lox" Lucignano
  *
  * List editor utility function
  */
 public static function renderImageBrowser()
 {
     global $wgRequest;
     $titleText = $wgRequest->getText('title');
     $selectedImageTitle = $wgRequest->getText('selected');
     $images = array();
     $selectedImage = null;
     if (!empty($titleText)) {
         $title = Title::newFromText($titleText);
         if (!empty($title) && $title->exists()) {
             $articleId = $title->getArticleId();
             $source = new ImageServing(array($articleId), 120, array("w" => 3, "h" => 2));
             $result = $source->getImages(7);
             if (!empty($result[$articleId])) {
                 $images = $result[$articleId];
             }
         }
     }
     if (!empty($selectedImageTitle)) {
         $source = new ImageServing(null, 120, array("w" => 3, "h" => 2));
         $thumbs = $source->getThumbnails(array($selectedImageTitle));
         if (!empty($thumbs[$selectedImageTitle])) {
             $selectedImage = $thumbs[$selectedImageTitle];
         }
     }
     $tpl = new EasyTemplate(dirname(__FILE__) . "/templates/");
     $tpl->set_vars(array('selectedImage' => $selectedImage, 'images' => $images));
     $text = $tpl->render('image_browser');
     $response = new AjaxResponse($text);
     $response->setContentType('text/plain; charset=utf-8');
     return $response;
 }
 function execute($editListName)
 {
     global $wgExtensionsPath, $wgJsMimeType, $wgSupressPageSubtitle, $wgRequest, $wgOut, $wgUser;
     wfProfileIn(__METHOD__);
     // set basic headers
     $this->setHeaders();
     if (wfReadOnly()) {
         $wgOut->readOnlyPage();
         wfProfileOut(__METHOD__);
         return;
     }
     //Check blocks
     if ($wgUser->isBlocked()) {
         wfProfileOut(__METHOD__);
         throw new UserBlockedError($wgUser->getBlock());
     }
     if (!F::app()->checkSkin('oasis')) {
         $this->getOutput()->showErrorPage('error', 'toplists-oasis-only');
         wfProfileOut(__METHOD__);
         return;
     }
     if (!$this->userCanExecute($wgUser)) {
         $this->displayRestrictionError();
         wfProfileOut(__METHOD__);
         return;
     }
     if (empty($editListName)) {
         $this->_redirectToCreateSP();
     }
     // include resources (css and js)
     $wgOut->addStyle(AssetsManager::getInstance()->getSassCommonURL('/extensions/wikia/TopLists/css/editor.scss'));
     $wgOut->addScript("<script type=\"{$wgJsMimeType}\" src=\"{$wgExtensionsPath}/wikia/TopLists/js/editor.js\"></script>\n");
     //hide specialpage subtitle in Oasis
     $wgSupressPageSubtitle = true;
     $errors = array();
     $listName = null;
     $listUrl = null;
     $relatedArticleName = null;
     $description = null;
     $selectedPictureName = null;
     $items = array();
     $removedItems = array();
     $list = TopList::newFromText($editListName);
     /** @var $list TopList */
     if (empty($list) || !$list->exists()) {
         $this->_redirectToCreateSP($editListName);
     } else {
         $title = $list->getTitle();
         $listName = $title->getText();
         $description = $list->getDescription();
         $listUrl = $title->getFullURL();
         $listItems = $list->getItems();
         $userCanEditItems = $list->checkUserItemsRight('edit');
         $userCanDeleteItems = $list->checkUserItemsRight('delete');
         if ($wgRequest->wasPosted()) {
             TopListHelper::clearSessionItemsErrors();
             $relatedArticleName = $wgRequest->getText('related_article_name');
             $selectedDescription = $wgRequest->getText('description');
             $selectedPictureName = $wgRequest->getText('selected_picture_name');
             $itemsNames = $wgRequest->getArray('items_names', array());
             $removedItems = $userCanDeleteItems ? $wgRequest->getArray('removed_items', array()) : array();
             //handle related article
             $title = $list->getRelatedArticle();
             $curValue = null;
             if (!empty($title)) {
                 $curValue = $title->getText();
             }
             $relatedArticleChanged = $curValue != $relatedArticleName;
             if ($relatedArticleChanged) {
                 if (!empty($relatedArticleName)) {
                     $title = Title::newFromText($relatedArticleName);
                     if (empty($title)) {
                         $errors['related_article_name'] = array(wfMsg('toplists-error-invalid-title'));
                     } else {
                         $setResult = $list->setRelatedArticle($title);
                         if ($setResult !== true) {
                             foreach ($setResult as $errorTuple) {
                                 $errors['related_article_name'][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                             }
                         }
                     }
                 } else {
                     $list->setRelatedArticle(null);
                 }
             }
             //handle picture
             $title = $list->getPicture();
             $curValue = null;
             if (!empty($title)) {
                 $curValue = $title->getText();
             }
             $selectedPictureChanged = $curValue != $selectedPictureName;
             if ($selectedPictureChanged) {
                 if (!empty($selectedPictureName)) {
                     $title = Title::newFromText($selectedPictureName, NS_FILE);
                     if (empty($title)) {
                         $errors['selected_picture_name'][] = wfMsg('toplists-error-invalid-picture');
                     } else {
                         $setResult = $list->setPicture($title);
                         if ($setResult !== true) {
                             foreach ($setResult as $errorTuple) {
                                 $errors['selected_picture_name'][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                             }
                         }
                     }
                 } else {
                     $list->setPicture(null);
                 }
             }
             //handle description
             $curValue = null;
             if (!empty($description)) {
                 $curValue = $description;
             }
             $selectedDescriptionChanged = $curValue != $selectedDescription;
             if ($selectedDescriptionChanged) {
                 $list->setDescription($selectedDescription);
             }
             //check the list for processability
             $checkResult = $list->checkForProcessing(TOPLISTS_SAVE_UPDATE);
             if ($checkResult !== true) {
                 foreach ($checkResult as $errorTuple) {
                     $errors['list_name'][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                 }
             }
             //filter input
             foreach ($itemsNames as $index => $item) {
                 $itemsNames[$index] = trim($item);
             }
             //collect existing items and related updates, filter out the removed ones (processed separately)
             $counter = 0;
             foreach ($listItems as $index => $item) {
                 if (!in_array($index, $removedItems)) {
                     $items[] = array('type' => 'existing', 'value' => $itemsNames[$counter], 'index' => $index, 'changed' => false, 'object' => null);
                     if (empty($itemsNames[$counter])) {
                         $errors['item_' . ($counter + 1)][] = wfMsg('toplists-error-empty-item-name');
                     } elseif ($userCanEditItems && $listItems[$index]->getArticle()->getContent() != $itemsNames[$counter]) {
                         $listItems[$index]->setNewContent($itemsNames[$counter]);
                         $items[$counter]['object'] = $listItems[$index];
                         $items[$counter]['changed'] = true;
                     }
                     $counter++;
                 }
             }
             //collect new items, filter out the empty ones
             $splitAt = count($listItems) - count($removedItems);
             $newItemsNames = array_filter(array_slice($itemsNames, $splitAt));
             foreach ($newItemsNames as $index => $item) {
                 $items[] = array('type' => 'new', 'value' => $item, 'changed' => true);
                 $newItem = $list->createItem();
                 $newItem->setNewContent($newItemsNames[$index]);
                 $items[$counter]['object'] = $newItem;
                 $counter++;
             }
             //check items for processing
             $usedNames = array();
             foreach ($items as $index => $item) {
                 $lcName = strtolower($item['value']);
                 if (in_array($lcName, $usedNames)) {
                     $errors['item_' . ++$index][] = wfMsg('toplists-error-duplicated-entry');
                 } else {
                     $usedNames[] = $lcName;
                 }
                 if ($item['changed'] && !empty($item['object'])) {
                     if ($item['type'] == 'new') {
                         $checkResult = $item['object']->checkForProcessing(TOPLISTS_SAVE_AUTODETECT, null, TOPLISTS_SAVE_CREATE);
                     } else {
                         $checkResult = $item['object']->checkForProcessing();
                     }
                     if ($checkResult !== true) {
                         foreach ($checkResult as $errorTuple) {
                             $errors['item_' . ++$index][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                         }
                     }
                 }
             }
             //with no errors or no save required, proceed with items
             $itemTouched = 0;
             if (empty($errors)) {
                 foreach ($items as $index => $item) {
                     if ($item['changed'] && !empty($item['object'])) {
                         $saveResult = $item['object']->save();
                         if ($saveResult !== true) {
                             foreach ($saveResult as $errorTuple) {
                                 $errors['item_' . ++$index][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                             }
                         } else {
                             $item['object']->getTitle()->invalidateCache();
                             $itemTouched++;
                         }
                     }
                 }
                 //purge items removed from the list
                 foreach ($removedItems as $index) {
                     $item = $listItems[$index];
                     if (is_object($item) && $item instanceof TopListItem) {
                         $removeResult = $item->remove();
                     } else {
                         // if item does not exist it means that it's deleted anyway
                         $removeResult = true;
                     }
                     if ($removeResult !== true) {
                         $items[] = array('type' => 'existing', 'value' => $item->getArticle()->getContent(), 'index' => $counter, 'changed' => false, 'object' => $item);
                         $counter++;
                         foreach ($removeResult as $errorTuple) {
                             $errors['item_' . $counter][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                         }
                     } else {
                         $itemTouched++;
                     }
                 }
             }
             //if no errors proceed with saving, list comes first
             if (empty($errors) || $itemTouched) {
                 if ($relatedArticleChanged || $selectedPictureChanged || $selectedDescriptionChanged || $itemTouched) {
                     $saveResult = $list->save();
                     if ($saveResult !== true) {
                         foreach ($saveResult as $errorTuple) {
                             $errors['list_name'] = array(wfMsg($errorTuple['msg'], $errorTuple['params']));
                         }
                     }
                 }
                 //invalidate caches
                 $list->invalidateCache();
                 if (empty($errors)) {
                     $wgOut->redirect($listUrl);
                 }
             }
         } else {
             $title = $list->getRelatedArticle();
             if (!empty($title)) {
                 $relatedArticleName = $title->getText();
             }
             $title = $list->getPicture();
             if (!empty($title)) {
                 $selectedPictureName = $title->getText();
             }
             foreach ($listItems as $index => $item) {
                 $items[] = array('type' => 'existing', 'value' => $item->getArticle()->getContent(), 'index' => $index);
             }
             list($sessionListName, $failedItemsNames, $sessionErrors) = TopListHelper::getSessionItemsErrors();
             if ($listName == $sessionListName && !empty($failedItemsNames)) {
                 $counter = count($items);
                 foreach ($failedItemsNames as $index => $itemName) {
                     $items[] = array('type' => 'new', 'value' => $itemName);
                     $errors['item_' . $counter++] = $sessionErrors[$index];
                 }
             }
             TopListHelper::clearSessionItemsErrors();
         }
         $selectedImage = null;
         if (!empty($selectedPictureName)) {
             $source = new ImageServing(null, 120, array("w" => 3, "h" => 2));
             $result = $source->getThumbnails(array($selectedPictureName));
             if (!empty($result[$selectedPictureName])) {
                 $selectedImage = $result[$selectedPictureName];
             }
         }
         //show at least 3 items by default, if not enough fill in with empty ones
         for ($x = !empty($items) ? count($items) : 0; $x < 3; $x++) {
             $items[] = array('type' => 'new', 'value' => null);
         }
         // pass data to template
         $template = new EasyTemplate(dirname(__FILE__) . '/../templates');
         $template->set_vars(array('mode' => 'update', 'listName' => $listName, 'listUrl' => $listUrl, 'relatedArticleName' => $relatedArticleName, 'description' => $description, 'selectedImage' => $selectedImage, 'errors' => $errors, 'items' => array_merge(array(array('type' => 'template', 'value' => null)), $items), 'removedItems' => $removedItems, 'userCanEditItems' => $userCanEditItems, 'userCanDeleteItems' => $userCanDeleteItems));
         // render template
         $wgOut->addHTML($template->render('editor'));
     }
     wfProfileOut(__METHOD__);
 }
	private function getTemplateData($element) {

		if (! isset($element['file'])) return array();

		$file = $element['file'];
		// crop the images correctly using extension:imageservice
		$is = new ImageServing(array(), self::THUMB_SIZE);
		$thumb_url = $is->getThumbnails(array($file));
		$thumb_url = array_pop($thumb_url);
		$thumb_url = $thumb_url['url'];
		$userName = $file->user_text;

		$retval = array (
			"file_url" => $element['url'],
			"image_url" => $file->getUrl(),
			"thumb_url" => $thumb_url,
			"image_filename" => $file->getTitle()->getFullText(),
			"user_href" => Wikia::link(Title::newFromText($userName, NS_USER), $userName),
			"links" => $this->getLinkedFiles($file->name),
			"isVideoThumb"  => WikiaFileHelper::isFileTypeVideo( $file ),
			"date" => wfTimestamp(TS_ISO_8601, $file->timestamp));
		return $retval;
	}
 function execute($par)
 {
     function purgeInput($elem)
     {
         $val = trim($elem);
         return !empty($val);
     }
     wfProfileIn(__METHOD__);
     global $wgExtensionsPath, $wgJsMimeType, $wgSupressPageSubtitle, $wgRequest, $wgOut, $wgUser;
     // set basic headers
     $this->setHeaders();
     if (wfReadOnly()) {
         $wgOut->readOnlyPage();
         wfProfileOut(__METHOD__);
         return;
     }
     //Check blocks
     if ($wgUser->isBlocked()) {
         wfProfileOut(__METHOD__);
         throw new UserBlockedError($wgUser->getBlock());
     }
     if (!F::app()->checkSkin('oasis')) {
         $this->getOutput()->showErrorPage('error', 'toplists-oasis-only');
         wfProfileOut(__METHOD__);
         return;
     }
     if (!$this->userCanExecute($wgUser)) {
         $this->displayRestrictionError();
         wfProfileOut(__METHOD__);
         return;
     }
     // include resources (css and js)
     $wgOut->addStyle(AssetsManager::getInstance()->getSassCommonURL('/extensions/wikia/TopLists/css/editor.scss'));
     $wgOut->addScript("<script type=\"{$wgJsMimeType}\" src=\"{$wgExtensionsPath}/wikia/TopLists/js/editor.js\"></script>\n");
     //hide specialpage subtitle in Oasis
     $wgSupressPageSubtitle = true;
     TopListHelper::clearSessionItemsErrors();
     $errors = array();
     $listName = null;
     $relatedArticleName = null;
     $description = null;
     $selectedPictureName = null;
     $selectedImage = null;
     $imageTitle = null;
     $items = null;
     $userCanEditItems = $userCanDeleteItems = true;
     if ($wgRequest->wasPosted()) {
         $listName = $wgRequest->getText('list_name');
         $relatedArticleName = $wgRequest->getText('related_article_name');
         $selectedPictureName = $wgRequest->getText('selected_picture_name');
         $description = $wgRequest->getText('description');
         $itemsNames = array_filter($wgRequest->getArray('items_names', array()), 'purgeInput');
         $listItems = array();
         $list = TopList::newFromText($listName);
         $listUrl = null;
         if (!empty($selectedPictureName)) {
             //check image
             $imageTitle = Title::newFromText($selectedPictureName, NS_FILE);
             if (empty($imageTitle)) {
                 $errors['selected_picture_name'] = array(wfMsg('toplists-error-invalid-picture'));
             } else {
                 $text = $imageTitle->getText();
                 $source = new ImageServing(null, 120, array("w" => 3, "h" => 2));
                 $result = $source->getThumbnails(array($imageTitle->getText()));
                 if (!empty($result[$text])) {
                     $selectedImage = $result[$text];
                 }
             }
         }
         if (!$list) {
             $errors['list_name'] = array(wfMsg('toplists-error-invalid-title'));
         } else {
             $title = $list->getTitle();
             $listName = $title->getText();
             $listUrl = $title->getFullUrl();
             if (!empty($relatedArticleName)) {
                 $title = Title::newFromText($relatedArticleName);
                 if (empty($title)) {
                     $errors['related_article_name'] = array(wfMsg('toplists-error-invalid-title'));
                 } else {
                     $setResult = $list->setRelatedArticle($title);
                     if ($setResult !== true) {
                         foreach ($setResult as $errorTuple) {
                             $errors['related_article_name'][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                         }
                     }
                 }
             }
             if (!empty($selectedImage)) {
                 $setResult = $list->setPicture($imageTitle);
                 if ($setResult !== true) {
                     foreach ($setResult as $errorTuple) {
                         $errors['selected_picture_name'][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                     }
                 }
             }
             if (!empty($description)) {
                 $list->setDescription($description);
             }
             $checkResult = $list->checkForProcessing(TOPLISTS_SAVE_CREATE);
             if ($checkResult !== true) {
                 foreach ($checkResult as $errorTuple) {
                     $errors['list_name'][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                 }
             } else {
                 //first check all the items and then save the list, saving items happens only after this
                 $alreadyProcessed = array();
                 foreach ($itemsNames as $index => $itemName) {
                     $lcName = strtolower($itemName);
                     $index++;
                     //index 0 refers to the empty template item in the form
                     if (in_array($lcName, $alreadyProcessed)) {
                         $errors["item_{$index}"] = array(wfMsg('toplists-error-duplicated-entry'));
                     } else {
                         $alreadyProcessed[] = $lcName;
                         $listItem = $list->createItem();
                         if (empty($listItem)) {
                             $errors["item_{$index}"] = array(wfMsg('toplists-error-invalid-title'));
                         } else {
                             $listItem->setNewContent($itemName);
                             $checkResult = $listItem->checkForProcessing(TOPLISTS_SAVE_CREATE, null, TOPLISTS_SAVE_CREATE);
                             if ($checkResult !== true) {
                                 foreach ($checkResult as $errorTuple) {
                                     $errors["item_{$index}"][] = wfMsg($errorTuple['msg'], $errorTuple['params']);
                                 }
                             } else {
                                 $listItems[] = $listItem;
                             }
                         }
                     }
                 }
                 if (empty($errors)) {
                     $saveResult = $list->save();
                     if ($saveResult !== true) {
                         foreach ($saveResult as $errorTuple) {
                             $errors['list_name'] = array(wfMsg($errorTuple['msg'], $errorTuple['params']));
                         }
                     } else {
                         //save items, in this case errors go in session and are displayed in the redirected edit specialpage
                         $unsavedItemNames = array();
                         $itemsErrors = array();
                         foreach ($listItems as $item) {
                             $saveResult = $item->save();
                             if ($saveResult !== true) {
                                 $unsavedItemNames[] = $item->getNewContent();
                                 $counter = 0;
                                 foreach ($saveResult as $errorTuple) {
                                     $itemsErrors[] = array(wfMsg($errorTuple['msg'], $errorTuple['params']));
                                     $counter++;
                                 }
                             } else {
                                 $item->getTitle()->invalidateCache();
                             }
                         }
                         //update page's cache, items where added
                         $list->invalidateCache();
                         if (empty($itemsErrors)) {
                             $wgOut->redirect($listUrl);
                         } else {
                             TopListHelper::setSessionItemsErrors($listName, $unsavedItemNames, $itemsErrors);
                             $specialPageTitle = Title::newFromText('EditTopList', NS_SPECIAL);
                             $wgOut->redirect($specialPageTitle->getFullUrl() . '/' . $list->getTitle()->getPrefixedURL());
                         }
                     }
                 }
             }
         }
         foreach ($itemsNames as $item) {
             $items[] = array('type' => 'new', 'value' => $item);
         }
     } elseif (!empty($par)) {
         $title = Title::newFromText($par);
         if (!empty($title)) {
             $listName = $title->getText();
         }
     }
     //show at least 3 items by default, if not enough fill in with empty ones
     for ($x = !empty($items) ? count($items) : 0; $x < 3; $x++) {
         $items[] = array('type' => 'new', 'value' => null);
     }
     // pass data to template
     $template = new EasyTemplate(dirname(__FILE__) . '/../templates');
     $template->set_vars(array('mode' => 'create', 'listName' => $listName, 'relatedArticleName' => $relatedArticleName, 'description' => $description, 'selectedImage' => $selectedImage, 'errors' => $errors, 'items' => array_merge(array(array('type' => 'template', 'value' => null)), $items), 'userCanEditItems' => $userCanEditItems, 'userCanDeleteItems' => $userCanDeleteItems));
     // render template
     $wgOut->addHTML($template->render('editor'));
     wfProfileOut(__METHOD__);
 }