/** * factory method, create either Item or List object from title * * @author ADi * @param Title $title * @return TopListBase */ public static function newFromTitle(Title $title) { $object = null; if ($title->getNamespace() == NS_TOPLIST) { if (!$title->isSubpage()) { $object = TopList::newFromTitle($title); } else { $object = TopListItem::newFromTitle($title); } } return $object; }
/** * @static * @return AjaxResponse * * @var */ public static function addItem() { global $wgRequest, $wgUser; $result = array('result' => false); $errors = array(); $listText = $wgRequest->getVal('list'); $itemText = trim($wgRequest->getVal('text')); if (!empty($listText) && !empty($itemText)) { $list = TopList::newFromText($listText); if ($wgUser->isAllowed('toplists-create-item')) { if (!empty($list) && $list->exists()) { //check for duplicated foreach ($list->getItems() as $item) { if (strtolower($item->getArticle()->getContent()) == strtolower($itemText)) { $errors[] = wfMsg('toplists-error-duplicated-entry'); break; } } if (empty($errors)) { $newItem = $list->createItem(); $newItem->setNewContent($itemText); $saveResult = $newItem->save(TOPLISTS_SAVE_CREATE); if ($saveResult !== true) { foreach ($saveResult as $errorTuple) { $errors[] = wfMsg($errorTuple['msg'], $errorTuple['params']); } } else { //invalidate caches and trigger save event for the list article $newItem->getTitle()->invalidateCache(); $list->save(TOPLISTS_SAVE_UPDATE); $list->invalidateCache(); $result['result'] = true; $result['listBody'] = TopListParser::parse($list); } } } else { $errors[] = wfMsg('toplists-error-add-item-list-not-exists', $listText); } } else { if ($wgUser->isAnon()) { $loginURL = SpecialPage::getTitleFor('Signup')->getLocalURL() . '?returnto=' . $list->getTitle()->getPrefixedDBkey(); $errors[] = wfMsg('toplists-error-add-item-anon', array("{$loginURL}&type=login", "{$loginURL}&type=signup")); } else { $errors[] = wfMsg('toplists-error-add-item-permission'); } } } else { $errors[] = wfMsg('toplists-error-empty-item-name'); } if (!empty($errors)) { $result['errors'] = $errors; } $json = json_encode($result); $response = new AjaxResponse($json); $response->setContentType('application/json; charset=utf-8'); return $response; }
/** * get list object for current item * * @author ADi * @return TopList */ public function getList() { if (!$this->mList instanceof TopList) { $this->mList = TopList::newFromText($this->getListTitleText()); } return $this->mList; }
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__); }
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__); }
/** * @author Federico "Lox" Lucignano <*****@*****.**> * * Helper method to trigger parsing of a list article comment manually * * @param TopList $list the list object representing the article to parse */ public static function parse(TopList $list) { self::$mList = $list; $text = F::app()->wg->Out->parse($list->getArticle()->getContent()); $list->invalidateCache(); return $text; }