00001 <?php
00002
00023 abstract class SMWAggregatablePrinter extends SMWResultPrinter {
00024
00032 protected abstract function getFormatOutput( array $data );
00033
00042 protected function addResources() {}
00043
00048 protected function getResultText( SMWQueryResult $result, $outputmode ) {
00049 $data = $this->getResults( $result, $outputmode );
00050
00051 if ( count( $data ) == 0 ) {
00052
00053 return '<span class="error">' . wfMsgForContent( 'srf-warn-empy-chart' ) . '</span>';
00054
00055
00056
00057
00058
00059
00060
00061
00062 }
00063 else {
00064 $this->applyDistributionParams( $data );
00065 $this->addResources();
00066 return $this->getFormatOutput( $data );
00067 }
00068 }
00069
00077 protected function applyDistributionParams( array &$data ) {
00078 if ( $this->params['distributionsort'] == 'asc' ) {
00079 asort( $data, SORT_NUMERIC );
00080 }
00081 elseif ( $this->params['distributionsort'] == 'desc' ) {
00082 arsort( $data, SORT_NUMERIC );
00083 }
00084
00085 if ( $this->params['distributionlimit'] !== false ) {
00086 $data = array_slice( $data, 0, $this->params['distributionlimit'], true );
00087 }
00088 }
00089
00102 protected function getResults( SMWQueryResult $result, $outputmode ) {
00103 if ( $this->params['distribution'] ) {
00104 return $this->getDistributionResults( $result, $outputmode );
00105 }
00106 else {
00107 return $this->getNumericResults( $result, $outputmode );
00108 }
00109 }
00110
00122 protected function getDistributionResults( SMWQueryResult $result, $outputmode ) {
00123 $values = array();
00124
00125 while ( $row = $result->getNext() ) {
00126 for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) {
00127 while ( ( $dataValue = $row[$i]->getNextDataValue() ) !== false ) {
00128
00129
00130 if ( $dataValue->getTypeID() == '_wpg' ) {
00131 $value = $dataValue->getTitle()->getText();
00132 }
00133 else {
00134 $value = $dataValue->getShortText( $outputmode, $this->getLinker( false ) );
00135 }
00136
00137 if ( !array_key_exists( $value, $values ) ) {
00138 $values[$value] = 0;
00139 }
00140
00141 $values[$value]++;
00142 }
00143 }
00144 }
00145
00146 return $values;
00147 }
00148
00159 protected function getNumericResults( SMWQueryResult $res, $outputmode ) {
00160 $values = array();
00161
00162
00163 while ( $row = $res->getNext() ) {
00164 $dataValue = $row[0]->getNextDataValue();
00165
00166 if ( $dataValue !== false ) {
00167 $name = $dataValue->getShortWikiText();
00168
00169 foreach ( $row as $field ) {
00170 while ( ( $dataItem = $field->getNextDataItem() ) !== false ) {
00171 $this->addNumbersForDataItem( $dataItem, $values, $name );
00172 }
00173 }
00174 }
00175 }
00176
00177 return $values;
00178 }
00179
00189 protected function addNumbersForDataItem( SMWDataItem $dataItem, array &$values, $name ) {
00190 switch ( $dataItem->getDIType() ) {
00191 case SMWDataItem::TYPE_NUMBER:
00192 $values[$name] = $dataItem->getNumber();
00193 break;
00194 case SMWDataItem::TYPE_CONTAINER:
00195 foreach ( $dataItem->getDataItems() as $di ) {
00196 $this->addNumbersForDataItem( $di, $values, $name );
00197 }
00198 break;
00199 default:
00200 }
00201 }
00202
00207 public function getParameters() {
00208 $params = parent::getParameters();
00209
00210 $params['distribution'] = new Parameter( 'distribution', Parameter::TYPE_BOOLEAN, false );
00211 $params['distribution']->setMessage( 'smw-paramdesc-distribution' );
00212
00213 $params['distributionsort'] = new Parameter( 'distributionsort', Parameter::TYPE_STRING, 'none' );
00214 $params['distributionsort']->setMessage( 'smw-paramdesc-distributionsort' );
00215 $params['distributionsort']->addCriteria( new CriterionInArray( 'asc', 'desc', 'none' ) );
00216
00217 $params['distributionlimit'] = new Parameter( 'distributionlimit', Parameter::TYPE_INTEGER );
00218 $params['distributionlimit']->setDefault( false, false );
00219 $params['distributionlimit']->setMessage( 'smw-paramdesc-distributionlimit' );
00220 $params['distributionlimit']->addCriteria( new CriterionInRange( 1, false ) );
00221
00222 return $params;
00223 }
00224
00225 }