http://www.mediawiki.org/wiki/Special:Code/MediaWiki/100719
Revision: 100719
Author: dasch
Date: 2011-10-25 18:11:46 +0000 (Tue, 25 Oct 2011)
Log Message:
-----------
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
trunk/extensions/SemanticResultFormats/SemanticResultFormats.php
Added Paths:
-----------
trunk/extensions/SemanticResultFormats/ValueRank/
trunk/extensions/SemanticResultFormats/ValueRank/SRF_ValueRank.php
Modified: trunk/extensions/SemanticResultFormats/SRF_Messages.php
===================================================================
--- trunk/extensions/SemanticResultFormats/SRF_Messages.php 2011-10-25
18:04:07 UTC (rev 100718)
+++ trunk/extensions/SemanticResultFormats/SRF_Messages.php 2011-10-25
18:11:46 UTC (rev 100719)
@@ -97,6 +97,9 @@
'srf_paramdesc_maxsize' => 'The size of the biggest tags in percent
(default: 177)',
'srf_paramdesc_maxtags' => 'The maximum amount of tags in the cloud',
+ // "valuerank" format
+ 'srf_printername_valuerank' => 'Value rank',
+
// format "Array" and "Hash"
'srf_printername_array' => 'Array',
'srf_paramdesc_pagetitle' => 'Whether to show page titles as result
entries or to hide them',
Modified: trunk/extensions/SemanticResultFormats/SemanticResultFormats.php
===================================================================
--- trunk/extensions/SemanticResultFormats/SemanticResultFormats.php
2011-10-25 18:04:07 UTC (rev 100718)
+++ trunk/extensions/SemanticResultFormats/SemanticResultFormats.php
2011-10-25 18:11:46 UTC (rev 100719)
@@ -97,6 +97,7 @@
$wgAutoloadClasses['SRFTagCloud'] = $formatDir .
'TagCloud/SRF_TagCloud.php';
$wgAutoloadClasses['SRFArray'] = $formatDir . 'Array/SRF_Array.php';
$wgAutoloadClasses['SRFHash'] = $formatDir . 'Array/SRF_Array.php';
+ $wgAutoloadClasses['SRFValueRank'] = $formatDir .
'ValueRank/SRF_ValueRank.php';
$formatClasses = array(
'timeline' => 'SRFTimeline',
@@ -122,12 +123,14 @@
'ploticusvbar' => 'SRFPloticusVBar',
'gallery' => 'SRFGallery',
'tagcloud' => 'SRFTagCloud',
+ 'valuerank' => 'SRFValueRank',
'array' => 'SRFArray',
'hash' => 'SRFHash',
);
$formatAliases = array(
- 'tagcloud' => array( 'tag cloud' )
+ 'tagcloud' => array( 'tag cloud' ),
+ 'valuerank' => array( 'value rank' )
);
foreach ( $srfgFormats as $format ) {
Property changes on: trunk/extensions/SemanticResultFormats/ValueRank
___________________________________________________________________
Added: bugtraq:number
+ true
Added: trunk/extensions/SemanticResultFormats/ValueRank/SRF_ValueRank.php
===================================================================
--- trunk/extensions/SemanticResultFormats/ValueRank/SRF_ValueRank.php
(rev 0)
+++ trunk/extensions/SemanticResultFormats/ValueRank/SRF_ValueRank.php
2011-10-25 18:11:46 UTC (rev 100719)
@@ -0,0 +1,280 @@
+<?php
+
+/**
+ * Result printer that prints query results as a valuerank.
+ *
+ * @since 1.7
+ *
+ * @file SRF_ValueRank.php
+ * @ingroup SemanticResultFormats
+ *
+ * @licence GNU GPL v3
+ * @author DaSch < [email protected] >
+ * build out of Tag Cloud Format
+ */
+class SRFValueRank extends SMWResultPrinter {
+
+ protected $includeName;
+ protected $sizeMode;
+ protected $tagOrder;
+ protected $minCount;
+ protected $maxSize;
+ protected $maxTags;
+ protected $minTagSize;
+
+ protected $tagsHtml = array();
+
+ public function __construct( $format, $inline, $useValidator = true ) {
+ parent::__construct( $format, $inline );
+ $this->useValidator = $useValidator;
+ }
+
+ public function getName() {
+ return wfMsg( 'srf_printername_valuerank' );
+ }
+
+ /**
+ * @see SMWResultPrinter::handleParameters
+ *
+ * @since 1.7
+ *
+ * @param array $params
+ * @param $outputmode
+ */
+ protected function handleParameters( array $params, $outputmode ) {
+ parent::handleParameters( $params, $outputmode );
+
+ $this->includeName = $params['includesubject'];
+ $this->sizeMode = $params['increase'];
+ $this->tagOrder = $params['tagorder'];
+ $this->minCount = $params['mincount'];
+ $this->maxTags = $params['maxtags'];
+ $this->minTagSize = $params['minsize'];
+ $this->maxSize = $params['maxsize'];
+ }
+
+ public function getResultText( SMWQueryResult $results, $outputmode ) {
+ $this->isHTML = $outputmode == SMW_OUTPUT_HTML;
+ return $this->getValueRank( $this->getRank( $this->getTags(
$results, $outputmode ) ) );
+ }
+
+ /**
+ * Returns an array with the tags (keys) and the number of times they
occur (values).
+ *
+ * @since 1.5.3
+ *
+ * @param SMWQueryResult $results
+ * @param $outputmode
+ *
+ * @return array
+ */
+ protected function getTags( SMWQueryResult $results, $outputmode ) {
+ $tags = array();
+
+ while ( /* array of SMWResultArray */ $row =
$results->getNext() ) { // Objects (pages)
+ for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) { //
SMWResultArray for a sinlge property
+ while ( ( /* SMWDataValue */ $dataValue =
efSRFGetNextDV( $row[$i] ) ) !== false ) { // Data values
+
+ $isSubject =
$row[$i]->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS;
+
+ // If the main object should not be
included, skip it.
+ if ( $i == 0 && !$this->includeName &&
$isSubject ) {
+ continue;
+ }
+
+ // Get the HTML for the tag content.
Pages are linked, other stuff is just plaintext.
+ if ( $dataValue->getTypeID() == '_wpg'
) {
+ $value =
$dataValue->getTitle()->getText();
+ $html =
$dataValue->getLongText( $outputmode, $this->getLinker( $isSubject ) );
+ }
+ else {
+ $html =
$dataValue->getShortText( $outputmode, $this->getLinker( false ) );
+ $value = $html;
+ }
+
+ if ( !array_key_exists( $value, $tags )
) {
+ $tags[$value] = 0;
+ $this->tagsHtml[$value] =
$html; // Store the HTML separetely, so sorting can be done easily.
+ }
+
+ $tags[$value]++;
+ }
+ }
+ }
+
+ foreach ( $tags as $name => $count ) {
+ if ( $count < $this->minCount ) {
+ unset( $tags[$name] );
+ }
+ }
+
+ return $tags;
+ }
+
+ /**
+ * Determines the sizes of tags.
+ * This method is based on code from the FolkTagCloud extension by
Katharina Wäschle.
+ *
+ * @since 1.5.3
+ *
+ * @param array $tags
+ *
+ * @return array
+ */
+ protected function getRank( array $tags ) {
+ if ( count( $tags ) == 0 ) {
+ return $tags;
+ }
+
+ // If the original order needs to be kept, we need a copy of
the current order.
+ if ( $this->tagOrder == 'unchanged' ) {
+ $unchangedTags = array_keys( $tags );
+ }
+
+ arsort( $tags, SORT_NUMERIC );
+
+ if ( count( $tags ) > $this->maxTags ) {
+ $tags = array_slice( $tags, 0, $this->maxTags, true );
+ }
+
+ $min = end( $tags ) or $min = 0;
+ $max = reset( $tags ) or $max = 1;
+ $maxSizeIncrease = $this->maxSize - $this->minTagSize;
+
+ // Loop over the tags, and replace their count by a size.
+ /*
+ foreach ( $tags as &$tag ) {
+ switch ( $this->sizeMode ) {
+ case 'linear':
+ $divisor = ($max == $min) ? 1 : $max -
$min;
+ $tag = $this->minTagSize +
$maxSizeIncrease * ( $tag -$min ) / $divisor;
+ break;
+ case 'log' : default :
+ $divisor = ($max == $min) ? 1 : log(
$max ) - log( $min );
+ $tag = $this->minTagSize +
$maxSizeIncrease * ( log( $tag ) - log( $min ) ) / $divisor ;
+ break;
+ }
+ }
+ */
+
+ switch ( $this->tagOrder ) {
+ case 'desc' :
+ // Tags are already sorted desc
+ break;
+ case 'asc' :
+ asort( $tags );
+ break;
+ case 'alphabetical' :
+ $tagNames = array_keys( $tags );
+ natcasesort( $tagNames );
+ $newTags = array();
+
+ foreach ( $tagNames as $name ) {
+ $newTags[$name] = $tags[$name];
+ }
+
+ $tags = $newTags;
+ break;
+ case 'random' :
+ $tagSizes = $tags;
+ shuffle( $tagSizes );
+ $newTags = array();
+
+ foreach ( $tagSizes as $size ) {
+ foreach ( $tags as $tagName => $tagSize
) {
+ if ( $tagSize == $size ) {
+ $newTags[$tagName] =
$tags[$tagName];
+ break;
+ }
+ }
+ }
+
+ $tags = $newTags;
+ break;
+ case 'unchanged' : default : // Restore the original
order.
+ $changedTags = $tags;
+ $tags = array();
+
+ foreach ( $unchangedTags as $name ) {
+ // Original tags might have been left
out at this point, so only add remaining ones.
+ if ( array_key_exists( $name,
$changedTags ) ) {
+ $tags[$name] =
$changedTags[$name];
+ }
+ }
+ break;
+ }
+
+ return $tags;
+ }
+
+ /**
+ * Returns the HTML for the tag cloud.
+ *
+ * @since 1.5.3
+ *
+ * @param array $tags
+ *
+ * @return string
+ */
+ protected function getValueRank( array $tags ) {
+ $htmlTags = array();
+
+ foreach ( $tags as $name => $size ) {
+ $htmlTags[] = Html::rawElement(
+ 'li',
+ array( 'style' => "font-size:100%" ),
+ $this->tagsHtml[$name . '(' . $size . ')']
+ );
+ }
+
+ return Html::rawElement(
+ 'ul',
+ array( 'align' => 'left' ),
+ implode( ' ', $htmlTags )
+ );
+ }
+
+ /**
+ * @see SMWResultPrinter::getParameters
+ *
+ * @since 1.5.3
+ *
+ * @return array
+ */
+ public function getParameters() {
+ $params = parent::getParameters();
+
+ $params['includesubject'] = new Parameter( 'includesubject',
Parameter::TYPE_BOOLEAN );
+ $params['includesubject']->setMessage(
'srf_paramdesc_includesubject' );
+ $params['includesubject']->setDefault( false );
+
+ $params['increase'] = new Parameter( 'increase' );
+ $params['increase']->setMessage( 'srf_paramdesc_increase' );
+ $params['increase']->addCriteria( new CriterionInArray(
'linear', 'log' ) );
+ $params['increase']->setDefault( 'log' );
+
+ $params['tagorder'] = new Parameter( 'tagorder' );
+ $params['tagorder']->setMessage( 'srf_paramdesc_tagorder' );
+ $params['tagorder']->addCriteria( new CriterionInArray(
'alphabetical', 'asc', 'desc', 'random', 'unchanged' ) );
+ $params['tagorder']->setDefault( 'desc' );
+
+ $params['mincount'] = new Parameter( 'mincount',
Parameter::TYPE_INTEGER );
+ $params['mincount']->setMessage( 'srf_paramdesc_mincount' );
+ $params['mincount']->setDefault( 1 );
+
+ $params['maxtags'] = new Parameter( 'maxtags',
Parameter::TYPE_INTEGER );
+ $params['maxtags']->setMessage( 'srf_paramdesc_maxtags' );
+ $params['maxtags']->setDefault( 1000 );
+
+ $params['minsize'] = new Parameter( 'minsize',
Parameter::TYPE_INTEGER );
+ $params['minsize']->setMessage( 'srf_paramdesc_minsize' );
+ $params['minsize']->setDefault( 77 );
+
+ $params['maxsize'] = new Parameter( 'maxsize',
Parameter::TYPE_INTEGER );
+ $params['maxsize']->setMessage( 'srf_paramdesc_maxsize' );
+ $params['maxsize']->setDefault( 242 );
+
+ return $params;
+ }
+
+}
Property changes on:
trunk/extensions/SemanticResultFormats/ValueRank/SRF_ValueRank.php
___________________________________________________________________
Added: svn:eol-style
+ native
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs