Index: trunk/extensions/ActiveAbstract/AbstractFilter.php |
— | — | @@ -46,8 +46,8 @@ |
47 | 47 | $this->title = Title::makeTitle( $page->page_namespace, $page->page_title ); |
48 | 48 | |
49 | 49 | $xml = "<doc>\n"; |
| 50 | + $xml .= wfElement( 'title', null, $wgSitename . ': ' . $this->title->getPrefixedText() ) . "\n"; |
50 | 51 | $xml .= wfElement( 'url', null, $this->title->getFullUrl() ) . "\n"; |
51 | | - $xml .= wfElement( 'title', null, $wgSitename . ': ' . $this->title->getPrefixedText() ) . "\n"; |
52 | 52 | |
53 | 53 | // add abstract and links when we have revision data... |
54 | 54 | $this->revision = null; |
— | — | @@ -60,9 +60,18 @@ |
61 | 61 | if( $this->revision ) { |
62 | 62 | $xml .= wfElement( 'abstract', null, $this->_abstract( $this->revision ) ) . "\n"; |
63 | 63 | $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 ); |
66 | 69 | } |
| 70 | + foreach( $links as $anchor => $url ) { |
| 71 | + $xml .= $this->_formatLink( $url, $anchor, 'nav' ); |
| 72 | + } |
| 73 | + |
| 74 | + // @todo: image links |
| 75 | + |
67 | 76 | $xml .= "</links>\n"; |
68 | 77 | } |
69 | 78 | $xml .= "</doc>\n"; |
— | — | @@ -138,12 +147,14 @@ |
139 | 148 | |
140 | 149 | /** |
141 | 150 | * 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 |
144 | 153 | * @access private |
145 | | - * @fixme extract TOC items |
| 154 | + * |
| 155 | + * @fixme extract TOC items properly |
| 156 | + * @fixme check for explicit __NOTOC__ |
146 | 157 | */ |
147 | | - function _links( $rev ) { |
| 158 | + function _sectionLinks( $rev ) { |
148 | 159 | $text = Revision::getRevisionText( $rev ); |
149 | 160 | $secs = |
150 | 161 | preg_split( |
— | — | @@ -156,10 +167,55 @@ |
157 | 168 | $header = preg_replace( '/^=+\s*(.*?)\s*=+/', '$1', $secs[$i] ); |
158 | 169 | $anchor = EditPage::sectionAnchor( $header ); |
159 | 170 | $url = $this->title->getFullUrl() . $anchor; |
160 | | - $headers[] = $url; |
| 171 | + $headers[$header] = $url; |
161 | 172 | } |
162 | 173 | return $headers; |
163 | 174 | } |
| 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 | + |
164 | 220 | } |
165 | 221 | |
166 | 222 | class NoredirectFilter extends DumpFilter { |