r12483 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r12482‎ | r12483 | r12484 >
Date:18:21, 7 January 2006
Author:vibber
Status:old
Tags:
Comment:
* update link format
* use category links when no headers found
* swap order of title and url per newer example fragments
Modified paths:
  • /trunk/extensions/ActiveAbstract/AbstractFilter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ActiveAbstract/AbstractFilter.php
@@ -46,8 +46,8 @@
4747 $this->title = Title::makeTitle( $page->page_namespace, $page->page_title );
4848
4949 $xml = "<doc>\n";
 50+ $xml .= wfElement( 'title', null, $wgSitename . ': ' . $this->title->getPrefixedText() ) . "\n";
5051 $xml .= wfElement( 'url', null, $this->title->getFullUrl() ) . "\n";
51 - $xml .= wfElement( 'title', null, $wgSitename . ': ' . $this->title->getPrefixedText() ) . "\n";
5252
5353 // add abstract and links when we have revision data...
5454 $this->revision = null;
@@ -60,9 +60,18 @@
6161 if( $this->revision ) {
6262 $xml .= wfElement( 'abstract', null, $this->_abstract( $this->revision ) ) . "\n";
6363 $xml .= "<links>\n";
64 - foreach( $this->_links( $this->revision ) as $url ) {
65 - $xml .= wfElement( 'link', null, $url ) . "\n";
 64+
 65+ $links = $this->_sectionLinks( $this->revision );
 66+ if( empty( $links ) ) {
 67+ // If no TOC, they want us to fall back to categories.
 68+ $links = $this->_categoryLinks( $this->revision );
6669 }
 70+ foreach( $links as $anchor => $url ) {
 71+ $xml .= $this->_formatLink( $url, $anchor, 'nav' );
 72+ }
 73+
 74+ // @todo: image links
 75+
6776 $xml .= "</links>\n";
6877 }
6978 $xml .= "</doc>\n";
@@ -138,12 +147,14 @@
139148
140149 /**
141150 * Extract a list of TOC links
142 - * @params object $rev Database rows with revision data
143 - * @return array of URL strings
 151+ * @param object $rev Database rows with revision data
 152+ * @return array of URL strings, indexed by name/title
144153 * @access private
145 - * @fixme extract TOC items
 154+ *
 155+ * @fixme extract TOC items properly
 156+ * @fixme check for explicit __NOTOC__
146157 */
147 - function _links( $rev ) {
 158+ function _sectionLinks( $rev ) {
148159 $text = Revision::getRevisionText( $rev );
149160 $secs =
150161 preg_split(
@@ -156,10 +167,55 @@
157168 $header = preg_replace( '/^=+\s*(.*?)\s*=+/', '$1', $secs[$i] );
158169 $anchor = EditPage::sectionAnchor( $header );
159170 $url = $this->title->getFullUrl() . $anchor;
160 - $headers[] = $url;
 171+ $headers[$header] = $url;
161172 }
162173 return $headers;
163174 }
 175+
 176+ /**
 177+ * Fetch the list of category links for this page
 178+ * @param object $rev Database rows with revision data
 179+ * @return array of URL strings, indexed by category name
 180+ * @access private
 181+ */
 182+ function _categoryLinks( $rev ) {
 183+ $id = $rev->page_id;
 184+ $dbr =& wfGetDB( DB_SLAVE );
 185+ $result = $dbr->select( 'categorylinks',
 186+ array( 'cl_to' ),
 187+ array( 'cl_from' => $id ),
 188+ 'AbstractFilter::_categoryLinks' );
 189+
 190+ $links = array();
 191+ while( $row = $dbr->fetchObject( $result ) ) {
 192+ $category = Title::makeTitle( NS_CATEGORY, $row->cl_to );
 193+ $links[$category->getText()] = $category->getFullUrl();
 194+ }
 195+ $dbr->freeResult( $result );
 196+
 197+ return $links;
 198+ }
 199+
 200+ /**
 201+ * Format a <sublink> element, like so:
 202+ * <sublink linktype="nav">
 203+ * <anchor>1939 Births</anchor>
 204+ * <link>http://en.wikipedia.org/wiki/Category:1939_births</link>
 205+ * </sublink>
 206+ *
 207+ * @param string $url
 208+ * @param string $anchor Human-readable link text; eg title or fragment
 209+ * @param string $linktype "nav" or "image"
 210+ * @return string XML fragment
 211+ * @access private
 212+ */
 213+ function _formatLink( $url, $anchor, $type ) {
 214+ return wfOpenElement( 'sublink', array( 'linktype' => $type ) ) .
 215+ wfElement( 'anchor', null, $anchor ) .
 216+ wfElement( 'link', null, $url ) .
 217+ wfCloseElement( 'sublink' ) . "\n";
 218+ }
 219+
164220 }
165221
166222 class NoredirectFilter extends DumpFilter {

Status & tagging log