http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88525

Revision: 88525
Author:   mkroetzsch
Date:     2011-05-21 18:16:22 +0000 (Sat, 21 May 2011)
Log Message:
-----------
cleaned up and modularized code for showing lists of wiki pages, so that this 
code can also be used beyond articlepages;
type pages no longer functional -- their functionality will soon move to 
Special:Types

Modified Paths:
--------------
    trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
    trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_ConceptPage.php
    
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_OrderedListPage.php
    
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_PropertyPage.php

Added Paths:
-----------
    trunk/extensions/SemanticMediaWiki/includes/SMW_PageLister.php

Added: trunk/extensions/SemanticMediaWiki/includes/SMW_PageLister.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/SMW_PageLister.php              
                (rev 0)
+++ trunk/extensions/SemanticMediaWiki/includes/SMW_PageLister.php      
2011-05-21 18:16:22 UTC (rev 88525)
@@ -0,0 +1,312 @@
+<?php
+
+/**
+ * Helper class to generate HTML lists of wiki pages, with support for paged
+ * navigation using the from/until and limit settings as in MediaWiki's
+ * CategoryPage.
+ *
+ * The class attempts to allow as much code as possible to be shared among
+ * different places where similar lists are used.
+ *
+ * Some code adapted from CategoryPage.php
+ * 
+ * @file SMW_OrderedListPage.php 
+ * @ingroup SMW
+ * 
+ * @author Nikolas Iwan
+ * @author Markus Krötzsch
+ * @author Jeroen De Dauw
+ */
+class SMWPageLister {
+
+       protected $mDiWikiPages;
+       protected $mDiProperty;
+       protected $mSkin;
+       protected $mLimit;
+       protected $mFrom;
+       protected $mUntil;
+
+       /**
+        * Constructor
+        *
+        * @param $diWikiPages array of SMWDIWikiPage
+        * @param $diProperty mixed SMWDIProperty that the wikipages are values 
of, or null
+        * @param $skin Skin object to use for making links
+        * @param $limit integer maximal amount of items to display
+        * @param $from string if the results were selected starting from this 
string
+        * @param $until string if the results were selected reaching until 
this string
+        */
+       public function __construct( array $diWikiPages, $diProperty, $skin, 
$limit, $from = '', $until = '' ) {
+               $this->mDiWikiPages = $diWikiPages;
+               $this->mDiProperty = $diProperty;
+               $this->mSkin = $skin;
+               $this->mLimit = $limit;
+               $this->mFrom = $from;
+               $this->mUntil = $until;
+       }
+
+       /**
+        * Generates the prev/next link part to the HTML code of the top and
+        * bottom section of the page. Whether and how these links appear
+        * depends on specified boundaries, limit, and results. The title is
+        * required to create a link to the right page. The query array gives
+        * optional further parameters to append to all navigation links.
+        *
+        * @param $title Title
+        * @param $query array that associates parameter names to parameter 
values
+        * @return string
+        */
+       public function getNavigationLinks( Title $title, $query = array() ) {
+               global $wgLang;
+
+               $limitText = $wgLang->formatNum( $this->mLimit );
+
+               $resultCount = count( $this->mDiWikiPages );
+               $beyondLimit = ( $resultCount > $this->mLimit );
+
+               if ( $this->mUntil != '' ) {
+                       if ( $beyondLimit ) {
+                               $first = smwfGetStore()->getWikiPageSortKey( 
$this->mDiWikiPages[1] );
+                       } else {
+                               $first = '';
+                       }
+
+                       $last = $this->mUntil;
+               } elseif ( $beyondLimit || ( $this->mFrom != '' ) ) {
+                       $first = $this->mFrom;
+
+                       if ( $beyondLimit ) {
+                               $last = smwfGetStore()->getWikiPageSortKey( 
$this->mDiWikiPages[$resultCount - 1] );
+                       } else {
+                               $last = '';
+                       }
+               } else {
+                       return '';
+               }
+
+               $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
+               if ( $first != '' ) {
+                       $prevLink = $this->makeSelfLink( $title, $prevLink, 
$query + array( 'until' => $first ) );
+               }
+
+               $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
+               if ( $last != '' ) {
+                       $nextLink = $this->makeSelfLink( $title, $nextLink, 
$query + array( 'from' => $last ) );
+               }
+
+               return "($prevLink) ($nextLink)";
+       }
+
+       /**
+        * Format an HTML link with the given text and parameters.
+        * 
+        * @return string
+        */
+       protected function makeSelfLink( Title $title, $linkText, array 
$parameters ) {
+               return $this->mSkin->makeLinkObj( $title, $linkText, 
wfArrayToCGI( $parameters ) );
+       }
+
+       /**
+        * Make SMWRequestOptions suitable for obtaining a list of results for
+        * the given limit, and from or until string. One more result than the
+        * limit will be created, and the results may have to be reversed in
+        * order if ascending is set to false in the resulting object.
+        *
+        * @param $limit integer
+        * @param $from string can be empty if no from condition is desired
+        * @param $until string can be empty if no until condition is desired
+        * @return SMWRequestOptions
+        */
+       public static function getRequestOptions( $limit, $from, $until ) {
+               $options = new SMWRequestOptions();
+               $options->limit = $limit + 1;
+               $options->sort = true;
+
+               if ( $from != '' ) {
+                       $options->boundary = $from;
+                       $options->ascending = true;
+                       $options->include_boundary = true;
+               } elseif ( $until != '' ) {
+                       $options->boundary = $until;
+                       $options->ascending = false;
+                       $options->include_boundary = false;
+               }
+
+               return $options;
+       }
+
+       /**
+        * Make SMWQuery suitable for obtaining a list of results based on the
+        * given description, limit, and from or until string. One more result
+        * than the limit will be created, and the results may have to be
+        * reversed in order if $until is nonempty.
+        *
+        * @param $description SMWDescription main query description
+        * @param $limit integer
+        * @param $from string can be empty if no from condition is desired
+        * @param $until string can be empty if no until condition is desired
+        * @return SMWQuery
+        */
+       public static function getQuery( SMWDescription $description, $limit, 
$from, $until ) {
+               if ( $from != '' ) {
+                       $diWikiPage = new SMWDIWikiPage( $from, NS_MAIN, '' ); 
// make a dummy wiki page as boundary
+                       $fromDescription = new SMWValueDescription( 
$diWikiPage, null, SMW_CMP_GEQ );
+                       $queryDescription = new SMWConjunction( array( 
$description, $fromDescription ) );
+                       $order = 'ASC';
+               } elseif ( $until != '' ) {
+                       $diWikiPage = new SMWDIWikiPage( $until, NS_MAIN, '' ); 
// make a dummy wiki page as boundary
+                       $untilDescription = new SMWValueDescription( 
$diWikiPage, null, SMW_CMP_LESS ); // do not include boundary in this case
+                       $queryDescription = new SMWConjunction( array( 
$description, $untilDescription ) );
+                       $order = 'DESC';
+               } else {
+                       $queryDescription = $description;
+                       $order = 'ASC';
+               }
+
+               $queryDescription->addPrintRequest( new SMWPrintRequest( 
SMWPrintRequest::PRINT_THIS, '' ) );
+
+               $query = new SMWQuery( $queryDescription );
+               $query->sortkeys[''] = $order;
+               $query->setLimit( $limit + 1 );
+
+               return $query;
+       }
+
+       /**
+        * Format a list of data items chunked by letter, either as a
+        * bullet list or a columnar format, depending on the length.
+        *
+        * @param $cutoff integer, use columns for more results than that
+        * @return string
+        */
+       public function formatList( $cutoff = 6 ) {
+               $end = count( $this->mDiWikiPages );
+               $start = 0;
+               if ( $end > $this->mLimit ) {
+                       if ( $this->mFrom != '' ) {
+                               $end -= 1;
+                       } else {
+                               $start += 1;
+                       }
+               }
+
+               if ( count ( $this->mDiWikiPages ) > $cutoff ) {
+                       return self::getColumnList( $start, $end, 
$this->mDiWikiPages, $this->mDiProperty, $this->mSkin );
+               } elseif ( count( $diWikiPages ) > 0 ) {
+                       return self::getShortList( $start, $end, 
$this->mDiWikiPages, $this->mDiProperty, $this->mSkin );
+               } else {
+                       return '';
+               }
+       }
+
+       /**
+        * Format a list of SMWDIWikiPage objects chunked by letter in a 
three-column
+        * list, ordered vertically.
+        * 
+        * @param $start integer
+        * @param $end integer
+        * @param $diWikiPages array of SMWDIWikiPage
+        * @param $diProperty SMWDIProperty that the wikipages are values of, 
or null
+        * @param $skin Skin object to use for making links
+        * 
+        * @return string
+        */
+       public static function getColumnList( $start, $end, array $diWikiPages, 
$diProperty, $skin ) {
+               global $wgContLang;
+               
+               // Divide list into three equal chunks.
+               $chunk = (int) ( ( $end - $start + 1 ) / 3 );
+
+               // Get and display header.
+               $r = '<table width="100%"><tr valign="top">';
+
+               $prevStartChar = 'none';
+
+               // Loop through the chunks.
+               for ( $startChunk = $start, $endChunk = $chunk, $chunkIndex = 0;
+                       $chunkIndex < 3;
+                       ++$chunkIndex, $startChunk = $endChunk, $endChunk += 
$chunk + 1 ) {
+                       $r .= "<td>\n";
+                       $atColumnTop = true;
+
+                       // output all diWikiPages
+                       for ( $index = $startChunk ; $index < $endChunk && 
$index < $end; ++$index ) {
+                               $dataValue = 
SMWDataValueFactory::newDataItemValue( $diWikiPages[$index], $diProperty );
+                               // check for change of starting letter or 
begining of chunk
+                               $sortkey = smwfGetStore()->getWikiPageSortKey( 
$diWikiPages[$index] );
+                               $startChar = $wgContLang->convert( 
$wgContLang->firstChar( $sortkey ) );
+
+                               if ( ( $index == $startChunk ) ||
+                                        ( $startChar != $prevStartChar ) ) {
+                                       if ( $atColumnTop ) {
+                                               $atColumnTop = false;
+                                       } else {
+                                               $r .= "</ul>\n";
+                                       }
+
+                                       if ( $startChar == $prevStartChar ) {
+                                               $cont_msg = ' ' . wfMsgHtml( 
'listingcontinuesabbrev' );
+                                       } else {
+                                               $cont_msg = '';
+                                       }
+
+                                       $r .= "<h3>" . htmlspecialchars( 
$startChar ) . $cont_msg . "</h3>\n<ul>";
+
+                                       $prevStartChar = $startChar;
+                               }
+
+                               $r .= "<li>" . $dataValue->getLongHTMLText( 
$skin ) . "</li>\n";
+                       }
+
+                       if ( !$atColumnTop ) {
+                               $r .= "</ul>\n";
+                       }
+
+                       $r .= "</td>\n";
+               }
+
+               $r .= '</tr></table>';
+
+               return $r;
+       }
+
+       /**
+        * Format a list of diWikiPages chunked by letter in a bullet list.
+        * 
+        * @param $start integer
+        * @param $end integer
+        * @param $diWikiPages array of SMWDataItem
+        * @param $diProperty SMWDIProperty that the wikipages are values of, 
or null
+        * @param $skin Skin object to use for making links
+        * 
+        * @return string
+        */
+       public static function getShortList( $start, $end, array $diWikiPages, 
$diProperty, $skin ) {
+               global $wgContLang;
+
+               $startDv = SMWDataValueFactory::newDataItemValue( 
$diWikiPages[$start], $diProperty );
+               $sortkey = smwfGetStore()->getWikiPageSortKey( 
$diWikiPages[$start] );
+               $startChar = $wgContLang->convert( $wgContLang->firstChar( 
$sortkey ) );
+               $r = '<h3>' . htmlspecialchars( $startChar ) . "</h3>\n" .
+                    '<ul><li>' . $startDv->getLongHTMLText( $skin ) . '</li>';
+
+               $prevStartChar = $startChar;
+               for ( $index = $start + 1; $index < $end; $index++ ) {
+                       $dataValue = SMWDataValueFactory::newDataItemValue( 
$diWikiPages[$index], $diProperty );
+                       $sortkey = smwfGetStore()->getWikiPageSortKey( 
$diWikiPages[$index] );
+                       $startChar = $wgContLang->convert( 
$wgContLang->firstChar( $sortkey ) );
+
+                       if ( $startChar != $prevStartChar ) {
+                               $r .= "</ul><h3>" . htmlspecialchars( 
$startChar ) . "</h3>\n<ul>";
+                               $prevStartChar = $startChar;
+                       }
+
+                       $r .= '<li>' . $dataValue->getLongHTMLText( $skin ) . 
'</li>';
+               }
+
+               $r .= '</ul>';
+
+               return $r;
+       }
+
+}
\ No newline at end of file


Property changes on: 
trunk/extensions/SemanticMediaWiki/includes/SMW_PageLister.php
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php   2011-05-21 
17:45:20 UTC (rev 88524)
+++ trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php   2011-05-21 
18:16:22 UTC (rev 88525)
@@ -109,9 +109,10 @@
        $wgAutoloadClasses['SMWParseData']              = $incDir . 
'SMW_ParseData.php';        
        $wgAutoloadClasses['SMWParserExtensions']       = $incDir . 
'SMW_ParserExtensions.php';
        $wgAutoloadClasses['SMWPropertyChange']         = $incDir . 
'SMW_PropertyChange.php';
-       $wgAutoloadClasses['SMWPropertyChanges']                = $incDir . 
'SMW_PropertyChanges.php';
+       $wgAutoloadClasses['SMWPropertyChanges']        = $incDir . 
'SMW_PropertyChanges.php';
        $wgAutoloadClasses['SMWQueryLanguage']          = $incDir . 
'SMW_QueryLanguage.php';    
        $wgAutoloadClasses['SMWSemanticData']           = $incDir . 
'SMW_SemanticData.php';
+       $wgAutoloadClasses['SMWPageLister']             = $incDir . 
'SMW_PageLister.php';
 
        // Article pages
        $apDir = $smwgIP . 'includes/articlepages/';

Modified: 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_ConceptPage.php
===================================================================
--- 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_ConceptPage.php    
    2011-05-21 17:45:20 UTC (rev 88524)
+++ 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_ConceptPage.php    
    2011-05-21 18:16:22 UTC (rev 88525)
@@ -14,10 +14,10 @@
  * @ingroup SMW
  */
 class SMWConceptPage extends SMWOrderedListPage {
-       protected $m_errors;
 
        /**
-        * Use higher limit. This operation is very similar to showing members 
of categories.
+        * Initialiye parameters to use a higher limit. This operation is very
+        * similar to showing members of categories.
         */
        protected function initParameters() {
                global $smwgConceptPagingLimit;
@@ -26,74 +26,46 @@
        }
 
        /**
-        * Fill the internal arrays with the list of wiki page data items to be
-        * displayed (possibly plus one additional article that indicates
-        * further results).
+        * Returns the HTML which is added to $wgOut after the article text.
+        * 
+        * @return string
         */
-       protected function doQuery() {
+       protected function getHtml() {
+               wfProfileIn( __METHOD__ . ' (SMW)' );
+
                if ( $this->limit > 0 ) {
                        $store = smwfGetStore();
-                       $thisDiWikiPage = SMWDIWikiPage::newFromTitle( 
$this->mTitle );
-                       $desc = new SMWConceptDescription( $thisDiWikiPage );
-                       
-                       if ( $this->from != '' ) {
-                               $diWikiPage = new SMWDIWikiPage( $this->from, 
NS_MAIN, '' ); // make a dummy wiki page as boundary
-                               $fromdesc = new SMWValueDescription( 
$diWikiPage, null, SMW_CMP_GEQ );
-                               $desc = new SMWConjunction( array( $desc, 
$fromdesc ) );
-                               $order = 'ASC';
-                       } elseif ( $this->until != '' ) {
-                               $diWikiPage = new SMWDIWikiPage( $this->until, 
NS_MAIN, '' ); // make a dummy wiki page as boundary
-                               $fromdesc = new SMWValueDescription( 
$diWikiPage, null, SMW_CMP_LEQ );
-                               $neqdesc = new SMWValueDescription( 
$diWikiPage, null, SMW_CMP_NEQ ); // do not include boundary in this case
-                               $desc = new SMWConjunction( array( $desc, 
$fromdesc, $neqdesc ) );
-                               $order = 'DESC';
-                       } else {
-                               $order = 'ASC';
-                       }
-                       
-                       $desc->addPrintRequest( new SMWPrintRequest( 
SMWPrintRequest::PRINT_THIS, '' ) );
-                       $query = new SMWQuery( $desc );
-                       $query->sortkeys[''] = $order;
-                       $query->setLimit( $this->limit + 1 );
+                       $description = new SMWConceptDescription( 
$this->getDataItem() );
+                       $query = SMWPageLister::getQuery( $description, 
$this->limit, $this->from, $this->until );
+                       $queryResult = $store->getQueryResult( $query );
 
-                       $result = $store->getQueryResult( $query );
-                       $row = $result->getNext();
-                       
-                       while ( $row !== false ) {
-                               $this->diWikiPages[] = end( $row 
)->getNextDataItem();
-                               $row = $result->getNext();
+                       $diWikiPages = $queryResult->getResults();
+                       if ($this->until != '' ) {
+                               $diWikiPages = array_reverse( $diWikiPages );
                        }
-                       
-                       if ( $order == 'DESC' ) {
-                               $this->diWikiPages = array_reverse( 
$this->diWikiPages );
-                       }
-                       
-                       $this->m_errors = $query->getErrors();
+
+                       $errors = $queryResult->getErrors();
                } else {
-                       $this->diWikiPages = array();
-                       $this->errors = array();
+                       $diWikiPages = array();
+                       $errors = array();
                }
-       }
 
-       /**
-        * Generates the headline for the page list and the HTML encoded list 
of pages which
-        * shall be shown.
-        */
-       protected function getPages() {
-               wfProfileIn( __METHOD__ . ' (SMW)' );
                smwfLoadExtensionMessages( 'SemanticMediaWiki' );
-               $r = '';
-               $ti = htmlspecialchars( $this->mTitle->getText() );
-               $nav = $this->getNavigationLinks();
-               $r .= '<a name="SMWResults"></a>' . $nav . "<div 
id=\"mw-pages\">\n";
+               $pageLister = new SMWPageLister( $diWikiPages, null, 
$this->getSkin(), $this->limit, $this->from, $this->until );
+               $this->mTitle->setFragment( '#SMWResults' ); // Make navigation 
point to the result list.
+               $navigation = $pageLister->getNavigationLinks( $this->mTitle );
 
-               $r .= '<h2>' . wfMsg( 'smw_concept_header', $ti ) . "</h2>\n";
-               $r .= wfMsgExt( 'smw_conceptarticlecount', array( 'parsemag' ), 
min( $this->limit, count( $this->diWikiPages ) ) ) . smwfEncodeMessages( 
$this->m_errors ) .  "\n";
+               $titleText = htmlspecialchars( $this->mTitle->getText() );
+               $resultNumber = min( $this->limit, count( $diWikiPages ) );
 
-               $r .= $this->formatList();
-               $r .= "\n</div>" . $nav;
+               $result = "<a name=\"SMWResults\"></a><div id=\"mw-pages\">\n" .
+                         '<h2>' . wfMsg( 'smw_concept_header', $titleText ) . 
"</h2>\n" .
+                         wfMsgExt( 'smw_conceptarticlecount', array( 
'parsemag' ), $resultNumber ) .
+                         smwfEncodeMessages( $errors ) . "\n" .
+                         $navigation . $pageLister->formatList() . $navigation 
. "</div>\n";
+
                wfProfileOut( __METHOD__ . ' (SMW)' );
-               return $r;
+               return $result;
        }
 
        /**
@@ -103,28 +75,28 @@
         * @param int $cutoff
         * @return string
         */
-       private function formatList( $cutoff = 6 ) {
-               $end = count( $this->diWikiPages );
-               
-               if ( $end > $this->limit ) {
-                       if ( $this->until != '' ) {
-                               $start = 1;
-                       } else {
-                               $start = 0;
-                               $end --;
-                       }
-               } else {
-                       $start = 0;
-               }
+//     private function formatList( $cutoff = 6 ) {
+//             $end = count( $this->diWikiPages );
+//             
+//             if ( $end > $this->limit ) {
+//                     if ( $this->until != '' ) {
+//                             $start = 1;
+//                     } else {
+//                             $start = 0;
+//                             $end --;
+//                     }
+//             } else {
+//                     $start = 0;
+//             }
+// 
+//             if ( count ( $this->diWikiPages ) > $cutoff ) {
+//                     return $this->columnList( $start, $end, 
$this->diWikiPages );
+//             } elseif ( count( $this->diWikiPages ) > 0 ) {
+//                     return $this->shortList( $start, $end, 
$this->diWikiPages );
+//             } else {
+//                     return '';
+//             }
+//     }
 
-               if ( count ( $this->diWikiPages ) > $cutoff ) {
-                       return $this->columnList( $start, $end, 
$this->diWikiPages );
-               } elseif ( count( $this->diWikiPages ) > 0 ) {
-                       return $this->shortList( $start, $end, 
$this->diWikiPages );
-               } else {
-                       return '';
-               }
-       }
-
 }
 

Modified: 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_OrderedListPage.php
===================================================================
--- 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_OrderedListPage.php
    2011-05-21 17:45:20 UTC (rev 88524)
+++ 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_OrderedListPage.php
    2011-05-21 18:16:22 UTC (rev 88525)
@@ -2,12 +2,9 @@
 
 /**
  * Abstract subclass of MediaWiki's Article that handles the common tasks of
- * article pages for Types and Properties. Mostly, it implements general 
processing
- * and the generation of suitable navigation links from results sets and HTTP
- * parameters.
- * 
- * Some code adapted from CategoryPage.php
- * 
+ * article pages for Concept and Property pages. This is mainly parameter
+ * handling and some very basic output control.
+ *
  * @file SMW_OrderedListPage.php 
  * @ingroup SMW
  * 
@@ -37,14 +34,6 @@
         * @var string
         */     
        protected $until;
-
-       /**
-        * Array of SMWDIWikiPage objects for which information is printed
-        * (primary ordering method).
-        * 
-        * @var array of SMWDIWikiPage
-        */
-       protected $diWikiPages; 
        
        /**
         * Cache for the current skin, obtained from $wgUser.
@@ -61,37 +50,36 @@
        protected $mProperty = null;
 
        /**
-        * Overwrite view() from Article.php to add additional html to the 
output.
+        * Overwrite view() from Article.php to add additional HTML to the
+        * output.
         */
        public function view() {
                global $wgRequest, $wgUser;
 
+               parent::view();
+
                // Copied from CategoryPage
                $diff = $wgRequest->getVal( 'diff' );
                $diffOnly = $wgRequest->getBool( 'diffonly', 
$wgUser->getOption( 'diffonly' ) );
-
-               if ( isset( $diff ) && $diffOnly ) {
-                       return Article::view();
+               if ( !isset( $diff ) || !$diffOnly ) {
+                       $this->showList();
                }
-
-               Article::view();
-               $this->showList();
        }
 
        /**
-        * Main method for addig all additional HTML to the output stream.
+        * Main method for adding all additional HTML to the output stream.
         */
        protected function showList() {
-               wfProfileIn( __METHOD__ . ' (SMW)' );
-
                global $wgOut, $wgRequest;
 
+               wfProfileIn( __METHOD__ . ' (SMW)' );
+
                $this->from = $wgRequest->getVal( 'from' );
                $this->until = $wgRequest->getVal( 'until' );
 
                if ( $this->initParameters() ) {
-                       $wgOut->addHTML( $this->getHTML() );
-                       SMWOutputs::commitToOutputPage( $wgOut ); // Flush 
required CSS to output
+                       $wgOut->addHTML( "<br id=\"smwfootbr\"/>\n" . 
$this->getHtml() );
+                       SMWOutputs::commitToOutputPage( $wgOut );
                }
 
                wfProfileOut( __METHOD__ . ' (SMW)' );
@@ -111,86 +99,13 @@
        }
 
        /**
-        * Returns HTML which is added to wgOut.
+        * Returns the HTML which is added to $wgOut after the article text.
         * 
         * @return string
         */
-       protected function getHTML() {
-               $this->clearPageState();
-               $this->doQuery();
-               $r = "<br id=\"smwfootbr\"/>\n" . $this->getPages();
+       protected abstract function getHtml();
 
-               return $r;
-       }
-
        /**
-        * Initialise internal data structures.
-        */
-       protected function clearPageState() {
-               $this->diWikiPages = array();
-       }
-
-       /**
-        * Execute the DB query and fill the diWikiPages array.
-        * Implemented by subclasses.
-        */
-       protected abstract function doQuery();
-
-       /**
-        * Generates the headline for the page list and the HTML encoded list 
of pages which
-        * shall be shown.
-        */
-       protected abstract function getPages();
-
-       /**
-        * Generates the prev/next link part to the HTML code of the top and 
bottom section of the page.
-        */
-       protected function getNavigationLinks( $query = array() ) {
-               global $wgLang;
-               
-               $sk = $this->getSkin();
-               $limitText = $wgLang->formatNum( $this->limit );
-
-               $ac = count( $this->diWikiPages );
-
-               if ( $this->until != '' ) {
-                       if ( $ac > $this->limit ) { // (we assume that limit is 
at least 1)
-                               $first = smwfGetStore()->getWikiPageSortKey( 
$this->diWikiPages[1] );
-                       } else {
-                               $first = '';
-                       }
-
-                       $last = $this->until;
-               } elseif ( ( $ac > $this->limit ) || ( $this->from != '' ) ) {
-                       $first = $this->from;
-
-                       if ( $ac > $this->limit ) {
-                               $last = smwfGetStore()->getWikiPageSortKey( 
$this->diWikiPages[$ac - 1] );
-                       } else {
-                               $last = '';
-                       }
-               } else {
-                       return '';
-               }
-
-               $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
-               $this->mTitle->setFragment( '#SMWResults' ); // Make navigation 
point to the result list.
-               
-               if ( $first != '' ) {
-                       $prevLink = $sk->makeLinkObj( $this->mTitle, $prevLink,
-                               wfArrayToCGI( $query + array( 'until' => $first 
) ) );
-               }
-               $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
-               
-               if ( $last != '' ) {
-                       $nextLink = $sk->makeLinkObj( $this->mTitle, $nextLink,
-                               wfArrayToCGI( $query + array( 'from' => $last ) 
) );
-               }
-               
-               return "($prevLink) ($nextLink)";
-       }
-
-       /**
         * Fetch and return the relevant skin object.
         * 
         * @return Skin
@@ -209,114 +124,7 @@
         * @return SMWDIWikiPage
         */
        protected function getDataItem() {
-               $title = $this->getTitle();
-               return new SMWDIWikiPage( $title->getDBKey(), 
$title->getNamespace(), $title->getInterwiki() );
+               return SMWDIWikiPage::newFromTitle( $this->getTitle() );
        }
 
-       /**
-        * Format a list of SMWDIWikiPage objects chunked by letter in a 
three-column
-        * list, ordered vertically.
-        * 
-        * @param $start integer
-        * @param $end integer
-        * @param $diWikiPages of SMWDIWikiPage
-        * 
-        * @return string
-        */
-       protected function columnList( $start, $end, $diWikiPages ) {
-               global $wgContLang;
-               
-               // Divide list into three equal chunks.
-               $chunk = (int) ( ( $end - $start + 1 ) / 3 );
-
-               // Get and display header.
-               $r = '<table width="100%"><tr valign="top">';
-
-               $prevStartChar = 'none';
-
-               // Loop through the chunks.
-               for ( $startChunk = $start, $endChunk = $chunk, $chunkIndex = 0;
-                       $chunkIndex < 3;
-                       ++$chunkIndex, $startChunk = $endChunk, $endChunk += 
$chunk + 1 ) {
-                       $r .= "<td>\n";
-                       $atColumnTop = true;
-
-                       // output all diWikiPages
-                       for ( $index = $startChunk ; $index < $endChunk && 
$index < $end; ++$index ) {
-                               $dataValue = 
SMWDataValueFactory::newDataItemValue( $diWikiPages[$index], $this->mProperty );
-                               // check for change of starting letter or 
begining of chunk
-                               $sortkey = smwfGetStore()->getWikiPageSortKey( 
$diWikiPages[$index] );
-                               $startChar = $wgContLang->convert( 
$wgContLang->firstChar( $sortkey ) );
-
-                               if ( ( $index == $startChunk ) ||
-                                        ( $startChar != $prevStartChar ) ) {
-                                       if ( $atColumnTop ) {
-                                               $atColumnTop = false;
-                                       } else {
-                                               $r .= "</ul>\n";
-                                       }
-
-                                       if ( $startChar == $prevStartChar ) {
-                                               $cont_msg = ' ' . wfMsgHtml( 
'listingcontinuesabbrev' );
-                                       } else {
-                                               $cont_msg = '';
-                                       }
-
-                                       $r .= "<h3>" . htmlspecialchars( 
$startChar ) . $cont_msg . "</h3>\n<ul>";
-
-                                       $prevStartChar = $startChar;
-                               }
-
-                               $r .= "<li>" . $dataValue->getLongHTMLText( 
$this->getSkin() ) . "</li>\n";
-                       }
-
-                       if ( !$atColumnTop ) {
-                               $r .= "</ul>\n";
-                       }
-
-                       $r .= "</td>\n";
-               }
-
-               $r .= '</tr></table>';
-               
-               return $r;
-       }
-
-       /**
-        * Format a list of diWikiPages chunked by letter in a bullet list.
-        * 
-        * @param $start integer
-        * @param $end integer
-        * @param $diWikiPages array of SMWDataItem
-        * 
-        * @return string
-        */
-       protected function shortList( $start, $end, array $diWikiPages ) {
-               global $wgContLang;
-
-               $startDv = SMWDataValueFactory::newDataItemValue( 
$diWikiPages[$start], $this->mProperty );
-               $sortkey = smwfGetStore()->getWikiPageSortKey( 
$diWikiPages[$start] );
-               $startChar = $wgContLang->convert( $wgContLang->firstChar( 
$sortkey ) );
-               $r = '<h3>' . htmlspecialchars( $startChar ) . "</h3>\n" .
-                    '<ul><li>' . $startDv->getLongHTMLText( $this->getSkin() ) 
. '</li>';
-
-               $prevStartChar = $startChar;
-               for ( $index = $start + 1; $index < $end; $index++ ) {
-                       $dataValue = SMWDataValueFactory::newDataItemValue( 
$diWikiPages[$index], $this->mProperty );
-                       $sortkey = smwfGetStore()->getWikiPageSortKey( 
$diWikiPages[$index] );
-                       $startChar = $wgContLang->convert( 
$wgContLang->firstChar( $sortkey ) );
-
-                       if ( $startChar != $prevStartChar ) {
-                               $r .= "</ul><h3>" . htmlspecialchars( 
$startChar ) . "</h3>\n<ul>";
-                               $prevStartChar = $startChar;
-                       }
-
-                       $r .= '<li>' . $dataValue->getLongHTMLText( 
$this->getSkin() ) . '</li>';
-               }
-
-               $r .= '</ul>';
-
-               return $r;
-       }
-
-}
\ No newline at end of file
+}

Modified: 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_PropertyPage.php
===================================================================
--- 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_PropertyPage.php   
    2011-05-21 17:45:20 UTC (rev 88524)
+++ 
trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_PropertyPage.php   
    2011-05-21 18:16:22 UTC (rev 88525)
@@ -4,8 +4,6 @@
  * Implementation of MediaWiki's Article that shows additional information on
  * property pages. Very similar to CategoryPage, but with different printout
  * that also displays values for each subject with the given property.
- * 
- * Some code based on CategoryPage.php
  *
  * @file SMW_PropertyPage.php
  * @ingroup SMW
@@ -14,8 +12,6 @@
  */
 class SMWPropertyPage extends SMWOrderedListPage {
 
-       private $subproperties;  // list of sub-properties of this property
-
        /**
         * @see SMWOrderedListPage::initParameters()
         * @note We use a smaller limit here; property pages might become large.
@@ -27,105 +23,113 @@
                return true;
        }
 
-       protected function clearPageState() {
-               parent::clearPageState();
-               $this->subproperties = array();
+       /**
+        * Returns the HTML which is added to $wgOut after the article text.
+        * 
+        * @return string
+        */
+       protected function getHtml() {
+               wfProfileIn( __METHOD__ . ' (SMW)' );
+               smwfLoadExtensionMessages( 'SemanticMediaWiki' );
+
+               $result = $this->getSubpropertyList() . 
$this->getPropertyValueList();
+
+               wfProfileOut( __METHOD__ . ' (SMW)' );
+               return $result;
        }
 
        /**
-        * Fill the internal arrays with the set of data items to be displayed
-        * (possibly plus one additional item that indicates further results).
+        * Get the HTML for displaying subproperties of this property. This list
+        * is usually short and we implement no additional navigation.
+        *
+        * @return string
         */
-       protected function doQuery() {
+       protected function getSubpropertyList() {
                $store = smwfGetStore();
-               
-               if ( $this->limit > 0 ) { // for limit==0 there is no paging, 
and no query
-                       $options = new SMWRequestOptions();
-                       $options->limit = $this->limit + 1;
-                       $options->sort = true;
-                       $reverse = false;
-                       
-                       if ( $this->from != '' ) {
-                               $options->boundary = $this->from;
-                               $options->ascending = true;
-                               $options->include_boundary = true;
-                       } elseif ( $this->until != '' ) {
-                               $options->boundary = $this->until;
-                               $options->ascending = false;
-                               $options->include_boundary = false;
-                               $reverse = true;
+               $options = new SMWRequestOptions();
+               $options->sort = true;
+               $options->ascending = true;
+               $subproperties = $store->getPropertySubjects( new 
SMWDIProperty( '_SUBP' ), $this->getDataItem(), $options );
+
+               $result = '';
+
+               $resultCount = count( $subproperties );
+               if ( $resultCount > 0 ) {
+                       $titleText = htmlspecialchars( $this->mTitle->getText() 
);
+                       $result .= "<div id=\"mw-subcategories\">\n<h2>" . 
wfMsg( 'smw_subproperty_header', $titleText ) . "</h2>\n<p>";
+
+                       if ( !$this->mProperty->isUserDefined() ) {
+                               $result .= wfMsg( 'smw_isspecprop' ) . ' ';
                        }
+
+                       $result .= wfMsgExt( 'smw_subpropertyarticlecount', 
array( 'parsemag' ), $resultCount ) . "</p>\n";
                        
-                       $this->diWikiPages = $store->getAllPropertySubjects( 
$this->mProperty, $options );
-                       
-                       if ( $reverse ) {
-                               $this->diWikiPages = array_reverse( 
$this->diWikiPages );
+                       if ( $resultCount < 6 ) {
+                               $result .= SMWPageLister::getShortList( 0, 
$resultCount, $subproperties, null, $this->getSkin() );
+                       } else {
+                               $result .= SMWPageLister::getColumnList( 0, 
$resultCount, $subproperties, null, $this->getSkin() );
                        }
-               } else {
-                       $this->diWikiPages = array();
+
+                       $result .= "\n</div>";
                }
 
-               // retrieve all subproperties of this property
-               $s_options = new SMWRequestOptions();
-               $s_options->sort = true;
-               $s_options->ascending = true;
-               $this->subproperties = $store->getPropertySubjects( new 
SMWDIProperty( '_SUBP' ), $this->getDataItem(), $s_options );
+               return $result;
        }
 
        /**
-        * Generates the headline for the page list and the HTML encoded list
-        * of pages which shall be shown.
+        * Get the HTML for displaying values of this property, based on the
+        * current from/until and limit settings.
+        *
+        * @return string
         */
-       protected function getPages() {
-               wfProfileIn( __METHOD__ . ' (SMW)' );
-               smwfLoadExtensionMessages( 'SemanticMediaWiki' );
-               $r = '';
-               $ti = htmlspecialchars( $this->mTitle->getText() );
-               
-               if ( count( $this->subproperties ) > 0 ) {
-                       $r .= "<div id=\"mw-subcategories\">\n<h2>" . wfMsg( 
'smw_subproperty_header', $ti ) . "</h2>\n<p>";
-                       
-                       if ( !$this->mProperty->isUserDefined() ) {
-                               $r .= wfMsg( 'smw_isspecprop' ) . ' ';
+       protected function getPropertyValueList() {
+               if ( $this->limit > 0 ) { // for limit==0 there is no paging, 
and no query
+                       $store = smwfGetStore();
+                       $options = SMWPageLister::getRequestOptions( 
$this->limit, $this->from, $this->until );
+                       $diWikiPages = $store->getAllPropertySubjects( 
$this->mProperty, $options );
+
+                       if ( !$options->ascending ) {
+                               $diWikiPages = array_reverse( $diWikiPages );
                        }
-                       
-                       $r .= wfMsgExt( 'smw_subpropertyarticlecount', array( 
'parsemag' ), count( $this->subproperties ) ) . "</p>\n";
-                       $r .= ( count( $this->subproperties ) < 6 ) ?
-                             $this->shortList( 0, count( $this->subproperties 
), $this->subproperties ):
-                                 $this->columnList( 0, count( 
$this->subproperties ), $this->subproperties );
-                                 
-                       $r .= "\n</div>";
+               } else {
+                       return '';
                }
-               
-               if ( count( $this->diWikiPages ) > 0 ) {
-                       $nav = $this->getNavigationLinks();
-                       
-                       $r .= '<a name="SMWResults"></a>' . $nav . "<div 
id=\"mw-pages\">\n" .
-                             '<h2>' . wfMsg( 'smw_attribute_header', $ti ) . 
"</h2>\n<p>";
-                       
+
+               $result = '';
+
+               if ( count( $diWikiPages ) > 0 ) {
+                       $pageLister = new SMWPageLister( $diWikiPages, null, 
$this->getSkin(), $this->limit, $this->from, $this->until );
+                       $this->mTitle->setFragment( '#SMWResults' ); // Make 
navigation point to the result list.
+                       $navigation = $pageLister->getNavigationLinks( 
$this->mTitle );
+
+                       $titleText = htmlspecialchars( $this->mTitle->getText() 
);
+                       $resultNumber = min( $this->limit, count( $diWikiPages 
) );
+
+                       $result .= "<a name=\"SMWResults\"></a><div 
id=\"mw-pages\">\n" .
+                                  '<h2>' . wfMsg( 'smw_attribute_header', 
$titleText ) . "</h2>\n<p>";
                        if ( !$this->mProperty->isUserDefined() ) {
-                               $r .= wfMsg( 'smw_isspecprop' ) . ' ';
+                               $result .= wfMsg( 'smw_isspecprop' ) . ' ';
                        }
-                       
-                       $r .= wfMsgExt( 'smw_attributearticlecount', array( 
'parsemag' ), min( $this->limit, count( $this->diWikiPages ) ) ) . "</p>\n" .
-                             $this->subjectObjectList() . "\n</div>" . $nav;
+                       $result .= wfMsgExt( 'smw_attributearticlecount', 
array( 'parsemag' ), $resultNumber ) . "</p>\n" .
+                                  $navigation . $this->subjectObjectList( 
$diWikiPages ) . $navigation . "\n</div>";
                }
-               
-               wfProfileOut( __METHOD__ . ' (SMW)' );
-               
-               return $r;
+
+               return $result;
        }
 
        /**
         * Format $diWikiPages chunked by letter in a table that shows subject
         * articles in one column and object articles/values in the other one.
+        *
+        * @param $diWikiPages array
+        * @return string
         */
-       private function subjectObjectList() {
+       protected function subjectObjectList( array $diWikiPages ) {
                global $wgContLang, $smwgMaxPropertyValues;
                $store = smwfGetStore();
 
-               $ac = count( $this->diWikiPages );
-               
+               $ac = count( $diWikiPages );
+
                if ( $ac > $this->limit ) {
                        if ( $this->until != '' ) {
                                $start = 1;
@@ -139,19 +143,19 @@
 
                $r = '<table style="width: 100%; ">';
                $prev_start_char = 'None';
-               
+
                for ( $index = $start; $index < $ac; $index++ ) {
-                       $diWikiPage = $this->diWikiPages[$index];
+                       $diWikiPage = $diWikiPages[$index];
                        $dvWikiPage = SMWDataValueFactory::newDataItemValue( 
$diWikiPage, null );
                        $sortkey = smwfGetStore()->getWikiPageSortKey( 
$diWikiPage );
                        $start_char = $wgContLang->convert( 
$wgContLang->firstChar( $sortkey ) );
-                       
+
                        // Header for index letters
                        if ( $start_char != $prev_start_char ) {
                                $r .= '<tr><th class="smwpropname"><h3>' . 
htmlspecialchars( $start_char ) . "</h3></th><th></th></tr>\n";
                                $prev_start_char = $start_char;
                        }
-                       
+
                        // Property name
                        $searchlink = SMWInfolink::newBrowsingLink( '+', 
$dvWikiPage->getShortHTMLText() );
                        $r .= '<tr><td class="smwpropname">' . 
$dvWikiPage->getLongHTMLText( $this->getSkin() ) .
@@ -180,9 +184,9 @@
 
                        $r .= "</td></tr>\n";
                }
-               
+
                $r .= '</table>';
-               
+
                return $r;
        }
 


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to