Parent5446 has uploaded a new change for review.
https://gerrit.wikimedia.org/r/66003
Change subject: Added NUMBEROFSUBPAGES core parser function
......................................................................
Added NUMBEROFSUBPAGES core parser function
Added a new core parser function NUMBEROFSUBPAGES that returns
the number of subpages under a given Title. Also added a function
Title::getSubpagesCount to facilitate the calculation of this
new parser function.
Bug: 48745
Change-Id: I93ce7bcd1e907da4ab01b3ca9f1026c5bdc229f6
---
M includes/MagicWord.php
M includes/Title.php
M includes/parser/CoreParserFunctions.php
M languages/messages/MessagesEn.php
4 files changed, 43 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/03/66003/1
diff --git a/includes/MagicWord.php b/includes/MagicWord.php
index ae7f8fe..833ec68 100644
--- a/includes/MagicWord.php
+++ b/includes/MagicWord.php
@@ -137,6 +137,7 @@
'numberofusers',
'numberofactiveusers',
'numberofpages',
+ 'numberofsubpages',
'currentversion',
'rootpagename',
'rootpagenamee',
@@ -184,6 +185,7 @@
'numberofusers' => 3600,
'numberofactiveusers' => 3600,
'numberofpages' => 3600,
+ 'numberofsubpages' => 3600,
'currentversion' => 86400,
'currenttimestamp' => 3600,
'localtimestamp' => 3600,
diff --git a/includes/Title.php b/includes/Title.php
index 66a6ce5..3ed5a55 100644
--- a/includes/Title.php
+++ b/includes/Title.php
@@ -2849,6 +2849,23 @@
}
/**
+ * Get the number of subpages this page has
+ *
+ * @return int The number of subpages
+ */
+ public function getSubpagesCount() {
+ if ( !MWNamespace::hasSubpages( $this->getNamespace() ) ) {
+ return 0;
+ }
+
+ $dbr = wfGetDB( DB_SLAVE );
+ $conds['page_namespace'] = $this->getNamespace();
+ $conds[] = 'page_title ' . $dbr->buildLike( $this->getDBkey() .
'/', $dbr->anyString() );
+
+ return (int)$dbr->selectField( 'page', 'COUNT(*)', $conds,
__METHOD__ );
+ }
+
+ /**
* Is there a version of this page in the deletion archive?
*
* @return Int the number of archived revisions
diff --git a/includes/parser/CoreParserFunctions.php
b/includes/parser/CoreParserFunctions.php
index 375ff2b..ea520c0 100644
--- a/includes/parser/CoreParserFunctions.php
+++ b/includes/parser/CoreParserFunctions.php
@@ -58,6 +58,7 @@
$parser->setFunctionHook( 'gender', array( __CLASS__,
'gender' ), SFH_NO_HASH );
$parser->setFunctionHook( 'plural', array( __CLASS__,
'plural' ), SFH_NO_HASH );
$parser->setFunctionHook( 'numberofpages', array( __CLASS__,
'numberofpages' ), SFH_NO_HASH );
+ $parser->setFunctionHook( 'numberofsubpages', array( __CLASS__,
'numberofsubpages' ), SFH_NO_HASH );
$parser->setFunctionHook( 'numberofusers', array( __CLASS__,
'numberofusers' ), SFH_NO_HASH );
$parser->setFunctionHook( 'numberofactiveusers', array(
__CLASS__, 'numberofactiveusers' ), SFH_NO_HASH );
$parser->setFunctionHook( 'numberofarticles', array( __CLASS__,
'numberofarticles' ), SFH_NO_HASH );
@@ -689,6 +690,28 @@
}
/**
+ * Return the number of sub-pages a page has. This is an expensive
parser
+ * function and cannot be called too many times.
+ */
+ static function numberofsubpages( $parser, $name = '', $raw = null ) {
+ static $cache = array();
+ $title = Title::newFromText( $name );
+
+ $length = 0;
+ if ( $title ) {
+ $key = $title->getPrefixedText();
+ if ( isset( $cache[$key] ) ) {
+ $length = $cache[$key];
+ } elseif ( $parser->incrementExpensiveFunctionCount() )
{
+ $length = $title->getSubpagesCount();
+ $cache[$key] = $length;
+ }
+ }
+
+ return self::formatRaw( $length, $raw );
+ }
+
+ /**
* Return the size of the given page, or 0 if it's nonexistent. This
is an
* expensive parser function and can't be called too many times per
page.
*
diff --git a/languages/messages/MessagesEn.php
b/languages/messages/MessagesEn.php
index bc3a381..cbfd5bd 100644
--- a/languages/messages/MessagesEn.php
+++ b/languages/messages/MessagesEn.php
@@ -234,6 +234,7 @@
'localtime' => array( 1, 'LOCALTIME' ),
'localhour' => array( 1, 'LOCALHOUR' ),
'numberofpages' => array( 1, 'NUMBEROFPAGES' ),
+ 'numberofsubpages' => array( 1, 'NUMBEROFSUBPAGES' ),
'numberofarticles' => array( 1, 'NUMBEROFARTICLES' ),
'numberoffiles' => array( 1, 'NUMBEROFFILES' ),
'numberofusers' => array( 1, 'NUMBEROFUSERS' ),
--
To view, visit https://gerrit.wikimedia.org/r/66003
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I93ce7bcd1e907da4ab01b3ca9f1026c5bdc229f6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Parent5446 <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits