http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89698
Revision: 89698
Author: yaron
Date: 2011-06-07 21:43:30 +0000 (Tue, 07 Jun 2011)
Log Message:
-----------
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
trunk/extensions/SelectCategory/SelectCategory.php
trunk/extensions/SelectCategory/SelectCategoryFunctions.php
Modified: trunk/extensions/SelectCategory/CHANGELOG
===================================================================
--- trunk/extensions/SelectCategory/CHANGELOG 2011-06-07 21:39:48 UTC (rev
89697)
+++ trunk/extensions/SelectCategory/CHANGELOG 2011-06-07 21:43:30 UTC (rev
89698)
@@ -1,3 +1,6 @@
+* v0.8.1 - 2011-06-07
+ - Fix for code structure and variable names (Yaron Koren)
+
* v0.8
- using JQuery Treeview to collapse / expand branches
Modified: trunk/extensions/SelectCategory/SelectCategory.php
===================================================================
--- trunk/extensions/SelectCategory/SelectCategory.php 2011-06-07 21:39:48 UTC
(rev 89697)
+++ trunk/extensions/SelectCategory/SelectCategory.php 2011-06-07 21:43:30 UTC
(rev 89698)
@@ -16,10 +16,7 @@
die();
}
-## Load the file containing the hook functions:
-require_once( 'SelectCategoryFunctions.php' );
-
-## Options:
+## Options
# $wgSelectCategoryNamespaces - list of namespaces in which this extension
should be active
if( !isset( $wgSelectCategoryNamespaces ) ) $wgSelectCategoryNamespaces
= array(
NS_MEDIA => true,
@@ -67,8 +64,8 @@
$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name' => 'SelectCategory',
- 'version' => '0.7dev',
- 'author' => 'Leon Weber & Manuel Schneider',
+ 'version' => '0.8.1',
+ 'author' => array( 'Leon Weber', 'Manuel Schneider' ),
'url' =>
'http://www.mediawiki.org/wiki/Extension:SelectCategory',
'descriptionmsg' => 'selectcategory-desc',
);
@@ -76,18 +73,17 @@
$dir = dirname(__FILE__) . '/';
$wgExtensionMessagesFiles['SelectCategory'] = $dir . 'SelectCategory.i18n.php';
-## Set Hook:
-global $wgHooks;
+$wgAutoloadClasses['SelectCategory'] = $dir . 'SelectCategoryFunctions.php';
## Showing the boxes
-# Hook when starting editing:
-$wgHooks['EditPage::showEditForm:initial'][] = array(
'fnSelectCategoryShowHook', false );
-# Hook for the upload page:
-$wgHooks['UploadForm:initial'][] = array( 'fnSelectCategoryShowHook', true );
+# Hook when starting editing
+$wgHooks['EditPage::showEditForm:initial'][] = array(
'SelectCategory::showHook', false );
+# Hook for the upload page
+$wgHooks['UploadForm:initial'][] = array( 'SelectCategory::showHook', true );
## Saving the data
-# Hook when saving page:
-$wgHooks['EditPage::attemptSave'][] = array( 'fnSelectCategorySaveHook', false
);
-# Hook when saving the upload:
-$wgHooks['UploadForm:BeforeProcessing'][] = array( 'fnSelectCategorySaveHook',
true );
+# Hook when saving page
+$wgHooks['EditPage::attemptSave'][] = array( 'SelectCategory::saveHook', false
);
+# Hook when saving the upload
+$wgHooks['UploadForm:BeforeProcessing'][] = array( 'SelectCategory::saveHook',
true );
Modified: trunk/extensions/SelectCategory/SelectCategoryFunctions.php
===================================================================
--- trunk/extensions/SelectCategory/SelectCategoryFunctions.php 2011-06-07
21:39:48 UTC (rev 89697)
+++ trunk/extensions/SelectCategory/SelectCategoryFunctions.php 2011-06-07
21:43:30 UTC (rev 89698)
@@ -17,317 +17,323 @@
die();
}
-## Entry point for the hook and main worker function for editing the page:
-function fnSelectCategoryShowHook( $m_isUpload = false, $m_pageObj ) {
- # check if we should do anything or sleep
- if ( fnSelectCategoryCheckConditions( $m_isUpload, $m_pageObj ) ) {
- # Register CSS file for our select box:
- global $wgOut, $wgScriptPath, $wgUser, $wgTitle;
- global $wgSelectCategoryMaxLevel;
+class SelectCategory {
- $wgOut->addLink(
- array(
- 'rel' => 'stylesheet',
- 'type' => 'text/css',
- 'href' =>
$wgScriptPath.'/extensions/SelectCategory/SelectCategory.css'
- )
- );
- $wgOut->addLink(
- array(
- 'rel' => 'stylesheet',
- 'type' => 'text/css',
- 'href' =>
$wgScriptPath.'/extensions/SelectCategory/jquery.treeview.css'
- )
- );
- $wgOut->addScript( '<script
src="'.$wgScriptPath.'/extensions/SelectCategory/jquery.treeview.js"
type="text/javascript"></script>' );
- $wgOut->addScript( '<script
src="'.$wgScriptPath.'/extensions/SelectCategory/SelectCategory.js"
type="text/javascript"></script>' );
+ ## Entry point for the hook and main function for editing the page
+ public static function showHook( $isUpload = false, $pageObj ) {
- $m_skin =& $wgUser->getSkin();
+ # check if we should do anything or sleep
+ if ( self::checkConditions( $isUpload, $pageObj ) ) {
+ # Register CSS file for our select box
+ global $wgOut, $wgScriptPath, $wgUser, $wgTitle;
+ global $wgSelectCategoryMaxLevel;
- # Get all categories from wiki:
- $m_allCats = fnSelectCategoryGetAllCategories();
- # Load system messages:
+ $wgOut->addLink(
+ array(
+ 'rel' => 'stylesheet',
+ 'type' => 'text/css',
+ 'href' =>
$wgScriptPath.'/extensions/SelectCategory/SelectCategory.css'
+ )
+ );
+ $wgOut->addLink(
+ array(
+ 'rel' => 'stylesheet',
+ 'type' => 'text/css',
+ 'href' =>
$wgScriptPath.'/extensions/SelectCategory/jquery.treeview.css'
+ )
+ );
+ $wgOut->addScript( '<script
src="'.$wgScriptPath.'/extensions/SelectCategory/jquery.treeview.js"
type="text/javascript"></script>' );
+ $wgOut->addScript( '<script
src="'.$wgScriptPath.'/extensions/SelectCategory/SelectCategory.js"
type="text/javascript"></script>' );
+
+ $skin = $wgUser->getSkin();
+
+ # Get all categories from wiki
+ $allCats = self::getAllCategories();
+ # Load system messages
- # Get the right member variables, depending on if we're on an
upload form or not:
- if( !$m_isUpload ) {
- # Extract all categorylinks from page:
- $m_pageCats = fnSelectCategoryGetPageCategories(
$m_pageObj );
+ # Get the right member variables, depending on if we're
on an upload form or not
+ if( !$isUpload ) {
+ # Extract all categorylinks from page
+ $pageCats = self::getPageCategories( $pageObj );
+
+ # Never ever use editFormTextTop here as it
resides outside
+ # the <form> so we will never get contents
+ $place = 'editFormTextAfterWarn';
+ # Print the localised title for the select box
+ $textBefore = '<b>'. wfMsg(
'selectcategory-title' ) . '</b>:';
+ } else {
+ # No need to get categories
+ $pageCats = array();
- # Never ever use editFormTextTop here as it resides
outside
- # the <form> so we will never get contents
- $m_place = 'editFormTextAfterWarn';
- # Print the localised title for the select box:
- $m_textBefore = '<b>'. wfMsg( 'selectcategory-title' )
. '</b>:';
- } else {
- # No need to get categories:
- $m_pageCats = array();
+ # Place output at the right place
+ $place = 'uploadFormTextAfterSummary';
+ # Print the part of the table including the
localised title for the select box
+ $textBefore = "\n</td></tr><tr><td
align='right'><label for='wpSelectCategory'>" . wfMsg( 'selectcategory-title' )
.":</label></td><td align='left'>";
+ }
+ # Introduce the output
+ $pageObj->$place .= "<!-- SelectCategory begin -->\n";
+ # Print the select box
+ $pageObj->$place .= "\n$textBefore";
- # Place output at the right place:
- $m_place = 'uploadFormTextAfterSummary';
- # Print the part of the table including the localised
title for the select box:
- $m_textBefore = "\n</td></tr><tr><td
align='right'><label for='wpSelectCategory'>" . wfMsg( 'selectcategory-title' )
.":</label></td><td align='left'>";
- }
- # Introduce the output:
- $m_pageObj->$m_place .= "<!-- SelectCategory begin -->\n";
- # Print the select box:
- $m_pageObj->$m_place .= "\n$m_textBefore";
+ # Begin list output, use <div> to enable custom
formatting
+ $level = 0;
+ $pageObj->$place .= '<ul id="SelectCategoryList">';
- # Begin list output, use <div> to enable custom formatting
- $m_level = 0;
- $m_pageObj->$m_place .= '<ul id="SelectCategoryList">';
+ foreach( $allCats as $cat => $depth ) {
+ $checked = '';
- foreach( $m_allCats as $m_cat => $m_depth ) {
- $checked = '';
+ # See if the category was already added, so
check it
+ if( isset( $pageCats[$cat] ) ) {
+ $checked = 'checked="checked"';
+ }
+ # Clean HTML Output
+ $category = htmlspecialchars( $cat );
- # See if the category was already added, so check it
- if( isset( $m_pageCats[$m_cat] ) ) {
- $checked = 'checked="checked"';
- }
- # Clean HTML Output:
- $category = htmlspecialchars( $m_cat );
+ # default for root category - otherwise it will
always be closed
+ $open = " class='open' ";
- # default for root category - otherwise it will always
be closed
- $m_open = " class='open' ";
-
- # iterate through levels and adjust divs accordingly
- while( $m_level < $m_depth ) {
- # Collapse subcategories after reaching the
configured MaxLevel
- if( $m_level >= ( $wgSelectCategoryMaxLevel - 1
) ) {
- $m_class = 'display:none;';
- $m_open = " class='closed' ";
- } else {
- $m_class = 'display:block;';
- $m_open = " class='open' ";
+ # iterate through levels and adjust divs
accordingly
+ while( $level < $depth ) {
+ # Collapse subcategories after reaching
the configured MaxLevel
+ if( $level >= (
$wgSelectCategoryMaxLevel - 1 ) ) {
+ $class = 'display:none;';
+ $open = " class='closed' ";
+ } else {
+ $class = 'display:block;';
+ $open = " class='open' ";
+ }
+ $pageObj->$place .= '<ul
style="'.$class.'">'."\n";
+ $level++;
}
- $m_pageObj->$m_place .= '<ul
style="'.$m_class.'">'."\n";
- $m_level++;
+ if( $level == $depth ) {
+ $pageObj->$place .= '</li>'."\n";
+ }
+ while( $level > $depth ) {
+ $pageObj->$place .= '</ul></li>'."\n";
+ $level--;
+ }
+ # Clean names for text output
+ $catName = str_replace( '_', ' ', $category );
+ $title = $wgTitle->newFromText( $category,
NS_CATEGORY );
+ # Output the actual checkboxes, indented
+ $pageObj->$place .= '<li' . $open . '><input
type="checkbox" name="SelectCategoryList[]" value="'.$category.'"
class="checkbox" '.$checked.' />'.$skin->link( $title, $catName )."\n";
+ # set id for next level
+ $level_id = 'sc_'.$cat;
+ } # End walking through cats (foreach)
+ # End of list output - close all remaining divs
+ while( $level > -1 ) {
+ $pageObj->$place .= '</li></ul>'."\n";
+ $level--;
}
- if( $m_level == $m_depth ) $m_pageObj->$m_place .=
'</li>'."\n";
- while( $m_level > $m_depth ) {
- $m_pageObj->$m_place .= '</ul></li>'."\n";
- $m_level--;
- }
- # Clean names for text output
- $title = str_replace( '_', ' ', $category );
- $m_title = $wgTitle->newFromText( $category,
NS_CATEGORY );
- # Output the actual checkboxes, indented
- $m_pageObj->$m_place .= '<li' . $m_open . '><input
type="checkbox" name="SelectCategoryList[]" value="'.$category.'"
class="checkbox" '.$checked.' />'.$m_skin->link( $m_title, $title )."\n";
- # set id for next level
- $m_level_id = 'sc_'.$m_cat;
- } # End walking through cats (foreach)
- # End of list output - close all remaining divs
- while( $m_level > -1 ) {
- $m_pageObj->$m_place .= '</li></ul>'."\n";
- $m_level--;
+
+ # Print localised help string
+ $pageObj->$place .= "<!-- SelectCategory end -->\n";
}
- # Print localised help string:
- $m_pageObj->$m_place .= "<!-- SelectCategory end -->\n";
+ # Return true to let the rest work
+ return true;
}
- # Return true to let the rest work:
- return true;
-}
+ ## Entry point for the hook and main function for saving the page
+ public static function saveHook( $isUpload, $pageObj ) {
+ global $wgContLang;
+ global $wgTitle;
-## Entry point for the hook and main worker function for saving the page:
-function fnSelectCategorySaveHook( $m_isUpload, $m_pageObj ) {
- global $wgContLang;
- global $wgTitle;
+ # check if we should do anything or sleep
+ if ( self::checkConditions( $isUpload, $pageObj ) ) {
- # check if we should do anything or sleep
- if ( fnSelectCategoryCheckConditions( $m_isUpload, $m_pageObj ) ) {
+ # Get localised namespace string
+ $catString = $wgContLang->getNsText( NS_CATEGORY );
- # Get localised namespace string:
- $m_catString = $wgContLang->getNsText( NS_CATEGORY );
+ # default sort key is page name with stripped namespace
name,
+ # otherwise sorting is ugly.
+ if( $wgTitle->getNamespace() == NS_MAIN ) {
+ $default_sortkey = "";
+ } else {
+ $default_sortkey = "|{{PAGENAME}}";
+ }
- # default sort key is page name with stripped namespace name,
- # otherwise sorting is ugly.
- if( $wgTitle->getNamespace() == NS_MAIN ) {
- $default_sortkey = "";
- } else {
- $default_sortkey = "|{{PAGENAME}}";
- }
+ # Get some distance from the rest of the content
+ $text = "\n";
- # Get some distance from the rest of the content:
- $m_text = "\n";
-
- # Iterate through all selected category entries:
- if (array_key_exists('SelectCategoryList', $_POST)) {
- foreach( $_POST['SelectCategoryList'] as $m_cat ) {
- $m_text .=
"\n[[$m_catString:$m_cat$default_sortkey]]";
+ # Iterate through all selected category entries
+ if (array_key_exists('SelectCategoryList', $_POST)) {
+ foreach( $_POST['SelectCategoryList'] as $cat )
{
+ $text .=
"\n[[$catString:$cat$default_sortkey]]";
+ }
}
+ # If it is an upload we have to call a different method
+ if ( $isUpload ) {
+ $pageObj->mUploadDescription .= $text;
+ } else {
+ $pageObj->textbox1 .= $text;
+ }
}
- # If it is an upload we have to call a different method:
- if ( $m_isUpload ) {
- $m_pageObj->mUploadDescription .= $m_text;
- } else {
- $m_pageObj->textbox1 .= $m_text;
- }
+
+ # Return to the let MediaWiki do the rest of the work
+ return true;
}
- # Return to the let MediaWiki do the rest of the work:
- return true;
-}
+ ## Get all categories from the wiki - starting with a given root or
otherwise detect root automagically (expensive)
+ ## Returns an array like this
+ ## array (
+ ## 'Name' => (int) Depth,
+ ## ...
+ ## )
+ public static function getAllCategories() {
+ global $wgTitle;
+ global $wgSelectCategoryRoot;
-## Get all categories from the wiki - starting with a given root or otherwise
detect root automagically (expensive)
-## Returns an array like this:
-## array (
-## 'Name' => (int) Depth,
-## ...
-## )
-function fnSelectCategoryGetAllCategories() {
- global $wgTitle;
- global $wgSelectCategoryRoot;
+ # Get current namespace (save duplicate call of method)
+ $namespace = $wgTitle->getNamespace();
+ if( $namespace >= 0 && $wgSelectCategoryRoot[$namespace] ) {
+ # Include root and step into the recursion
+ $allCats = array_merge( array(
$wgSelectCategoryRoot[$namespace] => 0 ),
+ self::getChildren(
$wgSelectCategoryRoot[$namespace] ) );
+ } else {
+ # Initialize return value
+ $allCats = array();
- # Get current namespace (save duplicate call of method):
- $m_namespace = $wgTitle->getNamespace();
- if( $m_namespace >= 0 && $wgSelectCategoryRoot[$m_namespace] ) {
- # Include root and step into the recursion:
- $m_allCats = array_merge( array(
$wgSelectCategoryRoot[$m_namespace] => 0 ),
- fnSelectCategoryGetChildren(
$wgSelectCategoryRoot[$m_namespace] ) );
- } else {
- # Initialize return value:
- $m_allCats = array();
+ # Get a database object
+ $dbObj = wfGetDB( DB_SLAVE );
+ # Get table names to access them in SQL query
+ $tblCatLink = $dbObj->tableName( 'categorylinks' );
+ $tblPage = $dbObj->tableName( 'page' );
- # Get a database object:
- $m_dbObj = wfGetDB( DB_SLAVE );
- # Get table names to access them in SQL query:
- $m_tblCatLink = $m_dbObj->tableName( 'categorylinks' );
- $m_tblPage = $m_dbObj->tableName( 'page' );
+ # Automagically detect root categories
+ $sql = " SELECT tmpSelectCat1.cl_to AS title
+FROM $tblCatLink AS tmpSelectCat1
+LEFT JOIN $tblPage AS tmpSelectCatPage ON (tmpSelectCat1.cl_to =
tmpSelectCatPage.page_title AND tmpSelectCatPage.page_namespace = 14)
+LEFT JOIN $tblCatLink AS tmpSelectCat2 ON tmpSelectCatPage.page_id =
tmpSelectCat2.cl_from
+WHERE tmpSelectCat2.cl_from IS NULL GROUP BY tmpSelectCat1.cl_to";
+ # Run the query
+ $res = $dbObj->query( $sql, __METHOD__ );
+ # Process the resulting rows
+ while ( $row = $dbObj->fetchRow( $res ) ) {
+ $allCats += array( $row['title'] => 0 );
+ $allCats += self::getChildren( $row['title'] );
+ }
+ # Free result
+ $dbObj->freeResult( $res );
+ }
- # Automagically detect root categories:
- $m_sql = " SELECT tmpSelectCat1.cl_to AS title
- FROM $m_tblCatLink AS tmpSelectCat1
- LEFT JOIN $m_tblPage AS tmpSelectCatPage ON
(tmpSelectCat1.cl_to = tmpSelectCatPage.page_title AND
tmpSelectCatPage.page_namespace = 14)
- LEFT JOIN $m_tblCatLink AS tmpSelectCat2 ON
tmpSelectCatPage.page_id = tmpSelectCat2.cl_from
- WHERE tmpSelectCat2.cl_from IS NULL GROUP BY
tmpSelectCat1.cl_to";
- # Run the query:
- $m_res = $m_dbObj->query( $m_sql, __METHOD__ );
- # Process the resulting rows:
- while ( $m_row = $m_dbObj->fetchRow( $m_res ) ) {
- $m_allCats += array( $m_row['title'] => 0 );
- $m_allCats += fnSelectCategoryGetChildren(
$m_row['title'] );
- }
- # Free result:
- $m_dbObj->freeResult( $m_res );
+ # Afterwards return the array to the caller
+ return $allCats;
}
- # Afterwards return the array to the caller:
- return $m_allCats;
-}
+ public static function getChildren( $root, $depth = 1 ) {
+ # Initialize return value
+ $allCats = array();
-function fnSelectCategoryGetChildren( $m_root, $m_depth = 1 ) {
- # Initialize return value:
- $m_allCats = array();
+ # Get a database object
+ $dbObj = wfGetDB( DB_SLAVE );
+ # Get table names to access them in SQL query
+ $tblCatLink = $dbObj->tableName( 'categorylinks' );
+ $tblPage = $dbObj->tableName( 'page' );
- # Get a database object:
- $m_dbObj = wfGetDB( DB_SLAVE );
- # Get table names to access them in SQL query:
- $m_tblCatLink = $m_dbObj->tableName( 'categorylinks' );
- $m_tblPage = $m_dbObj->tableName( 'page' );
+ # The normal query to get all children of a given root category
+ $sql = 'SELECT tmpSelectCatPage.page_title AS title
+FROM '.$tblCatLink.' AS tmpSelectCat
+LEFT JOIN '.$tblPage.' AS tmpSelectCatPage
+ ON tmpSelectCat.cl_from = tmpSelectCatPage.page_id
+WHERE tmpSelectCat.cl_to LIKE '.$dbObj->addQuotes( $root ).'
+ AND tmpSelectCatPage.page_namespace = 14
+ORDER BY tmpSelectCatPage.page_title ASC;';
+ # Run the query
+ $res = $dbObj->query( $sql, __METHOD__ );
+ # Process the resulting rows
+ while ( $row = $dbObj->fetchRow( $res ) ) {
+ # Survive category link loops
+ if( $root == $row['title'] ) {
+ continue;
+ }
+ # Add current entry to array
+ $allCats += array( $row['title'] => $depth );
+ $allCats += self::getChildren( $row['title'], $depth +
1 );
+ }
+ # Free result
+ $dbObj->freeResult( $res );
- # The normal query to get all children of a given root category:
- $m_sql = '
- SELECT tmpSelectCatPage.page_title AS title
- FROM '.$m_tblCatLink.' AS tmpSelectCat
- LEFT JOIN '.$m_tblPage.' AS tmpSelectCatPage
- ON tmpSelectCat.cl_from = tmpSelectCatPage.page_id
- WHERE tmpSelectCat.cl_to LIKE '.$m_dbObj->addQuotes( $m_root ).'
- AND tmpSelectCatPage.page_namespace = 14
- ORDER BY tmpSelectCatPage.page_title ASC;';
- # Run the query:
- $m_res = $m_dbObj->query( $m_sql, __METHOD__ );
- # Process the resulting rows:
- while ( $m_row = $m_dbObj->fetchRow( $m_res ) ) {
- # Survive category link loops:
- if( $m_root == $m_row['title'] ) {
- continue;
- }
- # Add current entry to array:
- $m_allCats += array( $m_row['title'] => $m_depth );
- $m_allCats += fnSelectCategoryGetChildren( $m_row['title'],
$m_depth + 1 );
+ # Afterwards return the array to the upper recursion level
+ return $allCats;
}
- # Free result:
- $m_dbObj->freeResult( $m_res );
- # Afterwards return the array to the upper recursion level:
- return $m_allCats;
-}
+ ## Returns an array with the categories the articles is in.
+ ## Also removes them from the text the user views in the editbox.
+ public static function getPageCategories( $pageObj ) {
-## Returns an array with the categories the articles is in.
-## Also removes them from the text the user views in the editbox.
-function fnSelectCategoryGetPageCategories( $m_pageObj ) {
-
- if (array_key_exists('SelectCategoryList', $_POST)) {
- # We have already extracted the categories, return them instead
- # of extracting zero categories from the page text.
- $m_catLinks = array();
- foreach( $_POST['SelectCategoryList'] as $m_cat ) {
- $m_catLinks[ $m_cat ] = true;
+ if (array_key_exists('SelectCategoryList', $_POST)) {
+ # We have already extracted the categories, return them
instead
+ # of extracting zero categories from the page text.
+ $catLinks = array();
+ foreach( $_POST['SelectCategoryList'] as $cat ) {
+ $catLinks[$cat] = true;
+ }
+ return $catLinks;
}
- return $m_catLinks;
- }
- global $wgContLang;
+ global $wgContLang;
- # Get page contents:
- $m_pageText = $m_pageObj->textbox1;
- # Get localised namespace string:
- $m_catString = strtolower( $wgContLang->getNsText( NS_CATEGORY ) );
- # The regular expression to find the category links:
- $m_pattern =
"\[\[({$m_catString}|category):([^\|\]]*)(\|{{PAGENAME}}|)\]\]";
- $m_replace = "$2";
- # The container to store all found category links:
- $m_catLinks = array ();
- # The container to store the processed text:
- $m_cleanText = '';
+ # Get page contents
+ $pageText = $pageObj->textbox1;
+ # Get localised namespace string
+ $catString = strtolower( $wgContLang->getNsText( NS_CATEGORY )
);
+ # The regular expression to find the category links
+ $pattern =
"\[\[({$catString}|category):([^\|\]]*)(\|{{PAGENAME}}|)\]\]";
+ $replace = "$2";
+ # The container to store all found category links
+ $catLinks = array ();
+ # The container to store the processed text
+ $cleanText = '';
- # Check linewise for category links:
- foreach( explode( "\n", $m_pageText ) as $m_textLine ) {
- # Filter line through pattern and store the result:
- $m_cleanText .= preg_replace( "/{$m_pattern}/i", "",
$m_textLine ) . "\n";
- # Check if we have found a category, else proceed with next
line:
- if( !preg_match( "/{$m_pattern}/i", $m_textLine) ) continue;
- # Get the category link from the original text and store it in
our list:
- $m_catLinks[ str_replace( ' ', '_', preg_replace(
"/.*{$m_pattern}/i", $m_replace, $m_textLine ) ) ] = true;
+ # Check linewise for category links
+ foreach( explode( "\n", $pageText ) as $textLine ) {
+ # Filter line through pattern and store the result
+ $cleanText .= preg_replace( "/{$pattern}/i", "",
$textLine ) . "\n";
+ # Check if we have found a category, else proceed with
next line
+ if( !preg_match( "/{$pattern}/i", $textLine) ) continue;
+ # Get the category link from the original text and
store it in our list
+ $catLinks[ str_replace( ' ', '_', preg_replace(
"/.*{$pattern}/i", $replace, $textLine ) ) ] = true;
+ }
+ # Place the cleaned text into the text box
+ $pageObj->textbox1 = trim( $cleanText );
+
+ # Return the list of categories as an array
+ return $catLinks;
}
- # Place the cleaned text into the text box:
- $m_pageObj->textbox1 = trim( $m_cleanText );
- # Return the list of categories as an array:
- return $m_catLinks;
-}
+ # Function that checks if we meet the run conditions of the extension
+ public static function checkConditions ($isUpload, $pageObj ) {
+ global $wgSelectCategoryNamespaces;
+ global $wgSelectCategoryEnableSubpages;
+ global $wgTitle;
-# Function that checks if we meet the run conditions of the extension
-function fnSelectCategoryCheckConditions ($m_isUpload, $m_pageObj ) {
- global $wgSelectCategoryNamespaces;
- global $wgSelectCategoryEnableSubpages;
- global $wgTitle;
+ # Run only if we are in an upload, an activated namespace or if
page is
+ # a subpage and subpages are enabled (unfortunately we can't use
+ # implication in PHP) but not if we do a sectionedit
- # Run only if we are in an upload, an activated namespace or if page is
- # a subpage and subpages are enabled (unfortunately we can't use
- # implication in PHP) but not if we do a sectionedit:
+ if ($isUpload == true) {
+ return true;
+ }
- if ($m_isUpload == true) {
- return true;
- }
+ $ns = $wgTitle->getNamespace();
+ if( array_key_exists( $ns, $wgSelectCategoryNamespaces ) ) {
+ $enabledForNamespace = $wgSelectCategoryNamespaces[$ns];
+ } else {
+ $enabledForNamespace = false;
+ }
- $ns = $wgTitle->getNamespace();
- if( array_key_exists( $ns, $wgSelectCategoryNamespaces ) ) {
- $enabledForNamespace = $wgSelectCategoryNamespaces[$ns];
- } else {
- $enabledForNamespace = false;
+ # Check if page is subpage once to save method calls below
+ $isSubpage = $wgTitle->isSubpage();
+
+ if ($enabledForNamespace
+ && (!$isSubpage
+ || $isSubpage && $wgSelectCategoryEnableSubpage)
+ && $pageObj->section == false) {
+ return true;
+ }
}
- # Check if page is subpage once to save method calls below:
- $m_isSubpage = $wgTitle->isSubpage();
-
- if ($enabledForNamespace
- && (!$m_isSubpage
- || $m_isSubpage && $wgSelectCategoryEnableSubpage)
- && $m_pageObj->section == false) {
- return true;
- }
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs