r89698 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89697‎ | r89698 | r89699 >
Date:21:43, 7 June 2011
Author:yaron
Status:deferred
Tags:
Comment:
Version 0.8.1: created new 'SelectCategory' class and turned all global functions into methods of that class; improved variable names and some comments
Modified paths:
  • /trunk/extensions/SelectCategory/CHANGELOG (modified) (history)
  • /trunk/extensions/SelectCategory/SelectCategory.php (modified) (history)
  • /trunk/extensions/SelectCategory/SelectCategoryFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SelectCategory/SelectCategory.php
@@ -16,10 +16,7 @@
1717 die();
1818 }
1919
20 -## Load the file containing the hook functions:
21 -require_once( 'SelectCategoryFunctions.php' );
22 -
23 -## Options:
 20+## Options
2421 # $wgSelectCategoryNamespaces - list of namespaces in which this extension should be active
2522 if( !isset( $wgSelectCategoryNamespaces ) ) $wgSelectCategoryNamespaces = array(
2623 NS_MEDIA => true,
@@ -67,8 +64,8 @@
6865 $wgExtensionCredits['parserhook'][] = array(
6966 'path' => __FILE__,
7067 'name' => 'SelectCategory',
71 - 'version' => '0.7dev',
72 - 'author' => 'Leon Weber & Manuel Schneider',
 68+ 'version' => '0.8.1',
 69+ 'author' => array( 'Leon Weber', 'Manuel Schneider' ),
7370 'url' => 'http://www.mediawiki.org/wiki/Extension:SelectCategory',
7471 'descriptionmsg' => 'selectcategory-desc',
7572 );
@@ -76,18 +73,17 @@
7774 $dir = dirname(__FILE__) . '/';
7875 $wgExtensionMessagesFiles['SelectCategory'] = $dir . 'SelectCategory.i18n.php';
7976
80 -## Set Hook:
81 -global $wgHooks;
 77+$wgAutoloadClasses['SelectCategory'] = $dir . 'SelectCategoryFunctions.php';
8278
8379 ## Showing the boxes
84 -# Hook when starting editing:
85 -$wgHooks['EditPage::showEditForm:initial'][] = array( 'fnSelectCategoryShowHook', false );
86 -# Hook for the upload page:
87 -$wgHooks['UploadForm:initial'][] = array( 'fnSelectCategoryShowHook', true );
 80+# Hook when starting editing
 81+$wgHooks['EditPage::showEditForm:initial'][] = array( 'SelectCategory::showHook', false );
 82+# Hook for the upload page
 83+$wgHooks['UploadForm:initial'][] = array( 'SelectCategory::showHook', true );
8884
8985 ## Saving the data
90 -# Hook when saving page:
91 -$wgHooks['EditPage::attemptSave'][] = array( 'fnSelectCategorySaveHook', false );
92 -# Hook when saving the upload:
93 -$wgHooks['UploadForm:BeforeProcessing'][] = array( 'fnSelectCategorySaveHook', true );
 86+# Hook when saving page
 87+$wgHooks['EditPage::attemptSave'][] = array( 'SelectCategory::saveHook', false );
 88+# Hook when saving the upload
 89+$wgHooks['UploadForm:BeforeProcessing'][] = array( 'SelectCategory::saveHook', true );
9490
Index: trunk/extensions/SelectCategory/CHANGELOG
@@ -1,3 +1,6 @@
 2+* v0.8.1 - 2011-06-07
 3+ - Fix for code structure and variable names (Yaron Koren)
 4+
25 * v0.8
36 - using JQuery Treeview to collapse / expand branches
47
Index: trunk/extensions/SelectCategory/SelectCategoryFunctions.php
@@ -17,317 +17,323 @@
1818 die();
1919 }
2020
21 -## Entry point for the hook and main worker function for editing the page:
22 -function fnSelectCategoryShowHook( $m_isUpload = false, $m_pageObj ) {
2321
24 - # check if we should do anything or sleep
25 - if ( fnSelectCategoryCheckConditions( $m_isUpload, $m_pageObj ) ) {
26 - # Register CSS file for our select box:
27 - global $wgOut, $wgScriptPath, $wgUser, $wgTitle;
28 - global $wgSelectCategoryMaxLevel;
 22+class SelectCategory {
2923
30 - $wgOut->addLink(
31 - array(
32 - 'rel' => 'stylesheet',
33 - 'type' => 'text/css',
34 - 'href' => $wgScriptPath.'/extensions/SelectCategory/SelectCategory.css'
35 - )
36 - );
37 - $wgOut->addLink(
38 - array(
39 - 'rel' => 'stylesheet',
40 - 'type' => 'text/css',
41 - 'href' => $wgScriptPath.'/extensions/SelectCategory/jquery.treeview.css'
42 - )
43 - );
44 - $wgOut->addScript( '<script src="'.$wgScriptPath.'/extensions/SelectCategory/jquery.treeview.js" type="text/javascript"></script>' );
45 - $wgOut->addScript( '<script src="'.$wgScriptPath.'/extensions/SelectCategory/SelectCategory.js" type="text/javascript"></script>' );
 24+ ## Entry point for the hook and main function for editing the page
 25+ public static function showHook( $isUpload = false, $pageObj ) {
4626
47 - $m_skin =& $wgUser->getSkin();
 27+ # check if we should do anything or sleep
 28+ if ( self::checkConditions( $isUpload, $pageObj ) ) {
 29+ # Register CSS file for our select box
 30+ global $wgOut, $wgScriptPath, $wgUser, $wgTitle;
 31+ global $wgSelectCategoryMaxLevel;
4832
49 - # Get all categories from wiki:
50 - $m_allCats = fnSelectCategoryGetAllCategories();
51 - # Load system messages:
 33+ $wgOut->addLink(
 34+ array(
 35+ 'rel' => 'stylesheet',
 36+ 'type' => 'text/css',
 37+ 'href' => $wgScriptPath.'/extensions/SelectCategory/SelectCategory.css'
 38+ )
 39+ );
 40+ $wgOut->addLink(
 41+ array(
 42+ 'rel' => 'stylesheet',
 43+ 'type' => 'text/css',
 44+ 'href' => $wgScriptPath.'/extensions/SelectCategory/jquery.treeview.css'
 45+ )
 46+ );
 47+ $wgOut->addScript( '<script src="'.$wgScriptPath.'/extensions/SelectCategory/jquery.treeview.js" type="text/javascript"></script>' );
 48+ $wgOut->addScript( '<script src="'.$wgScriptPath.'/extensions/SelectCategory/SelectCategory.js" type="text/javascript"></script>' );
 49+
 50+ $skin = $wgUser->getSkin();
 51+
 52+ # Get all categories from wiki
 53+ $allCats = self::getAllCategories();
 54+ # Load system messages
5255
53 - # Get the right member variables, depending on if we're on an upload form or not:
54 - if( !$m_isUpload ) {
55 - # Extract all categorylinks from page:
56 - $m_pageCats = fnSelectCategoryGetPageCategories( $m_pageObj );
 56+ # Get the right member variables, depending on if we're on an upload form or not
 57+ if( !$isUpload ) {
 58+ # Extract all categorylinks from page
 59+ $pageCats = self::getPageCategories( $pageObj );
 60+
 61+ # Never ever use editFormTextTop here as it resides outside
 62+ # the <form> so we will never get contents
 63+ $place = 'editFormTextAfterWarn';
 64+ # Print the localised title for the select box
 65+ $textBefore = '<b>'. wfMsg( 'selectcategory-title' ) . '</b>:';
 66+ } else {
 67+ # No need to get categories
 68+ $pageCats = array();
5769
58 - # Never ever use editFormTextTop here as it resides outside
59 - # the <form> so we will never get contents
60 - $m_place = 'editFormTextAfterWarn';
61 - # Print the localised title for the select box:
62 - $m_textBefore = '<b>'. wfMsg( 'selectcategory-title' ) . '</b>:';
63 - } else {
64 - # No need to get categories:
65 - $m_pageCats = array();
 70+ # Place output at the right place
 71+ $place = 'uploadFormTextAfterSummary';
 72+ # Print the part of the table including the localised title for the select box
 73+ $textBefore = "\n</td></tr><tr><td align='right'><label for='wpSelectCategory'>" . wfMsg( 'selectcategory-title' ) .":</label></td><td align='left'>";
 74+ }
 75+ # Introduce the output
 76+ $pageObj->$place .= "<!-- SelectCategory begin -->\n";
 77+ # Print the select box
 78+ $pageObj->$place .= "\n$textBefore";
6679
67 - # Place output at the right place:
68 - $m_place = 'uploadFormTextAfterSummary';
69 - # Print the part of the table including the localised title for the select box:
70 - $m_textBefore = "\n</td></tr><tr><td align='right'><label for='wpSelectCategory'>" . wfMsg( 'selectcategory-title' ) .":</label></td><td align='left'>";
71 - }
72 - # Introduce the output:
73 - $m_pageObj->$m_place .= "<!-- SelectCategory begin -->\n";
74 - # Print the select box:
75 - $m_pageObj->$m_place .= "\n$m_textBefore";
 80+ # Begin list output, use <div> to enable custom formatting
 81+ $level = 0;
 82+ $pageObj->$place .= '<ul id="SelectCategoryList">';
7683
77 - # Begin list output, use <div> to enable custom formatting
78 - $m_level = 0;
79 - $m_pageObj->$m_place .= '<ul id="SelectCategoryList">';
 84+ foreach( $allCats as $cat => $depth ) {
 85+ $checked = '';
8086
81 - foreach( $m_allCats as $m_cat => $m_depth ) {
82 - $checked = '';
 87+ # See if the category was already added, so check it
 88+ if( isset( $pageCats[$cat] ) ) {
 89+ $checked = 'checked="checked"';
 90+ }
 91+ # Clean HTML Output
 92+ $category = htmlspecialchars( $cat );
8393
84 - # See if the category was already added, so check it
85 - if( isset( $m_pageCats[$m_cat] ) ) {
86 - $checked = 'checked="checked"';
87 - }
88 - # Clean HTML Output:
89 - $category = htmlspecialchars( $m_cat );
 94+ # default for root category - otherwise it will always be closed
 95+ $open = " class='open' ";
9096
91 - # default for root category - otherwise it will always be closed
92 - $m_open = " class='open' ";
93 -
94 - # iterate through levels and adjust divs accordingly
95 - while( $m_level < $m_depth ) {
96 - # Collapse subcategories after reaching the configured MaxLevel
97 - if( $m_level >= ( $wgSelectCategoryMaxLevel - 1 ) ) {
98 - $m_class = 'display:none;';
99 - $m_open = " class='closed' ";
100 - } else {
101 - $m_class = 'display:block;';
102 - $m_open = " class='open' ";
 97+ # iterate through levels and adjust divs accordingly
 98+ while( $level < $depth ) {
 99+ # Collapse subcategories after reaching the configured MaxLevel
 100+ if( $level >= ( $wgSelectCategoryMaxLevel - 1 ) ) {
 101+ $class = 'display:none;';
 102+ $open = " class='closed' ";
 103+ } else {
 104+ $class = 'display:block;';
 105+ $open = " class='open' ";
 106+ }
 107+ $pageObj->$place .= '<ul style="'.$class.'">'."\n";
 108+ $level++;
103109 }
104 - $m_pageObj->$m_place .= '<ul style="'.$m_class.'">'."\n";
105 - $m_level++;
 110+ if( $level == $depth ) {
 111+ $pageObj->$place .= '</li>'."\n";
 112+ }
 113+ while( $level > $depth ) {
 114+ $pageObj->$place .= '</ul></li>'."\n";
 115+ $level--;
 116+ }
 117+ # Clean names for text output
 118+ $catName = str_replace( '_', ' ', $category );
 119+ $title = $wgTitle->newFromText( $category, NS_CATEGORY );
 120+ # Output the actual checkboxes, indented
 121+ $pageObj->$place .= '<li' . $open . '><input type="checkbox" name="SelectCategoryList[]" value="'.$category.'" class="checkbox" '.$checked.' />'.$skin->link( $title, $catName )."\n";
 122+ # set id for next level
 123+ $level_id = 'sc_'.$cat;
 124+ } # End walking through cats (foreach)
 125+ # End of list output - close all remaining divs
 126+ while( $level > -1 ) {
 127+ $pageObj->$place .= '</li></ul>'."\n";
 128+ $level--;
106129 }
107 - if( $m_level == $m_depth ) $m_pageObj->$m_place .= '</li>'."\n";
108 - while( $m_level > $m_depth ) {
109 - $m_pageObj->$m_place .= '</ul></li>'."\n";
110 - $m_level--;
111 - }
112 - # Clean names for text output
113 - $title = str_replace( '_', ' ', $category );
114 - $m_title = $wgTitle->newFromText( $category, NS_CATEGORY );
115 - # Output the actual checkboxes, indented
116 - $m_pageObj->$m_place .= '<li' . $m_open . '><input type="checkbox" name="SelectCategoryList[]" value="'.$category.'" class="checkbox" '.$checked.' />'.$m_skin->link( $m_title, $title )."\n";
117 - # set id for next level
118 - $m_level_id = 'sc_'.$m_cat;
119 - } # End walking through cats (foreach)
120 - # End of list output - close all remaining divs
121 - while( $m_level > -1 ) {
122 - $m_pageObj->$m_place .= '</li></ul>'."\n";
123 - $m_level--;
 130+
 131+ # Print localised help string
 132+ $pageObj->$place .= "<!-- SelectCategory end -->\n";
124133 }
125134
126 - # Print localised help string:
127 - $m_pageObj->$m_place .= "<!-- SelectCategory end -->\n";
 135+ # Return true to let the rest work
 136+ return true;
128137 }
129138
130 - # Return true to let the rest work:
131 - return true;
132 -}
 139+ ## Entry point for the hook and main function for saving the page
 140+ public static function saveHook( $isUpload, $pageObj ) {
 141+ global $wgContLang;
 142+ global $wgTitle;
133143
134 -## Entry point for the hook and main worker function for saving the page:
135 -function fnSelectCategorySaveHook( $m_isUpload, $m_pageObj ) {
136 - global $wgContLang;
137 - global $wgTitle;
 144+ # check if we should do anything or sleep
 145+ if ( self::checkConditions( $isUpload, $pageObj ) ) {
138146
139 - # check if we should do anything or sleep
140 - if ( fnSelectCategoryCheckConditions( $m_isUpload, $m_pageObj ) ) {
 147+ # Get localised namespace string
 148+ $catString = $wgContLang->getNsText( NS_CATEGORY );
141149
142 - # Get localised namespace string:
143 - $m_catString = $wgContLang->getNsText( NS_CATEGORY );
 150+ # default sort key is page name with stripped namespace name,
 151+ # otherwise sorting is ugly.
 152+ if( $wgTitle->getNamespace() == NS_MAIN ) {
 153+ $default_sortkey = "";
 154+ } else {
 155+ $default_sortkey = "|{{PAGENAME}}";
 156+ }
144157
145 - # default sort key is page name with stripped namespace name,
146 - # otherwise sorting is ugly.
147 - if( $wgTitle->getNamespace() == NS_MAIN ) {
148 - $default_sortkey = "";
149 - } else {
150 - $default_sortkey = "|{{PAGENAME}}";
151 - }
 158+ # Get some distance from the rest of the content
 159+ $text = "\n";
152160
153 - # Get some distance from the rest of the content:
154 - $m_text = "\n";
155 -
156 - # Iterate through all selected category entries:
157 - if (array_key_exists('SelectCategoryList', $_POST)) {
158 - foreach( $_POST['SelectCategoryList'] as $m_cat ) {
159 - $m_text .= "\n[[$m_catString:$m_cat$default_sortkey]]";
 161+ # Iterate through all selected category entries
 162+ if (array_key_exists('SelectCategoryList', $_POST)) {
 163+ foreach( $_POST['SelectCategoryList'] as $cat ) {
 164+ $text .= "\n[[$catString:$cat$default_sortkey]]";
 165+ }
160166 }
 167+ # If it is an upload we have to call a different method
 168+ if ( $isUpload ) {
 169+ $pageObj->mUploadDescription .= $text;
 170+ } else {
 171+ $pageObj->textbox1 .= $text;
 172+ }
161173 }
162 - # If it is an upload we have to call a different method:
163 - if ( $m_isUpload ) {
164 - $m_pageObj->mUploadDescription .= $m_text;
165 - } else {
166 - $m_pageObj->textbox1 .= $m_text;
167 - }
 174+
 175+ # Return to the let MediaWiki do the rest of the work
 176+ return true;
168177 }
169178
170 - # Return to the let MediaWiki do the rest of the work:
171 - return true;
172 -}
 179+ ## Get all categories from the wiki - starting with a given root or otherwise detect root automagically (expensive)
 180+ ## Returns an array like this
 181+ ## array (
 182+ ## 'Name' => (int) Depth,
 183+ ## ...
 184+ ## )
 185+ public static function getAllCategories() {
 186+ global $wgTitle;
 187+ global $wgSelectCategoryRoot;
173188
174 -## Get all categories from the wiki - starting with a given root or otherwise detect root automagically (expensive)
175 -## Returns an array like this:
176 -## array (
177 -## 'Name' => (int) Depth,
178 -## ...
179 -## )
180 -function fnSelectCategoryGetAllCategories() {
181 - global $wgTitle;
182 - global $wgSelectCategoryRoot;
 189+ # Get current namespace (save duplicate call of method)
 190+ $namespace = $wgTitle->getNamespace();
 191+ if( $namespace >= 0 && $wgSelectCategoryRoot[$namespace] ) {
 192+ # Include root and step into the recursion
 193+ $allCats = array_merge( array( $wgSelectCategoryRoot[$namespace] => 0 ),
 194+ self::getChildren( $wgSelectCategoryRoot[$namespace] ) );
 195+ } else {
 196+ # Initialize return value
 197+ $allCats = array();
183198
184 - # Get current namespace (save duplicate call of method):
185 - $m_namespace = $wgTitle->getNamespace();
186 - if( $m_namespace >= 0 && $wgSelectCategoryRoot[$m_namespace] ) {
187 - # Include root and step into the recursion:
188 - $m_allCats = array_merge( array( $wgSelectCategoryRoot[$m_namespace] => 0 ),
189 - fnSelectCategoryGetChildren( $wgSelectCategoryRoot[$m_namespace] ) );
190 - } else {
191 - # Initialize return value:
192 - $m_allCats = array();
 199+ # Get a database object
 200+ $dbObj = wfGetDB( DB_SLAVE );
 201+ # Get table names to access them in SQL query
 202+ $tblCatLink = $dbObj->tableName( 'categorylinks' );
 203+ $tblPage = $dbObj->tableName( 'page' );
193204
194 - # Get a database object:
195 - $m_dbObj = wfGetDB( DB_SLAVE );
196 - # Get table names to access them in SQL query:
197 - $m_tblCatLink = $m_dbObj->tableName( 'categorylinks' );
198 - $m_tblPage = $m_dbObj->tableName( 'page' );
 205+ # Automagically detect root categories
 206+ $sql = " SELECT tmpSelectCat1.cl_to AS title
 207+FROM $tblCatLink AS tmpSelectCat1
 208+LEFT JOIN $tblPage AS tmpSelectCatPage ON (tmpSelectCat1.cl_to = tmpSelectCatPage.page_title AND tmpSelectCatPage.page_namespace = 14)
 209+LEFT JOIN $tblCatLink AS tmpSelectCat2 ON tmpSelectCatPage.page_id = tmpSelectCat2.cl_from
 210+WHERE tmpSelectCat2.cl_from IS NULL GROUP BY tmpSelectCat1.cl_to";
 211+ # Run the query
 212+ $res = $dbObj->query( $sql, __METHOD__ );
 213+ # Process the resulting rows
 214+ while ( $row = $dbObj->fetchRow( $res ) ) {
 215+ $allCats += array( $row['title'] => 0 );
 216+ $allCats += self::getChildren( $row['title'] );
 217+ }
 218+ # Free result
 219+ $dbObj->freeResult( $res );
 220+ }
199221
200 - # Automagically detect root categories:
201 - $m_sql = " SELECT tmpSelectCat1.cl_to AS title
202 - FROM $m_tblCatLink AS tmpSelectCat1
203 - LEFT JOIN $m_tblPage AS tmpSelectCatPage ON (tmpSelectCat1.cl_to = tmpSelectCatPage.page_title AND tmpSelectCatPage.page_namespace = 14)
204 - LEFT JOIN $m_tblCatLink AS tmpSelectCat2 ON tmpSelectCatPage.page_id = tmpSelectCat2.cl_from
205 - WHERE tmpSelectCat2.cl_from IS NULL GROUP BY tmpSelectCat1.cl_to";
206 - # Run the query:
207 - $m_res = $m_dbObj->query( $m_sql, __METHOD__ );
208 - # Process the resulting rows:
209 - while ( $m_row = $m_dbObj->fetchRow( $m_res ) ) {
210 - $m_allCats += array( $m_row['title'] => 0 );
211 - $m_allCats += fnSelectCategoryGetChildren( $m_row['title'] );
212 - }
213 - # Free result:
214 - $m_dbObj->freeResult( $m_res );
 222+ # Afterwards return the array to the caller
 223+ return $allCats;
215224 }
216225
217 - # Afterwards return the array to the caller:
218 - return $m_allCats;
219 -}
 226+ public static function getChildren( $root, $depth = 1 ) {
 227+ # Initialize return value
 228+ $allCats = array();
220229
221 -function fnSelectCategoryGetChildren( $m_root, $m_depth = 1 ) {
222 - # Initialize return value:
223 - $m_allCats = array();
 230+ # Get a database object
 231+ $dbObj = wfGetDB( DB_SLAVE );
 232+ # Get table names to access them in SQL query
 233+ $tblCatLink = $dbObj->tableName( 'categorylinks' );
 234+ $tblPage = $dbObj->tableName( 'page' );
224235
225 - # Get a database object:
226 - $m_dbObj = wfGetDB( DB_SLAVE );
227 - # Get table names to access them in SQL query:
228 - $m_tblCatLink = $m_dbObj->tableName( 'categorylinks' );
229 - $m_tblPage = $m_dbObj->tableName( 'page' );
 236+ # The normal query to get all children of a given root category
 237+ $sql = 'SELECT tmpSelectCatPage.page_title AS title
 238+FROM '.$tblCatLink.' AS tmpSelectCat
 239+LEFT JOIN '.$tblPage.' AS tmpSelectCatPage
 240+ ON tmpSelectCat.cl_from = tmpSelectCatPage.page_id
 241+WHERE tmpSelectCat.cl_to LIKE '.$dbObj->addQuotes( $root ).'
 242+ AND tmpSelectCatPage.page_namespace = 14
 243+ORDER BY tmpSelectCatPage.page_title ASC;';
 244+ # Run the query
 245+ $res = $dbObj->query( $sql, __METHOD__ );
 246+ # Process the resulting rows
 247+ while ( $row = $dbObj->fetchRow( $res ) ) {
 248+ # Survive category link loops
 249+ if( $root == $row['title'] ) {
 250+ continue;
 251+ }
 252+ # Add current entry to array
 253+ $allCats += array( $row['title'] => $depth );
 254+ $allCats += self::getChildren( $row['title'], $depth + 1 );
 255+ }
 256+ # Free result
 257+ $dbObj->freeResult( $res );
230258
231 - # The normal query to get all children of a given root category:
232 - $m_sql = '
233 - SELECT tmpSelectCatPage.page_title AS title
234 - FROM '.$m_tblCatLink.' AS tmpSelectCat
235 - LEFT JOIN '.$m_tblPage.' AS tmpSelectCatPage
236 - ON tmpSelectCat.cl_from = tmpSelectCatPage.page_id
237 - WHERE tmpSelectCat.cl_to LIKE '.$m_dbObj->addQuotes( $m_root ).'
238 - AND tmpSelectCatPage.page_namespace = 14
239 - ORDER BY tmpSelectCatPage.page_title ASC;';
240 - # Run the query:
241 - $m_res = $m_dbObj->query( $m_sql, __METHOD__ );
242 - # Process the resulting rows:
243 - while ( $m_row = $m_dbObj->fetchRow( $m_res ) ) {
244 - # Survive category link loops:
245 - if( $m_root == $m_row['title'] ) {
246 - continue;
247 - }
248 - # Add current entry to array:
249 - $m_allCats += array( $m_row['title'] => $m_depth );
250 - $m_allCats += fnSelectCategoryGetChildren( $m_row['title'], $m_depth + 1 );
 259+ # Afterwards return the array to the upper recursion level
 260+ return $allCats;
251261 }
252 - # Free result:
253 - $m_dbObj->freeResult( $m_res );
254262
255 - # Afterwards return the array to the upper recursion level:
256 - return $m_allCats;
257 -}
 263+ ## Returns an array with the categories the articles is in.
 264+ ## Also removes them from the text the user views in the editbox.
 265+ public static function getPageCategories( $pageObj ) {
258266
259 -## Returns an array with the categories the articles is in.
260 -## Also removes them from the text the user views in the editbox.
261 -function fnSelectCategoryGetPageCategories( $m_pageObj ) {
262 -
263 - if (array_key_exists('SelectCategoryList', $_POST)) {
264 - # We have already extracted the categories, return them instead
265 - # of extracting zero categories from the page text.
266 - $m_catLinks = array();
267 - foreach( $_POST['SelectCategoryList'] as $m_cat ) {
268 - $m_catLinks[ $m_cat ] = true;
 267+ if (array_key_exists('SelectCategoryList', $_POST)) {
 268+ # We have already extracted the categories, return them instead
 269+ # of extracting zero categories from the page text.
 270+ $catLinks = array();
 271+ foreach( $_POST['SelectCategoryList'] as $cat ) {
 272+ $catLinks[$cat] = true;
 273+ }
 274+ return $catLinks;
269275 }
270 - return $m_catLinks;
271 - }
272276
273 - global $wgContLang;
 277+ global $wgContLang;
274278
275 - # Get page contents:
276 - $m_pageText = $m_pageObj->textbox1;
277 - # Get localised namespace string:
278 - $m_catString = strtolower( $wgContLang->getNsText( NS_CATEGORY ) );
279 - # The regular expression to find the category links:
280 - $m_pattern = "\[\[({$m_catString}|category):([^\|\]]*)(\|{{PAGENAME}}|)\]\]";
281 - $m_replace = "$2";
282 - # The container to store all found category links:
283 - $m_catLinks = array ();
284 - # The container to store the processed text:
285 - $m_cleanText = '';
 279+ # Get page contents
 280+ $pageText = $pageObj->textbox1;
 281+ # Get localised namespace string
 282+ $catString = strtolower( $wgContLang->getNsText( NS_CATEGORY ) );
 283+ # The regular expression to find the category links
 284+ $pattern = "\[\[({$catString}|category):([^\|\]]*)(\|{{PAGENAME}}|)\]\]";
 285+ $replace = "$2";
 286+ # The container to store all found category links
 287+ $catLinks = array ();
 288+ # The container to store the processed text
 289+ $cleanText = '';
286290
287 - # Check linewise for category links:
288 - foreach( explode( "\n", $m_pageText ) as $m_textLine ) {
289 - # Filter line through pattern and store the result:
290 - $m_cleanText .= preg_replace( "/{$m_pattern}/i", "", $m_textLine ) . "\n";
291 - # Check if we have found a category, else proceed with next line:
292 - if( !preg_match( "/{$m_pattern}/i", $m_textLine) ) continue;
293 - # Get the category link from the original text and store it in our list:
294 - $m_catLinks[ str_replace( ' ', '_', preg_replace( "/.*{$m_pattern}/i", $m_replace, $m_textLine ) ) ] = true;
 291+ # Check linewise for category links
 292+ foreach( explode( "\n", $pageText ) as $textLine ) {
 293+ # Filter line through pattern and store the result
 294+ $cleanText .= preg_replace( "/{$pattern}/i", "", $textLine ) . "\n";
 295+ # Check if we have found a category, else proceed with next line
 296+ if( !preg_match( "/{$pattern}/i", $textLine) ) continue;
 297+ # Get the category link from the original text and store it in our list
 298+ $catLinks[ str_replace( ' ', '_', preg_replace( "/.*{$pattern}/i", $replace, $textLine ) ) ] = true;
 299+ }
 300+ # Place the cleaned text into the text box
 301+ $pageObj->textbox1 = trim( $cleanText );
 302+
 303+ # Return the list of categories as an array
 304+ return $catLinks;
295305 }
296 - # Place the cleaned text into the text box:
297 - $m_pageObj->textbox1 = trim( $m_cleanText );
298306
299 - # Return the list of categories as an array:
300 - return $m_catLinks;
301 -}
 307+ # Function that checks if we meet the run conditions of the extension
 308+ public static function checkConditions ($isUpload, $pageObj ) {
 309+ global $wgSelectCategoryNamespaces;
 310+ global $wgSelectCategoryEnableSubpages;
 311+ global $wgTitle;
302312
303 -# Function that checks if we meet the run conditions of the extension
304 -function fnSelectCategoryCheckConditions ($m_isUpload, $m_pageObj ) {
305 - global $wgSelectCategoryNamespaces;
306 - global $wgSelectCategoryEnableSubpages;
307 - global $wgTitle;
308313
 314+ # Run only if we are in an upload, an activated namespace or if page is
 315+ # a subpage and subpages are enabled (unfortunately we can't use
 316+ # implication in PHP) but not if we do a sectionedit
309317
310 - # Run only if we are in an upload, an activated namespace or if page is
311 - # a subpage and subpages are enabled (unfortunately we can't use
312 - # implication in PHP) but not if we do a sectionedit:
 318+ if ($isUpload == true) {
 319+ return true;
 320+ }
313321
314 - if ($m_isUpload == true) {
315 - return true;
316 - }
 322+ $ns = $wgTitle->getNamespace();
 323+ if( array_key_exists( $ns, $wgSelectCategoryNamespaces ) ) {
 324+ $enabledForNamespace = $wgSelectCategoryNamespaces[$ns];
 325+ } else {
 326+ $enabledForNamespace = false;
 327+ }
317328
318 - $ns = $wgTitle->getNamespace();
319 - if( array_key_exists( $ns, $wgSelectCategoryNamespaces ) ) {
320 - $enabledForNamespace = $wgSelectCategoryNamespaces[$ns];
321 - } else {
322 - $enabledForNamespace = false;
 329+ # Check if page is subpage once to save method calls below
 330+ $isSubpage = $wgTitle->isSubpage();
 331+
 332+ if ($enabledForNamespace
 333+ && (!$isSubpage
 334+ || $isSubpage && $wgSelectCategoryEnableSubpage)
 335+ && $pageObj->section == false) {
 336+ return true;
 337+ }
323338 }
324339
325 - # Check if page is subpage once to save method calls below:
326 - $m_isSubpage = $wgTitle->isSubpage();
327 -
328 - if ($enabledForNamespace
329 - && (!$m_isSubpage
330 - || $m_isSubpage && $wgSelectCategoryEnableSubpage)
331 - && $m_pageObj->section == false) {
332 - return true;
333 - }
334340 }

Status & tagging log