Index: branches/REL1_16/extensions/SelectCategory/SelectCategoryFunctions.php |
— | — | @@ -1,26 +1,31 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -# Implementation of the SelectCategory extension, an extension of the |
5 | | -# edit box of MediaWiki to provide an easy way to add category links |
6 | | -# to a specific page. |
| 4 | +/** |
| 5 | + * Implementation of the SelectCategory extension, an extension of the |
| 6 | + * edit box of MediaWiki to provide an easy way to add category links |
| 7 | + * to a specific page. |
| 8 | + * |
| 9 | + * @file |
| 10 | + * @ingroup Extensions |
| 11 | + * @author Leon Weber <leon@leonweber.de> & Manuel Schneider <manuel.schneider@wikimedia.ch> |
| 12 | + * @copyright © 2006 by Leon Weber & Manuel Schneider |
| 13 | + * @licence GNU General Public Licence 2.0 or later |
| 14 | + */ |
7 | 15 | |
8 | | -# @addtogroup Extensions |
9 | | -# @author Leon Weber <leon@leonweber.de> & Manuel Schneider <manuel.schneider@wikimedia.ch> |
10 | | -# @copyright © 2006 by Leon Weber & Manuel Schneider |
11 | | -# @licence GNU General Public Licence 2.0 or later |
12 | | - |
13 | 16 | if( !defined( 'MEDIAWIKI' ) ) { |
14 | 17 | echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" ); |
15 | 18 | die(); |
16 | 19 | } |
17 | 20 | |
18 | 21 | ## Entry point for the hook and main worker function for editing the page: |
19 | | -function fnSelectCategoryShowHook( $m_isUpload = false, &$m_pageObj ) { |
| 22 | +function fnSelectCategoryShowHook( $m_isUpload = false, $m_pageObj ) { |
20 | 23 | |
21 | 24 | # check if we should do anything or sleep |
22 | 25 | if ( fnSelectCategoryCheckConditions( $m_isUpload, $m_pageObj ) ) { |
23 | 26 | # Register CSS file for our select box: |
24 | | - global $wgOut, $wgScriptPath; |
| 27 | + global $wgOut, $wgScriptPath, $wgUser, $wgTitle; |
| 28 | + global $wgSelectCategoryMaxLevel; |
| 29 | + |
25 | 30 | $wgOut->addLink( |
26 | 31 | array( |
27 | 32 | 'rel' => 'stylesheet', |
— | — | @@ -29,6 +34,8 @@ |
30 | 35 | ) |
31 | 36 | ); |
32 | 37 | |
| 38 | + $m_skin =& $wgUser->getSkin(); |
| 39 | + |
33 | 40 | # Get all categories from wiki: |
34 | 41 | $m_allCats = fnSelectCategoryGetAllCategories(); |
35 | 42 | # Load system messages: |
— | — | @@ -55,37 +62,51 @@ |
56 | 63 | $m_pageObj->$m_place .= "<!-- SelectCategory begin -->\n"; |
57 | 64 | # Print the select box: |
58 | 65 | $m_pageObj->$m_place .= "\n$m_textBefore"; |
59 | | -# # First come up with the JavaScript version of the select boxes: |
60 | | -# $m_pageObj->$m_place .= "<script type=\"text/javascript\" src=\"'/extensions/SelectCategory/SelectCategory.js\"></script>\n"; |
61 | | -# # Then the "old-style" select box for those without JavaScript: |
62 | | -# $m_pageObj->$m_place .= "<noscript>\n"; |
63 | | -# $m_pageObj->$m_place .= "</noscript>\n"; |
64 | 66 | |
65 | | - $m_pageObj->$m_place .= "<ul id='SelectCategoryList'>"; |
| 67 | + # Begin list output, use <div> to enable custom formatting |
| 68 | + $m_pageObj->$m_place .= '<div id="SelectCategoryList">'; |
66 | 69 | foreach( $m_allCats as $m_cat => $m_depth ) { |
67 | 70 | $checked = ''; |
68 | | - |
69 | | - if (isset($m_pageCats[$m_cat])) { |
70 | | - $checked = "checked='checked'"; |
| 71 | + # See if the category was already added, so check it |
| 72 | + if( isset( $m_pageCats[$m_cat] ) ) { |
| 73 | + $checked = 'checked="checked"'; |
71 | 74 | } |
72 | | - |
| 75 | + # Clean HTML Output: |
73 | 76 | $category = htmlspecialchars( $m_cat ); |
74 | 77 | |
75 | | - # Indent subcategories |
76 | | - $indention = ''; |
77 | | - for ($i = 0; $i < $m_depth; $i++) { |
78 | | - $indention .= ' '; |
| 78 | + # Calculate indention of subcategories |
| 79 | + $indention = 0; |
| 80 | + for( $i = 0; $i <= $m_depth; $i++ ) { |
| 81 | + $indention = 15 * $i; |
| 82 | + # Collapse subcategories after reaching the configured MaxLevel |
| 83 | + if( $i > $wgSelectCategoryMaxLevel ) { |
| 84 | + $display = 'display:none;'; |
| 85 | + } else { |
| 86 | + $display = ''; |
| 87 | + } |
| 88 | + # Check if we have reached the MaxLevel [-] or not [+] |
| 89 | + if( $i == $wgSelectCategoryMaxLevel ) { |
| 90 | + $sign = '[+]'; |
| 91 | + } else { |
| 92 | +# $sign = '[−]'; |
| 93 | + } |
| 94 | + # Check if there are more subcategories |
| 95 | +# if( $m_allCats[] > $m_depth ) |
79 | 96 | } |
| 97 | + # Clean names for text output |
| 98 | + $title = str_replace( '_', ' ', $category ); |
| 99 | + $m_title = $wgTitle->newFromText( $category, NS_CATEGORY ); |
| 100 | + # Output the actual checkboxes, indented |
| 101 | + $m_pageObj->$m_place .= ' |
| 102 | + <div id="sc_'.$category.'" style="'.$display.'"> |
| 103 | + <span style="padding-left:'.$indention.'px; width:10px; overflow:hidden;">'.$sign.'</span> |
| 104 | + <input type="checkbox" name="SelectCategoryList[]" value="'.$category.'" class="checkbox" '.$checked.' /> |
| 105 | + '.$m_skin->link( $m_title, $title ).' |
| 106 | + </div>'; |
| 107 | + } # End walking through cats (foreach) |
| 108 | + # End of list output |
| 109 | + $m_pageObj->$m_place .= '</div>'; |
80 | 110 | |
81 | | - $m_pageObj->$m_place .= " |
82 | | - <li> |
83 | | - $indention<input type='checkbox' name='SelectCategoryList[]' |
84 | | - value='$category' class='checkbox' $checked /> |
85 | | - $category |
86 | | - </li>"; |
87 | | - } |
88 | | - $m_pageObj->$m_place .= '</ul>'; |
89 | | - |
90 | 111 | # Print localised help string: |
91 | 112 | $m_pageObj->$m_place .= "<!-- SelectCategory end -->\n"; |
92 | 113 | } |
— | — | @@ -95,7 +116,7 @@ |
96 | 117 | } |
97 | 118 | |
98 | 119 | ## Entry point for the hook and main worker function for saving the page: |
99 | | -function fnSelectCategorySaveHook( $m_isUpload, &$m_pageObj ) { |
| 120 | +function fnSelectCategorySaveHook( $m_isUpload, $m_pageObj ) { |
100 | 121 | global $wgContLang; |
101 | 122 | global $wgTitle; |
102 | 123 | |
— | — | @@ -107,7 +128,7 @@ |
108 | 129 | |
109 | 130 | # default sort key is page name with stripped namespace name, |
110 | 131 | # otherwise sorting is ugly. |
111 | | - if ($wgTitle->getNamespace() == NS_MAIN) { |
| 132 | + if( $wgTitle->getNamespace() == NS_MAIN ) { |
112 | 133 | $default_sortkey = ""; |
113 | 134 | } else { |
114 | 135 | $default_sortkey = "|{{PAGENAME}}"; |
— | — | @@ -191,10 +212,14 @@ |
192 | 213 | $m_tblPage = $m_dbObj->tableName( 'page' ); |
193 | 214 | |
194 | 215 | # The normal query to get all children of a given root category: |
195 | | - $m_sql = " SELECT tmpSelectCatPage.page_title AS title |
196 | | - FROM $m_tblCatLink AS tmpSelectCat |
197 | | - LEFT JOIN $m_tblPage AS tmpSelectCatPage ON tmpSelectCat.cl_from = tmpSelectCatPage.page_id |
198 | | - WHERE tmpSelectCat.cl_to LIKE " . $m_dbObj->addQuotes( $m_root ) . " AND tmpSelectCatPage.page_namespace = 14"; |
| 216 | + $m_sql = ' |
| 217 | + SELECT tmpSelectCatPage.page_title AS title |
| 218 | + FROM '.$m_tblCatLink.' AS tmpSelectCat |
| 219 | + LEFT JOIN '.$m_tblPage.' AS tmpSelectCatPage |
| 220 | + ON tmpSelectCat.cl_from = tmpSelectCatPage.page_id |
| 221 | + WHERE tmpSelectCat.cl_to LIKE '.$m_dbObj->addQuotes( $m_root ).' |
| 222 | + AND tmpSelectCatPage.page_namespace = 14 |
| 223 | + ORDER BY tmpSelectCatPage.page_title ASC;'; |
199 | 224 | # Run the query: |
200 | 225 | $m_res = $m_dbObj->query( $m_sql, __METHOD__ ); |
201 | 226 | # Process the resulting rows: |
— | — | @@ -259,7 +284,7 @@ |
260 | 285 | } |
261 | 286 | |
262 | 287 | # Function that checks if we meet the run conditions of the extension |
263 | | -function fnSelectCategoryCheckConditions ($m_isUpload, &$m_pageObj ) { |
| 288 | +function fnSelectCategoryCheckConditions ($m_isUpload, $m_pageObj ) { |
264 | 289 | global $wgSelectCategoryNamespaces; |
265 | 290 | global $wgSelectCategoryEnableSubpages; |
266 | 291 | global $wgTitle; |
— | — | @@ -274,7 +299,7 @@ |
275 | 300 | } |
276 | 301 | |
277 | 302 | $ns = $wgTitle->getNamespace(); |
278 | | - if (array_key_exists ($ns, $wgSelectCategoryNamespaces)) { |
| 303 | + if( array_key_exists( $ns, $wgSelectCategoryNamespaces ) ) { |
279 | 304 | $enabledForNamespace = $wgSelectCategoryNamespaces[$ns]; |
280 | 305 | } else { |
281 | 306 | $enabledForNamespace = false; |