/** * get class instance * @return RelatedPages */ public static function getInstance() { if (RelatedPages::$instance == null) { RelatedPages::$instance = new RelatedPages(); } return RelatedPages::$instance; }
/** * Get RelatedPages for a given article ID * * @requestParam array $id Id of an article to fetch related pages for * @requestParam integer $limit [OPTIONAL] Limit the number of related pages to return default: 3 * * @responseParam object $items List of articles with related pages * @responseParam array $basepath domain of a wiki to create a url for an article * * @example &ids=2087 * @example &ids=2087,3090 * @example &ids=2087&limit=5 */ function getList() { wfProfileIn(__METHOD__); if (!empty($this->wg->EnableRelatedPagesExt) && empty($this->wg->EnableAnswers)) { $ids = $this->request->getArray(self::PARAMETER_ARTICLE_IDS, null); $limit = $this->request->getInt(self::PARAMETER_LIMIT, 3); $related = []; if (is_array($ids)) { $relatedPages = RelatedPages::getInstance(); foreach ($ids as $id) { if (is_numeric($id)) { $related[$id] = $relatedPages->get($id, $limit); } else { throw new InvalidParameterApiException(self::PARAMETER_ARTICLE_IDS); } $relatedPages->reset(); } } else { wfProfileOut(__METHOD__); throw new MissingParameterApiException('ids'); } $this->setResponseData(['items' => $related, 'basepath' => $this->wg->Server], ['imgFields' => 'imgUrl', 'urlFields' => ['imgUrl', 'url']], WikiaResponse::CACHE_SHORT); wfProfileOut(__METHOD__); } else { wfProfileOut(__METHOD__); throw new NotFoundApiException('Related Pages extension not available'); } }
/** * @desc Related pages are lazy-loaded on article pages for mobile, oasis and monobook. However, there are extensions * dependent on this method where related pages module isn't lazy-loaded such as: FilePage (FilePageController.class.php) */ public function section() { global $wgTitle, $wgContentNamespaces, $wgRequest, $wgMemc; // request params $altTitle = $this->request->getVal('altTitle', null); $relatedPages = RelatedPages::getInstance(); $anyNs = $this->request->getVal('anyNS', false); $title = empty($altTitle) ? $wgTitle : $altTitle; $articleid = $title->getArticleId(); if (!$anyNs) { $ignoreNS = !empty($wgTitle) && in_array($wgTitle->getNamespace(), $wgContentNamespaces); } else { $ignoreNS = false; } $this->skipRendering = Wikia::isMainPage() || $ignoreNS || count($relatedPages->getCategories($articleid)) == 0 || $wgRequest->getVal('action', 'view') != 'view' || $relatedPages->isRendered(); if (!$this->skipRendering) { $mKey = wfMemcKey('mOasisRelatedPages', $articleid, self::MEMC_KEY_VER); $this->pages = $wgMemc->get($mKey); $this->srcAttrName = $this->app->checkSkin('monobook') ? 'src' : 'data-src'; if (empty($this->pages)) { $this->pages = $relatedPages->get($articleid); if (count($this->pages) > 0) { $wgMemc->set($mKey, $this->pages, 3 * 3600); } else { $this->skipRendering = true; } } } $this->mobileSkin = false; $this->relatedPagesHeading = wfMessage('wikiarelatedpages-heading')->plain(); }
/** * Get articles using RelatedPages * * @param int $articleId * @param int $limit * @return array */ public function get($articleId, $limit) { $relatedPages = \RelatedPages::getInstance(); $relatedPages->reset(); $res = $relatedPages->get($articleId, $limit); $recommendations = $this->prepareData($res); return $recommendations; }
public function executeIndex() { global $wgTitle, $wgContentNamespaces, $wgRequest, $wgMemc, $wgRelatedPagesAddAfterSection; $relatedPages = RelatedPages::getInstance(); $categories = $this->request->getVal('categories'); if (!is_null($categories)) { $relatedPages->setCategories($categories); } // check for mainpage if (Wikia::isMainPage()) { $this->skipRendering = true; } // check for content namespaces if (!empty($wgTitle) && !in_array($wgTitle->getNamespace(), $wgContentNamespaces)) { $this->skipRendering = true; } // check if we have any categories if (count($relatedPages->getCategories()) == 0) { $this->skipRendering = true; } // check action if ($wgRequest->getVal('action', 'view') != 'view') { $this->skipRendering = true; } // skip, if module was already rendered if ($relatedPages->isRendered()) { $this->skipRendering = true; } if (!$this->skipRendering) { $mKey = wfMemcKey('mOasisRelatedPages', $wgTitle->getArticleId()); $this->pages = $wgMemc->get($mKey); $this->srcAttrName = $this->app->checkSkin('monobook') ? 'src' : 'data-src'; if (empty($this->pages)) { $this->pages = $relatedPages->get($wgTitle->getArticleId()); if (count($this->pages) > 0) { $wgMemc->set($mKey, $this->pages, 3 * 3600); } else { $this->skipRendering = true; } } // RT #84264 if (!empty($wgRelatedPagesAddAfterSection) && is_numeric($wgRelatedPagesAddAfterSection)) { $this->addAfterSection = intval($wgRelatedPagesAddAfterSection); } } if ($this->app->checkSkin('wikiamobile')) { $this->overrideTemplate('WikiaMobileIndex'); } }
/** * @desc Returns related pages * * @param int $articleId * @param int $limit * @return mixed */ private function getRelatedPages($articleId, $limit = 6) { if (class_exists('RelatedPages')) { return RelatedPages::getInstance()->get($articleId, $limit); } else { return false; } }
/** * Controller to handle showing pages related to the current file. Uses * the RelatedPages extension to render the final HTML */ public function relatedPages() { wfProfileIn(__METHOD__); $this->text = ''; if (!class_exists('RelatedPages')) { wfProfileOut(__METHOD__); return; } # Find the first page that links to this current file page that has a category $pageId = $this->firstPageWithCategory(); if (empty($pageId)) { wfProfileOut(__METHOD__); return; } # Get the title object $title = Title::newFromID($pageId); if (empty($title)) { wfProfileOut(__METHOD__); return; } # Get the categories for this title $cats = $title->getParentCategories(); if (!count($cats)) { wfProfileOut(__METHOD__); return; } $titleCats = array(); # Construct an array of category names to feed to the RelatedPages extension foreach ($cats as $cat_text => $title_text) { $categoryTitle = Title::newFromText($cat_text); $titleCats[] = $categoryTitle->getDBkey(); } # Seed the RelatedPages instance with the categories we found. Normally # categories are set via a hook in the page render process, so we have to # supply our own here. $relatedPages = RelatedPages::getInstance(); $relatedPages->setCategories($titleCats); # Rendering the RelatedPages index with our alternate title and pre-seeded categories. $this->text = $this->app->renderView('RelatedPages', 'section', ["altTitle" => $title, "anyNS" => true]); wfProfileOut(__METHOD__); }