00001 <?php
00002
00020 class SMWPageLister {
00021
00022 protected $mDiWikiPages;
00023 protected $mDiProperty;
00024 protected $mLimit;
00025 protected $mFrom;
00026 protected $mUntil;
00027
00037 public function __construct( array $diWikiPages, $diProperty, $limit, $from = '', $until = '' ) {
00038 $this->mDiWikiPages = $diWikiPages;
00039 $this->mDiProperty = $diProperty;
00040 $this->mLimit = $limit;
00041 $this->mFrom = $from;
00042 $this->mUntil = $until;
00043 }
00044
00056 public function getNavigationLinks( Title $title, $query = array() ) {
00057 global $wgLang;
00058
00059 $limitText = $wgLang->formatNum( $this->mLimit );
00060
00061 $resultCount = count( $this->mDiWikiPages );
00062 $beyondLimit = ( $resultCount > $this->mLimit );
00063
00064 if ( !is_null( $this->mUntil ) && $this->mUntil !== '' ) {
00065 if ( $beyondLimit ) {
00066 $first = smwfGetStore()->getWikiPageSortKey( $this->mDiWikiPages[1] );
00067 } else {
00068 $first = '';
00069 }
00070
00071 $last = $this->mUntil;
00072 } elseif ( $beyondLimit || ( !is_null( $this->mFrom ) && $this->mFrom !== '' ) ) {
00073 $first = $this->mFrom;
00074
00075 if ( $beyondLimit ) {
00076 $last = smwfGetStore()->getWikiPageSortKey( $this->mDiWikiPages[$resultCount - 1] );
00077 } else {
00078 $last = '';
00079 }
00080 } else {
00081 return '';
00082 }
00083
00084 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
00085 if ( $first !== '' ) {
00086 $prevLink = $this->makeSelfLink( $title, $prevLink, $query + array( 'until' => $first ) );
00087 }
00088
00089 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
00090 if ( $last !== '' ) {
00091 $nextLink = $this->makeSelfLink( $title, $nextLink, $query + array( 'from' => $last ) );
00092 }
00093
00094 return "($prevLink) ($nextLink)";
00095 }
00096
00102 protected function makeSelfLink( Title $title, $linkText, array $parameters ) {
00103 return smwfGetLinker()->link( $title, $linkText, array(), $parameters );
00104 }
00105
00117 public static function getRequestOptions( $limit, $from, $until ) {
00118 $options = new SMWRequestOptions();
00119 $options->limit = $limit + 1;
00120 $options->sort = true;
00121
00122 if ( $from !== '' ) {
00123 $options->boundary = $from;
00124 $options->ascending = true;
00125 $options->include_boundary = true;
00126 } elseif ( $until !== '' ) {
00127 $options->boundary = $until;
00128 $options->ascending = false;
00129 $options->include_boundary = false;
00130 }
00131
00132 return $options;
00133 }
00134
00147 public static function getQuery( SMWDescription $description, $limit, $from, $until ) {
00148 if ( $from !== '' ) {
00149 $diWikiPage = new SMWDIWikiPage( $from, NS_MAIN, '' );
00150 $fromDescription = new SMWValueDescription( $diWikiPage, null, SMW_CMP_GEQ );
00151 $queryDescription = new SMWConjunction( array( $description, $fromDescription ) );
00152 $order = 'ASC';
00153 } elseif ( $until !== '' ) {
00154 $diWikiPage = new SMWDIWikiPage( $until, NS_MAIN, '' );
00155 $untilDescription = new SMWValueDescription( $diWikiPage, null, SMW_CMP_LESS );
00156 $queryDescription = new SMWConjunction( array( $description, $untilDescription ) );
00157 $order = 'DESC';
00158 } else {
00159 $queryDescription = $description;
00160 $order = 'ASC';
00161 }
00162
00163 $queryDescription->addPrintRequest( new SMWPrintRequest( SMWPrintRequest::PRINT_THIS, '' ) );
00164
00165 $query = new SMWQuery( $queryDescription );
00166 $query->sortkeys[''] = $order;
00167 $query->setLimit( $limit + 1 );
00168
00169 return $query;
00170 }
00171
00179 public function formatList( $cutoff = 6 ) {
00180 $end = count( $this->mDiWikiPages );
00181 $start = 0;
00182 if ( $end > $this->mLimit ) {
00183 if ( $this->mFrom !== '' ) {
00184 $end -= 1;
00185 } else {
00186 $start += 1;
00187 }
00188 }
00189
00190 if ( count ( $this->mDiWikiPages ) > $cutoff ) {
00191 return self::getColumnList( $start, $end, $this->mDiWikiPages, $this->mDiProperty );
00192 } elseif ( count( $this->mDiWikiPages ) > 0 ) {
00193 return self::getShortList( $start, $end, $this->mDiWikiPages, $this->mDiProperty );
00194 } else {
00195 return '';
00196 }
00197 }
00198
00210 public static function getColumnList( $start, $end, array $diWikiPages, $diProperty ) {
00211 global $wgContLang;
00212
00213
00214 $chunk = (int) ( ( $end - $start + 1 ) / 3 );
00215
00216
00217 $r = '<table width="100%"><tr valign="top">';
00218
00219 $prevStartChar = 'none';
00220
00221
00222 for ( $startChunk = $start, $endChunk = $chunk, $chunkIndex = 0;
00223 $chunkIndex < 3;
00224 ++$chunkIndex, $startChunk = $endChunk, $endChunk += $chunk + 1 ) {
00225 $r .= "<td>\n";
00226 $atColumnTop = true;
00227
00228
00229 for ( $index = $startChunk ; $index < $endChunk && $index < $end; ++$index ) {
00230 $dataValue = SMWDataValueFactory::newDataItemValue( $diWikiPages[$index], $diProperty );
00231
00232 $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$index] );
00233 $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
00234
00235 if ( ( $index == $startChunk ) ||
00236 ( $startChar != $prevStartChar ) ) {
00237 if ( $atColumnTop ) {
00238 $atColumnTop = false;
00239 } else {
00240 $r .= "</ul>\n";
00241 }
00242
00243 if ( $startChar == $prevStartChar ) {
00244 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
00245 } else {
00246 $cont_msg = '';
00247 }
00248
00249 $r .= "<h3>" . htmlspecialchars( $startChar ) . $cont_msg . "</h3>\n<ul>";
00250
00251 $prevStartChar = $startChar;
00252 }
00253
00254 $r .= "<li>" . $dataValue->getLongHTMLText( smwfGetLinker() ) . "</li>\n";
00255 }
00256
00257 if ( !$atColumnTop ) {
00258 $r .= "</ul>\n";
00259 }
00260
00261 $r .= "</td>\n";
00262 }
00263
00264 $r .= '</tr></table>';
00265
00266 return $r;
00267 }
00268
00279 public static function getShortList( $start, $end, array $diWikiPages, $diProperty ) {
00280 global $wgContLang;
00281
00282 $startDv = SMWDataValueFactory::newDataItemValue( $diWikiPages[$start], $diProperty );
00283 $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$start] );
00284 $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
00285 $r = '<h3>' . htmlspecialchars( $startChar ) . "</h3>\n" .
00286 '<ul><li>' . $startDv->getLongHTMLText( smwfGetLinker() ) . '</li>';
00287
00288 $prevStartChar = $startChar;
00289 for ( $index = $start + 1; $index < $end; $index++ ) {
00290 $dataValue = SMWDataValueFactory::newDataItemValue( $diWikiPages[$index], $diProperty );
00291 $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$index] );
00292 $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
00293
00294 if ( $startChar != $prevStartChar ) {
00295 $r .= "</ul><h3>" . htmlspecialchars( $startChar ) . "</h3>\n<ul>";
00296 $prevStartChar = $startChar;
00297 }
00298
00299 $r .= '<li>' . $dataValue->getLongHTMLText( smwfGetLinker() ) . '</li>';
00300 }
00301
00302 $r .= '</ul>';
00303
00304 return $r;
00305 }
00306
00307 }