r89769 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89768‎ | r89769 | r89770 >
Date:11:54, 9 June 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
ensure that very long DEFAULTSORTKEYs do not cause errors
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_ParseData.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/dataitems/SMW_DI_String.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_ParseData.php
@@ -351,7 +351,12 @@
352352 $sortkey = $parser->mDefaultSort ? $parser->mDefaultSort :
353353 str_replace( '_', ' ', self::getSMWData( $parser )->getSubject()->getDBkey() );
354354 $pskey = new SMWDIProperty( '_SKEY' );
355 - $sortkeyDi = new SMWDIString( $sortkey );
 355+ try {
 356+ $sortkeyDi = new SMWDIString( $sortkey );
 357+ } catch (SMWStringLengthException $e) { // cut it down to a reasonable length; no further bytes should be needed for sorting
 358+ $sortkey = substr( $sortkey, 0, $e->getMaxLength() );
 359+ $sortkeyDi = new SMWDIString( $sortkey );
 360+ }
356361 self::getSMWData( $parser )->addPropertyObjectValue( $pskey, $sortkeyDi );
357362
358363 return true;
Index: trunk/extensions/SemanticMediaWiki/includes/dataitems/SMW_DI_String.php
@@ -10,9 +10,20 @@
1111 * @since 1.6
1212 */
1313 class SMWStringLengthException extends SMWDataItemException {
14 - public function __construct( $string ) {
 14+ protected $m_maxlength;
 15+
 16+ public function __construct( $string, $maxlength ) {
1517 parent::__construct( 'String "' . mb_substr( $string, 0, 10 ) . '...' . mb_substr( $string, mb_strlen( $string ) - 10 ) . ' exceeds length limit for strings. Use SMWDIBlob for long strings.' );
 18+ $this->m_maxlength = $maxlength;
1619 }
 20+
 21+ /**
 22+ * Get the maximum length that the string is allowed to have. The length
 23+ * is counted with str_len, i.e. multibyte (UTF-8) strings are ignored.
 24+ */
 25+ public function getMaxLength() {
 26+ return $this->m_maxlength;
 27+ }
1728 }
1829
1930 /**
@@ -27,9 +38,17 @@
2839
2940 const MAXLENGTH = 255;
3041
 42+ /**
 43+ * Constructor.
 44+ *
 45+ * @throws SMWStringLengthException if the string is longer than
 46+ * SMWDIString::MAXLENGTH. The bytes are counted, not the (possibly
 47+ * multibyte) glyphs of UTF-8, since we care about byte length in (any)
 48+ * storage backend.
 49+ */
3150 public function __construct( $string ) {
3251 if ( strlen( $string ) > SMWDIString::MAXLENGTH ) {
33 - throw new SMWStringLengthException( $string );
 52+ throw new SMWStringLengthException( $string, SMWDIString::MAXLENGTH );
3453 }
3554 parent::__construct( $string );
3655 }