r100719 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100718‎ | r100719 | r100720 >
Date:18:11, 25 October 2011
Author:dasch
Status:deferred (Comments)
Tags:
Comment:
added new format value rank #31938 clean up will be done step by step if it's clear what's not needed
Modified paths:
  • /trunk/extensions/SemanticResultFormats/SRF_Messages.php (modified) (history)
  • /trunk/extensions/SemanticResultFormats/SemanticResultFormats.php (modified) (history)
  • /trunk/extensions/SemanticResultFormats/ValueRank (added) (history)
  • /trunk/extensions/SemanticResultFormats/ValueRank/SRF_ValueRank.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/SemanticResultFormats.php
@@ -97,6 +97,7 @@
9898 $wgAutoloadClasses['SRFTagCloud'] = $formatDir . 'TagCloud/SRF_TagCloud.php';
9999 $wgAutoloadClasses['SRFArray'] = $formatDir . 'Array/SRF_Array.php';
100100 $wgAutoloadClasses['SRFHash'] = $formatDir . 'Array/SRF_Array.php';
 101+ $wgAutoloadClasses['SRFValueRank'] = $formatDir . 'ValueRank/SRF_ValueRank.php';
101102
102103 $formatClasses = array(
103104 'timeline' => 'SRFTimeline',
@@ -122,12 +123,14 @@
123124 'ploticusvbar' => 'SRFPloticusVBar',
124125 'gallery' => 'SRFGallery',
125126 'tagcloud' => 'SRFTagCloud',
 127+ 'valuerank' => 'SRFValueRank',
126128 'array' => 'SRFArray',
127129 'hash' => 'SRFHash',
128130 );
129131
130132 $formatAliases = array(
131 - 'tagcloud' => array( 'tag cloud' )
 133+ 'tagcloud' => array( 'tag cloud' ),
 134+ 'valuerank' => array( 'value rank' )
132135 );
133136
134137 foreach ( $srfgFormats as $format ) {
Index: trunk/extensions/SemanticResultFormats/ValueRank/SRF_ValueRank.php
@@ -0,0 +1,280 @@
 2+<?php
 3+
 4+/**
 5+ * Result printer that prints query results as a valuerank.
 6+ *
 7+ * @since 1.7
 8+ *
 9+ * @file SRF_ValueRank.php
 10+ * @ingroup SemanticResultFormats
 11+ *
 12+ * @licence GNU GPL v3
 13+ * @author DaSch < dasch@daschmedia.de >
 14+ * build out of Tag Cloud Format
 15+ */
 16+class SRFValueRank extends SMWResultPrinter {
 17+
 18+ protected $includeName;
 19+ protected $sizeMode;
 20+ protected $tagOrder;
 21+ protected $minCount;
 22+ protected $maxSize;
 23+ protected $maxTags;
 24+ protected $minTagSize;
 25+
 26+ protected $tagsHtml = array();
 27+
 28+ public function __construct( $format, $inline, $useValidator = true ) {
 29+ parent::__construct( $format, $inline );
 30+ $this->useValidator = $useValidator;
 31+ }
 32+
 33+ public function getName() {
 34+ return wfMsg( 'srf_printername_valuerank' );
 35+ }
 36+
 37+ /**
 38+ * @see SMWResultPrinter::handleParameters
 39+ *
 40+ * @since 1.7
 41+ *
 42+ * @param array $params
 43+ * @param $outputmode
 44+ */
 45+ protected function handleParameters( array $params, $outputmode ) {
 46+ parent::handleParameters( $params, $outputmode );
 47+
 48+ $this->includeName = $params['includesubject'];
 49+ $this->sizeMode = $params['increase'];
 50+ $this->tagOrder = $params['tagorder'];
 51+ $this->minCount = $params['mincount'];
 52+ $this->maxTags = $params['maxtags'];
 53+ $this->minTagSize = $params['minsize'];
 54+ $this->maxSize = $params['maxsize'];
 55+ }
 56+
 57+ public function getResultText( SMWQueryResult $results, $outputmode ) {
 58+ $this->isHTML = $outputmode == SMW_OUTPUT_HTML;
 59+ return $this->getValueRank( $this->getRank( $this->getTags( $results, $outputmode ) ) );
 60+ }
 61+
 62+ /**
 63+ * Returns an array with the tags (keys) and the number of times they occur (values).
 64+ *
 65+ * @since 1.5.3
 66+ *
 67+ * @param SMWQueryResult $results
 68+ * @param $outputmode
 69+ *
 70+ * @return array
 71+ */
 72+ protected function getTags( SMWQueryResult $results, $outputmode ) {
 73+ $tags = array();
 74+
 75+ while ( /* array of SMWResultArray */ $row = $results->getNext() ) { // Objects (pages)
 76+ for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) { // SMWResultArray for a sinlge property
 77+ while ( ( /* SMWDataValue */ $dataValue = efSRFGetNextDV( $row[$i] ) ) !== false ) { // Data values
 78+
 79+ $isSubject = $row[$i]->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS;
 80+
 81+ // If the main object should not be included, skip it.
 82+ if ( $i == 0 && !$this->includeName && $isSubject ) {
 83+ continue;
 84+ }
 85+
 86+ // Get the HTML for the tag content. Pages are linked, other stuff is just plaintext.
 87+ if ( $dataValue->getTypeID() == '_wpg' ) {
 88+ $value = $dataValue->getTitle()->getText();
 89+ $html = $dataValue->getLongText( $outputmode, $this->getLinker( $isSubject ) );
 90+ }
 91+ else {
 92+ $html = $dataValue->getShortText( $outputmode, $this->getLinker( false ) );
 93+ $value = $html;
 94+ }
 95+
 96+ if ( !array_key_exists( $value, $tags ) ) {
 97+ $tags[$value] = 0;
 98+ $this->tagsHtml[$value] = $html; // Store the HTML separetely, so sorting can be done easily.
 99+ }
 100+
 101+ $tags[$value]++;
 102+ }
 103+ }
 104+ }
 105+
 106+ foreach ( $tags as $name => $count ) {
 107+ if ( $count < $this->minCount ) {
 108+ unset( $tags[$name] );
 109+ }
 110+ }
 111+
 112+ return $tags;
 113+ }
 114+
 115+ /**
 116+ * Determines the sizes of tags.
 117+ * This method is based on code from the FolkTagCloud extension by Katharina Wäschle.
 118+ *
 119+ * @since 1.5.3
 120+ *
 121+ * @param array $tags
 122+ *
 123+ * @return array
 124+ */
 125+ protected function getRank( array $tags ) {
 126+ if ( count( $tags ) == 0 ) {
 127+ return $tags;
 128+ }
 129+
 130+ // If the original order needs to be kept, we need a copy of the current order.
 131+ if ( $this->tagOrder == 'unchanged' ) {
 132+ $unchangedTags = array_keys( $tags );
 133+ }
 134+
 135+ arsort( $tags, SORT_NUMERIC );
 136+
 137+ if ( count( $tags ) > $this->maxTags ) {
 138+ $tags = array_slice( $tags, 0, $this->maxTags, true );
 139+ }
 140+
 141+ $min = end( $tags ) or $min = 0;
 142+ $max = reset( $tags ) or $max = 1;
 143+ $maxSizeIncrease = $this->maxSize - $this->minTagSize;
 144+
 145+ // Loop over the tags, and replace their count by a size.
 146+ /*
 147+ foreach ( $tags as &$tag ) {
 148+ switch ( $this->sizeMode ) {
 149+ case 'linear':
 150+ $divisor = ($max == $min) ? 1 : $max - $min;
 151+ $tag = $this->minTagSize + $maxSizeIncrease * ( $tag -$min ) / $divisor;
 152+ break;
 153+ case 'log' : default :
 154+ $divisor = ($max == $min) ? 1 : log( $max ) - log( $min );
 155+ $tag = $this->minTagSize + $maxSizeIncrease * ( log( $tag ) - log( $min ) ) / $divisor ;
 156+ break;
 157+ }
 158+ }
 159+ */
 160+
 161+ switch ( $this->tagOrder ) {
 162+ case 'desc' :
 163+ // Tags are already sorted desc
 164+ break;
 165+ case 'asc' :
 166+ asort( $tags );
 167+ break;
 168+ case 'alphabetical' :
 169+ $tagNames = array_keys( $tags );
 170+ natcasesort( $tagNames );
 171+ $newTags = array();
 172+
 173+ foreach ( $tagNames as $name ) {
 174+ $newTags[$name] = $tags[$name];
 175+ }
 176+
 177+ $tags = $newTags;
 178+ break;
 179+ case 'random' :
 180+ $tagSizes = $tags;
 181+ shuffle( $tagSizes );
 182+ $newTags = array();
 183+
 184+ foreach ( $tagSizes as $size ) {
 185+ foreach ( $tags as $tagName => $tagSize ) {
 186+ if ( $tagSize == $size ) {
 187+ $newTags[$tagName] = $tags[$tagName];
 188+ break;
 189+ }
 190+ }
 191+ }
 192+
 193+ $tags = $newTags;
 194+ break;
 195+ case 'unchanged' : default : // Restore the original order.
 196+ $changedTags = $tags;
 197+ $tags = array();
 198+
 199+ foreach ( $unchangedTags as $name ) {
 200+ // Original tags might have been left out at this point, so only add remaining ones.
 201+ if ( array_key_exists( $name, $changedTags ) ) {
 202+ $tags[$name] = $changedTags[$name];
 203+ }
 204+ }
 205+ break;
 206+ }
 207+
 208+ return $tags;
 209+ }
 210+
 211+ /**
 212+ * Returns the HTML for the tag cloud.
 213+ *
 214+ * @since 1.5.3
 215+ *
 216+ * @param array $tags
 217+ *
 218+ * @return string
 219+ */
 220+ protected function getValueRank( array $tags ) {
 221+ $htmlTags = array();
 222+
 223+ foreach ( $tags as $name => $size ) {
 224+ $htmlTags[] = Html::rawElement(
 225+ 'li',
 226+ array( 'style' => "font-size:100%" ),
 227+ $this->tagsHtml[$name . '(' . $size . ')']
 228+ );
 229+ }
 230+
 231+ return Html::rawElement(
 232+ 'ul',
 233+ array( 'align' => 'left' ),
 234+ implode( ' ', $htmlTags )
 235+ );
 236+ }
 237+
 238+ /**
 239+ * @see SMWResultPrinter::getParameters
 240+ *
 241+ * @since 1.5.3
 242+ *
 243+ * @return array
 244+ */
 245+ public function getParameters() {
 246+ $params = parent::getParameters();
 247+
 248+ $params['includesubject'] = new Parameter( 'includesubject', Parameter::TYPE_BOOLEAN );
 249+ $params['includesubject']->setMessage( 'srf_paramdesc_includesubject' );
 250+ $params['includesubject']->setDefault( false );
 251+
 252+ $params['increase'] = new Parameter( 'increase' );
 253+ $params['increase']->setMessage( 'srf_paramdesc_increase' );
 254+ $params['increase']->addCriteria( new CriterionInArray( 'linear', 'log' ) );
 255+ $params['increase']->setDefault( 'log' );
 256+
 257+ $params['tagorder'] = new Parameter( 'tagorder' );
 258+ $params['tagorder']->setMessage( 'srf_paramdesc_tagorder' );
 259+ $params['tagorder']->addCriteria( new CriterionInArray( 'alphabetical', 'asc', 'desc', 'random', 'unchanged' ) );
 260+ $params['tagorder']->setDefault( 'desc' );
 261+
 262+ $params['mincount'] = new Parameter( 'mincount', Parameter::TYPE_INTEGER );
 263+ $params['mincount']->setMessage( 'srf_paramdesc_mincount' );
 264+ $params['mincount']->setDefault( 1 );
 265+
 266+ $params['maxtags'] = new Parameter( 'maxtags', Parameter::TYPE_INTEGER );
 267+ $params['maxtags']->setMessage( 'srf_paramdesc_maxtags' );
 268+ $params['maxtags']->setDefault( 1000 );
 269+
 270+ $params['minsize'] = new Parameter( 'minsize', Parameter::TYPE_INTEGER );
 271+ $params['minsize']->setMessage( 'srf_paramdesc_minsize' );
 272+ $params['minsize']->setDefault( 77 );
 273+
 274+ $params['maxsize'] = new Parameter( 'maxsize', Parameter::TYPE_INTEGER );
 275+ $params['maxsize']->setMessage( 'srf_paramdesc_maxsize' );
 276+ $params['maxsize']->setDefault( 242 );
 277+
 278+ return $params;
 279+ }
 280+
 281+}
Property changes on: trunk/extensions/SemanticResultFormats/ValueRank/SRF_ValueRank.php
___________________________________________________________________
Added: svn:eol-style
1282 + native
Property changes on: trunk/extensions/SemanticResultFormats/ValueRank
___________________________________________________________________
Added: bugtraq:number
2283 + true
Index: trunk/extensions/SemanticResultFormats/SRF_Messages.php
@@ -97,6 +97,9 @@
9898 'srf_paramdesc_maxsize' => 'The size of the biggest tags in percent (default: 177)',
9999 'srf_paramdesc_maxtags' => 'The maximum amount of tags in the cloud',
100100
 101+ // "valuerank" format
 102+ 'srf_printername_valuerank' => 'Value rank',
 103+
101104 // format "Array" and "Hash"
102105 'srf_printername_array' => 'Array',
103106 'srf_paramdesc_pagetitle' => 'Whether to show page titles as result entries or to hide them',

Follow-up revisions

RevisionCommit summaryAuthorDate
r100727following r100719 cleanup...dasch18:54, 25 October 2011

Comments

#Comment by Siebrand (talk | contribs)   19:43, 28 October 2011

Please add message documentation for the newly added messages. Thanks.

#Comment by P858snake (talk | contribs)   06:17, 10 December 2011

Is i18n really a FIXME issue when its not a cluster extension?

Status & tagging log