/**
  * Prepares data for list output
  * @param BsDiagram $oDiagram The diagram to prepare chart for
  * @param StatsDataProvider $provider Provider class for chart data
  */
 public static function prepareList($oDiagram, $provider)
 {
     // TODO MRG (13.12.10 17:29): make configurable
     set_time_limit(120);
     $intervals = Interval::getIntervalsFromDiagram($oDiagram);
     //$oDiagram->addFilterText( wfMessage( 'bs-statistics-from-to', $oDiagram->getStartTime(), $oDiagram->getEndTime() )->plain() );
     //$oDiagram->addFilterText( "<br/>".wfMessage( 'bs-statistics-mode' )->plain().": ".wfMessage( $oDiagram->getMessage() )->plain() );
     // PostgreSQL-Check (uses mwuser instead of user)
     global $wgDBtype;
     // Pls. keep the space after user, otherwise, user_groups is also replaced
     $sql = $oDiagram->getSQL();
     if ($wgDBtype == 'postgres') {
         $sql = str_replace('#__user', '#__mwuser', $sql);
     }
     if ($wgDBtype == 'postgres') {
         $sql = str_replace('#__mwuser_', '#__user_', $sql);
     }
     global $wgDBprefix;
     $sql = str_replace("#__", $wgDBprefix, $sql);
     foreach ($oDiagram->getFilters() as $oFilter) {
         $sFilterSql = $oFilter->getSql();
         $sql = str_replace($oFilter->getSqlKey(), $sFilterSql, $sql);
         $sActiveFilterText = $oFilter->getActiveFilterText();
         if (!empty($sActiveFilterText)) {
             $oDiagram->addFilterText("<br/>" . $oFilter->getLabel() . ": " . $sActiveFilterText);
         }
     }
     $provider->match = $sql;
     $starttime = strtotime($oDiagram->getStartTime());
     // end at last second of the day, hence +24*60*60-1 seconds
     $endtime = strtotime($oDiagram->getEndTime()) + 84599;
     $interval = new Interval();
     $interval->setStartTS($starttime);
     $interval->setEndTS($endtime);
     $oDiagram->setData($provider->uniqueValues($interval, $oDiagram->isListable(), count($oDiagram->getListLabel())));
     //arsort($diag['data']);
     return BsCharting::drawTable($oDiagram);
 }
 /**
  *
  * @global User $wgUser
  * @global WebRequest $wgRequest
  * @return type
  */
 public static function ajaxSave()
 {
     $aResult = array("success" => false, "errors" => array(), "message" => '', "data" => array());
     global $wgUser, $wgRequest;
     if (!$wgUser->isAllowed('read')) {
         $aResult["message"] = wfMessage('bs-statistics-not-allowed')->plain();
         return json_encode($aResult);
     }
     $sDiagram = $wgRequest->getVal('inputDiagrams', '');
     $sGrain = $wgRequest->getVal('InputDepictionGrain', 'Y');
     $sFrom = $wgRequest->getVal('inputFrom', '');
     $sMode = $wgRequest->getVal('rgInputDepictionMode', '');
     $sTo = $wgRequest->getVal('inputTo', '');
     $aAvailableDiagrams = Statistics::getAvailableDiagrams();
     $aAllowedDiaKeys = array_keys($aAvailableDiagrams);
     if (empty($sDiagram)) {
         $aResult["errors"]['inputDiagrams'] = wfMessage('bs-statistics-err-emptyinput')->plain();
     } elseif (!in_array($sDiagram, $aAllowedDiaKeys)) {
         $aResult["errors"]['inputDiagrams'] = wfMessage('bs-statistics-err-unknowndia')->plain();
     }
     if (!array_key_exists($sGrain, BsConfig::get('MW::Statistics::AvailableGrains'))) {
         $aResult["errors"]['InputDepictionGrain'] = wfMessage('bs-statistics-err-unknowngrain')->plain();
     }
     if (empty($sFrom)) {
         $aResult["errors"]['inputFrom'] = wfMessage('bs-statistics-err-emptyinput')->plain();
     } elseif (!($oFrom = DateTime::createFromFormat('d.m.Y', $sFrom))) {
         $aResult["errors"]['inputFrom'] = wfMessage('bs-statistics-err-invaliddate')->plain();
     }
     if (empty($sTo)) {
         $aResult["errors"]['inputTo'] = wfMessage('bs-statistics-err-emptyinput')->plain();
     } elseif (!($oTo = DateTime::createFromFormat('d.m.Y', $sFrom))) {
         $aResult["errors"]['inputTo'] = wfMessage('bs-statistics-err-invaliddate')->plain();
     } elseif ($oTo > new DateTime()) {
         $aResult["errors"]['inputTo'] = wfMessage('bs-statistics-err-invaliddate')->plain();
     }
     if (isset($oFrom) && isset($oTo) && $oFrom > $oTo) {
         $aResult["errors"]['inputTo'] = wfMessage('bs-statistics-err-invalidtofromrelation')->plain();
     }
     if (empty($sMode)) {
         $aResult["errors"]['rgInputDepictionMode'] = wfMessage('bs-statistics-err-emptyinput')->plain();
     } elseif (!in_array($sMode, array('absolute', 'aggregated', 'list'))) {
         $aResult["errors"]['rgInputDepictionMode'] = wfMessage('bs-statistics-err-unknownmode')->plain();
     } elseif (!isset($aResult["errors"]['inputDiagrams']) && $sMode == 'list' && !$aAvailableDiagrams[$sDiagram]->isListable()) {
         $aResult["errors"]['rgInputDepictionMode'] = wfMessage('bs-statistics-err-modeunsupported')->plain();
     }
     if (!empty($aResult['errors'])) {
         return json_encode($aResult);
     }
     $oDiagram = Statistics::getDiagram($sDiagram);
     $oDiagram->setStartTime($sFrom);
     $oDiagram->setEndTime($sTo);
     $oDiagram->setActualGrain($sGrain);
     $oDiagram->setModLabel($sGrain);
     $oDiagram->setMode($sMode);
     //$oDiagram->setMessage( $sMessage );
     //$oDiagram->setFilters( $aDiagFilter );
     switch ($oDiagram->getActualGrain()) {
         // Here, only those grains are listed where label code differs from grain code.
         case 'm':
             $oDiagram->setModLabel('M y');
             break;
         case 'd':
             $oDiagram->setModLabel('d.m');
             break;
             //default  : $oDiagram->modLabel = false;
     }
     switch ($oDiagram->getDataSource()) {
         case BsDiagram::DATASOURCE_DATABASE:
             global $wgDBtype, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname;
             switch ($wgDBtype) {
                 case "postgres":
                     $oReader = new PostGreSQLDbReader();
                     break;
                 case "oracle":
                     $oReader = new OracleDbReader();
                     break;
                 default:
                     $oReader = new MySQLDbReader();
             }
             //$oReader = $sDbType == 'mysql' ? new MySQLDbReader() : new PostGreSQLDbReader();
             $oReader->host = $wgDBserver;
             $oReader->user = $wgDBuser;
             $oReader->pass = $wgDBpassword;
             $oReader->db = $wgDBname;
             break;
     }
     $intervals = Interval::getIntervalsFromDiagram($oDiagram);
     if (count($intervals) > BsConfig::get('MW::Statistics::MaxNumberOfIntervals')) {
         $aResult['message'] = wfMessage('bs-statistics-interval-too-big')->plain();
         return json_encode($aResult);
     }
     //set_time_limit( 60 );
     // TODO MRG (20.12.10 00:01): already called before
     //$intervals = Interval::getIntervalsFromDiagram( $oDiagram );
     //$oDiagram->addFilterText( wfMessage( 'bs-statistics-from-to', $oDiagram->getStartTime(), $oDiagram->getEndTime() )->plain() );
     // TODO MRG (20.12.10 00:19): This should be getModeText
     //$oDiagram->addFilterText( "\n".wfMessage( 'bs-statistics-mode' )->plain().": ".wfMessage( $oDiagram->getMessage() )->plain() );
     // PostgreSQL-Check (uses mwuser instead of user)
     global $wgDBtype;
     // Pls. keep the space after user, otherwise, user_groups is also replaced
     $sql = $oDiagram->getSQL();
     if ($wgDBtype == 'postgres') {
         $sql = str_replace('#__user', '#__mwuser', $sql);
     }
     if ($wgDBtype == 'postgres') {
         $sql = str_replace('#__mwuser_', '#__user_', $sql);
     }
     global $wgDBprefix;
     $sql = str_replace("#__", $wgDBprefix, $sql);
     foreach ($oDiagram->getFilters() as $oFilter) {
         $sFilterSql = $oFilter->getSql();
         $sql = str_replace($oFilter->getSqlKey(), $sFilterSql, $sql);
     }
     $oReader->match = $sql;
     $oDiagram->setData(BsCharting::getDataPerDateInterval($oReader, $oDiagram->getMode(), $intervals, $oDiagram->isListable()));
     if ($oDiagram->isList()) {
         //$aResult['data']['list'] = BsCharting::drawTable($oDiagram);
         $aResult['data']['list'] = BsCharting::prepareList($oDiagram, $oReader);
         $aResult['label'] = $oDiagram->getTitle();
         $aResult['success'] = true;
         return json_encode($aResult);
     }
     $aData = $oDiagram->getData();
     $i = 0;
     foreach ($intervals as $interval) {
         $aResult['data'][] = array('name' => $interval->getLabel(), 'hits' => (int) $aData[$i]);
         $i++;
     }
     $aAvalableGrains = BsConfig::get('MW::Statistics::AvailableGrains');
     $sLabelMsgKey = 'bs-statistics-label-time';
     if (isset($aAvalableGrains[$oDiagram->getActualGrain()])) {
         $sLabelMsgKey = $aAvalableGrains[$oDiagram->getActualGrain()];
     }
     $aResult['label'] = wfMessage($sLabelMsgKey)->plain();
     $aResult['success'] = true;
     return json_encode($aResult);
 }