r88636 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88635‎ | r88636 | r88637 >
Date:12:44, 23 May 2011
Author:raylton.sousa
Status:deferred
Tags:
Comment:
*Code convection.
*Add first draft with auto categorization for books.
*Modifying sidebar
Modified paths:
  • /trunk/extensions/BookManager/BookManager.body.php (modified) (history)
  • /trunk/extensions/BookManager/BookManager.php (modified) (history)

Diff [purge]

Index: trunk/extensions/BookManager/BookManager.body.php
@@ -8,7 +8,7 @@
99 private static $chapterList;
1010 /**
1111 * Get Title
12 - * @param $text String Text for title of current page
 12+ * @param $text String Text for title of current page
1313 * @return Object
1414 */
1515 protected static function newTitleObject( &$parser, $text = null ) {
@@ -102,20 +102,20 @@
103103 }
104104
105105 /**
106 - * Get the book or chapter name from page title
107 - * @param $text String Text for title of current page
 106+ * Get the book or chapter name from page title
 107+ * @param $text String Text for title of current page
108108 * @param $part Integer. Title like 'Foo/Bar/Baz' "0" is Foo , the book name, and "1" is Bar/Baz, the chapter name.
109109 * @return String with book or chapter
110110 */
111 - protected static function bookparts( &$parser, $text = null, $part = 1 ) {
112 - $t = self::newTitleObject( $parser, $text );
 111+ protected static function bookParts( &$parser, $text = null, $part = 1 ) {
 112+ $t = self::newTitleObject( $parser, $text )->getText();
113113 // No book should have '/' in it's name, so...
114 - $book = explode( "/", $t->getText(), 2 );
 114+ $book = explode( "/", $t , 2 );
115115 if ( count( $book ) > 1 ) {
116116 return $book[$part];
117117 }
118118 else {
119 - return $t->getText();
 119+ return $t;
120120 }
121121
122122 }
@@ -126,30 +126,30 @@
127127 * @return String The prefixed title or empty string if not found or found but not valid
128128 */
129129 protected static function pageText( &$parser, $text = null, $p = 0 ) {
130 - $pagetitle = self::newTitleObject( $parser, $text );
 130+ $pageTitle = self::newTitleObject( $parser, $text );
131131 $prefixes = self::getBookPagePrefixes();
132 - $booktitle = Title::newFromText( $prefixes['community-prefix'] . self::bookparts( $parser, $text, 0 ) ); // ...the book name will be 'Foo'.
 132+ $bookTitle = Title::newFromText( $prefixes['community-prefix'] . self::bookParts( $parser, $text, 0 ) ); // ...the book name will be 'Foo'.
133133
134134 if ( !self::$chapterList ) {
135 - self::$chapterList = self::loadListFromCollection( $booktitle );
 135+ self::$chapterList = self::loadListFromCollection( $bookTitle );
136136 }
137137 if ( self::$chapterList === false ) {
138138 return '';
139139 }
140 - $current = array_search( $pagetitle, self::$chapterList );
 140+ $current = array_search( $pageTitle, self::$chapterList );
141141 if ( $current === false || !isset( self::$chapterList[ $current + $p ] ) ) {
142142 return '';
143143 }
144 - $otherpagetitle = Title::newFromText( self::$chapterList[ $current + $p ] );
145 - if ( is_null( $otherpagetitle ) ) {
 144+ $otherPageTitle = Title::newFromText( self::$chapterList[ $current + $p ] );
 145+ if ( is_null( $otherPageTitle ) ) {
146146 return '';
147147 }
148 - if ( $p == 'rand' ){
 148+ if ( $p == 'rand' ) {
149149 $limit = count( self::$chapterList ) - 1;
150150 $randPosition = rand( 0, $limit );
151 - return Title::newFromText( self::$chapterList[ $randPosition ] )->getText();
 151+ $otherPageTitle = Title::newFromText( self::$chapterList[ $randPosition ] );
152152 }
153 - return wfEscapeWikiText( $otherpagetitle->getText() );
 153+ return wfEscapeWikiText( $otherPageTitle->getText() );
154154 }
155155 }
156156 /**
@@ -208,19 +208,19 @@
209209 return wfUrlEncode( $t );
210210 }
211211 static function rootpagename( &$parser, $text = null ) {
212 - $t = self::bookparts( $parser, $text, 0 );
 212+ $t = self::bookParts( $parser, $text, 0 );
213213 return $t;
214214 }
215215 static function rootpagenamee( &$parser, $text = null ) {
216 - $t = self::bookparts( $parser, $text, 0 );
 216+ $t = self::bookParts( $parser, $text, 0 );
217217 return wfUrlEncode( $t );
218218 }
219219 static function chaptername( &$parser, $text = null ) {
220 - $t = self::bookparts( $parser, $text, 1 );
 220+ $t = self::bookParts( $parser, $text, 1 );
221221 return $t;
222222 }
223223 static function chapternamee( &$parser, $text = null ) {
224 - $t = self::bookparts( $parser, $text, 1 );
 224+ $t = self::bookParts( $parser, $text, 1 );
225225 return wfUrlEncode( $t );
226226 }
227227 static function randomchapter( &$parser, $text = null ) {
@@ -278,9 +278,9 @@
279279
280280 class BookManagerNavBar extends BookManagerCore {
281281
282 - private static function camDisplayNavBar( &$out ) {
 282+ private static function camDisplayNavBar( $title ) {
283283 global $wgRequest, $wgBookManagerNamespaces, $wgBookManagerNavBar;
284 - $ns = $out->getTitle()->getNamespace();
 284+ $ns = $title->getNamespace();
285285 $action = $wgRequest->getVal( 'action', 'view' );
286286 $isViewAction = ( $action == 'view' || $action == 'purge' || $action == 'submit' );
287287 $t = ( $wgBookManagerNavBar && in_array( $ns, $wgBookManagerNamespaces ) && $isViewAction );
@@ -288,40 +288,41 @@
289289 }
290290 static function addNavBar( &$out, &$sk ) {
291291 global $wgParser;
292 - if ( !BookManagerNavBar::camDisplayNavBar( $out ) ) {
 292+ $title = $out->getTitle();
 293+ if ( !BookManagerNavBar::camDisplayNavBar( $title ) ) {
293294 return true;
294295 }
295296 $opt = array(
296297 'parseinline',
297298 );
298299 # Get $out title
299 - $currenttitletext = $out->getTitle()->getText();
 300+ $currentTitleText = $title->getText();
300301 # Get: prev, next and base chapter from the list
301 - $prev = self::pageText( $wgParser, $currenttitletext, - 1 );
302 - $base = Title::newFromText( $currenttitletext )->getBaseText();
303 - $next = self::pageText( $wgParser, $currenttitletext, + 1 );
 302+ $prev = self::pageText( $wgParser, $currentTitleText, - 1 );
 303+ $base = Title::newFromText( $currentTitleText )->getBaseText();
 304+ $next = self::pageText( $wgParser, $currentTitleText, + 1 );
304305 if ( $prev === '' && $next === '' ) {
305306 return true;
306307 }
307308 # 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 ).
308 - $prevtext = ( $prev !== '' ) ? Title::newFromText( $prev )->getSubpageText(): '' ;
309 - $basetext = Title::newFromText( $base )->getSubpageText();
310 - $nexttext = ( $next !== '' ) ? Title::newFromText( $next )->getSubpageText(): '' ;
 309+ $prevText = ( $prev !== '' ) ? Title::newFromText( $prev )->getSubpageText(): '' ;
 310+ $baseText = Title::newFromText( $base )->getSubpageText();
 311+ $nextText = ( $next !== '' ) ? Title::newFromText( $next )->getSubpageText(): '' ;
311312 $defaultBar = Xml::openElement( 'ul', array( 'class' => 'mw-book-navigation' ) );
312313 if ( $prev !== '' ) {
313 - $prevlink = Title::newFromText( $prev )->getLocalURL();
 314+ $prevLink = Title::newFromText( $prev )->getLocalURL();
314315 $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-prev' ) );
315 - $defaultBar .= Xml::element( 'a', array( 'href' => $prevlink, 'title' => $prev ), $prevtext );
 316+ $defaultBar .= Xml::element( 'a', array( 'href' => $prevLink, 'title' => $prev ), $prevText );
316317 $defaultBar .= Xml::closeElement( 'li' );
317318 }
318 - $baselink = Title::newFromText( $base )->getLocalURL();
 319+ $baseLink = Title::newFromText( $base )->getLocalURL();
319320 $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-index' ) );
320 - $defaultBar .= Xml::element( 'a', array( 'href' => $baselink, 'title' => $base ), $basetext );
 321+ $defaultBar .= Xml::element( 'a', array( 'href' => $baseLink, 'title' => $base ), $baseText );
321322 $defaultBar .= Xml::closeElement( 'li' );
322323 if ( $next !== '' ) {
323 - $nextlink = Title::newFromText( $next )->getLocalURL();
 324+ $nextLink = Title::newFromText( $next )->getLocalURL();
324325 $defaultBar .= Xml::openElement( 'li', array( 'class' => 'mw-next' ) );
325 - $defaultBar .= Xml::element( 'a', array( 'href' => $nextlink, 'title' => $next ), $nexttext );
 326+ $defaultBar .= Xml::element( 'a', array( 'href' => $nextLink, 'title' => $next ), $nextText );
326327 $defaultBar .= Xml::closeElement( 'li' );
327328 }
328329 $defaultBar .= Xml::closeElement( 'ul' );
@@ -333,13 +334,13 @@
334335 'parseinline',
335336 );
336337 if ( $customTop ) {
337 - $top = wfMsgExt( $customTop, $opt, $prev, $prevtext, $base, $basetext, $next, $nexttext );
 338+ $top = wfMsgExt( $customTop, $opt, $prev, $prevText, $base, $baseText, $next, $nextText );
338339 }
339340 else {
340341 $top = $defaultBar;
341342 }
342343 if ( $customBottom ) {
343 - $bottom = wfMsgExt( $customBottom, $opt, $prev, $prevtext, $base, $basetext, $next, $nexttext );
 344+ $bottom = wfMsgExt( $customBottom, $opt, $prev, $prevText, $base, $baseText, $next, $nextText );
344345 }
345346 else {
346347 $bottom = $defaultBar;
@@ -352,39 +353,58 @@
353354 $out->addModules( 'ext.BookManager' );
354355 return true;
355356 }
356 - //known BUG: The category appears more than once when action is not 'view'
 357+ // @bug The category appears more than once when action is not 'view'
357358 static function CatByPrefix( &$parser, &$text ) {
358 - global $wgOut, $wgCategorizationByPrefix ;
359 - if ( $wgCategorizationByPrefix && BookManagerNavBar::camDisplayNavBar( $wgOut ) ) {
360 - $catTitle = Title::newFromText( self::bookparts( $parser, $text, 0 ));
 359+ global $wgCategorizeSubPages, $wgCategorizeRootPages;
 360+ if ( BookManagerNavBar::camDisplayNavBar( $parser->getTitle() ) ) {
 361+ $opt = array(
 362+ 'parseinline',
 363+ );
361364 $parserOutput = $parser->getOutput();
362 - $parserOutput->addCategory( $catTitle->getDBkey() , $catTitle->getText() );
 365+ $rootTitle = Title::newFromText( self::bookParts( $parser, $text, 0 ) );
 366+ if ( $wgCategorizeRootPages && $rootTitle->getText() == $parser->getTitle()->getText() && !wfEmptyMsg( 'bm-allrootpages' ) ) {
 367+ $rootPagesCat = Title::newFromText( wfMsgExt( 'bm-allrootpages', $opt ) );
 368+ $parserOutput->addCategory( $rootPagesCat->getDBkey() , $rootPagesCat->getText() );
 369+ }
 370+ if ( $wgCategorizeSubPages ) {
 371+ $subPagesCat = $rootTitle;
 372+ $parserOutput->addCategory( $subPagesCat->getDBkey() , $subPagesCat->getText() );
 373+ }
 374+
363375 }
364376 return true;
365377 }
366378
367379 static function bookToolboxSection( &$sk, &$toolbox ) {
368 - global $wgTitle, $wgParser, $wgBookSidebarSection ;
369 - $currenttitletext = $wgTitle->getText();
370 - $randchapter = self::pageText( $wgParser, $currenttitletext, 'rand' );
371 - # Add book tools section and all your items
372 - if ( $wgBookSidebarSection && $randchapter ){
373 - ?><div class="portal" id='p-tb'><?php
374 - ?><h5><?php $sk->msg( 'bm-booktools-section' ); ?></h5><?php
375 - ?><div class="body"><?php
376 - ?><ul><?php
377 - ?><li id="t-rating"><?php
378 - ?><a href="<?php echo htmlspecialchars( Title::newFromText( $randchapter )->getLocalURL() ) ?>"><?php
379 - echo $sk->msg( 'bm-randomchapter-link' );
380 - ?></a><?php
381 - ?></li><?php
382 - ?></ul><?php
383 - ?></div><?php
384 - ?></div><?php
 380+ global $wgParser, $wgBookSidebarSection ;
 381+ $currentTitleText = $sk->data['title'];
 382+ $randChapter = self::pageText( $wgParser, $currentTitleText, 'rand' );
 383+ # Add book tools section and all your items
 384+ if ( $wgBookSidebarSection && $randChapter ) {
 385+ $opt = array(
 386+ 'parseinline',
 387+ );
 388+ $bookSection = wfMsgExt( 'bm-booktools-section', $opt );
 389+ $randLink = Title::newFromText( $randChapter )->getLocalURL();
 390+ $randTitle = wfMsgExt( 'bm-randomchapter-link', $opt );
 391+ $toolBox = Xml::openElement( 'div', array( 'class' => 'portal', 'id' => 'p-tb' ) );
 392+ $toolBox .= Xml::openElement( 'h5' );
 393+ $toolBox .= $bookSection;
 394+ $toolBox .= Xml::closeElement( 'h5' );
 395+ $toolBox .= Xml::openElement( 'div', array( 'class' => 'body' ) );
 396+ $toolBox .= Xml::openElement( 'ul' );
 397+ $toolBox .= Xml::openElement( 'li' , array( 'id' => 't-booktools' ) );
 398+ $toolBox .= Xml::element( 'a', array( 'href' => $randLink ) , $randTitle );
 399+ $toolBox .= Xml::closeElement( 'li' );
 400+ $toolBox .= Xml::closeElement( 'ul' );
 401+ $toolBox .= Xml::closeElement( 'div' );
 402+ $toolBox .= Xml::closeElement( 'div' );
 403+ echo $toolBox;
385404 }
386405 return true;
387406 }
388407 }
 408+
389409 /**
390410 * BookManager Functions [PrintVersion]
391411 */
@@ -394,31 +414,31 @@
395415 parent::__construct( 'PrintVersion' );
396416 }
397417 function execute( $book ) {
398 - global $wgOut, $wgRequest;
399 -
 418+ $request = $this->getRequest();
 419+ $out = $this->getOutput();
400420 $this->setHeaders();
401421 $this->outputHeader();
402 -
403 - $book = !is_null( $book ) ? $book : $wgRequest->getVal( 'book' );
 422+ $book = !is_null( $book ) ? $book : $request->getVal( 'book' );
404423 if ( !isset( $book ) ) {
405 - $wgOut->addWikiMsg( 'bm-printversion-no-book' );
 424+ $out->addWikiMsg( 'bm-printversion-no-book' );
406425 return;
407426 }
408427 $prefixes = self::getBookPagePrefixes();
409 - $booktitle = Title::newFromText( $prefixes['community-prefix'] . $book );
410 - $chapterList = self::loadListFromCollection( $booktitle );
 428+ $bookTitle = Title::newFromText( $prefixes['community-prefix'] . $book );
 429+ $chapterList = self::loadListFromCollection( $bookTitle );
411430 if ( $chapterList === false ) {
412 - $wgOut->addWikiMsg( 'bm-printversion-inexistent-book' );
 431+ $out->addWikiMsg( 'bm-printversion-inexistent-book' );
413432 return;
414433 }
415434 $text = '';
416435 foreach ( $chapterList as $chapter ) {
417 - $chaptertitle = Title::newFromText( $chapter );
418 - $sectionname = $chaptertitle->getSubpageText();
 436+ $chapterTitle = Title::newFromText( $chapter );
 437+ $chapterPage = new Article( $chapterTitle );
 438+ $sectionname = $chapterTitle->getSubpageText();
419439 $text .= "= $sectionname =\n";
420 - $text .= "{{:$chapter}}\n\n";
 440+ $text .= $chapterPage->getContent() . "\n\n";
421441 }
422 - $wgOut->addWikiText( $text );
 442+ $out->addWikiText( $text );
423443 }
424444
425445 }
Index: trunk/extensions/BookManager/BookManager.php
@@ -12,18 +12,28 @@
1313 * - CHAPTERNAME (get chapter)
1414 * - CHAPTERNAMEE (get chapter encode)
1515 * - RANDOMCHAPTER (get random page)
16 - * - RANDOMCHAPTERE (get random page encode)
 16+ * - RANDOMCHAPTERE (get random page encode)
1717 * @addtogroup Extensions
1818 * @author Raylton P. Sousa
1919 * @author Helder.wiki
2020 * @copyright Copyright © 2011 Raylton P. Sousa <raylton.sousa@gmail.com>
21 - * @copyright Copyright © 2011 Helder.wiki
 21+ * @copyright Copyright © 2011 Helder.wiki
2222 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 3.0 or later
2323 */
2424
2525 if ( !defined( 'MEDIAWIKI' ) ) {
2626 die( 'This file is a MediaWiki extension, it is not a valid entry point' );
2727 }
 28+$wgBookSidebarSection = true;
 29+$wgBookManagerNamespaces = array( NS_MAIN );
 30+$wgBookManagerVariables = true ;
 31+$wgBookManagerNavBar = true;
 32+$wgCategorizeSubPages = true;
 33+$wgCategorizeRootPages = true;
 34+/* Copyied from extensions/Collection/Collection.php */
 35+/** Namespace for "community books" */
 36+$wgBookManagerPrefixNamespace = NS_PROJECT;
 37+
2838 $dir = dirname( __FILE__ );
2939 $wgParserTestFiles[] = $dir . "/BookManagerParserTests.txt";
3040 $wgAutoloadClasses['BookManagerCore'] = $dir . '/BookManager.body.php';
@@ -53,17 +63,10 @@
5464 'styles' => 'bookmanager.css',
5565 'messages' => array( 'BookManager', 'BookManager-top', 'BookManager-bottom' ),
5666 'dependencies' => array( 'jquery', 'mediawiki.util' ),
57 - 'localBasePath' => $dir. '/client',
 67+ 'localBasePath' => $dir . '/client',
5868 'remoteExtPath' => 'BookManager/client'
5969 );
6070
61 -$wgBookManagerNamespaces = array( NS_MAIN );
62 -$wgBookManagerVariables = true ;
63 -$wgBookManagerNavBar = true;
64 -$wgCategorizationByPrefix = true;
65 -/* Copyied from extensions/Collection/Collection.php */
66 -/** Namespace for "community books" */
67 -$wgBookManagerPrefixNamespace = NS_PROJECT;
6871 /**** Register magic words ****/
6972 if ( $wgBookManagerVariables ) {
7073 $wgAutoloadClasses['BookManagerVariables'] = $dir . '/BookManager.body.php';
@@ -77,8 +80,9 @@
7881 /**** Navbar ****/
7982
8083 $wgHooks['BeforePageDisplay'][] = 'BookManagerNavBar::addNavBar';
81 -/**** Toolbox Section ***/
 84+/**** Toolbox Section ***/
 85+// $wgHooks['SkinTemplateToolboxEnd'][] = 'BookManagerNavBar::bookToolboxSection';
8286 $wgHooks['BaseTemplateToolbox'][] = 'BookManagerNavBar::bookToolboxSection';
83 -
 87+/*** Cat ***/
8488 $wgHooks['ParserAfterTidy'][] = 'BookManagerNavBar::CatByPrefix';
8589

Status & tagging log