/**
  * Render the special page
  *
  * @param string $subPage Tag name with underscores instead of spaces
  */
 public function executeWhenAvailable($subPage)
 {
     $mfConfig = $this->getMFConfig();
     if (!$mfConfig->get('MFIsBrowseEnabled')) {
         $this->renderError(array('browseDisabled' => true));
         return;
     }
     if (!class_exists('Gather\\Hooks')) {
         $this->renderError(array('noGather' => true));
         return;
     }
     $tagName = str_replace('_', ' ', $subPage);
     $titles = $this->getTagService()->getTitlesForTag($tagName);
     if (!$titles) {
         $this->renderError(array('unknownTag' => true));
         return;
     }
     $this->tagName = $tagName;
     $out = $this->getOutput();
     $out->addModules(array('ext.gather.special', 'mobile.special.browse.topicTag.styles', 'mobile.special.browse.topicTag.scripts'));
     $out->addModuleStyles(array('mediawiki.ui.anchor', 'mediawiki.ui.icon', 'ext.gather.icons', 'ext.gather.styles'));
     $collectionItems = array();
     $pageIds = array_map(function (Title $title) {
         return $title->getArticleID();
     }, $titles);
     // get page images and extracts
     $pages = $this->getPages($pageIds);
     if ($pages) {
         foreach ($pages as $page) {
             if (!isset($page['title']) || !$page['title']) {
                 continue;
             }
             $title = Title::newFromText($page['title']);
             $image = false;
             if (isset($page['pageimage'])) {
                 $image = wfFindFile($page['pageimage']);
             }
             $extract = '';
             if (isset($page['extract']['*'])) {
                 $extract = $page['extract']['*'];
             }
             $item = new models\CollectionItem($title, $image, $extract);
             $collectionItems[$page['pageid']] = $item;
         }
     }
     $orderedCollectionItems = array();
     foreach ($pageIds as $id) {
         if (isset($collectionItems[$id])) {
             array_push($orderedCollectionItems, $collectionItems[$id]);
         }
     }
     $collection = new models\Collection(null, $this->getUser());
     $collection->batch($orderedCollectionItems);
     $this->render(new views\Collection($this->getUser(), $collection));
 }
 /**
  * Renders a user collection
  * @param int $id collection id
  */
 public function renderUserCollection($id)
 {
     if (!is_int($id)) {
         throw new InvalidArgumentException(__METHOD__ . ' requires the parameter to be an integer, ' . gettype($id) . ' given.');
     }
     $collection = models\Collection::newFromListsApi($id, null, $this->getRequest()->getValues());
     $this->renderCollection($collection);
 }
 /**
  * Generate a Collection from api with provided query parameters
  * @param Array $params to pass to api.
  * @param models\Collections [$collection] an existing collection to add to
  * @return models\Collections a collection
  */
 private static function newFromApiParams($params, $collection = null)
 {
     $api = new ApiMain(new FauxRequest($params));
     $id = isset($params['lstids']) ? $params['lstids'] : -1;
     try {
         $api->execute();
         $data = $api->getResult()->getResultData(null, array('Strip' => 'all'));
         // When present is response we override optional parameter
         if (isset($data['query']['lists'])) {
             $lists = $data['query']['lists'];
             if (count($lists) === 1) {
                 $list = $lists[0];
                 $image = $list['image'] ? wfFindFile($list['image']) : null;
                 $owner = User::newFromName($list['owner']);
                 $collection = new Collection($id, $owner, $list['label'], $list['description'], $list['perm'] === 'public', $image);
                 if ($list['perm'] === 'hidden') {
                     $collection->setHidden();
                 }
             }
         }
         if ($collection && isset($data['query']['pages'])) {
             $pages = $data['query']['pages'];
             foreach ($pages as $page) {
                 $title = Title::newFromText($page['title'], $page['ns']);
                 $pi = false;
                 if (isset($page['pageimage'])) {
                     $pi = wfFindFile($page['pageimage']);
                 }
                 $extract = '';
                 // See https://phabricator.wikimedia.org/T92673
                 if (isset($page['extract'])) {
                     $extract = $page['extract'];
                     if (isset($extract['*'])) {
                         $extract = $extract['*'];
                     }
                 }
                 $item = new CollectionItem($title, $pi, $extract);
                 if (isset($page['missing'])) {
                     $item->setMissing(true);
                 }
                 $collection->add($item);
             }
         }
         if (isset($data['continue'])) {
             $collection->setContinue($data['continue'], $params);
         }
     } catch (Exception $e) {
         // just return collection
     }
     return $collection;
 }