00001 <?php
00002
00024 class SRFValueRank extends SMWResultPrinter {
00025
00026 protected $includeName;
00027 protected $minCount;
00028 protected $maxTags;
00029
00030 protected $tagsHtml = array();
00031
00032 public function getName() {
00033 return wfMsg( 'srf_printername_valuerank' );
00034 }
00035
00044 protected function handleParameters( array $params, $outputmode ) {
00045 parent::handleParameters( $params, $outputmode );
00046
00047 $this->includeName = $params['includesubject'];
00048 $this->minCount = $params['mincount'];
00049 $this->maxTags = $params['maxtags'];
00050 }
00051
00052 public function getResultText( SMWQueryResult $results, $outputmode ) {
00053 $this->isHTML = $outputmode == SMW_OUTPUT_HTML;
00054 return $this->getVRValueRank( $this->getVRRank( $this->getVRValues( $results, $outputmode ) ) );
00055 }
00056
00067 protected function getVRValues( SMWQueryResult $results, $outputmode ) {
00068 $tags = array();
00069
00070 while ( $row = $results->getNext() ) {
00071 for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) {
00072 while ( ( $dataValue = $row[$i]->getNextDataValue() ) !== false ) {
00073
00074 $isSubject = $row[$i]->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS;
00075
00076
00077 if ( $i == 0 && !$this->includeName && $isSubject ) {
00078 continue;
00079 }
00080
00081
00082 if ( $dataValue->getTypeID() == '_wpg' ) {
00083 $value = $dataValue->getTitle()->getText();
00084 $html = $dataValue->getLongText( $outputmode, $this->getLinker( $isSubject ) );
00085 }
00086 else {
00087 $html = $dataValue->getShortText( $outputmode, $this->getLinker( false ) );
00088 $value = $html;
00089 }
00090
00091 if ( !array_key_exists( $value, $tags ) ) {
00092 $tags[$value] = 0;
00093 $this->tagsHtml[$value] = $html;
00094 }
00095
00096 $tags[$value]++;
00097 }
00098 }
00099 }
00100
00101 foreach ( $tags as $name => $count ) {
00102 if ( $count < $this->minCount ) {
00103 unset( $tags[$name] );
00104 }
00105 }
00106 return $tags;
00107 }
00108
00119 protected function getVRRank( array $tags ) {
00120 if ( count( $tags ) == 0 ) {
00121 return $tags;
00122 }
00123
00124 arsort( $tags, SORT_NUMERIC );
00125
00126 if ( count( $tags ) > $this->maxTags ) {
00127 $tags = array_slice( $tags, 0, $this->maxTags, true );
00128 }
00129
00130 return $tags;
00131 }
00132
00142 protected function getVRValueRank( array $tags ) {
00143 $htmlTags = array();
00144
00145 foreach ( $tags as $name => $size ) {
00146 $htmlTags[] = Html::rawElement(
00147 'li',
00148 array( 'style' => "font-size:$size" ),
00149 $this->tagsHtml[$name] . ' (' . $size . ')'
00150 );
00151 }
00152
00153 return Html::rawElement(
00154 'ol',
00155 array( 'align' => 'left' ),
00156 implode( ' ', $htmlTags )
00157 );
00158 }
00159
00167 public function getParameters() {
00168 $params = parent::getParameters();
00169
00170 $params['includesubject'] = new Parameter( 'includesubject', Parameter::TYPE_BOOLEAN );
00171 $params['includesubject']->setMessage( 'srf_paramdesc_includesubject' );
00172 $params['includesubject']->setDefault( false );
00173
00174 $params['mincount'] = new Parameter( 'mincount', Parameter::TYPE_INTEGER );
00175 $params['mincount']->setMessage( 'srf_paramdesc_mincount' );
00176 $params['mincount']->setDefault( 1 );
00177
00178 $params['maxtags'] = new Parameter( 'maxtags', Parameter::TYPE_INTEGER );
00179 $params['maxtags']->setMessage( 'srf_paramdesc_maxtags' );
00180 $params['maxtags']->setDefault( 1000 );
00181
00182 return $params;
00183 }
00184
00185 }