r84252 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84251‎ | r84252 | r84253 >
Date:18:01, 18 March 2011
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
apply patch from bug 26930
Modified paths:
  • /trunk/extensions/SemanticResultFormats/Array (added) (history)
  • /trunk/extensions/SemanticResultFormats/Array/SRF_Array.php (added) (history)
  • /trunk/extensions/SemanticResultFormats/SRF_Messages.php (modified) (history)
  • /trunk/extensions/SemanticResultFormats/SRF_Settings.php (modified) (history)
  • /trunk/extensions/SemanticResultFormats/SemanticResultFormats.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/SemanticResultFormats.php
@@ -63,6 +63,7 @@
6464 '[http://simia.net Denny Vrandecic]',
6565 'Hans-Jörg Happel',
6666 'Rowan Rodrik van der Molen',
 67+ '[http://www.mediawiki.org/wiki/User:Danwe Daniel Werner]',
6768 '[http://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]'
6869 ),
6970 'url' => 'http://www.mediawiki.org/wiki/Extension:Semantic_Result_Formats',
@@ -96,6 +97,8 @@
9798 $wgAutoloadClasses['SRFPloticusVBar'] = $formatDir . 'Ploticus/SRF_PloticusVBar.php';
9899 $wgAutoloadClasses['SRFGallery'] = $formatDir . 'Gallery/SRF_Gallery.php';
99100 $wgAutoloadClasses['SRFTagCloud'] = $formatDir . 'TagCloud/SRF_TagCloud.php';
 101+ $wgAutoloadClasses['SRFArray'] = $formatDir . 'Array/SRF_Array.php';
 102+ $wgAutoloadClasses['SRFHash'] = $formatDir . 'Array/SRF_Array.php';
100103
101104 $formatClasses = array(
102105 'timeline' => 'SRFTimeline',
@@ -119,6 +122,8 @@
120123 'ploticusvbar' => 'SRFPloticusVBar',
121124 'gallery' => 'SRFGallery',
122125 'tagcloud' => 'SRFTagCloud',
 126+ 'array' => 'SRFArray',
 127+ 'hash' => 'SRFHash',
123128 );
124129
125130 $formatAliases = array(
Index: trunk/extensions/SemanticResultFormats/SRF_Settings.php
@@ -25,3 +25,11 @@
2626
2727 # Used for jqplot formats.
2828 $srfgJQPlotIncluded = false;
 29+
 30+# Used for Array and Hash formats.
 31+# Allows value as string or object instances of Title or Article classes or an array
 32+# where index 0 is the page title and 1 is the namespace-index (by default NS_MAIN)
 33+$srfgArraySep = ', ';
 34+$srfgArrayPropSep = '<PROP>';
 35+$srfgArrayManySep = '<MANY>';
 36+$srfgArrayRecordSep = '<RCRD>';
Index: trunk/extensions/SemanticResultFormats/SRF_Messages.php
@@ -87,6 +87,17 @@
8888 'srf_paramdesc_minsize' => 'The size of the smallest tags in percent (default: 77)',
8989 'srf_paramdesc_maxsize' => 'The size of the biggest tags in percent (default: 177)',
9090 'srf_paramdesc_maxtags' => 'The maximum amount of tags in the cloud',
 91+
 92+ // format "Array" and "Hash"
 93+ 'srf_printername_array' => 'Array',
 94+ 'srf_paramdesc_pagetitle' => 'Whether to show page titles as result entries or to hide them',
 95+ 'srf_paramdesc_hidegaps' => 'Whether to show empty property and record values separated by separators or to hide them',
 96+ 'srf_paramdesc_arrayname' => 'If given and ArrayExtension is available this will create an array with the specified name',
 97+ 'srf_paramdesc_propsep' => 'Separator between the requested properties',
 98+ 'srf_paramdesc_manysep' => 'Separator between many valued properties',
 99+ 'srf_paramdesc_recordsep' => 'Separator between values of record properties',
 100+ 'srf_printername_hash' => 'Hash',
 101+ 'srf_paramdesc_hashname' => 'If given and the HashTables extension is available this will create an hash with the specified name',
91102 );
92103
93104 /** Message documentation (Message documentation)
@@ -474,6 +485,13 @@
475486 'srf_paramdesc_minsize' => 'Die Darstellungsgröße des kleinsten Schlagwortes in Prozent (Standard ist 77)',
476487 'srf_paramdesc_maxsize' => 'Die Darstellungsgröße des größten Schlagwortes in Prozent (Standard ist 177)',
477488 'srf_paramdesc_maxtags' => 'Die maximale Anzahl der Schlagworte in der Schlagwortwolke',
 489+ 'srf_paramdesc_pagetitle' => 'Legt fest ob Seitentitel in Auflistung mit aufgenommen werden soll',
 490+ 'srf_paramdesc_hidegaps' => 'Definiert ob auf einer Seite nicht vorhandene Werte einen leeren Listeneintrag erzeugen',
 491+ 'srf_paramdesc_arrayname' => 'Wenn die Erweiterung ArrayExtension vorhanden ist wird ein Array mit diesem Namen angelegt',
 492+ 'srf_paramdesc_propsep' => 'Trennzeichen zwischen angeforderten Attributen',
 493+ 'srf_paramdesc_manysep' => 'Trennzeichen zwischen mehreren Werten die für ein Attribut angegeben sind',
 494+ 'srf_paramdesc_recordsep' => 'Trennzeichen zwischen einzelnen Werten eines Verbund-Attributes',
 495+ 'srf_paramdesc_hashname' => 'Wenn die Erweiterung HashTables vorhanden ist wird ein Hash mit diesem Namen angelegt',
478496 );
479497
480498 /** Lower Sorbian (Dolnoserbski)
Index: trunk/extensions/SemanticResultFormats/Array/SRF_Array.php
@@ -0,0 +1,326 @@
 2+<?php
 3+/**
 4+ * Query format for arrays with features for Extensions ArrayExtension and HashTables
 5+ * @file
 6+ * @ingroup SemanticResultFormats
 7+ * @author Daniel Werner
 8+ */
 9+
 10+/**
 11+ * Array format
 12+ */
 13+if( !defined('MEDIAWIKI') ) die();
 14+
 15+class SRFArray extends SMWResultPrinter {
 16+ protected $mSep;
 17+ protected $mPropSep;
 18+ protected $mManySep;
 19+ protected $mRecordSep;
 20+ protected $mArrayName = null;
 21+ protected $mDeliverPageTitle = true;
 22+
 23+ protected $mHideRecordGaps = false;
 24+ protected $mHidePropertyGaps = false;
 25+
 26+ public function __construct( $format, $inline ) {
 27+ parent::__construct( $format, $inline );
 28+ //overwrite default behavior for linking:
 29+ $this->mLinkFirst = false;
 30+ $this->mLinkOthers = false;
 31+
 32+ //initialize user configuration from localsettings or load default values:
 33+ $this->initializeUserConfig();
 34+ }
 35+
 36+ protected function initializeUserConfig() {
 37+ global $srfgArraySep, $srfgArrayPropSep, $srfgArrayManySep, $srfgArrayRecordSep,
 38+ $srfgArraySepDefault, $srfgArrayPropSepDefault, $srfgArrayManySepDefault, $srfgArrayRecordSepDefault;
 39+
 40+ //Sep:
 41+ if( ! isset( $srfgArraySepDefault ) ) {
 42+ $srfgArraySepDefault = self::initializeDefaultSepText( $srfgArraySep );
 43+ } $this->mSep = $srfgArraySepDefault;
 44+ //PropSep:
 45+ if( ! isset( $srfgArrayPropSepDefault ) ) {
 46+ $srfgArrayPropSepDefault = self::initializeDefaultSepText( $srfgArrayPropSep );
 47+ } $this->mPropSep = $srfgArrayPropSepDefault;
 48+ //ManySep:
 49+ if( ! isset( $srfgArrayManySepDefault ) ) {
 50+ $srfgArrayManySepDefault = self::initializeDefaultSepText( $srfgArrayManySep );
 51+ } $this->mManySep = $srfgArrayManySepDefault;
 52+ //Sep:
 53+ if( ! isset( $srfgArrayRecordSepDefault ) ) {
 54+ $srfgArrayRecordSepDefault = self::initializeDefaultSepText( $srfgArrayRecordSep );
 55+ } $this->mRecordSep = $srfgArrayRecordSepDefault;
 56+
 57+ }
 58+
 59+ static function initializeDefaultSepText( $obj ) {
 60+ if( is_array( $obj ) ) {
 61+ if( ! array_key_exists( 0, $obj ) )
 62+ return '';
 63+ $obj = Title::newFromText( $obj[0], ( array_key_exists( 1, $obj ) ? $obj[1] : NS_MAIN ) );
 64+ }
 65+ if( $obj instanceof Title ) {
 66+ $article = new Article( $obj );
 67+ } elseif( $obj instanceof Article ) {
 68+ $article = obj;
 69+ } else {
 70+ return $obj; //only text
 71+ }
 72+ global $wgParser;
 73+ return trim( $wgParser->recursiveTagParse( $article->getRawText() ) ); //return rendered text from page
 74+ }
 75+
 76+ protected function readParameters( $params, $outputmode ) {
 77+ SMWResultPrinter::readParameters( $params, $outputmode );
 78+
 79+ //separators:
 80+ if( array_key_exists('sep', $params) ) $this->mSep = trim( $params['sep'] );
 81+ if( array_key_exists('propsep', $params) ) $this->mPropSep = trim( $params['propsep'] );
 82+ if( array_key_exists('manysep', $params) ) $this->mManySep = trim( $params['manysep'] );
 83+ if( array_key_exists('recordsep', $params) ) $this->mRecordSep = trim( $params['recordsep'] );
 84+
 85+ if( array_key_exists( 'name', $params ) )
 86+ $this->mArrayName = trim( $params['name'] );
 87+
 88+ if( array_key_exists( 'pagetitle', $params ) )
 89+ $this->mDeliverPageTitle = !( trim( strtolower( $params['pagetitle'] ) ) == 'hide' );
 90+
 91+ if( array_key_exists( 'hidegaps', $params ) ) {
 92+ switch( trim( strtolower( $params['hidegaps'] ) ) ) {
 93+ case 'none':
 94+ $this->mHideRecordGaps = false;
 95+ $this->mHidePropertyGaps = false;
 96+ break;
 97+ case 'all':
 98+ $this->mHideRecordGaps = true;
 99+ $this->mHidePropertyGaps = true;
 100+ break;
 101+ case 'property': case 'prop': case 'attribute': case 'attr':
 102+ $this->mHideRecordGaps = false;
 103+ $this->mHidePropertyGaps = true;
 104+ break;
 105+ case 'record': case 'rec': case 'n-ary': case 'nary':
 106+ $this->mHideRecordGaps = true;
 107+ $this->mHidePropertyGaps = false;
 108+ break;
 109+ }
 110+ }
 111+ }
 112+
 113+ public function getQueryMode($context) {
 114+ return SMWQuery::MODE_INSTANCES;
 115+ }
 116+
 117+ public function getName() {
 118+ wfLoadExtensionMessages('SemanticResultFormats');
 119+ return wfMsg('srf_printername_' . $this->mFormat);
 120+ }
 121+
 122+ protected function getResultText( $res, $outputmode ) {
 123+ /*
 124+ * @ToDo:
 125+ * labels of requested properties could define default values. Seems not possible at the moment because
 126+ * SMWPrintRequest::getLable() always returns the property name even if no specific label is defined.
 127+ */
 128+
 129+ $perPage_items = array();
 130+
 131+ //for each page:
 132+ while( $row = $res->getNext() ) {
 133+ $perProperty_items = array();
 134+ $isPageTitle = true; //first field is always the page title;
 135+
 136+ //for each property on that page:
 137+ foreach( $row as $field ) { // $row is array(), $field of type SMWResultArray
 138+ $manyValue_items = array();
 139+ $missingProperty = false;
 140+
 141+ $manyValues = $field->getContent();
 142+
 143+ //If property is not set (has no value) on a page:
 144+ if( count( $manyValues ) < 1 ) {
 145+ $delivery = $this->deliverMissingProperty( $field );
 146+ $manyValue_items = $this->fillDeliveryArray( $manyValue_items, $delivery );
 147+ $missingProperty = true;
 148+ } else
 149+ //otherwise collect property value (potentially many values)
 150+ foreach( $manyValues as $obj ) { // $manyValues of type SMWResultArray, contains many values (or just one) of one property of type SMWDataValue
 151+
 152+ $value_items = array();
 153+
 154+ if( $isPageTitle ) {
 155+ $isPageTitle = false;
 156+ if( ! $this->mDeliverPageTitle ) {
 157+ continue 2; //next property
 158+ }
 159+ $value_items = $this->fillDeliveryArray( $value_items, $this->deliverPageTitle( $obj, $this->mLinkFirst ) );
 160+ $isRecord = false;
 161+ } elseif( $obj instanceof SMWRecordValue ) {
 162+ $record = $obj->getDVs();
 163+ $recordLength = count( $obj->getTypeValues() );
 164+ for( $i = 0; $i < $recordLength; $i++ ) {
 165+ $recordField = $record[$i];
 166+ $value_items = $this->fillDeliveryArray( $value_items, $this->deliverRecordField( $recordField, $this->mLinkOthers ) );
 167+ }
 168+ $isRecord = true;
 169+ } else {
 170+ $value_items = $this->fillDeliveryArray( $value_items, $this->deliverSingleValue( $obj, $this->mLinkOthers ) );
 171+ $isRecord = false;
 172+ }
 173+ $delivery = $this->deliverSingleManyValuesData( $value_items, $isRecord );
 174+ $manyValue_items = $this->fillDeliveryArray( $manyValue_items, $delivery );
 175+ } // foreach...
 176+ $delivery = $this->deliverPropertiesManyValues( $manyValue_items, $missingProperty );
 177+ $perProperty_items = $this->fillDeliveryArray( $perProperty_items, $delivery );
 178+ } // foreach...
 179+ $delivery = $this->deliverPageProperties( $perProperty_items );
 180+ $perPage_items = $this->fillDeliveryArray( $perPage_items, $delivery );
 181+ } // while...
 182+
 183+ $output = $this->deliverQueryResultPages( $perPage_items );
 184+
 185+ return $output;
 186+ }
 187+
 188+ protected function fillDeliveryArray( $array = array(), $value = null ) {
 189+ if( ! is_null( $value ) ) { //don't create any empty entries
 190+ $array[] = $value;
 191+ }
 192+ return $array;
 193+ }
 194+
 195+ protected function deliverPageTitle( $value, $link = false ) {
 196+ return $this->deliverSingleValue( $value, $link );
 197+ }
 198+ protected function deliverRecordField( $value, $link = false ) {
 199+ if( $value !== null ) //void value (null)
 200+ return $this->deliverSingleValue( $value, $link );
 201+ elseif( $this->mHideRecordGaps )
 202+ return null; //hide empty entry
 203+ else
 204+ return ''; //empty string will make sure that array separator will be generated (for record separators)
 205+ }
 206+ protected function deliverSingleValue( $value, $link = false ) {
 207+ return trim( Sanitizer::decodeCharReferences( $value->getShortWikiText( $link ) ) ); // decode: better for further processing with array extension
 208+ }
 209+ // Property not declared on a page:
 210+ protected function deliverMissingProperty( $field ) {
 211+ if( $this->mHidePropertyGaps )
 212+ return null;
 213+ else
 214+ return ''; //empty string will make sure that array separator will be generated
 215+ //@ToDo: System for Default values...
 216+ }
 217+ //represented by an array of record fields or just a single array value:
 218+ protected function deliverSingleManyValuesData( $value_items, $containsRecord = false ) {
 219+ if( count( $value_items ) < 1 ) //happens when one of the higher functions delivers null
 220+ return null;
 221+ return implode( $this->mRecordSep, $value_items );
 222+ }
 223+ protected function deliverPropertiesManyValues( $manyValue_items, $propertyIsMissing = false ) {
 224+ if( count( $manyValue_items ) < 1 )
 225+ return null;
 226+ return implode( $this->mManySep, $manyValue_items );
 227+ }
 228+ protected function deliverPageProperties( $perProperty_items ) {
 229+ if( count( $perProperty_items ) < 1 )
 230+ return null;
 231+ return implode( $this->mPropSep, $perProperty_items );
 232+ }
 233+ protected function deliverQueryResultPages( $perPage_items ) {
 234+ if( $this->mArrayName !== null ) {
 235+ $this->createArray( $perPage_items ); //create Array
 236+ return '';
 237+ } else {
 238+ return implode( $this->mSep, $perPage_items );
 239+ }
 240+ }
 241+
 242+ protected function createArray( $arr ) {
 243+ global $wgArrayExtension;
 244+ if( ! isset( $wgArrayExtension ) ) //Hash extension is not installed in this wiki
 245+ return false;
 246+
 247+ $arrExtClass = new ReflectionClass( get_class( $wgArrayExtension ) );
 248+
 249+ if( $arrExtClass->hasConstant( 'VERSION' ) && version_compare( $wgArrayExtension::VERSION, '1.3.2', '>=' ) ) {
 250+ $wgArrayExtension->createArray( $this->mArrayName, $arr ); //requires Extension:ArrayExtension 1.3.2 or higher
 251+ } else {
 252+ $wgArrayExtension->mArrayExtension[ $this->mArrayName ] = $arr; //dirty way
 253+ }
 254+ return true;
 255+ }
 256+
 257+ public function getParameters() {
 258+ return array (
 259+ array( 'name' => 'limit', 'type' => 'int', 'description' => wfMsg( 'smw_paramdesc_limit' ) ),
 260+
 261+ array( 'name' => 'link', 'type' => 'enumeration', 'description' => wfMsg( 'smw_paramdesc_link' ), 'values' => array( 'all', 'subject', 'none' ) ),
 262+ array( 'name' => 'pagetitle', 'type' => 'enumeration', 'description' => wfMsg( 'srf_paramdesc_pagetitle' ), 'values' => array( 'show', 'hide' ) ),
 263+ array( 'name' => 'hidegaps', 'type' => 'enumeration', 'description' => wfMsg( 'srf_paramdesc_hidegaps' ), 'values' => array( 'none', 'all', 'property', 'record' ) ),
 264+
 265+ array( 'name' => 'name', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_arrayname' ) ),
 266+ array( 'name' => 'sep', 'type' => 'string', 'description' => wfMsg( 'smw_paramdesc_sep' ) ),
 267+ array( 'name' => 'propsep', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_propsep' ) ),
 268+ array( 'name' => 'manysep', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_manysep' ) ),
 269+ array( 'name' => 'recordsep', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_recordsep' ) ),
 270+ );
 271+ }
 272+}
 273+
 274+
 275+class SRFHash extends SRFArray {
 276+ protected $mLastPageTitle;
 277+
 278+ protected function readParameters( $params, $outputmode ) {
 279+ parent::readParameters( $params, $outputmode );
 280+ //if( array_key_exists('sep', $params) ) $this->mSep = trim( $params['sep'] );
 281+ $this->mDeliverPageTitle = true;
 282+ }
 283+ protected function deliverPageTitle( $value ) {
 284+ $this->mLastPageTitle = $this->deliverSingleValue( $value, false ); //remember the page title
 285+ return null; //don't add page title into property list
 286+ }
 287+ protected function deliverPageProperties( $perProperty_items ) {
 288+ if( count( $perProperty_items ) < 1 )
 289+ return null;
 290+ return array( $this->mLastPageTitle, implode( $this->mPropSep, $perProperty_items ) );
 291+ }
 292+ protected function deliverQueryResultPages( $perPage_items ) {
 293+ foreach( $perPage_items as $page ) {
 294+ $hash[ $page[0] ] = $page[1]; //name of page as key, Properties as value
 295+ }
 296+ return parent::deliverQueryResultPages( $hash );
 297+ }
 298+ protected function createArray( $hash ) {
 299+ global $wgHashTables;
 300+ if( ! isset( $wgHashTables ) ) //Hash extension is not installed in this wiki
 301+ return false;
 302+
 303+ $hashExtClass = new ReflectionClass( get_class( $wgHashTables ) );
 304+
 305+ if( $hashExtClass->hasConstant( 'VERSION' ) && version_compare( $wgHashTables::VERSION, '0.6', '>=' ) ) {
 306+ $wgHashTables->createHash( $this->mArrayName, $hash ); //requires Extension:HashTables 0.6 or higher
 307+ } else {
 308+ $wgHashTables->mHashTables[ $this->mArrayName ] = $hash; //dirty way
 309+ }
 310+ return true;
 311+ }
 312+
 313+ public function getParameters() {
 314+ return array (
 315+ array( 'name' => 'limit', 'type' => 'int', 'description' => wfMsg( 'smw_paramdesc_limit' ) ),
 316+
 317+ array( 'name' => 'link', 'type' => 'enumeration', 'description' => wfMsg( 'smw_paramdesc_link' ), 'values' => array( 'all', 'subject', 'none' ) ),
 318+ array( 'name' => 'hidegaps', 'type' => 'enumeration', 'description' => wfMsg( 'srf_paramdesc_hidegaps' ), 'values' => array( 'none', 'all', 'property', 'record' ) ),
 319+
 320+ array( 'name' => 'name', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_hashname' ) ),
 321+ array( 'name' => 'sep', 'type' => 'string', 'description' => wfMsg( 'smw_paramdesc_sep' ) ),
 322+ array( 'name' => 'propsep', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_propsep' ) ),
 323+ array( 'name' => 'manysep', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_manysep' ) ),
 324+ array( 'name' => 'recordsep', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_recordsep' ) ),
 325+ );
 326+ }
 327+}
Property changes on: trunk/extensions/SemanticResultFormats/Array/SRF_Array.php
___________________________________________________________________
Added: svn:eol-style
1328 + native

Comments

#Comment by Siebrand (talk | contribs)   19:29, 18 March 2011

Can you please provide a more informative commit message next time? I do not mind the bug reference, I do mind that I have to read the code changes or the bug to know what you changed.

#Comment by Jeroen De Dauw (talk | contribs)   19:43, 18 March 2011

The description is on bugzilla, this is not a bugfix, but a patch that adds entirely new functionality

#Comment by Siebrand (talk | contribs)   19:53, 18 March 2011

And that means that a reference to whatever may be in that bug cannot be summarised in your commit message? That sounds a bit too easy to me.

Status & tagging log