r85238 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85237‎ | r85238 | r85239 >
Date:10:21, 3 April 2011
Author:raylton.sousa
Status:deferred (Comments)
Tags:
Comment:
fix of last commit
Modified paths:
  • /trunk/extensions/BookManager/BookManager.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/BookManager/BookManager.body.php
@@ -1,15 +1,15 @@
22 <?php
3 -class BookManagerFunctions {
43 /**
5 -* BookManager private functions [Core]
 4+* BookManager protected functions [Core]
65 */
 6+class BookManagerCore extends SpecialPage {
77 const VERSION = "0.1.6 ";
88 private static $chapterList;
99 /**
1010 * Get Title
1111 * @return Object
1212 */
13 - private static function newTitleObject( &$parser, $text = null ) {
 13+ protected static function newTitleObject( &$parser, $text = null ) {
1414 $t = Title::newFromText( $text );
1515 if ( is_null( $t ) ) {
1616 return $parser->getTitle();
@@ -22,9 +22,9 @@
2323 * Adaptation of the function "getBookPagePrefixes" from collection extension
2424 * (http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Collection/Collection.body.php?revision=79895&view=markup#l440)
2525 */
26 - private static function getBookPagePrefixes() {
 26+ protected static function getBookPagePrefixes() {
2727 // global $wgUser;
28 - global $wgCommunityCollectionNamespace;
 28+ global $wgBookManagerPrefixNamespace;
2929
3030 $result = array();
3131 /*
@@ -40,7 +40,7 @@
4141 $t = wfMsgForContent( 'coll-community_book_prefix' );
4242 if ( wfEmptyMsg( 'coll-community_book_prefix', $t ) || $t == '-' ) {
4343 $title = Title::makeTitle(
44 - $wgCommunityCollectionNamespace,
 44+ $wgBookManagerPrefixNamespace,
4545 wfMsgForContent( 'coll-collections' )
4646 );
4747 $result['community-prefix'] = $title->getPrefixedText() . '/';
@@ -55,7 +55,7 @@
5656 * Simplification of the function "parseCollectionLine" from collection extension
5757 * (http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Collection/Collection.body.php?revision=79895&view=markup#l709)
5858 */
59 - private static function parseCollectionLine( /* Sem uso por enquanto: &$collection, */ $line ) {
 59+ protected static function parseCollectionLine( /* Sem uso por enquanto: &$collection, */ $line ) {
6060 $line = trim( $line );
6161 if ( substr( $line, 0, 1 ) == ':' ) { // article
6262 $pagename = trim( substr( $line, 1 ) );
@@ -82,7 +82,7 @@
8383 * Adaptation of the function "loadCollection" from collection extension
8484 * (http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Collection/Collection.body.php?revision=79895&view=markup#l780)
8585 */
86 - private static function loadListFromCollection( $collectiontitle ) {
 86+ protected static function loadListFromCollection( $collectiontitle ) {
8787 if ( is_null( $collectiontitle ) || !$collectiontitle->exists() ) {
8888 return false;
8989 }
@@ -101,7 +101,7 @@
102102 /**
103103 * Get the book or chapter name
104104 */
105 - private static function bookparts( &$parser, $text = null, $part = 1 ) {
 105+ protected static function bookparts( &$parser, $text = null, $part = 1 ) {
106106 $t = self::newTitleObject( $parser, $text );
107107 // No book should have '/' in it's name, so...
108108 $book = explode( "/", $t->getText(), 2 ); // ...given a page with title like 'Foo/Bar/Baz'...
@@ -119,8 +119,7 @@
120120 * @param $n Integer Position of wanted page. Next page is +1; Previous page is -1
121121 * @return String The prefixed title or empty string if not found or found but not valid
122122 */
123 -
124 - private static function pageText( &$parser, $text = null, $n = 0 ) {
 123+ protected static function pageText( &$parser, $text = null, $n = 0 ) {
125124 $pagetitle = self::newTitleObject( $parser, $text );
126125 $prefixes = self::getBookPagePrefixes();
127126 $booktitle = Title::newFromText( $prefixes['community-prefix'] . self::bookparts( $parser, $text, 0 ) ); // ...the book name will be 'Foo'.
@@ -141,9 +140,11 @@
142141 }
143142 return wfEscapeWikiText( $otherpagetitle->getText() );
144143 }
 144+}
145145 /**
146146 * BookManager Functions [Variables]
147147 */
 148+class BookManagerVariables extends BookManagerCore {
148149 static function register( $parser ) {
149150 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
150151 # instead of {{#int:...}})
@@ -198,18 +199,15 @@
199200 $t = self::pageText( $parser, $text, - 1 );
200201 return $t;
201202 }
202 -
203203 static function prevpagenamee( &$parser, $text = null ) {
204204 $t = self::pageText( $parser, $text, - 1 );
205205 return wfUrlEncode( $t );
206206 }
207 -
208207 static function nextpagename( &$parser, $text = null ) {
209208
210209 $t = self::pageText( $parser, $text, + 1 );
211210 return $t;
212211 }
213 -
214212 static function nextpagenamee( &$parser, $text = null ) {
215213 $t = self::pageText( $parser, $text, + 1 );
216214 return wfUrlEncode( $t );
@@ -235,47 +233,52 @@
236234 static function AssignAValue( &$parser, &$cache, &$magicWordId, &$ret ) {
237235 switch( $magicWordId ) {
238236 case 'prevpagename':
239 - $ret = BookManagerFunctions::prevpagename( $parser );
 237+ $ret = BookManagerVariables::prevpagename( $parser );
240238 return true;
241239 case 'prevpagenamee':
242 - $ret = BookManagerFunctions::prevpagenamee( $parser );
 240+ $ret = BookManagerVariables::prevpagenamee( $parser );
243241 return true;
244242 case 'nextpagename':
245 - $ret = BookManagerFunctions::nextpagename( $parser );
 243+ $ret = BookManagerVariables::nextpagename( $parser );
246244 return true;
247245 case 'nextpagenamee':
248 - $ret = BookManagerFunctions::nextpagenamee( $parser );
 246+ $ret = BookManagerVariables::nextpagenamee( $parser );
249247 return true;
250248 case 'rootpagename':
251 - $ret = BookManagerFunctions::rootpagename( $parser );
 249+ $ret = BookManagerVariables::rootpagename( $parser );
252250 return true;
253251 case 'rootpagenamee':
254 - $ret = BookManagerFunctions::rootpagenamee( $parser );
 252+ $ret = BookManagerVariables::rootpagenamee( $parser );
255253 return true;
256254 case 'chaptername':
257 - $ret = BookManagerFunctions::chaptername( $parser );
 255+ $ret = BookManagerVariables::chaptername( $parser );
258256 return true;
259257 case 'chapternamee':
260 - $ret = BookManagerFunctions::chapternamee( $parser );
 258+ $ret = BookManagerVariables::chapternamee( $parser );
261259 return true;
262260 }
263261 return false;
264262 }
265 -
 263+}
266264 /**
267265 * BookManager Functions [Navigation Bar]
268266 * inspired by PageNotice extension
269267 * (http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/PageNotice/PageNotice.php&view=markup)
270268 */
271269
 270+class BookManagerNavBar extends BookManagerCore {
272271
273 - static function addText( &$out, &$text ) {
 272+ private static function camDisplayNavBar( &$out ) {
274273 global $wgRequest, $wgBookManagerNamespaces, $wgBookManagerNavBar;
275274 $ns = $out->getTitle()->getNamespace();
276 - # Return True if action is suported
277275 $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 ) ) {
280283 return true;
281284 }
282285 $opt = array(
@@ -284,43 +287,34 @@
285288 # Get $out title
286289 $currenttitletext = $out->getTitle()->getText();
287290 # 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 );
291294 if ( $prev === '' && $next === '' ) {
292295 return true;
293296 }
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" ));
300297 # 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 ).
301298 $prevtext = ( $prev !== '' ) ? Title::newFromText( $prev )->getSubpageText(): '' ;
 299+ $basetext = Title::newFromText( $base )->getSubpageText();
302300 $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 );
316306 $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' );
324307 }
 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' );
325319 # Gets navigation bar from custom system messages or from default defined above
326320 $customBoth = wfEmptyMsg( 'BookManager' ) ? false : 'BookManager';
327321 $customTop = wfEmptyMsg( 'BookManager-top' ) ? $customBoth : 'BookManager-top';
@@ -330,22 +324,62 @@
331325 );
332326 if ( $customTop ) {
333327 $top = wfMsgExt( $customTop, $opt, $prev, $prevtext, $base, $basetext, $next, $nexttext );
334 - } else {
 328+ }
 329+ else {
335330 $top = $defaultBar;
336331 }
337332 if ( $customBottom ) {
338333 $bottom = wfMsgExt( $customBottom, $opt, $prev, $prevtext, $base, $basetext, $next, $nexttext );
339 - } else {
 334+ }
 335+ else {
340336 $bottom = $defaultBar;
341337 }
342338 # 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' );
344344 return true;
345345 }
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' );
351354 }
 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+
352383 }
 384+
 385+
 386+

Comments

#Comment by He7d3r (talk | contribs)   14:35, 19 September 2011

There is a typo on "camDisplayNavBar". It should be "canDisplayNavBar".

Status & tagging log