00001 <?php
00002
00012 class SRFOutlineItem {
00013
00014 var $mRow;
00015 var $mVals;
00016
00017 function __construct( $row ) {
00018 $this->mRow = $row;
00019 $this->mVals = array();
00020 }
00021
00022 function addFieldValue( $field_name, $field_val ) {
00023 if ( array_key_exists( $field_name, $this->mVals ) ) {
00024 $this->mVals[$field_name][] = $field_val;
00025 } else {
00026 $this->mVals[$field_name] = array( $field_val );
00027 }
00028 }
00029
00030 function getFieldValues( $field_name ) {
00031 if ( array_key_exists( $field_name, $this->mVals ) )
00032 return $this->mVals[$field_name];
00033 else {
00034 return array( wfMsg( 'srf_outline_novalue' ) );
00035 }
00036 }
00037 }
00038
00042 class SRFOutlineTree {
00043 var $mTree;
00044 var $mUnsortedItems;
00045
00046 function __construct( $items = array() ) {
00047 $this->mTree = array();
00048 $this->mUnsortedItems = $items;
00049 }
00050
00051 function addItem( $item ) {
00052 $this->mUnsortedItems[] = $item;
00053 }
00054
00055 function categorizeItem( $vals, $item ) {
00056 foreach ( $vals as $val ) {
00057 if ( array_key_exists( $val, $this->mTree ) ) {
00058 $this->mTree[$val]->mUnsortedItems[] = $item;
00059 } else {
00060 $this->mTree[$val] = new SRFOutlineTree( array( $item ) );
00061 }
00062 }
00063 }
00064
00065 function addProperty( $property ) {
00066 if ( count( $this->mUnsortedItems ) > 0 ) {
00067 foreach ( $this->mUnsortedItems as $item ) {
00068 $cur_vals = $item->getFieldValues( $property );
00069 $this->categorizeItem( $cur_vals, $item );
00070 }
00071 $this->mUnsortedItems = null;
00072 } else {
00073 foreach ( $this->mTree as $i => $node ) {
00074 $this->mTree[$i]->addProperty( $property );
00075 }
00076 }
00077 }
00078
00079 }
00080
00081 class SRFOutline extends SMWResultPrinter {
00082
00083 protected $mOutlineProperties = array();
00084 protected $mInnerFormat = '';
00085
00086 protected function handleParameters( array $params, $outputmode ) {
00087 parent::handleParameters( $params, $outputmode );
00088 $this->mOutlineProperties = $params['outlineproperties'];
00089 }
00090
00091 public function getName() {
00092 return wfMsg( 'srf_printername_outline' );
00093 }
00094
00098 function printItem( $item ) {
00099 $first_col = true;
00100 $found_values = false;
00101 $result = "";
00102 foreach ( $item->mRow as $orig_ra ) {
00103
00104 $realFunction = array( 'SMWQueryResult', 'getResults' );
00105 if ( is_callable( $realFunction ) ) {
00106
00107
00108 $ra = clone ( $orig_ra );
00109 } else {
00110
00111
00112 $ra = new SMWResultArray( $orig_ra->getContent(), $orig_ra->getPrintRequest() );
00113 }
00114 $val = $ra->getPrintRequest()->getText( SMW_OUTPUT_WIKI, null );
00115 if ( in_array( $val, $this->mOutlineProperties ) ) {
00116 continue;
00117 }
00118 $first_value = true;
00119 while ( ( $text = $ra->getNextText( SMW_OUTPUT_WIKI, $this->mLinker ) ) !== false ) {
00120 if ( !$first_col && !$found_values ) {
00121 $result .= ' (';
00122 $found_values = true;
00123 } elseif ( $found_values || !$first_value ) {
00124
00125 $result .= ', ';
00126 }
00127 if ( $first_value ) {
00128 $first_value = false;
00129 if ( $this->mShowHeaders && ( '' != $ra->getPrintRequest()->getLabel() ) ) {
00130 $result .= $ra->getPrintRequest()->getText( SMW_OUTPUT_WIKI, $this->mLinker ) . ' ';
00131 }
00132 }
00133 $result .= $text;
00134 }
00135 $first_col = false;
00136 }
00137 if ( $found_values ) $result .= ')';
00138 return $result;
00139 }
00140
00141 function printTree( $outline_tree, $level = 0 ) {
00142 $text = "";
00143 if ( ! is_null( $outline_tree->mUnsortedItems ) ) {
00144 $text .= "<ul>\n";
00145 foreach ( $outline_tree->mUnsortedItems as $item ) {
00146 $text .= "<li>{$this->printItem($item)}</li>\n";
00147 }
00148 $text .= "</ul>\n";
00149 }
00150 if ( $level > 0 ) $text .= "<ul>\n";
00151 $num_levels = count( $this->mOutlineProperties );
00152
00153 $font_level = $level;
00154 if ( $num_levels < 4 ) {
00155 $font_level += ( 4 - $num_levels );
00156 }
00157 if ( $font_level == 0 )
00158 $font_size = 'x-large';
00159 elseif ( $font_level == 1 )
00160 $font_size = 'large';
00161 elseif ( $font_level == 2 )
00162 $font_size = 'medium';
00163 else
00164 $font_size = 'small';
00165 if ( $font_level == 3 )
00166 $font_weight = 'bold';
00167 else
00168 $font_weight = 'regular';
00169 foreach ( $outline_tree->mTree as $key => $node ) {
00170 $text .= "<p style=\"font-size: $font_size; font-weight: $font_weight;\">$key</p>\n";
00171 $text .= $this->printTree( $node, $level + 1 );
00172 }
00173 if ( $level > 0 ) $text .= "</ul>\n";
00174 return $text;
00175 }
00176
00177 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00178 $print_fields = array();
00179 foreach ( $res->getPrintRequests() as $pr ) {
00180 $field_name = $pr->getText( $outputmode, $this->mLinker );
00181
00182
00183 if ( ! in_array( $field_name, $this->mOutlineProperties ) ) {
00184 $print_fields[] = $field_name;
00185 }
00186 }
00187
00188
00189
00190
00191 $outline_tree = new SRFOutlineTree();
00192 while ( $row = $res->getNext() ) {
00193 $item = new SRFOutlineItem( $row );
00194 foreach ( $row as $field ) {
00195 $first = true;
00196 $field_name = $field->getPrintRequest()->getText( SMW_OUTPUT_HTML );
00197 if ( in_array( $field_name, $this->mOutlineProperties ) ) {
00198 while ( ( $object = $field->getNextDataValue() ) !== false ) {
00199 $field_val = $object->getLongWikiText( $this->mLinker );
00200 $item->addFieldValue( $field_name, $field_val );
00201 }
00202 }
00203 }
00204 $outline_tree->addItem( $item );
00205 }
00206
00207
00208
00209 foreach ( $this->mOutlineProperties as $outline_prop ) {
00210 $outline_tree->addProperty( $outline_prop );
00211 }
00212 $result = $this->printTree( $outline_tree );
00213
00214
00215 if ( $this->linkFurtherResults( $res ) ) {
00216 $link = $res->getQueryLink();
00217 if ( $this->getSearchLabel( $outputmode ) ) {
00218 $link->setCaption( $this->getSearchLabel( $outputmode ) );
00219 }
00220 $link->setParameter( 'outline', 'format' );
00221 if ( array_key_exists( 'outlineproperties', $this->m_params ) ) {
00222 $link->setParameter( $this->m_params['outlineproperties'], 'outlineproperties' );
00223 }
00224 $result .= $link->getText( $outputmode, $this->mLinker ) . "\n";
00225 }
00226 return $result;
00227 }
00228
00229 public function getParameters() {
00230 $params = parent::getParameters();
00231
00232 $params['outlineproperties'] = new ListParameter( 'outlineproperties' );
00233 $params['outlineproperties']->setMessage( 'srf_paramdesc_outlineproperties' );
00234 $params['outlineproperties']->setDefault( array() );
00235 $params['outlineproperties']->addManipulations( new ParamManipulationFunctions( 'trim' ) );
00236
00237 return $params;
00238 }
00239
00240 }