r87814 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87813‎ | r87814 | r87815 >
Date:09:41, 10 May 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
validate property names that users provide as sortkeys in queries, and report errors appropriately
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Property.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
@@ -113,20 +113,19 @@
114114 $query->sort = true;
115115 $query->sortkeys = array();
116116
117 - foreach ( explode( ',', trim( $params['sort'] ) ) as $sort ) {
118 - $sort = smwfNormalTitleDBKey( trim( $sort ) ); // slight normalisation
119 - $order = current( $orders );
120 - if ( $order === false ) { // default
121 - $order = 'ASC';
122 - }
123 -
124 - if ( array_key_exists( $sort, $query->sortkeys ) ) {
125 - // maybe throw an error here?
 117+ foreach ( explode( ',', $params['sort'] ) as $sort ) {
 118+ $propertyValue = SMWPropertyValue::makeUserProperty( trim( $sort ) );
 119+ if ( $propertyValue->isValid() ) {
 120+ $sortkey = $propertyValue->getDataItem()->getKey();
 121+ $order = current( $orders );
 122+ if ( $order === false ) { // default
 123+ $order = 'ASC';
 124+ }
 125+ $query->sortkeys[$sortkey] = $order; // should we check for duplicate sort keys?
 126+ next( $orders );
126127 } else {
127 - $query->sortkeys[$sort] = $order;
 128+ $query->addErrors( $propertyValue->getErrors() );
128129 }
129 -
130 - next( $orders );
131130 }
132131
133132 if ( current( $orders ) !== false ) { // sort key remaining, apply to page name
Index: trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Property.php
@@ -115,18 +115,19 @@
116116 if ( $this->m_caption === false ) { // always use this as caption
117117 $this->m_caption = $value;
118118 }
119 - $value = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) ); // slightly normalise label
 119+ $propertyName = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) ); // slightly normalise label
120120 $inverse = false;
121 - if ( ( $value !== '' ) && ( $value { 0 } == '-' ) ) { // check if this property refers to an inverse
122 - $value = substr( $value, 1 );
 121+ if ( ( $propertyName !== '' ) && ( $propertyName{ 0 } == '-' ) ) { // property refers to an inverse
 122+ $propertyName = (string)substr( $value, 1 );
 123+ /// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false.
123124 $inverse = true;
124125 }
125126
126127 try {
127 - $this->m_dataitem = SMWDIProperty::newFromUserLabel( $value, $inverse, $this->m_typeid );
128 - } catch ( SMWDataItemException $e ) {
 128+ $this->m_dataitem = SMWDIProperty::newFromUserLabel( $propertyName, $inverse, $this->m_typeid );
 129+ } catch ( SMWDataItemException $e ) { // happens, e.g., when trying to sort queries by property "-"
129130 smwfLoadExtensionMessages( 'SemanticMediaWiki' );
130 - $this->addError( wfMsgForContent( 'smw_parseerror' ) ); // very rare to get an error here, don't bother with detailed reporting
 131+ $this->addError( wfMsgForContent( 'smw_noproperty', $value ) );
131132 $this->m_dataitem = new SMWDIProperty( 'ERROR', false, $this->m_typeid ); // just to have something
132133 }
133134 }
Index: trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php
@@ -101,6 +101,7 @@
102102 'smw_decseparator' => '.',
103103 'smw_kiloseparator' => ',',
104104 'smw_notitle' => '"$1" cannot be used as a page name in this wiki.',
 105+ 'smw_noproperty' => '"$1" cannot be used as a property name in this wiki.',
105106 'smw_wrong_namespace' => 'Only pages in namespace "$1" are allowed here.',
106107 'smw_unknowntype' => 'Unsupported type "$1" defined for property.',
107108 'smw_manytypes' => 'More than one type defined for property.',

Status & tagging log