r15876 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r15875‎ | r15876 | r15877 >
Date:09:18, 29 July 2006
Author:daniel
Status:old
Tags:
Comment:
Fixes for CategoryTreeExtension: Implemented dynamic load with hide-root,
postpone on-demand JS/CSS until ParserOutput supports it. Fixed some glitches.
Modified paths:
  • /trunk/extensions/CategoryTree/CategoryTree.js (modified) (history)
  • /trunk/extensions/CategoryTree/CategoryTree.php (modified) (history)
  • /trunk/extensions/CategoryTree/CategoryTreeFunctions.php (modified) (history)
  • /trunk/extensions/CategoryTree/CategoryTreePage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CategoryTree/CategoryTreePage.php
@@ -26,6 +26,7 @@
2727 * Constructor
2828 */
2929 function CategoryTree() {
 30+ global $wgOut;
3031 SpecialPage::SpecialPage( 'CategoryTree', 'categorytree' );
3132
3233 #inject messages
@@ -60,7 +61,7 @@
6162 $wgOut->addHtml( $this->makeInputForm() );
6263
6364 if( $this->target !== '' && $this->target !== NULL ) {
64 - efCategoryTreeHeader();
 65+ $wgOut->addScript( efCategoryTreeGetJsMessages() ); #TODO: move it...
6566
6667 $title = efCategoryTreeMakeTitle( $this->target );
6768
@@ -77,7 +78,7 @@
7879 $html .= wfCloseElement( 'div' );
7980
8081 $html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeResult' ) );
81 - $html .= efCategoryTreeRenderNode( $title, $this->mode, true );
 82+ $html .= efCategoryTreeRenderNode( $title, $this->mode, true, false );
8283 $html .= wfCloseElement( 'div' );
8384 $wgOut->addHtml( $html );
8485 }
Index: trunk/extensions/CategoryTree/CategoryTree.php
@@ -59,6 +59,7 @@
6060 $wgAutoloadClasses['CategoryTree'] = dirname( __FILE__ ) . '/CategoryTreePage.php';
6161 $wgSpecialPages['CategoryTree'] = 'CategoryTree';
6262 $wgHooks['SkinTemplateTabs'][] = 'efCategoryTreeInstallTabs';
 63+#$wgHooks['OutputPageBeforeHTML'][] = 'efCategoryTreeHeadHook';
6364
6465 /**
6566 * register Ajax function
@@ -66,18 +67,21 @@
6768 $wgAjaxExportList[] = 'efCategoryTreeAjaxWrapper';
6869
6970 /**
70 - * Internal state
71 - */
72 -$wgCategoryTreeHeaderDone = false; #set to true by efCategoryTreeHeader after registering the code
73 -$wgCategoryTreeMessagesDone = false; #set to true by efInjectCategoryTreeMessages after registering the messages
74 -
75 -/**
7671 * Hook it up
7772 */
7873 function efCategoryTree() {
79 - global $wgParser, $wgCategoryTreeAllowTag;
 74+ global $wgParser, $wgOut, $wgCategoryTreeAllowTag;
 75+ global $wgJsMimeType, $wgScriptPath;
8076
8177 if ( $wgCategoryTreeAllowTag ) $wgParser->setHook( 'categorytree' , 'efCategoryTreeParserHook' );
 78+
 79+ #TODO: injecting scripts should be done on demand, by "somehow" using the ParserOutput
 80+
 81+ #register css file for CategoryTree
 82+ $wgOut->addLink( array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => $wgScriptPath . '/extensions/CategoryTree/CategoryTree.css' ) );
 83+
 84+ #register main js file for CategoryTree
 85+ $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/CategoryTree/CategoryTree.js\"></script>\n" );
8286 }
8387
8488 /**
@@ -87,6 +91,8 @@
8892 function efCategoryTreeAjaxWrapper( $category, $mode = CT_MODE_CATEGORIES ) {
8993 require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' );
9094
 95+ efInjectCategoryTreeMessages();
 96+
9197 return efCategoryTreeAjax( $category, $mode );
9298 }
9399
@@ -95,6 +101,9 @@
96102 * This loads CategoryTreeFunctions.php and calls efCategoryTreeTag()
97103 */
98104 function efCategoryTreeParserHook( $cat, $argv ) {
 105+ #global $wgParser;
 106+ #$wgParser->mOutput->mCategoryTreeTag = true; #HACK: flag for use by efCategoryTreeHeadHook
 107+
99108 require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' );
100109
101110 $style= @$argv[ 'style' ];
@@ -118,8 +127,24 @@
119128 if ( $hideroot === '1' || $hideroot === 'yes' || $hideroot === 'on' || $hideroot === 'true' ) $hideroot = true;
120129 else if ( $hideroot === '0' || $hideroot === 'no' || $hideroot === 'off' || $hideroot === 'false' ) $hideroot = false;
121130 }
 131+
 132+ efInjectCategoryTreeMessages();
 133+ #efCategoryTreeHeader();
122134
123 - return efCategoryTreeTag( $cat, $mode, $hideroot, $style );
 135+ #HACK for inlining JS messages "on demand". Putting them into the head would be nicer,
 136+ # but that would require some changes to ParserOutput to deal with the parser cache
 137+ static $messages = true;
 138+
 139+ if ( $messages ) {
 140+ efInjectCategoryTreeMessages();
 141+ $m = efCategoryTreeGetJsMessages();
 142+ $messages = false;
 143+ }
 144+ else {
 145+ $m = '';
 146+ }
 147+
 148+ return $m . efCategoryTreeTag( $cat, $mode, $hideroot, $style );
124149 }
125150
126151 /**
@@ -142,14 +167,28 @@
143168 }
144169
145170 /**
 171+* Hook callback that injects messages and things into the <head> tag
 172+* Does nothing if $parserOutput->mCategoryTreeTag is not set
 173+*/
 174+/* function efCategoryTreeHeadHook( &$parserOutput, &$text ) {
 175+ if ( ! @$parserOutput->mCategoryTreeTag ) return;
 176+
 177+ require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' );
 178+
 179+ efInjectCategoryTreeMessages();
 180+ efCategoryTreeHeader();
 181+} */
 182+
 183+/**
146184 * inject messages used by CategoryTree into the message cache
147185 */
148186 function efInjectCategoryTreeMessages() {
149 - global $wgMessageCache, $wgCategoryTreeMessagesDone;
 187+ global $wgMessageCache;
150188
151 - if ( $wgCategoryTreeMessagesDone ) return;
152 - else $wgCategoryTreeMessagesDone = true;
153 -
 189+ static $done = false;
 190+ if ( $done ) return;
 191+ else $done = true;
 192+
154193 $msg = efLoadCategoryTreeMessages();
155194 $wgMessageCache->addMessages( $msg );
156195 }
@@ -158,14 +197,14 @@
159198 * load the CategoryTree internationalization file
160199 */
161200 function efLoadCategoryTreeMessages() {
162 - global $wgLanguageCode, $wgContLang;
 201+ global $wgLang;
163202
164203 $messages= array();
165204
166205 $f= dirname( __FILE__ ) . '/CategoryTree.i18n.php';
167206 include( $f );
168207
169 - $f= dirname( __FILE__ ) . '/CategoryTree.i18n.' . $wgContLang->getCode() . '.php';
 208+ $f= dirname( __FILE__ ) . '/CategoryTree.i18n.' . $wgLang->getCode() . '.php';
170209 if ( file_exists( $f ) ) include( $f );
171210
172211 return $messages;
Index: trunk/extensions/CategoryTree/CategoryTree.js
@@ -42,15 +42,18 @@
4343 }
4444
4545 function categoryTreeLoadNode(cat, mode, lnk, div) {
46 - var page_request = false;
47 -
48 - div.innerHTML= '<i class="CategoryTreeNotice">' + categoryTreeLoadingMsg + '</i>';
4946 div.style.display= 'block';
5047 lnk.className= 'CategoryTreeLoaded';
5148 lnk.innerHTML= '&ndash;';
5249 lnk.title= categoryTreeCollapseMsg;
5350 lnk.onclick= function() { categoryTreeCollapseNode(cat, mode, lnk) }
54 -
 51+
 52+ categoryTreeLoadChildren(cat, mode, div)
 53+ }
 54+
 55+ function categoryTreeLoadChildren(cat, mode, div) {
 56+ div.innerHTML= '<i class="CategoryTreeNotice">' + categoryTreeLoadingMsg + '</i>';
 57+
5558 function f( result ) {
5659 if ( result == '' ) result= '<i class="CategorTreeNotice">' + categoryTreeNothingFoundMsg + '</i>';
5760 div.innerHTML= result;
Index: trunk/extensions/CategoryTree/CategoryTreeFunctions.php
@@ -19,12 +19,14 @@
2020 /**
2121 * Inserts code into the HTML head. This mainly links CategoryTree.js and CategoryTree.css
2222 */
23 -function efCategoryTreeHeader() {
24 - global $wgOut, $wgCategoryTreeHeaderDone;
 23+/* function efCategoryTreeHeader() {
 24+ global $wgOut;
2525 global $wgJsMimeType, $wgScriptPath;
 26+
 27+ static $done = false;
2628
27 - if ( $wgCategoryTreeHeaderDone ) return;
28 - else $wgCategoryTreeHeaderDone = true;
 29+ if ( $done ) return;
 30+ else $done = true;
2931
3032 #register css file for CategoryTree
3133 $wgOut->addLink( array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => $wgScriptPath . '/extensions/CategoryTree/CategoryTree.css' ) );
@@ -39,6 +41,21 @@
4042 categoryTreeLoadingMsg = \"".Xml::escapeJsString(wfMsg('categorytree-loading'))."\";
4143 categoryTreeNothingFoundMsg = \"".Xml::escapeJsString(wfMsg('categorytree-nothing-found'))."\";
4244 </script>\n" );
 45+} */
 46+
 47+/*
 48+* Returns a JS script that sets up messages as global JS variables.
 49+*/
 50+function efCategoryTreeGetJsMessages() {
 51+ global $wgJsMimeType;
 52+
 53+ return
 54+" <script type=\"{$wgJsMimeType}\">
 55+ categoryTreeCollapseMsg = \"".Xml::escapeJsString(wfMsg('categorytree-collapse'))."\";
 56+ categoryTreeExpandMsg = \"".Xml::escapeJsString(wfMsg('categorytree-expand'))."\";
 57+ categoryTreeLoadingMsg = \"".Xml::escapeJsString(wfMsg('categorytree-loading'))."\";
 58+ categoryTreeNothingFoundMsg = \"".Xml::escapeJsString(wfMsg('categorytree-nothing-found'))."\";
 59+ </script>\n";
4360 }
4461
4562 /**
@@ -46,8 +63,6 @@
4764 * load CategoryTreeFunctions.php on demand.
4865 */
4966 function efCategoryTreeAjax( $category, $mode ) {
50 - efInjectCategoryTreeMessages();
51 -
5267 $title = efCategoryTreeMakeTitle( $category );
5368
5469 return efCategoryTreeRenderChildren( $title, $mode );
@@ -59,10 +74,8 @@
6075 */
6176 function efCategoryTreeTag( $category, $mode, $hideroot = false, $style = '' ) {
6277 global $wgOut, $wgParser, $wgCategoryTreeDisableCache, $wgCategoryTreeDynamicTag;
 78+ static $uniq = 0;
6379
64 - efInjectCategoryTreeMessages();
65 - efCategoryTreeHeader();
66 -
6780 if ( $wgCategoryTreeDisableCache && !$wgCategoryTreeDynamicTag ) $wgParser->disableCache();
6881
6982 $title = efCategoryTreeMakeTitle( $category );
@@ -76,11 +89,20 @@
7790 $html .= wfCloseElement( 'span' );
7891 }
7992 else {
80 - if ( !$hideroot ) $html .= efCategoryTreeRenderNode( $title, $mode, true );
81 - else $html .= efCategoryTreeRenderChildren( $title, $mode );
 93+ if ( !$hideroot ) $html .= efCategoryTreeRenderNode( $title, $mode, true, $wgCategoryTreeDynamicTag );
 94+ else if ( !$wgCategoryTreeDynamicTag ) $html .= efCategoryTreeRenderChildren( $title, $mode );
 95+ else {
 96+ $uniq += 1;
 97+ $load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 );
 98+
 99+ $html .= wfOpenElement( 'script', array( 'type' => 'text/javascript', 'id' => $load ) );
 100+ $html .= 'categoryTreeLoadChildren("' . Xml::escapeJsString( $title->getDBKey() ) . '", "' . $mode . '", document.getElementById("' . $load . '").parentNode );';
 101+ $html .= wfCloseElement( 'script' );
 102+ }
82103 }
83104
84105 $html .= wfCloseElement( 'div' );
 106+ $html .= "\n\t\t";
85107
86108 return $html;
87109 }
@@ -196,13 +218,12 @@
197219 * Returns a string with a HTML represenation of the given page.
198220 * $title must be a Title object
199221 */
200 -function efCategoryTreeRenderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false ) {
201 - global $wgCategoryTreeDynamicTag;
 222+function efCategoryTreeRenderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false ) {
202223 static $uniq = 0;
203224
204225 $load = false;
205226
206 - if ( $children && $wgCategoryTreeDynamicTag ) {
 227+ if ( $loadchildren ) {
207228 $uniq += 1;
208229
209230 $load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 );