/**
  * Renders the pagetemplates form which is displayed when creating a new article
  * @param bool $bReturnHTML If set, the form is returned as HTML, otherwise as wiki code.
  * @return string The rendered output
  */
 protected function renderPageTemplates()
 {
     global $wgDBtype;
     $oTitle = $this->getTitle();
     // if we are not on a wiki page, return. This is important when calling import scripts that try to create nonexistent pages, e.g. importImages
     if (!is_object($oTitle)) {
         return true;
     }
     $aRes = array();
     $aOutNs = array();
     $dbr = wfGetDB(DB_SLAVE);
     $aConds = array();
     if (BsConfig::get('MW::PageTemplates::HideIfNotInTargetNs')) {
         if ($wgDBtype == 'postgres') {
             $aConds[] = "pt_target_namespace IN ('" . $oTitle->getNamespace() . "', '-99')";
         } else {
             $aConds[] = 'pt_target_namespace IN (' . $oTitle->getNamespace() . ', -99)';
         }
     }
     if ($wgDBtype == 'postgres') {
         $aFields = array("pt_template_title, pt_template_namespace, pt_label, pt_desc, pt_target_namespace");
     } else {
         $aFields = array('pt_template_title', 'pt_template_namespace', 'pt_label', 'pt_desc', 'pt_target_namespace');
     }
     $res = $dbr->select(array('bs_pagetemplate'), $aFields, $aConds, __METHOD__, array('ORDER BY' => 'pt_label'));
     // There is always one template for empty page it is added some lines beneath that
     $iCount = $dbr->numRows($res) + 1;
     $sOut = wfMessage('bs-pagetemplates-choose-template', $iCount)->parse();
     $sOutAll = '';
     $oTargetNsTitle = null;
     $sOut .= '<br /><br /><ul><li>';
     $sOut .= BsLinkProvider::makeLink($oTitle, wfMessage('bs-pagetemplates-empty-page')->plain(), array(), array('preload' => ''));
     $sOut .= '<br />' . wfMessage('bs-pagetemplates-empty-page-desc')->plain();
     $sOut .= '</li></ul>';
     $oSortingTitle = Title::makeTitle(NS_MEDIAWIKI, 'PageTemplatesSorting');
     $vOrder = BsPageContentProvider::getInstance()->getContentFromTitle($oSortingTitle);
     $vOrder = explode('*', $vOrder);
     $vOrder = array_map('trim', $vOrder);
     if ($res && $dbr->numRows($res) > 0) {
         while ($row = $dbr->fetchObject($res)) {
             $aRes[] = $row;
         }
     }
     $dbr->freeResult($res);
     foreach ($aRes as $row) {
         $oNsTitle = Title::makeTitle($row->pt_template_namespace, $row->pt_template_title);
         // TODO MRG (06.09.11 12:53): -99 is "all namespaces". Pls use a more telling constant
         if (BsConfig::get('MW::PageTemplates::ForceNamespace') && $row->pt_target_namespace != "-99" || $row->pt_target_namespace == $oTitle->getNamespace() || BsConfig::get('MW::PageTemplates::HideIfNotInTargetNs') == false) {
             $sNamespaceName = BsNamespaceHelper::getNamespaceName($row->pt_target_namespace);
             if (!isset($aOutNs[$sNamespaceName])) {
                 $aOutNs[$sNamespaceName] = array();
             }
             if (BsConfig::get('MW::PageTemplates::ForceNamespace')) {
                 $oTargetNsTitle = Title::makeTitle($row->pt_target_namespace, $oTitle->getText());
             } else {
                 $oTargetNsTitle = $oTitle;
             }
             $sLink = BsLinkProvider::makeLink($oTargetNsTitle, $row->pt_label, array(), array('preload' => $oNsTitle->getPrefixedText()));
             $sLink = '<li>' . $sLink;
             if ($row->pt_desc) {
                 $sLink .= '<br/>' . $row->pt_desc;
             }
             $sLink .= '</li>';
             $aOutNs[$sNamespaceName][] = array('link' => $sLink, 'id' => $row->pt_target_namespace);
         } elseif ($row->pt_target_namespace == "-99") {
             $sLink = BsLinkProvider::makeLink($oTitle, $row->pt_label, array(), array('preload' => $oNsTitle->getPrefixedText()));
             $sOutAll .= '<li>' . $sLink;
             if ($row->pt_desc) {
                 $sOutAll .= '<br />' . $row->pt_desc;
             }
             $sOutAll .= '</li>';
         }
     }
     if (!empty($vOrder)) {
         $aTmp = array();
         foreach ($vOrder as $key => $value) {
             if (empty($value)) {
                 continue;
             }
             if (array_key_exists($value, $aOutNs)) {
                 $aTmp[$value] = $aOutNs[$value];
             }
         }
         $aOutNs = $aTmp + array_diff_key($aOutNs, $aTmp);
     }
     $aLeftCol = array();
     $aRightCol = array();
     foreach ($aOutNs as $sNs => $aTmpOut) {
         foreach ($aTmpOut as $key => $aAttribs) {
             $sNamespaceName = BsNamespaceHelper::getNamespaceName($aAttribs['id']);
             if ($aAttribs['id'] == $oTitle->getNamespace() || $aAttribs['id'] == -99) {
                 $aLeftCol[$sNamespaceName][] = '<ul>' . $aAttribs['link'] . '</ul>';
             } else {
                 $aRightCol[$sNamespaceName][] = '<ul>' . $aAttribs['link'] . '</ul>';
             }
         }
     }
     if ($sOutAll !== '') {
         $sSectionGeneral = wfMessage('bs-pagetemplates-general-section')->plain();
         $aLeftCol[$sSectionGeneral][] = '<ul>' . $sOutAll . '</ul>';
     }
     $sOut .= '<br />';
     if (!empty($aLeftCol) || !empty($aRightCol) && BsConfig::get('MW::PageTemplates::HideIfNotInTargetNs') == false) {
         $sOut .= '<table><tr>';
         if (!empty($aLeftCol)) {
             $sOut .= '<td style="vertical-align:top;">';
             foreach ($aLeftCol as $sNamespace => $aHtml) {
                 if ($sNamespace == wfMessage('bs-ns_all')->plain()) {
                     $sNamespace = wfMessage('bs-pagetemplates-general-section')->plain();
                 }
                 $sOut .= '<br />';
                 $sOut .= '<h3>' . $sNamespace . '</h3>';
                 $sOut .= implode('', $aHtml);
             }
             $sOut .= '</td>';
         }
         if (BsConfig::get('MW::PageTemplates::HideIfNotInTargetNs') == false) {
             if (!empty($aRightCol)) {
                 $sOut .= '<td style="vertical-align:top;">';
                 foreach ($aRightCol as $sNamespace => $aHtml) {
                     $sOut .= '<br />';
                     $sOut .= '<h3>' . $sNamespace . '</h3>';
                     $sOut .= implode('', $aHtml);
                 }
                 $sOut .= '</td>';
             }
         }
         $sOut .= '</tr></table>';
     }
     return $sOut;
 }
 /**
  *
  * @param Title $oTitle
  * @return false|\ViewStateBarBodyElement
  */
 private function makeStateBarBodyCategories($oTitle)
 {
     global $wgUser;
     $aCurrentPagesCategories = $oTitle->getParentCategories();
     if (empty($aCurrentPagesCategories)) {
         return false;
     }
     wfProfileIn('BS::' . __METHOD__);
     $bIsProcessed = false;
     wfRunHooks('BSArticleInfoBeforeAddLastEditorView', array($this, &$aCurrentPagesCategories, &$bIsProcessed));
     if ($bIsProcessed === false) {
         ksort($aCurrentPagesCategories);
     }
     $oDbr = wfGetDB(DB_SLAVE);
     $res = $oDbr->select(array('page_props'), array('pp_page'), array('pp_propname' => 'hiddencat'));
     $aHiddenPageIDs = array();
     while ($row = $oDbr->fetchObject($res)) {
         $aHiddenPageIDs[] = $row->pp_page;
     }
     $sCategories = '';
     $sAllCategoriesWithUrls = '';
     $sAllCategoriesWithUrls = '';
     foreach ($aCurrentPagesCategories as $sCat => $sName) {
         $oCat = Title::newFromText($sCat);
         if (in_array($oCat->getArticleID(), $aHiddenPageIDs)) {
             $sAllCategoriesWithUrls .= '<li>' . BsLinkProvider::makeLink($oCat, $oCat->getText()) . '</li>';
             continue;
         }
         $sAllCategoriesWithUrls .= '<li>' . BsLinkProvider::makeLink($oCat, $oCat->getText()) . '</li>';
         //But all for the body element
     }
     if (!empty($sAllCategoriesWithUrls)) {
         $sCategories = '<ul>' . $sAllCategoriesWithUrls . '</ul>';
     }
     if ($wgUser->getBoolOption('showhiddencats')) {
         if (!empty($sAllCategoriesWithUrls)) {
             $sCategories .= '<h4>' . wfMessage('bs-articleinfo-hiddencats')->plain() . '</h4>' . '<ul>' . $sAllCategoriesWithUrls . '</ul>';
         }
     }
     if (empty($sCategories)) {
         $sCategories = wfMessage('bs-articleinfo-nocategories')->plain();
     }
     $oCategoriesLinkBodyElement = new ViewStateBarBodyElement();
     $oCategoriesLinkBodyElement->setKey('AllCategories');
     $oCategoriesLinkBodyElement->setHeading(wfMessage('bs-articleinfo-all-categories-heading')->plain());
     $oCategoriesLinkBodyElement->setBodyText($sCategories);
     wfRunHooks('BSArticleInfoBeforeAddCategoryBodyView', array($this, &$oCategoriesLinkBodyElement));
     wfProfileOut('BS::' . __METHOD__);
     return $oCategoriesLinkBodyElement;
 }
 /**
  * Generate a link to a wiki page for a given result
  * @param object $oDocument Apache_Solr_Document
  * @param object $oTitle Title of wiki page
  * @param object $oParser Parser
  * @return string Anchor link to the wiki page
  */
 private function getWikiLink($oDocument, $oTitle, $oParser)
 {
     $sHtml = null;
     if (isset($this->oResponse->highlighting->{$oDocument->uid}->titleWord)) {
         $sHtml = $this->oResponse->highlighting->{$oDocument->uid}->titleWord[0];
     } elseif (isset($this->oResponse->highlighting->{$oDocument->uid}->titleReverse)) {
         $sHtml = $this->oResponse->highlighting->{$oDocument->uid}->titleReverse[0];
     }
     if (!is_null($sHtml)) {
         if ($oDocument->namespace != '0' && $oDocument->namespace != '998' && $oDocument->namespace != '999') {
             $sHtml = BsNamespaceHelper::getNamespaceName($oDocument->namespace) . ':' . $sHtml;
         }
         $sHtml = str_replace('_', ' ', $sHtml);
     }
     $sSearchLink = Linker::link($oTitle, $sHtml, $aCustomAttribs = array('class' => 'bs-extendedsearch-result-headline'), array(), array('known'));
     if (isset($this->oResponse->highlighting->{$oDocument->uid}->sections)) {
         $sSection = strip_tags($this->oResponse->highlighting->{$oDocument->uid}->sections[0], '<em>');
         $sSectionAnchor = $oParser->guessSectionNameFromWikiText($sSection);
         $sSectionLink = BsLinkProvider::makeLink($oTitle, $sSection, array(), array(), array('known'));
         $aMatches = array();
         preg_match('#.*?href="(.*?)".*?#', $sSectionLink, $aMatches);
         if (isset($aMatches[1])) {
             $sAnchor = $aMatches[1] . $sSectionAnchor;
             $sSectionLink = str_replace($aMatches[1], $sAnchor, $sSectionLink);
         }
         $sSearchLink .= ' <span class="bs-extendedsearch-sectionresult">(' . wfMessage('bs-extendedsearch-section')->plain() . ' ' . $sSectionLink . ')</span>';
     }
     return $sSearchLink;
 }
 public static function getResponsibleEditorsPortletData($iCount, $iUserId)
 {
     $iCount = BsCore::sanitize($iCount, 10, BsPARAMTYPE::INT);
     $oDbr = wfGetDB(DB_SLAVE);
     $res = $oDbr->select('bs_responsible_editors', 're_page_id', array('re_user_id' => $iUserId));
     $aResults = array();
     if ($oDbr->numRows($res) > 0) {
         $aResults[] = Html::openElement('ul');
         foreach ($res as $row) {
             $oTitle = Title::newFromID($row->re_page_id);
             if ($oTitle->exists()) {
                 $aResults[] = Html::openElement('li') . BsLinkProvider::makeLink($oTitle, $oTitle->getPrefixedText()) . Html::closeElement('li');
             }
         }
         $aResults[] = Html::closeElement('ul');
     } else {
         $aResults[] = wfMessage('bs-responsibleeditors-no-own-responsibilities')->plain();
     }
     return implode('', $aResults);
 }
 /**
  * Send email notification to next user(s) on review list.
  * @param BsReviewProcess $oReviewProcess Review process users should be notified for.
  * @return Status
  */
 public function emailNotifyNextUsers($oReviewProcess)
 {
     $aNextUsers = $oReviewProcess->getNextUsers();
     // Identify owner
     $oOwner = User::newFromId($oReviewProcess->getOwner());
     $sOwnerRealName = BsCore::getUserDisplayName($oOwner);
     $oTitle = Title::newFromID($oReviewProcess->pid);
     $sTitleText = $oTitle->getPrefixedText();
     $sLink = BsLinkProvider::makeLink($oTitle, $oTitle->getFullURL());
     foreach ($aNextUsers as $aReviewer) {
         // dirty workaround, sometimes id comes as username
         if (is_numeric($aReviewer['id'])) {
             $oReviewer = User::newFromId($aReviewer['id']);
         } else {
             $oReviewer = User::newFromName($aReviewer['id']);
         }
         if (!BsConfig::getVarForUser('MW::Review::EmailNotifyReviewer', $oReviewer->getName())) {
             continue;
         }
         // Identify reviewer
         //PW(10.03.2015): Echo notifications
         $sReviewerMail = $oReviewer->getEmail();
         if (!$sReviewerMail) {
             continue;
         }
         $sReviewerLang = $oReviewer->getOption('language');
         $sSubject = wfMessage('bs-review-mail-invite-header', $sTitleText)->inLanguage($sReviewerLang)->text();
         $sMsg = wfMessage('bs-review-mail-invite-body', $sOwnerRealName, $oOwner->getName(), $sTitleText)->inLanguage($sReviewerLang)->text();
         $sMsg .= "\n\n" . $sLink;
         if ($aReviewer['comment']) {
             $sMsg .= "\n" . wfMessage('bs-review-mail-comment', $aReviewer['comment'])->inLanguage($sReviewerLang)->text();
         }
         //Send mail to next user in queue
         BsMailer::getInstance('MW')->send($oReviewer, $sSubject, $sMsg);
     }
 }
 /**
  * Generates list of most edited pages
  * @return String list of pages or empty
  */
 public function getActivePortlet($iCount, $iTime)
 {
     $oDbr = wfGetDB(DB_SLAVE);
     $iCount = BsCore::sanitize($iCount, 10, BsPARAMTYPE::INT);
     $iTime = BsCore::sanitize($iTime, 0, BsPARAMTYPE::INT);
     $aConditions = array();
     if ($iTime !== 0) {
         $this->getTimestampForQuery($aConditions, $iTime);
     }
     $res = $oDbr->select('revision', array('COUNT(rev_user) as edit_count', 'rev_user'), $aConditions, __METHOD__, array('GROUP BY' => 'rev_user', 'ORDER BY' => 'edit_count DESC'));
     $aList = array();
     if ($oDbr->numRows($res) > 0) {
         $aList[] = '<ol>';
         $i = 1;
         foreach ($res as $row) {
             if ($i > $iCount) {
                 break;
             }
             $oUser = User::newFromId($row->rev_user);
             if ($oUser->isIP($oUser->getName())) {
                 continue;
             }
             $oTitle = Title::makeTitle(NS_USER, $oUser->getName());
             $sLink = BsLinkProvider::makeLink($oTitle);
             $aList[] = '<li>' . $sLink . ' (' . $row->edit_count . ')</li>';
             $i++;
         }
         $aList[] = '</ol>';
     }
     $oDbr->freeResult($res);
     return implode("\n", $aList);
 }