00001 <?php
00002
00014 class SRFTagCloud extends SMWResultPrinter {
00015
00016 protected $includeName;
00017 protected $sizeMode;
00018 protected $tagOrder;
00019 protected $minCount;
00020 protected $maxSize;
00021 protected $maxTags;
00022 protected $minTagSize;
00023
00024 protected $tagsHtml = array();
00025
00026 public function getName() {
00027 return wfMsg( 'srf_printername_tagcloud' );
00028 }
00029
00038 protected function handleParameters( array $params, $outputmode ) {
00039 parent::handleParameters( $params, $outputmode );
00040
00041 $this->includeName = $params['includesubject'];
00042 $this->sizeMode = $params['increase'];
00043 $this->tagOrder = $params['tagorder'];
00044 $this->minCount = $params['mincount'];
00045 $this->maxTags = $params['maxtags'];
00046 $this->minTagSize = $params['minsize'];
00047 $this->maxSize = $params['maxsize'];
00048 }
00049
00050 public function getResultText( SMWQueryResult $results, $outputmode ) {
00051 $this->isHTML = $outputmode == SMW_OUTPUT_HTML;
00052 return $this->getTagCloud( $this->getTagSizes( $this->getTags( $results, $outputmode ) ) );
00053 }
00054
00065 protected function getTags( SMWQueryResult $results, $outputmode ) {
00066 $tags = array();
00067
00068 while ( $row = $results->getNext() ) {
00069 for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) {
00070 while ( ( $dataValue = $row[$i]->getNextDataValue() ) !== false ) {
00071
00072 $isSubject = $row[$i]->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS;
00073
00074
00075 if ( $i == 0 && !$this->includeName && $isSubject ) {
00076 continue;
00077 }
00078
00079
00080 if ( $dataValue->getTypeID() == '_wpg' ) {
00081 $value = $dataValue->getTitle()->getText();
00082 $html = $dataValue->getLongText( $outputmode, $this->getLinker( $isSubject ) );
00083 }
00084 else {
00085 $html = $dataValue->getShortText( $outputmode, $this->getLinker( false ) );
00086 $value = $html;
00087 }
00088
00089 if ( !array_key_exists( $value, $tags ) ) {
00090 $tags[$value] = 0;
00091 $this->tagsHtml[$value] = $html;
00092 }
00093
00094 $tags[$value]++;
00095 }
00096 }
00097 }
00098
00099 foreach ( $tags as $name => $count ) {
00100 if ( $count < $this->minCount ) {
00101 unset( $tags[$name] );
00102 }
00103 }
00104
00105 return $tags;
00106 }
00107
00118 protected function getTagSizes( array $tags ) {
00119 if ( count( $tags ) == 0 ) {
00120 return $tags;
00121 }
00122
00123
00124 if ( $this->tagOrder == 'unchanged' ) {
00125 $unchangedTags = array_keys( $tags );
00126 }
00127
00128 arsort( $tags, SORT_NUMERIC );
00129
00130 if ( count( $tags ) > $this->maxTags ) {
00131 $tags = array_slice( $tags, 0, $this->maxTags, true );
00132 }
00133
00134 $min = end( $tags ) or $min = 0;
00135 $max = reset( $tags ) or $max = 1;
00136 $maxSizeIncrease = $this->maxSize - $this->minTagSize;
00137
00138
00139 foreach ( $tags as &$tag ) {
00140 switch ( $this->sizeMode ) {
00141 case 'linear':
00142 $divisor = ($max == $min) ? 1 : $max - $min;
00143 $tag = $this->minTagSize + $maxSizeIncrease * ( $tag -$min ) / $divisor;
00144 break;
00145 case 'log' : default :
00146 $divisor = ($max == $min) ? 1 : log( $max ) - log( $min );
00147 $tag = $this->minTagSize + $maxSizeIncrease * ( log( $tag ) - log( $min ) ) / $divisor ;
00148 break;
00149 }
00150 }
00151
00152 switch ( $this->tagOrder ) {
00153 case 'desc' :
00154
00155 break;
00156 case 'asc' :
00157 asort( $tags );
00158 break;
00159 case 'alphabetical' :
00160 $tagNames = array_keys( $tags );
00161 natcasesort( $tagNames );
00162 $newTags = array();
00163
00164 foreach ( $tagNames as $name ) {
00165 $newTags[$name] = $tags[$name];
00166 }
00167
00168 $tags = $newTags;
00169 break;
00170 case 'random' :
00171 $tagSizes = $tags;
00172 shuffle( $tagSizes );
00173 $newTags = array();
00174
00175 foreach ( $tagSizes as $size ) {
00176 foreach ( $tags as $tagName => $tagSize ) {
00177 if ( $tagSize == $size ) {
00178 $newTags[$tagName] = $tags[$tagName];
00179 break;
00180 }
00181 }
00182 }
00183
00184 $tags = $newTags;
00185 break;
00186 case 'unchanged' : default :
00187 $changedTags = $tags;
00188 $tags = array();
00189
00190 foreach ( $unchangedTags as $name ) {
00191
00192 if ( array_key_exists( $name, $changedTags ) ) {
00193 $tags[$name] = $changedTags[$name];
00194 }
00195 }
00196 break;
00197 }
00198
00199 return $tags;
00200 }
00201
00211 protected function getTagCloud( array $tags ) {
00212 $htmlTags = array();
00213
00214 foreach ( $tags as $name => $size ) {
00215 $htmlTags[] = Html::rawElement(
00216 'span',
00217 array( 'style' => "font-size:$size%" ),
00218 $this->tagsHtml[$name]
00219 );
00220 }
00221
00222 return Html::rawElement(
00223 'div',
00224 array( 'align' => 'justify' ),
00225 implode( ' ', $htmlTags )
00226 );
00227 }
00228
00236 public function getParameters() {
00237 $params = parent::getParameters();
00238
00239 $params['includesubject'] = new Parameter( 'includesubject', Parameter::TYPE_BOOLEAN );
00240 $params['includesubject']->setMessage( 'srf_paramdesc_includesubject' );
00241 $params['includesubject']->setDefault( false );
00242
00243 $params['increase'] = new Parameter( 'increase' );
00244 $params['increase']->setMessage( 'srf_paramdesc_increase' );
00245 $params['increase']->addCriteria( new CriterionInArray( 'linear', 'log' ) );
00246 $params['increase']->setDefault( 'log' );
00247
00248 $params['tagorder'] = new Parameter( 'tagorder' );
00249 $params['tagorder']->setMessage( 'srf_paramdesc_tagorder' );
00250 $params['tagorder']->addCriteria( new CriterionInArray( 'alphabetical', 'asc', 'desc', 'random', 'unchanged' ) );
00251 $params['tagorder']->setDefault( 'alphabetical' );
00252
00253 $params['mincount'] = new Parameter( 'mincount', Parameter::TYPE_INTEGER );
00254 $params['mincount']->setMessage( 'srf_paramdesc_mincount' );
00255 $params['mincount']->setDefault( 1 );
00256
00257 $params['maxtags'] = new Parameter( 'maxtags', Parameter::TYPE_INTEGER );
00258 $params['maxtags']->setMessage( 'srf_paramdesc_maxtags' );
00259 $params['maxtags']->setDefault( 1000 );
00260
00261 $params['minsize'] = new Parameter( 'minsize', Parameter::TYPE_INTEGER );
00262 $params['minsize']->setMessage( 'srf_paramdesc_minsize' );
00263 $params['minsize']->setDefault( 77 );
00264
00265 $params['maxsize'] = new Parameter( 'maxsize', Parameter::TYPE_INTEGER );
00266 $params['maxsize']->setMessage( 'srf_paramdesc_maxsize' );
00267 $params['maxsize']->setDefault( 242 );
00268
00269 return $params;
00270 }
00271
00272 }