r95053 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95052‎ | r95053 | r95054 >
Date:23:13, 19 August 2011
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
rewrote most of the table QP and fixed numeric sorting JS
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/skins/SMW_sorttable.js (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php
@@ -13,6 +13,8 @@
1414 */
1515 class SMWTableResultPrinter extends SMWResultPrinter {
1616
 17+ protected $columnsWithSortKey = array();
 18+
1719 public function getName() {
1820 smwfLoadExtensionMessages( 'SemanticMediaWiki' );
1921 return wfMsg( 'smw_printername_' . $this->mFormat );
@@ -22,58 +24,39 @@
2325 global $smwgIQRunningNumber;
2426 SMWOutputs::requireHeadItem( SMW_HEADER_SORTTABLE );
2527
 28+ $tableRows = array();
 29+
 30+ while ( $record = $res->getNext() ) {
 31+ $tableRows[] = $this->getRowForRecord( $record, $outputmode );
 32+ }
 33+
2634 // print header
2735 $result = '<table class="smwtable"' .
2836 ( $this->mFormat == 'broadtable' ? ' width="100%"' : '' ) .
2937 " id=\"querytable$smwgIQRunningNumber\">\n";
3038
3139 if ( $this->mShowHeaders != SMW_HEADERS_HIDE ) { // building headers
32 - $result .= "\t<tr>\n";
 40+ $headers = array();
3341
3442 foreach ( $res->getPrintRequests() as $pr ) {
35 - $result .= "\t\t<th>" . $pr->getText( $outputmode, ( $this->mShowHeaders == SMW_HEADERS_PLAIN ? null:$this->mLinker ) ) . "</th>\n";
 43+ $attribs = array();
 44+
 45+ if ( array_key_exists( $pr->getHash(), $this->columnsWithSortKey ) ) {
 46+ $attribs['class'] = 'numericsort';
 47+ }
 48+
 49+ $headers[] = Html::rawElement(
 50+ 'th',
 51+ $attribs,
 52+ $pr->getText( $outputmode, ( $this->mShowHeaders == SMW_HEADERS_PLAIN ? null:$this->mLinker ) )
 53+ );
3654 }
3755
38 - $result .= "\t</tr>\n";
 56+ array_unshift( $tableRows, '<tr>' . implode( "\n", $headers ) . '</tr>' );
3957 }
4058
41 - // print all result rows
42 - while ( $row = $res->getNext() ) {
43 - $result .= "\t<tr>\n";
44 - $firstcol = true;
45 - $fieldcount = - 1;
46 - foreach ( $row as $field ) {
47 - $fieldcount = $fieldcount + 1;
48 -
49 - $result .= "\t\t<td";
50 - $alignment = trim( $field->getPrintRequest()->getParameter( 'align' ) );
51 - if ( ( $alignment == 'right' ) || ( $alignment == 'left' ) || ( $alignment == 'center' ) ) {
52 - $result .= ' style="text-align:' . $alignment . ';"';
53 - }
54 - $result .= ">";
55 -
56 - $first = true;
57 - while ( ( $dv = $field->getNextDataValue() ) !== false ) {
58 - if ( $first ) {
59 - $sortkey = $dv->getDataItem()->getSortKey();
60 - if ( is_numeric( $sortkey ) ) { // additional hidden sortkey for numeric entries
61 - $result .= '<span class="smwsortkey">' . $sortkey . '</span>';
62 - }
63 - $first = false;
64 - } else {
65 - $result .= '<br />';
66 - }
67 - // use shorter "LongText" for wikipage
68 - $result .= ( ( $dv->getTypeID() == '_wpg' ) || ( $dv->getTypeID() == '__sin' ) ) ?
69 - $dv->getLongText( $outputmode, $this->getLinker( $firstcol ) ) :
70 - $dv->getShortText( $outputmode, $this->getLinker( $firstcol ) );
71 - }
72 - $result .= "</td>\n";
73 - $firstcol = false;
74 - }
75 - $result .= "\t</tr>\n";
76 - }
77 -
 59+ $result .= implode( "\n", $tableRows );
 60+
7861 // print further results footer
7962 if ( $this->linkFurtherResults( $res ) ) {
8063 $link = $res->getQueryLink();
@@ -82,11 +65,67 @@
8366 }
8467 $result .= "\t<tr class=\"smwfooter\"><td class=\"sortbottom\" colspan=\"" . $res->getColumnCount() . '"> ' . $link->getText( $outputmode, $this->mLinker ) . "</td></tr>\n";
8568 }
 69+
8670 $result .= "</table>\n"; // print footer
8771 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed
 72+
8873 return $result;
8974 }
9075
 76+ protected function getRowForRecord( array /* of SMWResultArray */ $record, $outputmode ) {
 77+ $cells = array();
 78+
 79+ foreach ( $record as $field ) {
 80+ $cells[] = $this->getCellForPropVals( $field, $outputmode );
 81+ }
 82+
 83+ return "<tr>\n\t" . implode( "\n\t", $cells ) . "\n</tr>";
 84+ }
 85+
 86+ protected function getCellForPropVals( SMWResultArray $resultArray, $outputmode ) {
 87+ $attribs = array();
 88+
 89+ $alignment = trim( $resultArray->getPrintRequest()->getParameter( 'align' ) );
 90+
 91+ if ( in_array( $alignment, array( 'right', 'left', 'center' ) ) ) {
 92+ $attribs['style'] = "text-align:' . $alignment . ';";
 93+ }
 94+
 95+ return Html::rawElement(
 96+ 'td',
 97+ $attribs,
 98+ $this->getCellContent( $resultArray, $outputmode )
 99+ );
 100+ }
 101+
 102+ protected function getCellContent( SMWResultArray $resultArray, $outputmode ) {
 103+ $values = array();
 104+ $isFirst = true;
 105+
 106+ while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {
 107+ $sortKey = '';
 108+
 109+ if ( $isFirst ) {
 110+ $isFirst = false;
 111+ $sortkey = $dv->getDataItem()->getSortKey();
 112+
 113+ if ( is_numeric( $sortkey ) ) { // additional hidden sortkey for numeric entries
 114+ $this->columnsWithSortKey[$resultArray->getPrintRequest()->getHash()] = true;
 115+ $sortKey .= '<span class="smwsortkey">' . $sortkey . '</span>';
 116+ }
 117+ }
 118+
 119+ $isSubject = $resultArray->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS;
 120+ $value = ( ( $dv->getTypeID() == '_wpg' ) || ( $dv->getTypeID() == '__sin' ) ) ?
 121+ $dv->getLongText( $outputmode, $this->getLinker( $isSubject ) ) :
 122+ $dv->getShortText( $outputmode, $this->getLinker( $isSubject ) );
 123+
 124+ $values[] = $sortKey . $value;
 125+ }
 126+
 127+ return implode( '<br />', $values );
 128+ }
 129+
91130 public function getParameters() {
92131 return array_merge( parent::getParameters(), parent::textDisplayParameters() );
93132 }
Index: trunk/extensions/SemanticMediaWiki/skins/SMW_sorttable.js
@@ -126,17 +126,12 @@
127127 return;
128128 }
129129
130 - sortfn = smw_sort_caseinsensitive; // sorting w/o keys
131 - // check for sorting keys and change sorting function
132 - var itm = table.rows[1].cells[column];
133 - var spans = itm.getElementsByTagName( 'span' );
134 - if( spans.length > 0 ) {
135 - for ( var i = 0; i < spans.length; i++ ) {
136 - if( spans[i].className == 'smwsortkey' ) {
137 - sortfn = smw_sort_numeric; // sorting with keys
138 - }
139 - }
 130+ if ( table.rows[0].cells[column].className == 'numericsort' ) {
 131+ sortfn = smw_sort_numeric; // sorting with keys
140132 }
 133+ else {
 134+ sortfn = smw_sort_caseinsensitive;
 135+ }
141136
142137 SORT_COLUMN_INDEX = column;
143138 var firstRow = new Array();

Comments

#Comment by TheDJ (talk | contribs)   08:04, 20 August 2011

Hmm, SMW has it's own sortable.js ?

We should probably see if it can be 'merged' with the core sortable code now that that uses jquery. http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/resources/jquery/jquery.tablesorter.js

#Comment by Jeroen De Dauw (talk | contribs)   14:36, 20 August 2011

Yeah, it'd definitely be cool just to use the native MW code. I'll have a look at this soonish :)