Index: trunk/extensions/BookManager/BookManager.body.php |
— | — | @@ -1,15 +1,15 @@ |
2 | 2 | <?php |
3 | | -class BookManagerFunctions { |
4 | 3 | /** |
5 | | -* BookManager private functions [Core] |
| 4 | +* BookManager protected functions [Core] |
6 | 5 | */ |
| 6 | +class BookManagerCore extends SpecialPage { |
7 | 7 | const VERSION = "0.1.6 "; |
8 | 8 | private static $chapterList; |
9 | 9 | /** |
10 | 10 | * Get Title |
11 | 11 | * @return Object |
12 | 12 | */ |
13 | | - private static function newTitleObject( &$parser, $text = null ) { |
| 13 | + protected static function newTitleObject( &$parser, $text = null ) { |
14 | 14 | $t = Title::newFromText( $text ); |
15 | 15 | if ( is_null( $t ) ) { |
16 | 16 | return $parser->getTitle(); |
— | — | @@ -22,9 +22,9 @@ |
23 | 23 | * Adaptation of the function "getBookPagePrefixes" from collection extension |
24 | 24 | * (http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Collection/Collection.body.php?revision=79895&view=markup#l440) |
25 | 25 | */ |
26 | | - private static function getBookPagePrefixes() { |
| 26 | + protected static function getBookPagePrefixes() { |
27 | 27 | // global $wgUser; |
28 | | - global $wgCommunityCollectionNamespace; |
| 28 | + global $wgBookManagerPrefixNamespace; |
29 | 29 | |
30 | 30 | $result = array(); |
31 | 31 | /* |
— | — | @@ -40,7 +40,7 @@ |
41 | 41 | $t = wfMsgForContent( 'coll-community_book_prefix' ); |
42 | 42 | if ( wfEmptyMsg( 'coll-community_book_prefix', $t ) || $t == '-' ) { |
43 | 43 | $title = Title::makeTitle( |
44 | | - $wgCommunityCollectionNamespace, |
| 44 | + $wgBookManagerPrefixNamespace, |
45 | 45 | wfMsgForContent( 'coll-collections' ) |
46 | 46 | ); |
47 | 47 | $result['community-prefix'] = $title->getPrefixedText() . '/'; |
— | — | @@ -55,7 +55,7 @@ |
56 | 56 | * Simplification of the function "parseCollectionLine" from collection extension |
57 | 57 | * (http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Collection/Collection.body.php?revision=79895&view=markup#l709) |
58 | 58 | */ |
59 | | - private static function parseCollectionLine( /* Sem uso por enquanto: &$collection, */ $line ) { |
| 59 | + protected static function parseCollectionLine( /* Sem uso por enquanto: &$collection, */ $line ) { |
60 | 60 | $line = trim( $line ); |
61 | 61 | if ( substr( $line, 0, 1 ) == ':' ) { // article |
62 | 62 | $pagename = trim( substr( $line, 1 ) ); |
— | — | @@ -82,7 +82,7 @@ |
83 | 83 | * Adaptation of the function "loadCollection" from collection extension |
84 | 84 | * (http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Collection/Collection.body.php?revision=79895&view=markup#l780) |
85 | 85 | */ |
86 | | - private static function loadListFromCollection( $collectiontitle ) { |
| 86 | + protected static function loadListFromCollection( $collectiontitle ) { |
87 | 87 | if ( is_null( $collectiontitle ) || !$collectiontitle->exists() ) { |
88 | 88 | return false; |
89 | 89 | } |
— | — | @@ -101,7 +101,7 @@ |
102 | 102 | /** |
103 | 103 | * Get the book or chapter name |
104 | 104 | */ |
105 | | - private static function bookparts( &$parser, $text = null, $part = 1 ) { |
| 105 | + protected static function bookparts( &$parser, $text = null, $part = 1 ) { |
106 | 106 | $t = self::newTitleObject( $parser, $text ); |
107 | 107 | // No book should have '/' in it's name, so... |
108 | 108 | $book = explode( "/", $t->getText(), 2 ); // ...given a page with title like 'Foo/Bar/Baz'... |
— | — | @@ -119,8 +119,7 @@ |
120 | 120 | * @param $n Integer Position of wanted page. Next page is +1; Previous page is -1 |
121 | 121 | * @return String The prefixed title or empty string if not found or found but not valid |
122 | 122 | */ |
123 | | - |
124 | | - private static function pageText( &$parser, $text = null, $n = 0 ) { |
| 123 | + protected static function pageText( &$parser, $text = null, $n = 0 ) { |
125 | 124 | $pagetitle = self::newTitleObject( $parser, $text ); |
126 | 125 | $prefixes = self::getBookPagePrefixes(); |
127 | 126 | $booktitle = Title::newFromText( $prefixes['community-prefix'] . self::bookparts( $parser, $text, 0 ) ); // ...the book name will be 'Foo'. |
— | — | @@ -141,9 +140,11 @@ |
142 | 141 | } |
143 | 142 | return wfEscapeWikiText( $otherpagetitle->getText() ); |
144 | 143 | } |
| 144 | +} |
145 | 145 | /** |
146 | 146 | * BookManager Functions [Variables] |
147 | 147 | */ |
| 148 | +class BookManagerVariables extends BookManagerCore { |
148 | 149 | static function register( $parser ) { |
149 | 150 | # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}} |
150 | 151 | # instead of {{#int:...}}) |
— | — | @@ -198,18 +199,15 @@ |
199 | 200 | $t = self::pageText( $parser, $text, - 1 ); |
200 | 201 | return $t; |
201 | 202 | } |
202 | | - |
203 | 203 | static function prevpagenamee( &$parser, $text = null ) { |
204 | 204 | $t = self::pageText( $parser, $text, - 1 ); |
205 | 205 | return wfUrlEncode( $t ); |
206 | 206 | } |
207 | | - |
208 | 207 | static function nextpagename( &$parser, $text = null ) { |
209 | 208 | |
210 | 209 | $t = self::pageText( $parser, $text, + 1 ); |
211 | 210 | return $t; |
212 | 211 | } |
213 | | - |
214 | 212 | static function nextpagenamee( &$parser, $text = null ) { |
215 | 213 | $t = self::pageText( $parser, $text, + 1 ); |
216 | 214 | return wfUrlEncode( $t ); |
— | — | @@ -235,47 +233,52 @@ |
236 | 234 | static function AssignAValue( &$parser, &$cache, &$magicWordId, &$ret ) { |
237 | 235 | switch( $magicWordId ) { |
238 | 236 | case 'prevpagename': |
239 | | - $ret = BookManagerFunctions::prevpagename( $parser ); |
| 237 | + $ret = BookManagerVariables::prevpagename( $parser ); |
240 | 238 | return true; |
241 | 239 | case 'prevpagenamee': |
242 | | - $ret = BookManagerFunctions::prevpagenamee( $parser ); |
| 240 | + $ret = BookManagerVariables::prevpagenamee( $parser ); |
243 | 241 | return true; |
244 | 242 | case 'nextpagename': |
245 | | - $ret = BookManagerFunctions::nextpagename( $parser ); |
| 243 | + $ret = BookManagerVariables::nextpagename( $parser ); |
246 | 244 | return true; |
247 | 245 | case 'nextpagenamee': |
248 | | - $ret = BookManagerFunctions::nextpagenamee( $parser ); |
| 246 | + $ret = BookManagerVariables::nextpagenamee( $parser ); |
249 | 247 | return true; |
250 | 248 | case 'rootpagename': |
251 | | - $ret = BookManagerFunctions::rootpagename( $parser ); |
| 249 | + $ret = BookManagerVariables::rootpagename( $parser ); |
252 | 250 | return true; |
253 | 251 | case 'rootpagenamee': |
254 | | - $ret = BookManagerFunctions::rootpagenamee( $parser ); |
| 252 | + $ret = BookManagerVariables::rootpagenamee( $parser ); |
255 | 253 | return true; |
256 | 254 | case 'chaptername': |
257 | | - $ret = BookManagerFunctions::chaptername( $parser ); |
| 255 | + $ret = BookManagerVariables::chaptername( $parser ); |
258 | 256 | return true; |
259 | 257 | case 'chapternamee': |
260 | | - $ret = BookManagerFunctions::chapternamee( $parser ); |
| 258 | + $ret = BookManagerVariables::chapternamee( $parser ); |
261 | 259 | return true; |
262 | 260 | } |
263 | 261 | return false; |
264 | 262 | } |
265 | | - |
| 263 | +} |
266 | 264 | /** |
267 | 265 | * BookManager Functions [Navigation Bar] |
268 | 266 | * inspired by PageNotice extension |
269 | 267 | * (http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/PageNotice/PageNotice.php&view=markup) |
270 | 268 | */ |
271 | 269 | |
| 270 | +class BookManagerNavBar extends BookManagerCore { |
272 | 271 | |
273 | | - static function addText( &$out, &$text ) { |
| 272 | + private static function camDisplayNavBar( &$out ) { |
274 | 273 | global $wgRequest, $wgBookManagerNamespaces, $wgBookManagerNavBar; |
275 | 274 | $ns = $out->getTitle()->getNamespace(); |
276 | | - # Return True if action is suported |
277 | 275 | $action = $wgRequest->getVal( 'action', 'view' ); |
278 | | - $isViewAction = ( $action == 'view' || $action == 'purge' ); |
279 | | - if ( !$wgBookManagerNavBar || !in_array( $ns, $wgBookManagerNamespaces ) || !$isViewAction ) { |
| 276 | + $isViewAction = ( $action == 'view' || $action == 'purge' || $action == 'submit' ); |
| 277 | + $t = ( $wgBookManagerNavBar && in_array( $ns, $wgBookManagerNamespaces ) && $isViewAction ); |
| 278 | + return $t; |
| 279 | + } |
| 280 | + static function addNavBar( &$out, &$sk ) { |
| 281 | + global $wgParser; |
| 282 | + if ( !BookManagerNavBar::camDisplayNavBar( $out ) ) { |
280 | 283 | return true; |
281 | 284 | } |
282 | 285 | $opt = array( |
— | — | @@ -284,43 +287,34 @@ |
285 | 288 | # Get $out title |
286 | 289 | $currenttitletext = $out->getTitle()->getText(); |
287 | 290 | # Get: prev, next and base chapter from the list |
288 | | - $prev = self::pageText( $parser, $currenttitletext, - 1 ); |
289 | | - |
290 | | - $next = self::pageText( $parser, $currenttitletext, + 1 ); |
| 291 | + $prev = self::pageText( $wgParser, $currenttitletext, - 1 ); |
| 292 | + $base = Title::newFromText( $currenttitletext )->getBaseText(); |
| 293 | + $next = self::pageText( $wgParser, $currenttitletext, + 1 ); |
291 | 294 | if ( $prev === '' && $next === '' ) { |
292 | 295 | return true; |
293 | 296 | } |
294 | | - # Return True if page exists in the chapter list |
295 | | - $is_inChapterList = ( $prev !== '' || $next !== '' ); |
296 | | - # Return True if Message is non empty |
297 | | - $MsgIsEmpty = ( wfEmptyMsg( "BookManager" ) |
298 | | - && wfEmptyMsg( "BookManager-top" ) |
299 | | - && wfEmptyMsg( "BookManager-bottom" )); |
300 | 297 | # Generate HTML or system messages values( $1 for $prev, $2 for $prevtext, $3 for $base, $4 for $basetext, $5 for $next and $6 for $nexttext ). |
301 | 298 | $prevtext = ( $prev !== '' ) ? Title::newFromText( $prev )->getSubpageText(): '' ; |
| 299 | + $basetext = Title::newFromText( $base )->getSubpageText(); |
302 | 300 | $nexttext = ( $next !== '' ) ? Title::newFromText( $next )->getSubpageText(): '' ; |
303 | | - if ( $MsgIsEmpty ) { |
304 | | - $defaultBar = Xml::openElement( 'ul', array( 'class' => 'mw-book-navigation' ) ); |
305 | | - if ( $prev !== '' ) { |
306 | | - $prevlink = Title::newFromText( $prev )->getLocalURL(); |
307 | | - $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-prev' ) ); |
308 | | - $defaultBar .= Xml::element( 'a', array( 'href' => $prevlink, 'title' => $prev ), $prevtext ); |
309 | | - $defaultBar .= Xml::closeElement( 'li' ); |
310 | | - } |
311 | | - $base = Title::newFromText( $currenttitletext )->getBaseText(); |
312 | | - $basetext = Title::newFromText( $base )->getSubpageText(); |
313 | | - $baselink = Title::newFromText( $base )->getLocalURL(); |
314 | | - $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-index' ) ); |
315 | | - $defaultBar .= Xml::element( 'a', array( 'href' => $baselink, 'title' => $base ), $basetext ); |
| 301 | + $defaultBar = Xml::openElement( 'ul', array( 'class' => 'mw-book-navigation' ) ); |
| 302 | + if ( $prev !== '' ) { |
| 303 | + $prevlink = Title::newFromText( $prev )->getLocalURL(); |
| 304 | + $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-prev' ) ); |
| 305 | + $defaultBar .= Xml::element( 'a', array( 'href' => $prevlink, 'title' => $prev ), $prevtext ); |
316 | 306 | $defaultBar .= Xml::closeElement( 'li' ); |
317 | | - if ( $next !== '' ) { |
318 | | - $nextlink = Title::newFromText( $next )->getLocalURL(); |
319 | | - $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-next' ) ); |
320 | | - $defaultBar .= Xml::element( 'a', array( 'href' => $nextlink, 'title' => $next ), $nexttext ); |
321 | | - $defaultBar .= Xml::closeElement( 'li' ); |
322 | | - } |
323 | | - $defaultBar .= Xml::closeElement( 'ul' ); |
324 | 307 | } |
| 308 | + $baselink = Title::newFromText( $base )->getLocalURL(); |
| 309 | + $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-index' ) ); |
| 310 | + $defaultBar .= Xml::element( 'a', array( 'href' => $baselink, 'title' => $base ), $basetext ); |
| 311 | + $defaultBar .= Xml::closeElement( 'li' ); |
| 312 | + if ( $next !== '' ) { |
| 313 | + $nextlink = Title::newFromText( $next )->getLocalURL(); |
| 314 | + $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-next' ) ); |
| 315 | + $defaultBar .= Xml::element( 'a', array( 'href' => $nextlink, 'title' => $next ), $nexttext ); |
| 316 | + $defaultBar .= Xml::closeElement( 'li' ); |
| 317 | + } |
| 318 | + $defaultBar .= Xml::closeElement( 'ul' ); |
325 | 319 | # Gets navigation bar from custom system messages or from default defined above |
326 | 320 | $customBoth = wfEmptyMsg( 'BookManager' ) ? false : 'BookManager'; |
327 | 321 | $customTop = wfEmptyMsg( 'BookManager-top' ) ? $customBoth : 'BookManager-top'; |
— | — | @@ -330,22 +324,62 @@ |
331 | 325 | ); |
332 | 326 | if ( $customTop ) { |
333 | 327 | $top = wfMsgExt( $customTop, $opt, $prev, $prevtext, $base, $basetext, $next, $nexttext ); |
334 | | - } else { |
| 328 | + } |
| 329 | + else { |
335 | 330 | $top = $defaultBar; |
336 | 331 | } |
337 | 332 | if ( $customBottom ) { |
338 | 333 | $bottom = wfMsgExt( $customBottom, $opt, $prev, $prevtext, $base, $basetext, $next, $nexttext ); |
339 | | - } else { |
| 334 | + } |
| 335 | + else { |
340 | 336 | $bottom = $defaultBar; |
341 | 337 | } |
342 | 338 | # Adds navigation before and after the page text |
343 | | - $text = "<div>$top</div>\n$text\n<div>$bottom</div>"; |
| 339 | + $out->prependHTML( "<div>$top</div>" ); |
| 340 | + $out->addHTML( "<div>$bottom</div>" ); |
| 341 | + # adds CSS and JS to navigation bar |
| 342 | + $out->addModuleStyles( 'ext.BookManager' ); |
| 343 | + //$out->addModules( 'ext.BookManager' ); |
344 | 344 | return true; |
345 | 345 | } |
346 | | - # adds CSS and JS to navigation bar |
347 | | - static function injectStyleAndJS( &$out, &$sk ) { |
348 | | - $out->addModuleStyles( 'ext.BookManager' ); |
349 | | - $out->addModules( 'ext.BookManager' ); |
350 | | - return true; |
| 346 | +} |
| 347 | +/** |
| 348 | +* BookManager Functions [PrintVersion] |
| 349 | +*/ |
| 350 | +class PrintVersion extends BookManagerCore { |
| 351 | + |
| 352 | + function __construct() { |
| 353 | + parent::__construct( 'PrintVersion' ); |
351 | 354 | } |
| 355 | + function execute( $book ) { |
| 356 | + global $wgOut, $wgRequest; |
| 357 | + |
| 358 | + $this->setHeaders(); |
| 359 | + $this->outputHeader(); |
| 360 | + |
| 361 | + $book = !is_null( $book ) ? $book : $wgRequest->getVal( 'book' ); |
| 362 | + if ( !isset( $book ) ) { |
| 363 | + $wgOut->addWikiMsg( 'printversion-no-book' ); |
| 364 | + return; |
| 365 | + } |
| 366 | + $prefixes = self::getBookPagePrefixes(); |
| 367 | + $booktitle = Title::newFromText( $prefixes['community-prefix'] . $book ); |
| 368 | + $chapterList = self::loadListFromCollection( $booktitle ); |
| 369 | + if ( $chapterList === false ) { |
| 370 | + $wgOut->addWikiMsg( 'printversion-inexistent-book' ); |
| 371 | + return; |
| 372 | + } |
| 373 | + $text = ''; |
| 374 | + foreach ( $chapterList as $chapter ) { |
| 375 | + $chaptertitle = Title::newFromText( $chapter ); |
| 376 | + $sectionname = $chaptertitle->getSubpageText(); |
| 377 | + $text .= "= $sectionname =\n"; |
| 378 | + $text .= "{{:$chapter}}\n\n"; |
| 379 | + } |
| 380 | + $wgOut->addWikiText( $text ); |
| 381 | + } |
| 382 | + |
352 | 383 | } |
| 384 | + |
| 385 | + |
| 386 | + |