public function testGetDiffCategories() { $categories = self::$data; $newCategories = self::$data; $newCategories[0]['name'] = '3rd test category'; $expected = [$newCategories[0]]; $actual = CategoryHelper::getDiffCategories($categories, $newCategories); $this->assertEquals($expected, $actual); }
/** * Save categories sent via AJAX into article */ public function save() { wfProfileIn(__METHOD__); $articleId = $this->request->getVal('articleId', 0); $categories = $this->request->getVal('categories', array()); $response = array(); $title = Title::newFromID($articleId); if (wfReadOnly()) { $response['error'] = wfMessage('categoryselect-error-db-locked')->text(); } else { if (is_null($title)) { $response['error'] = wfMessage('categoryselect-error-article-doesnt-exist', $articleId)->text(); } else { if (!$title->userCan('edit') || $this->wg->User->isBlocked()) { $response['error'] = wfMessage('categoryselect-error-user-rights')->text(); } else { if (!empty($categories) && is_array($categories)) { Wikia::setVar('EditFromViewMode', 'CategorySelect'); $article = new Article($title); $wikitext = $article->fetchContent(); // Pull in categories from templates inside of the article (BugId:100980) $options = new ParserOptions(); $preprocessedWikitext = ParserPool::preprocess($wikitext, $title, $options); $preprocessedData = CategoryHelper::extractCategoriesFromWikitext($preprocessedWikitext, true); // Compare the new categories with those already in the article to weed out duplicates $newCategories = CategoryHelper::getDiffCategories($preprocessedData['categories'], $categories); // Append the new categories to the end of the article wikitext $wikitext .= CategoryHelper::changeFormat($newCategories, 'array', 'wikitext'); // Update the array of categories for the front-end $categories = array_merge($preprocessedData['categories'], $newCategories); $dbw = wfGetDB(DB_MASTER); $dbw->begin(); $editPage = new EditPage($article); $editPage->edittime = $article->getTimestamp(); $editPage->recreate = true; $editPage->textbox1 = $wikitext; $editPage->summary = wfMessage('categoryselect-edit-summary')->inContentLanguage()->text(); $editPage->watchthis = $editPage->mTitle->userIsWatching(); $bot = $this->wg->User->isAllowed('bot'); $status = $editPage->internalAttemptSave($result, $bot)->value; $response['status'] = $status; switch ($status) { case EditPage::AS_SUCCESS_UPDATE: case EditPage::AS_SUCCESS_NEW_ARTICLE: $dbw->commit(); $title->invalidateCache(); Article::onArticleEdit($title); $response['html'] = $this->app->renderView('CategorySelectController', 'categories', array('categories' => $categories)); wfRunHooks('CategorySelectSave', array($title, $newCategories)); break; case EditPage::AS_SPAM_ERROR: $dbw->rollback(); $response['error'] = wfMessage('spamprotectiontext')->text() . '<p>( Case #8 )</p>'; break; default: $dbw->rollback(); $response['error'] = wfMessage('categoryselect-error-edit-abort')->text(); } } } } } $this->response->setData($response); wfProfileOut(__METHOD__); }