r88525 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88524‎ | r88525 | r88526 >
Date:18:16, 21 May 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
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_PageLister.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_ConceptPage.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_OrderedListPage.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_PropertyPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_ConceptPage.php
@@ -14,10 +14,10 @@
1515 * @ingroup SMW
1616 */
1717 class SMWConceptPage extends SMWOrderedListPage {
18 - protected $m_errors;
1918
2019 /**
21 - * Use higher limit. This operation is very similar to showing members of categories.
 20+ * Initialiye parameters to use a higher limit. This operation is very
 21+ * similar to showing members of categories.
2222 */
2323 protected function initParameters() {
2424 global $smwgConceptPagingLimit;
@@ -26,74 +26,46 @@
2727 }
2828
2929 /**
30 - * Fill the internal arrays with the list of wiki page data items to be
31 - * displayed (possibly plus one additional article that indicates
32 - * further results).
 30+ * Returns the HTML which is added to $wgOut after the article text.
 31+ *
 32+ * @return string
3333 */
34 - protected function doQuery() {
 34+ protected function getHtml() {
 35+ wfProfileIn( __METHOD__ . ' (SMW)' );
 36+
3537 if ( $this->limit > 0 ) {
3638 $store = smwfGetStore();
37 - $thisDiWikiPage = SMWDIWikiPage::newFromTitle( $this->mTitle );
38 - $desc = new SMWConceptDescription( $thisDiWikiPage );
39 -
40 - if ( $this->from != '' ) {
41 - $diWikiPage = new SMWDIWikiPage( $this->from, NS_MAIN, '' ); // make a dummy wiki page as boundary
42 - $fromdesc = new SMWValueDescription( $diWikiPage, null, SMW_CMP_GEQ );
43 - $desc = new SMWConjunction( array( $desc, $fromdesc ) );
44 - $order = 'ASC';
45 - } elseif ( $this->until != '' ) {
46 - $diWikiPage = new SMWDIWikiPage( $this->until, NS_MAIN, '' ); // make a dummy wiki page as boundary
47 - $fromdesc = new SMWValueDescription( $diWikiPage, null, SMW_CMP_LEQ );
48 - $neqdesc = new SMWValueDescription( $diWikiPage, null, SMW_CMP_NEQ ); // do not include boundary in this case
49 - $desc = new SMWConjunction( array( $desc, $fromdesc, $neqdesc ) );
50 - $order = 'DESC';
51 - } else {
52 - $order = 'ASC';
53 - }
54 -
55 - $desc->addPrintRequest( new SMWPrintRequest( SMWPrintRequest::PRINT_THIS, '' ) );
56 - $query = new SMWQuery( $desc );
57 - $query->sortkeys[''] = $order;
58 - $query->setLimit( $this->limit + 1 );
 39+ $description = new SMWConceptDescription( $this->getDataItem() );
 40+ $query = SMWPageLister::getQuery( $description, $this->limit, $this->from, $this->until );
 41+ $queryResult = $store->getQueryResult( $query );
5942
60 - $result = $store->getQueryResult( $query );
61 - $row = $result->getNext();
62 -
63 - while ( $row !== false ) {
64 - $this->diWikiPages[] = end( $row )->getNextDataItem();
65 - $row = $result->getNext();
 43+ $diWikiPages = $queryResult->getResults();
 44+ if ($this->until != '' ) {
 45+ $diWikiPages = array_reverse( $diWikiPages );
6646 }
67 -
68 - if ( $order == 'DESC' ) {
69 - $this->diWikiPages = array_reverse( $this->diWikiPages );
70 - }
71 -
72 - $this->m_errors = $query->getErrors();
 47+
 48+ $errors = $queryResult->getErrors();
7349 } else {
74 - $this->diWikiPages = array();
75 - $this->errors = array();
 50+ $diWikiPages = array();
 51+ $errors = array();
7652 }
77 - }
7853
79 - /**
80 - * Generates the headline for the page list and the HTML encoded list of pages which
81 - * shall be shown.
82 - */
83 - protected function getPages() {
84 - wfProfileIn( __METHOD__ . ' (SMW)' );
8554 smwfLoadExtensionMessages( 'SemanticMediaWiki' );
86 - $r = '';
87 - $ti = htmlspecialchars( $this->mTitle->getText() );
88 - $nav = $this->getNavigationLinks();
89 - $r .= '<a name="SMWResults"></a>' . $nav . "<div id=\"mw-pages\">\n";
 55+ $pageLister = new SMWPageLister( $diWikiPages, null, $this->getSkin(), $this->limit, $this->from, $this->until );
 56+ $this->mTitle->setFragment( '#SMWResults' ); // Make navigation point to the result list.
 57+ $navigation = $pageLister->getNavigationLinks( $this->mTitle );
9058
91 - $r .= '<h2>' . wfMsg( 'smw_concept_header', $ti ) . "</h2>\n";
92 - $r .= wfMsgExt( 'smw_conceptarticlecount', array( 'parsemag' ), min( $this->limit, count( $this->diWikiPages ) ) ) . smwfEncodeMessages( $this->m_errors ) . "\n";
 59+ $titleText = htmlspecialchars( $this->mTitle->getText() );
 60+ $resultNumber = min( $this->limit, count( $diWikiPages ) );
9361
94 - $r .= $this->formatList();
95 - $r .= "\n</div>" . $nav;
 62+ $result = "<a name=\"SMWResults\"></a><div id=\"mw-pages\">\n" .
 63+ '<h2>' . wfMsg( 'smw_concept_header', $titleText ) . "</h2>\n" .
 64+ wfMsgExt( 'smw_conceptarticlecount', array( 'parsemag' ), $resultNumber ) .
 65+ smwfEncodeMessages( $errors ) . "\n" .
 66+ $navigation . $pageLister->formatList() . $navigation . "</div>\n";
 67+
9668 wfProfileOut( __METHOD__ . ' (SMW)' );
97 - return $r;
 69+ return $result;
9870 }
9971
10072 /**
@@ -103,28 +75,28 @@
10476 * @param int $cutoff
10577 * @return string
10678 */
107 - private function formatList( $cutoff = 6 ) {
108 - $end = count( $this->diWikiPages );
109 -
110 - if ( $end > $this->limit ) {
111 - if ( $this->until != '' ) {
112 - $start = 1;
113 - } else {
114 - $start = 0;
115 - $end --;
116 - }
117 - } else {
118 - $start = 0;
119 - }
 79+// private function formatList( $cutoff = 6 ) {
 80+// $end = count( $this->diWikiPages );
 81+//
 82+// if ( $end > $this->limit ) {
 83+// if ( $this->until != '' ) {
 84+// $start = 1;
 85+// } else {
 86+// $start = 0;
 87+// $end --;
 88+// }
 89+// } else {
 90+// $start = 0;
 91+// }
 92+//
 93+// if ( count ( $this->diWikiPages ) > $cutoff ) {
 94+// return $this->columnList( $start, $end, $this->diWikiPages );
 95+// } elseif ( count( $this->diWikiPages ) > 0 ) {
 96+// return $this->shortList( $start, $end, $this->diWikiPages );
 97+// } else {
 98+// return '';
 99+// }
 100+// }
120101
121 - if ( count ( $this->diWikiPages ) > $cutoff ) {
122 - return $this->columnList( $start, $end, $this->diWikiPages );
123 - } elseif ( count( $this->diWikiPages ) > 0 ) {
124 - return $this->shortList( $start, $end, $this->diWikiPages );
125 - } else {
126 - return '';
127 - }
128 - }
129 -
130102 }
131103
Index: trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_OrderedListPage.php
@@ -2,12 +2,9 @@
33
44 /**
55 * Abstract subclass of MediaWiki's Article that handles the common tasks of
6 - * article pages for Types and Properties. Mostly, it implements general processing
7 - * and the generation of suitable navigation links from results sets and HTTP
8 - * parameters.
9 - *
10 - * Some code adapted from CategoryPage.php
11 - *
 6+ * article pages for Concept and Property pages. This is mainly parameter
 7+ * handling and some very basic output control.
 8+ *
129 * @file SMW_OrderedListPage.php
1310 * @ingroup SMW
1411 *
@@ -37,14 +34,6 @@
3835 * @var string
3936 */
4037 protected $until;
41 -
42 - /**
43 - * Array of SMWDIWikiPage objects for which information is printed
44 - * (primary ordering method).
45 - *
46 - * @var array of SMWDIWikiPage
47 - */
48 - protected $diWikiPages;
4938
5039 /**
5140 * Cache for the current skin, obtained from $wgUser.
@@ -61,37 +50,36 @@
6251 protected $mProperty = null;
6352
6453 /**
65 - * Overwrite view() from Article.php to add additional html to the output.
 54+ * Overwrite view() from Article.php to add additional HTML to the
 55+ * output.
6656 */
6757 public function view() {
6858 global $wgRequest, $wgUser;
6959
 60+ parent::view();
 61+
7062 // Copied from CategoryPage
7163 $diff = $wgRequest->getVal( 'diff' );
7264 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
73 -
74 - if ( isset( $diff ) && $diffOnly ) {
75 - return Article::view();
 65+ if ( !isset( $diff ) || !$diffOnly ) {
 66+ $this->showList();
7667 }
77 -
78 - Article::view();
79 - $this->showList();
8068 }
8169
8270 /**
83 - * Main method for addig all additional HTML to the output stream.
 71+ * Main method for adding all additional HTML to the output stream.
8472 */
8573 protected function showList() {
86 - wfProfileIn( __METHOD__ . ' (SMW)' );
87 -
8874 global $wgOut, $wgRequest;
8975
 76+ wfProfileIn( __METHOD__ . ' (SMW)' );
 77+
9078 $this->from = $wgRequest->getVal( 'from' );
9179 $this->until = $wgRequest->getVal( 'until' );
9280
9381 if ( $this->initParameters() ) {
94 - $wgOut->addHTML( $this->getHTML() );
95 - SMWOutputs::commitToOutputPage( $wgOut ); // Flush required CSS to output
 82+ $wgOut->addHTML( "<br id=\"smwfootbr\"/>\n" . $this->getHtml() );
 83+ SMWOutputs::commitToOutputPage( $wgOut );
9684 }
9785
9886 wfProfileOut( __METHOD__ . ' (SMW)' );
@@ -111,86 +99,13 @@
112100 }
113101
114102 /**
115 - * Returns HTML which is added to wgOut.
 103+ * Returns the HTML which is added to $wgOut after the article text.
116104 *
117105 * @return string
118106 */
119 - protected function getHTML() {
120 - $this->clearPageState();
121 - $this->doQuery();
122 - $r = "<br id=\"smwfootbr\"/>\n" . $this->getPages();
 107+ protected abstract function getHtml();
123108
124 - return $r;
125 - }
126 -
127109 /**
128 - * Initialise internal data structures.
129 - */
130 - protected function clearPageState() {
131 - $this->diWikiPages = array();
132 - }
133 -
134 - /**
135 - * Execute the DB query and fill the diWikiPages array.
136 - * Implemented by subclasses.
137 - */
138 - protected abstract function doQuery();
139 -
140 - /**
141 - * Generates the headline for the page list and the HTML encoded list of pages which
142 - * shall be shown.
143 - */
144 - protected abstract function getPages();
145 -
146 - /**
147 - * Generates the prev/next link part to the HTML code of the top and bottom section of the page.
148 - */
149 - protected function getNavigationLinks( $query = array() ) {
150 - global $wgLang;
151 -
152 - $sk = $this->getSkin();
153 - $limitText = $wgLang->formatNum( $this->limit );
154 -
155 - $ac = count( $this->diWikiPages );
156 -
157 - if ( $this->until != '' ) {
158 - if ( $ac > $this->limit ) { // (we assume that limit is at least 1)
159 - $first = smwfGetStore()->getWikiPageSortKey( $this->diWikiPages[1] );
160 - } else {
161 - $first = '';
162 - }
163 -
164 - $last = $this->until;
165 - } elseif ( ( $ac > $this->limit ) || ( $this->from != '' ) ) {
166 - $first = $this->from;
167 -
168 - if ( $ac > $this->limit ) {
169 - $last = smwfGetStore()->getWikiPageSortKey( $this->diWikiPages[$ac - 1] );
170 - } else {
171 - $last = '';
172 - }
173 - } else {
174 - return '';
175 - }
176 -
177 - $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
178 - $this->mTitle->setFragment( '#SMWResults' ); // Make navigation point to the result list.
179 -
180 - if ( $first != '' ) {
181 - $prevLink = $sk->makeLinkObj( $this->mTitle, $prevLink,
182 - wfArrayToCGI( $query + array( 'until' => $first ) ) );
183 - }
184 - $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
185 -
186 - if ( $last != '' ) {
187 - $nextLink = $sk->makeLinkObj( $this->mTitle, $nextLink,
188 - wfArrayToCGI( $query + array( 'from' => $last ) ) );
189 - }
190 -
191 - return "($prevLink) ($nextLink)";
192 - }
193 -
194 - /**
195110 * Fetch and return the relevant skin object.
196111 *
197112 * @return Skin
@@ -209,114 +124,7 @@
210125 * @return SMWDIWikiPage
211126 */
212127 protected function getDataItem() {
213 - $title = $this->getTitle();
214 - return new SMWDIWikiPage( $title->getDBKey(), $title->getNamespace(), $title->getInterwiki() );
 128+ return SMWDIWikiPage::newFromTitle( $this->getTitle() );
215129 }
216130
217 - /**
218 - * Format a list of SMWDIWikiPage objects chunked by letter in a three-column
219 - * list, ordered vertically.
220 - *
221 - * @param $start integer
222 - * @param $end integer
223 - * @param $diWikiPages of SMWDIWikiPage
224 - *
225 - * @return string
226 - */
227 - protected function columnList( $start, $end, $diWikiPages ) {
228 - global $wgContLang;
229 -
230 - // Divide list into three equal chunks.
231 - $chunk = (int) ( ( $end - $start + 1 ) / 3 );
232 -
233 - // Get and display header.
234 - $r = '<table width="100%"><tr valign="top">';
235 -
236 - $prevStartChar = 'none';
237 -
238 - // Loop through the chunks.
239 - for ( $startChunk = $start, $endChunk = $chunk, $chunkIndex = 0;
240 - $chunkIndex < 3;
241 - ++$chunkIndex, $startChunk = $endChunk, $endChunk += $chunk + 1 ) {
242 - $r .= "<td>\n";
243 - $atColumnTop = true;
244 -
245 - // output all diWikiPages
246 - for ( $index = $startChunk ; $index < $endChunk && $index < $end; ++$index ) {
247 - $dataValue = SMWDataValueFactory::newDataItemValue( $diWikiPages[$index], $this->mProperty );
248 - // check for change of starting letter or begining of chunk
249 - $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$index] );
250 - $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
251 -
252 - if ( ( $index == $startChunk ) ||
253 - ( $startChar != $prevStartChar ) ) {
254 - if ( $atColumnTop ) {
255 - $atColumnTop = false;
256 - } else {
257 - $r .= "</ul>\n";
258 - }
259 -
260 - if ( $startChar == $prevStartChar ) {
261 - $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
262 - } else {
263 - $cont_msg = '';
264 - }
265 -
266 - $r .= "<h3>" . htmlspecialchars( $startChar ) . $cont_msg . "</h3>\n<ul>";
267 -
268 - $prevStartChar = $startChar;
269 - }
270 -
271 - $r .= "<li>" . $dataValue->getLongHTMLText( $this->getSkin() ) . "</li>\n";
272 - }
273 -
274 - if ( !$atColumnTop ) {
275 - $r .= "</ul>\n";
276 - }
277 -
278 - $r .= "</td>\n";
279 - }
280 -
281 - $r .= '</tr></table>';
282 -
283 - return $r;
284 - }
285 -
286 - /**
287 - * Format a list of diWikiPages chunked by letter in a bullet list.
288 - *
289 - * @param $start integer
290 - * @param $end integer
291 - * @param $diWikiPages array of SMWDataItem
292 - *
293 - * @return string
294 - */
295 - protected function shortList( $start, $end, array $diWikiPages ) {
296 - global $wgContLang;
297 -
298 - $startDv = SMWDataValueFactory::newDataItemValue( $diWikiPages[$start], $this->mProperty );
299 - $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$start] );
300 - $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
301 - $r = '<h3>' . htmlspecialchars( $startChar ) . "</h3>\n" .
302 - '<ul><li>' . $startDv->getLongHTMLText( $this->getSkin() ) . '</li>';
303 -
304 - $prevStartChar = $startChar;
305 - for ( $index = $start + 1; $index < $end; $index++ ) {
306 - $dataValue = SMWDataValueFactory::newDataItemValue( $diWikiPages[$index], $this->mProperty );
307 - $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$index] );
308 - $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
309 -
310 - if ( $startChar != $prevStartChar ) {
311 - $r .= "</ul><h3>" . htmlspecialchars( $startChar ) . "</h3>\n<ul>";
312 - $prevStartChar = $startChar;
313 - }
314 -
315 - $r .= '<li>' . $dataValue->getLongHTMLText( $this->getSkin() ) . '</li>';
316 - }
317 -
318 - $r .= '</ul>';
319 -
320 - return $r;
321 - }
322 -
323 -}
\ No newline at end of file
 131+}
Index: trunk/extensions/SemanticMediaWiki/includes/articlepages/SMW_PropertyPage.php
@@ -4,8 +4,6 @@
55 * Implementation of MediaWiki's Article that shows additional information on
66 * property pages. Very similar to CategoryPage, but with different printout
77 * that also displays values for each subject with the given property.
8 - *
9 - * Some code based on CategoryPage.php
108 *
119 * @file SMW_PropertyPage.php
1210 * @ingroup SMW
@@ -14,8 +12,6 @@
1513 */
1614 class SMWPropertyPage extends SMWOrderedListPage {
1715
18 - private $subproperties; // list of sub-properties of this property
19 -
2016 /**
2117 * @see SMWOrderedListPage::initParameters()
2218 * @note We use a smaller limit here; property pages might become large.
@@ -27,105 +23,113 @@
2824 return true;
2925 }
3026
31 - protected function clearPageState() {
32 - parent::clearPageState();
33 - $this->subproperties = array();
 27+ /**
 28+ * Returns the HTML which is added to $wgOut after the article text.
 29+ *
 30+ * @return string
 31+ */
 32+ protected function getHtml() {
 33+ wfProfileIn( __METHOD__ . ' (SMW)' );
 34+ smwfLoadExtensionMessages( 'SemanticMediaWiki' );
 35+
 36+ $result = $this->getSubpropertyList() . $this->getPropertyValueList();
 37+
 38+ wfProfileOut( __METHOD__ . ' (SMW)' );
 39+ return $result;
3440 }
3541
3642 /**
37 - * Fill the internal arrays with the set of data items to be displayed
38 - * (possibly plus one additional item that indicates further results).
 43+ * Get the HTML for displaying subproperties of this property. This list
 44+ * is usually short and we implement no additional navigation.
 45+ *
 46+ * @return string
3947 */
40 - protected function doQuery() {
 48+ protected function getSubpropertyList() {
4149 $store = smwfGetStore();
42 -
43 - if ( $this->limit > 0 ) { // for limit==0 there is no paging, and no query
44 - $options = new SMWRequestOptions();
45 - $options->limit = $this->limit + 1;
46 - $options->sort = true;
47 - $reverse = false;
48 -
49 - if ( $this->from != '' ) {
50 - $options->boundary = $this->from;
51 - $options->ascending = true;
52 - $options->include_boundary = true;
53 - } elseif ( $this->until != '' ) {
54 - $options->boundary = $this->until;
55 - $options->ascending = false;
56 - $options->include_boundary = false;
57 - $reverse = true;
 50+ $options = new SMWRequestOptions();
 51+ $options->sort = true;
 52+ $options->ascending = true;
 53+ $subproperties = $store->getPropertySubjects( new SMWDIProperty( '_SUBP' ), $this->getDataItem(), $options );
 54+
 55+ $result = '';
 56+
 57+ $resultCount = count( $subproperties );
 58+ if ( $resultCount > 0 ) {
 59+ $titleText = htmlspecialchars( $this->mTitle->getText() );
 60+ $result .= "<div id=\"mw-subcategories\">\n<h2>" . wfMsg( 'smw_subproperty_header', $titleText ) . "</h2>\n<p>";
 61+
 62+ if ( !$this->mProperty->isUserDefined() ) {
 63+ $result .= wfMsg( 'smw_isspecprop' ) . ' ';
5864 }
 65+
 66+ $result .= wfMsgExt( 'smw_subpropertyarticlecount', array( 'parsemag' ), $resultCount ) . "</p>\n";
5967
60 - $this->diWikiPages = $store->getAllPropertySubjects( $this->mProperty, $options );
61 -
62 - if ( $reverse ) {
63 - $this->diWikiPages = array_reverse( $this->diWikiPages );
 68+ if ( $resultCount < 6 ) {
 69+ $result .= SMWPageLister::getShortList( 0, $resultCount, $subproperties, null, $this->getSkin() );
 70+ } else {
 71+ $result .= SMWPageLister::getColumnList( 0, $resultCount, $subproperties, null, $this->getSkin() );
6472 }
65 - } else {
66 - $this->diWikiPages = array();
 73+
 74+ $result .= "\n</div>";
6775 }
6876
69 - // retrieve all subproperties of this property
70 - $s_options = new SMWRequestOptions();
71 - $s_options->sort = true;
72 - $s_options->ascending = true;
73 - $this->subproperties = $store->getPropertySubjects( new SMWDIProperty( '_SUBP' ), $this->getDataItem(), $s_options );
 77+ return $result;
7478 }
7579
7680 /**
77 - * Generates the headline for the page list and the HTML encoded list
78 - * of pages which shall be shown.
 81+ * Get the HTML for displaying values of this property, based on the
 82+ * current from/until and limit settings.
 83+ *
 84+ * @return string
7985 */
80 - protected function getPages() {
81 - wfProfileIn( __METHOD__ . ' (SMW)' );
82 - smwfLoadExtensionMessages( 'SemanticMediaWiki' );
83 - $r = '';
84 - $ti = htmlspecialchars( $this->mTitle->getText() );
85 -
86 - if ( count( $this->subproperties ) > 0 ) {
87 - $r .= "<div id=\"mw-subcategories\">\n<h2>" . wfMsg( 'smw_subproperty_header', $ti ) . "</h2>\n<p>";
88 -
89 - if ( !$this->mProperty->isUserDefined() ) {
90 - $r .= wfMsg( 'smw_isspecprop' ) . ' ';
 86+ protected function getPropertyValueList() {
 87+ if ( $this->limit > 0 ) { // for limit==0 there is no paging, and no query
 88+ $store = smwfGetStore();
 89+ $options = SMWPageLister::getRequestOptions( $this->limit, $this->from, $this->until );
 90+ $diWikiPages = $store->getAllPropertySubjects( $this->mProperty, $options );
 91+
 92+ if ( !$options->ascending ) {
 93+ $diWikiPages = array_reverse( $diWikiPages );
9194 }
92 -
93 - $r .= wfMsgExt( 'smw_subpropertyarticlecount', array( 'parsemag' ), count( $this->subproperties ) ) . "</p>\n";
94 - $r .= ( count( $this->subproperties ) < 6 ) ?
95 - $this->shortList( 0, count( $this->subproperties ), $this->subproperties ):
96 - $this->columnList( 0, count( $this->subproperties ), $this->subproperties );
97 -
98 - $r .= "\n</div>";
 95+ } else {
 96+ return '';
9997 }
100 -
101 - if ( count( $this->diWikiPages ) > 0 ) {
102 - $nav = $this->getNavigationLinks();
103 -
104 - $r .= '<a name="SMWResults"></a>' . $nav . "<div id=\"mw-pages\">\n" .
105 - '<h2>' . wfMsg( 'smw_attribute_header', $ti ) . "</h2>\n<p>";
106 -
 98+
 99+ $result = '';
 100+
 101+ if ( count( $diWikiPages ) > 0 ) {
 102+ $pageLister = new SMWPageLister( $diWikiPages, null, $this->getSkin(), $this->limit, $this->from, $this->until );
 103+ $this->mTitle->setFragment( '#SMWResults' ); // Make navigation point to the result list.
 104+ $navigation = $pageLister->getNavigationLinks( $this->mTitle );
 105+
 106+ $titleText = htmlspecialchars( $this->mTitle->getText() );
 107+ $resultNumber = min( $this->limit, count( $diWikiPages ) );
 108+
 109+ $result .= "<a name=\"SMWResults\"></a><div id=\"mw-pages\">\n" .
 110+ '<h2>' . wfMsg( 'smw_attribute_header', $titleText ) . "</h2>\n<p>";
107111 if ( !$this->mProperty->isUserDefined() ) {
108 - $r .= wfMsg( 'smw_isspecprop' ) . ' ';
 112+ $result .= wfMsg( 'smw_isspecprop' ) . ' ';
109113 }
110 -
111 - $r .= wfMsgExt( 'smw_attributearticlecount', array( 'parsemag' ), min( $this->limit, count( $this->diWikiPages ) ) ) . "</p>\n" .
112 - $this->subjectObjectList() . "\n</div>" . $nav;
 114+ $result .= wfMsgExt( 'smw_attributearticlecount', array( 'parsemag' ), $resultNumber ) . "</p>\n" .
 115+ $navigation . $this->subjectObjectList( $diWikiPages ) . $navigation . "\n</div>";
113116 }
114 -
115 - wfProfileOut( __METHOD__ . ' (SMW)' );
116 -
117 - return $r;
 117+
 118+ return $result;
118119 }
119120
120121 /**
121122 * Format $diWikiPages chunked by letter in a table that shows subject
122123 * articles in one column and object articles/values in the other one.
 124+ *
 125+ * @param $diWikiPages array
 126+ * @return string
123127 */
124 - private function subjectObjectList() {
 128+ protected function subjectObjectList( array $diWikiPages ) {
125129 global $wgContLang, $smwgMaxPropertyValues;
126130 $store = smwfGetStore();
127131
128 - $ac = count( $this->diWikiPages );
129 -
 132+ $ac = count( $diWikiPages );
 133+
130134 if ( $ac > $this->limit ) {
131135 if ( $this->until != '' ) {
132136 $start = 1;
@@ -139,19 +143,19 @@
140144
141145 $r = '<table style="width: 100%; ">';
142146 $prev_start_char = 'None';
143 -
 147+
144148 for ( $index = $start; $index < $ac; $index++ ) {
145 - $diWikiPage = $this->diWikiPages[$index];
 149+ $diWikiPage = $diWikiPages[$index];
146150 $dvWikiPage = SMWDataValueFactory::newDataItemValue( $diWikiPage, null );
147151 $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPage );
148152 $start_char = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
149 -
 153+
150154 // Header for index letters
151155 if ( $start_char != $prev_start_char ) {
152156 $r .= '<tr><th class="smwpropname"><h3>' . htmlspecialchars( $start_char ) . "</h3></th><th></th></tr>\n";
153157 $prev_start_char = $start_char;
154158 }
155 -
 159+
156160 // Property name
157161 $searchlink = SMWInfolink::newBrowsingLink( '+', $dvWikiPage->getShortHTMLText() );
158162 $r .= '<tr><td class="smwpropname">' . $dvWikiPage->getLongHTMLText( $this->getSkin() ) .
@@ -180,9 +184,9 @@
181185
182186 $r .= "</td></tr>\n";
183187 }
184 -
 188+
185189 $r .= '</table>';
186 -
 190+
187191 return $r;
188192 }
189193
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_PageLister.php
@@ -0,0 +1,312 @@
 2+<?php
 3+
 4+/**
 5+ * Helper class to generate HTML lists of wiki pages, with support for paged
 6+ * navigation using the from/until and limit settings as in MediaWiki's
 7+ * CategoryPage.
 8+ *
 9+ * The class attempts to allow as much code as possible to be shared among
 10+ * different places where similar lists are used.
 11+ *
 12+ * Some code adapted from CategoryPage.php
 13+ *
 14+ * @file SMW_OrderedListPage.php
 15+ * @ingroup SMW
 16+ *
 17+ * @author Nikolas Iwan
 18+ * @author Markus Krötzsch
 19+ * @author Jeroen De Dauw
 20+ */
 21+class SMWPageLister {
 22+
 23+ protected $mDiWikiPages;
 24+ protected $mDiProperty;
 25+ protected $mSkin;
 26+ protected $mLimit;
 27+ protected $mFrom;
 28+ protected $mUntil;
 29+
 30+ /**
 31+ * Constructor
 32+ *
 33+ * @param $diWikiPages array of SMWDIWikiPage
 34+ * @param $diProperty mixed SMWDIProperty that the wikipages are values of, or null
 35+ * @param $skin Skin object to use for making links
 36+ * @param $limit integer maximal amount of items to display
 37+ * @param $from string if the results were selected starting from this string
 38+ * @param $until string if the results were selected reaching until this string
 39+ */
 40+ public function __construct( array $diWikiPages, $diProperty, $skin, $limit, $from = '', $until = '' ) {
 41+ $this->mDiWikiPages = $diWikiPages;
 42+ $this->mDiProperty = $diProperty;
 43+ $this->mSkin = $skin;
 44+ $this->mLimit = $limit;
 45+ $this->mFrom = $from;
 46+ $this->mUntil = $until;
 47+ }
 48+
 49+ /**
 50+ * Generates the prev/next link part to the HTML code of the top and
 51+ * bottom section of the page. Whether and how these links appear
 52+ * depends on specified boundaries, limit, and results. The title is
 53+ * required to create a link to the right page. The query array gives
 54+ * optional further parameters to append to all navigation links.
 55+ *
 56+ * @param $title Title
 57+ * @param $query array that associates parameter names to parameter values
 58+ * @return string
 59+ */
 60+ public function getNavigationLinks( Title $title, $query = array() ) {
 61+ global $wgLang;
 62+
 63+ $limitText = $wgLang->formatNum( $this->mLimit );
 64+
 65+ $resultCount = count( $this->mDiWikiPages );
 66+ $beyondLimit = ( $resultCount > $this->mLimit );
 67+
 68+ if ( $this->mUntil != '' ) {
 69+ if ( $beyondLimit ) {
 70+ $first = smwfGetStore()->getWikiPageSortKey( $this->mDiWikiPages[1] );
 71+ } else {
 72+ $first = '';
 73+ }
 74+
 75+ $last = $this->mUntil;
 76+ } elseif ( $beyondLimit || ( $this->mFrom != '' ) ) {
 77+ $first = $this->mFrom;
 78+
 79+ if ( $beyondLimit ) {
 80+ $last = smwfGetStore()->getWikiPageSortKey( $this->mDiWikiPages[$resultCount - 1] );
 81+ } else {
 82+ $last = '';
 83+ }
 84+ } else {
 85+ return '';
 86+ }
 87+
 88+ $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
 89+ if ( $first != '' ) {
 90+ $prevLink = $this->makeSelfLink( $title, $prevLink, $query + array( 'until' => $first ) );
 91+ }
 92+
 93+ $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
 94+ if ( $last != '' ) {
 95+ $nextLink = $this->makeSelfLink( $title, $nextLink, $query + array( 'from' => $last ) );
 96+ }
 97+
 98+ return "($prevLink) ($nextLink)";
 99+ }
 100+
 101+ /**
 102+ * Format an HTML link with the given text and parameters.
 103+ *
 104+ * @return string
 105+ */
 106+ protected function makeSelfLink( Title $title, $linkText, array $parameters ) {
 107+ return $this->mSkin->makeLinkObj( $title, $linkText, wfArrayToCGI( $parameters ) );
 108+ }
 109+
 110+ /**
 111+ * Make SMWRequestOptions suitable for obtaining a list of results for
 112+ * the given limit, and from or until string. One more result than the
 113+ * limit will be created, and the results may have to be reversed in
 114+ * order if ascending is set to false in the resulting object.
 115+ *
 116+ * @param $limit integer
 117+ * @param $from string can be empty if no from condition is desired
 118+ * @param $until string can be empty if no until condition is desired
 119+ * @return SMWRequestOptions
 120+ */
 121+ public static function getRequestOptions( $limit, $from, $until ) {
 122+ $options = new SMWRequestOptions();
 123+ $options->limit = $limit + 1;
 124+ $options->sort = true;
 125+
 126+ if ( $from != '' ) {
 127+ $options->boundary = $from;
 128+ $options->ascending = true;
 129+ $options->include_boundary = true;
 130+ } elseif ( $until != '' ) {
 131+ $options->boundary = $until;
 132+ $options->ascending = false;
 133+ $options->include_boundary = false;
 134+ }
 135+
 136+ return $options;
 137+ }
 138+
 139+ /**
 140+ * Make SMWQuery suitable for obtaining a list of results based on the
 141+ * given description, limit, and from or until string. One more result
 142+ * than the limit will be created, and the results may have to be
 143+ * reversed in order if $until is nonempty.
 144+ *
 145+ * @param $description SMWDescription main query description
 146+ * @param $limit integer
 147+ * @param $from string can be empty if no from condition is desired
 148+ * @param $until string can be empty if no until condition is desired
 149+ * @return SMWQuery
 150+ */
 151+ public static function getQuery( SMWDescription $description, $limit, $from, $until ) {
 152+ if ( $from != '' ) {
 153+ $diWikiPage = new SMWDIWikiPage( $from, NS_MAIN, '' ); // make a dummy wiki page as boundary
 154+ $fromDescription = new SMWValueDescription( $diWikiPage, null, SMW_CMP_GEQ );
 155+ $queryDescription = new SMWConjunction( array( $description, $fromDescription ) );
 156+ $order = 'ASC';
 157+ } elseif ( $until != '' ) {
 158+ $diWikiPage = new SMWDIWikiPage( $until, NS_MAIN, '' ); // make a dummy wiki page as boundary
 159+ $untilDescription = new SMWValueDescription( $diWikiPage, null, SMW_CMP_LESS ); // do not include boundary in this case
 160+ $queryDescription = new SMWConjunction( array( $description, $untilDescription ) );
 161+ $order = 'DESC';
 162+ } else {
 163+ $queryDescription = $description;
 164+ $order = 'ASC';
 165+ }
 166+
 167+ $queryDescription->addPrintRequest( new SMWPrintRequest( SMWPrintRequest::PRINT_THIS, '' ) );
 168+
 169+ $query = new SMWQuery( $queryDescription );
 170+ $query->sortkeys[''] = $order;
 171+ $query->setLimit( $limit + 1 );
 172+
 173+ return $query;
 174+ }
 175+
 176+ /**
 177+ * Format a list of data items chunked by letter, either as a
 178+ * bullet list or a columnar format, depending on the length.
 179+ *
 180+ * @param $cutoff integer, use columns for more results than that
 181+ * @return string
 182+ */
 183+ public function formatList( $cutoff = 6 ) {
 184+ $end = count( $this->mDiWikiPages );
 185+ $start = 0;
 186+ if ( $end > $this->mLimit ) {
 187+ if ( $this->mFrom != '' ) {
 188+ $end -= 1;
 189+ } else {
 190+ $start += 1;
 191+ }
 192+ }
 193+
 194+ if ( count ( $this->mDiWikiPages ) > $cutoff ) {
 195+ return self::getColumnList( $start, $end, $this->mDiWikiPages, $this->mDiProperty, $this->mSkin );
 196+ } elseif ( count( $diWikiPages ) > 0 ) {
 197+ return self::getShortList( $start, $end, $this->mDiWikiPages, $this->mDiProperty, $this->mSkin );
 198+ } else {
 199+ return '';
 200+ }
 201+ }
 202+
 203+ /**
 204+ * Format a list of SMWDIWikiPage objects chunked by letter in a three-column
 205+ * list, ordered vertically.
 206+ *
 207+ * @param $start integer
 208+ * @param $end integer
 209+ * @param $diWikiPages array of SMWDIWikiPage
 210+ * @param $diProperty SMWDIProperty that the wikipages are values of, or null
 211+ * @param $skin Skin object to use for making links
 212+ *
 213+ * @return string
 214+ */
 215+ public static function getColumnList( $start, $end, array $diWikiPages, $diProperty, $skin ) {
 216+ global $wgContLang;
 217+
 218+ // Divide list into three equal chunks.
 219+ $chunk = (int) ( ( $end - $start + 1 ) / 3 );
 220+
 221+ // Get and display header.
 222+ $r = '<table width="100%"><tr valign="top">';
 223+
 224+ $prevStartChar = 'none';
 225+
 226+ // Loop through the chunks.
 227+ for ( $startChunk = $start, $endChunk = $chunk, $chunkIndex = 0;
 228+ $chunkIndex < 3;
 229+ ++$chunkIndex, $startChunk = $endChunk, $endChunk += $chunk + 1 ) {
 230+ $r .= "<td>\n";
 231+ $atColumnTop = true;
 232+
 233+ // output all diWikiPages
 234+ for ( $index = $startChunk ; $index < $endChunk && $index < $end; ++$index ) {
 235+ $dataValue = SMWDataValueFactory::newDataItemValue( $diWikiPages[$index], $diProperty );
 236+ // check for change of starting letter or begining of chunk
 237+ $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$index] );
 238+ $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
 239+
 240+ if ( ( $index == $startChunk ) ||
 241+ ( $startChar != $prevStartChar ) ) {
 242+ if ( $atColumnTop ) {
 243+ $atColumnTop = false;
 244+ } else {
 245+ $r .= "</ul>\n";
 246+ }
 247+
 248+ if ( $startChar == $prevStartChar ) {
 249+ $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
 250+ } else {
 251+ $cont_msg = '';
 252+ }
 253+
 254+ $r .= "<h3>" . htmlspecialchars( $startChar ) . $cont_msg . "</h3>\n<ul>";
 255+
 256+ $prevStartChar = $startChar;
 257+ }
 258+
 259+ $r .= "<li>" . $dataValue->getLongHTMLText( $skin ) . "</li>\n";
 260+ }
 261+
 262+ if ( !$atColumnTop ) {
 263+ $r .= "</ul>\n";
 264+ }
 265+
 266+ $r .= "</td>\n";
 267+ }
 268+
 269+ $r .= '</tr></table>';
 270+
 271+ return $r;
 272+ }
 273+
 274+ /**
 275+ * Format a list of diWikiPages chunked by letter in a bullet list.
 276+ *
 277+ * @param $start integer
 278+ * @param $end integer
 279+ * @param $diWikiPages array of SMWDataItem
 280+ * @param $diProperty SMWDIProperty that the wikipages are values of, or null
 281+ * @param $skin Skin object to use for making links
 282+ *
 283+ * @return string
 284+ */
 285+ public static function getShortList( $start, $end, array $diWikiPages, $diProperty, $skin ) {
 286+ global $wgContLang;
 287+
 288+ $startDv = SMWDataValueFactory::newDataItemValue( $diWikiPages[$start], $diProperty );
 289+ $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$start] );
 290+ $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
 291+ $r = '<h3>' . htmlspecialchars( $startChar ) . "</h3>\n" .
 292+ '<ul><li>' . $startDv->getLongHTMLText( $skin ) . '</li>';
 293+
 294+ $prevStartChar = $startChar;
 295+ for ( $index = $start + 1; $index < $end; $index++ ) {
 296+ $dataValue = SMWDataValueFactory::newDataItemValue( $diWikiPages[$index], $diProperty );
 297+ $sortkey = smwfGetStore()->getWikiPageSortKey( $diWikiPages[$index] );
 298+ $startChar = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
 299+
 300+ if ( $startChar != $prevStartChar ) {
 301+ $r .= "</ul><h3>" . htmlspecialchars( $startChar ) . "</h3>\n<ul>";
 302+ $prevStartChar = $startChar;
 303+ }
 304+
 305+ $r .= '<li>' . $dataValue->getLongHTMLText( $skin ) . '</li>';
 306+ }
 307+
 308+ $r .= '</ul>';
 309+
 310+ return $r;
 311+ }
 312+
 313+}
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_PageLister.php
___________________________________________________________________
Added: svn:eol-style
1314 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -109,9 +109,10 @@
110110 $wgAutoloadClasses['SMWParseData'] = $incDir . 'SMW_ParseData.php';
111111 $wgAutoloadClasses['SMWParserExtensions'] = $incDir . 'SMW_ParserExtensions.php';
112112 $wgAutoloadClasses['SMWPropertyChange'] = $incDir . 'SMW_PropertyChange.php';
113 - $wgAutoloadClasses['SMWPropertyChanges'] = $incDir . 'SMW_PropertyChanges.php';
 113+ $wgAutoloadClasses['SMWPropertyChanges'] = $incDir . 'SMW_PropertyChanges.php';
114114 $wgAutoloadClasses['SMWQueryLanguage'] = $incDir . 'SMW_QueryLanguage.php';
115115 $wgAutoloadClasses['SMWSemanticData'] = $incDir . 'SMW_SemanticData.php';
 116+ $wgAutoloadClasses['SMWPageLister'] = $incDir . 'SMW_PageLister.php';
116117
117118 // Article pages
118119 $apDir = $smwgIP . 'includes/articlepages/';

Status & tagging log