/**
  * Concatenate categories on EditPage POST
  *
  * @param EditPage $editPage
  * @param WebRequest $request
  *
  * @return Boolean because it's a hook
  */
 public static function onEditPageImportFormData($editPage, $request)
 {
     $app = F::app();
     if ($request->wasPosted()) {
         $categories = $editPage->safeUnicodeInput($request, 'categories');
         $categories = CategoryHelper::changeFormat($categories, 'json', 'array');
         // Concatenate categories to article wikitext (if there are any).
         if (!empty($categories)) {
             if (!empty($app->wg->EnableAnswers)) {
                 // don't add categories if the page is a redirect
                 $magicWords = $app->wg->ContLang->getMagicWords();
                 $redirects = $magicWords['redirect'];
                 // first element doesn't interest us
                 array_shift($redirects);
                 // check for localized versions of #REDIRECT
                 foreach ($redirects as $alias) {
                     if (stripos($editPage->textbox1, $alias) === 0) {
                         return true;
                     }
                 }
             }
             // Extract categories from the article, merge them with those passed in, weed out
             // duplicates and finally append them back to the article (BugId:99348).
             $data = CategoryHelper::extractCategoriesFromWikitext($editPage->textbox1, true);
             $categories = CategoryHelper::getUniqueCategories($data['categories'], $categories);
             $categories = CategoryHelper::changeFormat($categories, 'array', 'wikitext');
             // Remove trailing whitespace (BugId:11238)
             $editPage->textbox1 = $data['wikitext'] . rtrim($categories);
         }
     }
     return true;
 }
 /**
  * @desc Alias to CategoryHelper::extractCategoriesFromWikitext helpful for unit tests
  *
  * @param $wikitext
  * @return Array
  */
 public static function getCategoriesFromCategorySelect($wikitext)
 {
     return CategoryHelper::extractCategoriesFromWikitext($wikitext, true);
 }
 /**
  * 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__);
 }