r87708 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87707‎ | r87708 | r87709 >
Date:11:29, 9 May 2011
Author:raylton.sousa
Status:deferred
Tags:
Comment:
Solve Bug 28012(Add {{RANDOMCHAPTER}} functionality)
Modified paths:
  • /trunk/extensions/BookManager/BookManager.body.php (modified) (history)
  • /trunk/extensions/BookManager/BookManager.i18n.magic.php (modified) (history)
  • /trunk/extensions/BookManager/BookManager.php (modified) (history)

Diff [purge]

Index: trunk/extensions/BookManager/BookManager.body.php
@@ -116,10 +116,10 @@
117117 /**
118118 * Get the prefixed title of a page near the given page.
119119 * @param $text String Text for title of current page
120 - * @param $n Integer Position of wanted page. Next page is +1; Previous page is -1
 120+ * @param $p Integer/String Position of wanted page. Next page is +1; Previous page is -1; Random position is 'rand'
121121 * @return String The prefixed title or empty string if not found or found but not valid
122122 */
123 - protected static function pageText( &$parser, $text = null, $n = 0 ) {
 123+ protected static function pageText( &$parser, $text = null, $p = 0 ) {
124124 $pagetitle = self::newTitleObject( $parser, $text );
125125 $prefixes = self::getBookPagePrefixes();
126126 $booktitle = Title::newFromText( $prefixes['community-prefix'] . self::bookparts( $parser, $text, 0 ) ); // ...the book name will be 'Foo'.
@@ -131,13 +131,18 @@
132132 return '';
133133 }
134134 $current = array_search( $pagetitle, self::$chapterList );
135 - if ( $current === false || !isset( self::$chapterList[ $current + $n ] ) ) {
 135+ if ( $current === false || !isset( self::$chapterList[ $current + $p ] ) ) {
136136 return '';
137137 }
138 - $otherpagetitle = Title::newFromText( self::$chapterList[ $current + $n ] );
 138+ $otherpagetitle = Title::newFromText( self::$chapterList[ $current + $p ] );
139139 if ( is_null( $otherpagetitle ) ) {
140140 return '';
141141 }
 142+ if ( $p == 'rand' ){
 143+ $limit = count( self::$chapterList ) - 1;
 144+ $randPosition = rand( 0, $limit );
 145+ return Title::newFromText( self::$chapterList[ $randPosition ] );
 146+ }
142147 return wfEscapeWikiText( $otherpagetitle->getText() );
143148 }
144149 }
@@ -156,7 +161,8 @@
157162 $parser->setFunctionHook( 'rootpagenamee', array( __CLASS__, 'rootpagenamee' ), SFH_NO_HASH );
158163 $parser->setFunctionHook( 'chaptername', array( __CLASS__, 'chaptername' ), SFH_NO_HASH );
159164 $parser->setFunctionHook( 'chapternamee', array( __CLASS__, 'chapternamee' ), SFH_NO_HASH );
160 -
 165+ $parser->setFunctionHook( 'randomchapter', array( __CLASS__, 'randomchapter' ), SFH_NO_HASH );
 166+ $parser->setFunctionHook( 'randomchaptere', array( __CLASS__, 'randomchaptere' ), SFH_NO_HASH );
161167 return true;
162168 }
163169 # Function to declare magicword id
@@ -171,6 +177,9 @@
172178 $aCustomVariableIds[] = 'rootpagenamee';
173179 $aCustomVariableIds[] = 'chaptername';
174180 $aCustomVariableIds[] = 'chapternamee';
 181+ $aCustomVariableIds[] = 'randomchapter';
 182+ $aCustomVariableIds[] = 'randomchaptere';
 183+
175184 return true;
176185 }
177186
@@ -208,6 +217,14 @@
209218 $t = self::bookparts( $parser, $text, 1 );
210219 return wfUrlEncode( $t );
211220 }
 221+ static function randomchapter( &$parser, $text = null ) {
 222+ $t = self::pageText( $parser, $text, 'rand' );
 223+ return $t;
 224+ }
 225+ static function randomchaptere( &$parser, $text = null ) {
 226+ $t = self::pageText( $parser, $text, 'rand' );
 227+ return wfUrlEncode( $t );
 228+ }
212229
213230 # Function for use with MW Variables on the current page
214231 static function AssignAValue( &$parser, &$cache, &$magicWordId, &$ret ) {
@@ -236,6 +253,13 @@
237254 case 'chapternamee':
238255 $ret = BookManagerVariables::chapternamee( $parser );
239256 return true;
 257+ case 'randomchapter':
 258+ $ret = BookManagerVariables::randomchapter( $parser );
 259+ return true;
 260+ case 'randomchaptere':
 261+ $ret = BookManagerVariables::randomchaptere( $parser );
 262+ return true;
 263+
240264 }
241265 return false;
242266 }
@@ -360,6 +384,3 @@
361385 }
362386
363387 }
364 -
365 -
366 -
Index: trunk/extensions/BookManager/BookManager.i18n.magic.php
@@ -14,4 +14,6 @@
1515 'rootpagenamee' => array( 0, 'ROOTPAGENAMEE', 'BOOKNAMEE' ),
1616 'chaptername' => array( 0, 'CHAPTERNAME' ),
1717 'chapternamee' => array( 0, 'CHAPTERNAMEE' ),
 18+ 'randomchapter' => array( 0, 'RANDOMCHAPTER' ),
 19+ 'randomchaptere' => array( 0, 'RANDOMCHAPTERE' ),
1820 );
Index: trunk/extensions/BookManager/BookManager.php
@@ -9,8 +9,10 @@
1010 * - NEXTPAGENAMEE (get next page encode)
1111 * - ROOTPAGENAME (get root page)
1212 * - ROOTPAGENAMEE (get root page encode)
13 - * - CHAPTERNAME (get root page)
14 - * - CHAPTERNAMEE (get root page encode)
 13+ * - CHAPTERNAME (get chapter)
 14+ * - CHAPTERNAMEE (get chapter encode)
 15+ * - RANDOMCHAPTER (get random page)
 16+ * - RANDOMCHAPTERE (get random page encode)
1517 * @addtogroup Extensions
1618 * @author Raylton P. Sousa <raylton.sousa@gmail.com>
1719 * @author Helder.wiki

Status & tagging log