UltrasonicNXT has uploaded a new change for review. https://gerrit.wikimedia.org/r/98375
Change subject: {{NUMBEROFCOMMENTSPAGE}} magic word/parser funct ...................................................................... {{NUMBEROFCOMMENTSPAGE}} magic word/parser funct Magic word to allow getting the number of comments on the current page, and number of comments on any given page ({{NUMBEROFCOMMENTS:<page}}). To allow both situations, a magic word and parser function are needed. (This is what makes the code a bit messy). To organise things, I have moved the existing {{NUMBEROFCOMMENTS}} magic word code into NumberOfComments.php, along with the new magic word/parser function code. Change-Id: Ibc66c0d1e26653136f59b72be6fa17650ac740a8 --- M Comment.php M Comments.i18n.magic.php M CommentsHooks.php A NumberOfComments.php 4 files changed, 163 insertions(+), 62 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Comments refs/changes/75/98375/1 diff --git a/Comment.php b/Comment.php index 41014c1..346c61a 100644 --- a/Comment.php +++ b/Comment.php @@ -95,6 +95,14 @@ $wgAutoloadClasses['CommentsHooks'] = $dir . 'CommentsHooks.php'; $wgHooks['ParserFirstCallInit'][] = 'CommentsHooks::onParserFirstCallInit'; $wgHooks['LoadExtensionSchemaUpdates'][] = 'CommentsHooks::onLoadExtensionSchemaUpdates'; -$wgHooks['ParserGetVariableValueSwitch'][] = 'CommentsHooks::assignValueToNumberOfComments'; -$wgHooks['MagicWordwgVariableIDs'][] = 'CommentsHooks::registerNumberOfCommentsMagicWord'; -$wgHooks['RenameUserSQL'][] = 'CommentsHooks::onRenameUserSQL'; \ No newline at end of file +$wgHooks['RenameUserSQL'][] = 'CommentsHooks::onRenameUserSQL'; +// Number of comments hooks +$wgHooks['ParserFirstCallInit'][] = 'NumberOfComments::setupNumberOfCommentsPageParser'; +$wgHooks['MagicWordwgVariableIDs'][] = 'NumberOfComments::registerNumberOfCommentsMagicWord'; +$wgHooks['MagicWordwgVariableIDs'][] = 'NumberOfComments::registerNumberOfCommentsPageMagicWord'; +$wgHooks['ParserGetVariableValueSwitch'][] = 'NumberOfComments::getNumberOfCommentsMagic'; +$wgHooks['ParserGetVariableValueSwitch'][] = 'NumberOfComments::getNumberOfCommentsPageMagic'; + +// NumberOfComments magic word setup +$wgAutoloadClasses['NumberOfComments'] = __DIR__ . '/NumberOfComments.php'; +$wgExtensionMessagesFiles['NumberOfCommentsMagic'] = __DIR__ . '/Comments.i18n.magic.php'; \ No newline at end of file diff --git a/Comments.i18n.magic.php b/Comments.i18n.magic.php index 9f163eb..b91b254 100644 --- a/Comments.i18n.magic.php +++ b/Comments.i18n.magic.php @@ -8,4 +8,5 @@ /** English (English) */ $magicWords['en'] = array( 'NUMBEROFCOMMENTS' => array( 0, 'NUMBEROFCOMMENTS' ), + 'NUMBEROFCOMMENTSPAGE' => array( 0, 'NUMBEROFCOMMENTSPAGE' ), ); diff --git a/CommentsHooks.php b/CommentsHooks.php index ca0a608..279b96c 100644 --- a/CommentsHooks.php +++ b/CommentsHooks.php @@ -93,65 +93,6 @@ } /** - * Registers NUMBEROFCOMMENTS as a valid magic word identifier. - * - * @param $variableIds Array: array of valid magic word identifiers - * @return Boolean - */ - public static function registerNumberOfCommentsMagicWord( &$variableIds ) { - $variableIds[] = 'NUMBEROFCOMMENTS'; - return true; - } - - /** - * Main backend logic for the {{NUMBEROFCOMMENTS}} magic word. - * If the {{NUMBEROFCOMMENTS}} magic word is found, first checks memcached - * to see if we can get the value from cache, but if that fails for some - * reason, then a COUNT(*) SQL query is done to fetch the amount from the - * database. - * - * @param $parser Parser - * @param $cache - * @param $magicWordId String: magic word identifier - * @param $ret Integer: what to return to the user (in our case, the number of comments) - * @return Boolean - */ - public static function assignValueToNumberOfComments( &$parser, &$cache, &$magicWordId, &$ret ) { - global $wgMemc; - - if ( $magicWordId == 'NUMBEROFCOMMENTS' ) { - $key = wfMemcKey( 'comments', 'magic-word' ); - $data = $wgMemc->get( $key ); - if ( $data != '' ) { - // We have it in cache? Oh goody, let's just use the cached value! - wfDebugLog( - 'Comments', - 'Got the amount of comments from memcached' - ); - // return value - $ret = $data; - } else { - // Not cached → have to fetch it from the database - $dbr = wfGetDB( DB_SLAVE ); - $commentCount = (int)$dbr->selectField( - 'Comments', - 'COUNT(*) AS count', - array(), - __METHOD__ - ); - wfDebugLog( 'Comments', 'Got the amount of comments from DB' ); - // Store the count in cache... - // (86400 = seconds in a day) - $wgMemc->set( $key, $commentCount, 86400 ); - // ...and return the value to the user - $ret = $commentCount; - } - } - - return true; - } - - /** * Adds the three new required database tables into the database when the * user runs /maintenance/update.php (the core database updater script). * diff --git a/NumberOfComments.php b/NumberOfComments.php new file mode 100644 index 0000000..4456fab --- /dev/null +++ b/NumberOfComments.php @@ -0,0 +1,151 @@ +<?php + +class NumberOfComments { + // NUMBEROFCOMMENTS - Total comments on wiki + /** + * Registers NUMBEROFCOMMENTS as a valid magic word identifier. + * + * @param $variableIds Array: array of valid magic word identifiers + * @return Boolean + */ + public static function registerNumberOfCommentsMagicWord( &$variableIds ) { + $variableIds[] = 'NUMBEROFCOMMENTS'; + return true; + } + + /** + * Main backend logic for the {{NUMBEROFCOMMENTS}} magic word. + * If the {{NUMBEROFCOMMENTS}} magic word is found, first checks memcached + * to see if we can get the value from cache, but if that fails for some + * reason, then a COUNT(*) SQL query is done to fetch the amount from the + * database. + * + * @param $parser Parser + * @param $cache + * @param $magicWordId String: magic word identifier + * @param $ret Integer: what to return to the user (in our case, the number of comments) + * @return Boolean + */ + public static function getNumberOfCommentsMagic( &$parser, &$cache, &$magicWordId, &$ret ) { + global $wgMemc; + + if ( $magicWordId == 'NUMBEROFCOMMENTS' ) { + $key = wfMemcKey( 'comments', 'magic-word' ); + $data = $wgMemc->get( $key ); + if ( $data != '' ) { + // We have it in cache? Oh goody, let's just use the cached value! + wfDebugLog( + 'Comments', + 'Got the amount of comments from memcached' + ); + // return value + $ret = $data; + } else { + // Not cached → have to fetch it from the database + $dbr = wfGetDB( DB_SLAVE ); + $commentCount = (int)$dbr->selectField( + 'Comments', + 'COUNT(*) AS count', + array(), + __METHOD__ + ); + wfDebugLog( 'Comments', 'Got the amount of comments from DB' ); + // Store the count in cache... + // (86400 = seconds in a day) + $wgMemc->set( $key, $commentCount, 86400 ); + // ...and return the value to the user + $ret = $commentCount; + } + } + + return true; + } + + // NUMBEROFCOMMENTSPAGE - Total comments on one page on the wiki + /** + * Hook to setup magic word + * @param array $customVariableIds + * @return boolean + */ + static function registerNumberOfCommentsPageMagicWord( &$customVariableIds ) { + $customVariableIds[] = 'NUMBEROFCOMMENTSPAGE'; + return true; + } + + /** + * Hook to setup parser function + * @param Parser $parser + * @return boolean + */ + static function setupNumberOfCommentsPageParser( &$parser ) { + $parser->setFunctionHook( 'NUMBEROFCOMMENTSPAGE', 'NumberOfComments::getNumberOfCommentsPageParser', SFH_NO_HASH ); + return true; + } + + /** + * Hook to for magic word {{NUMBEROFCOMMENTSPAGE}} + * @param Parser $parser + * @param unknown $cache + * @param unknown $magicWordId + * @param unknown $ret + * @return boolean + */ + static function getNumberOfCommentsPageMagic( &$parser, &$cache, &$magicWordId, &$ret ) { + $id = $parser->getTitle()->getArticleID(); + $ret = NumberOfComments::getNumberOfCommentsPage( $id ); + + return true; + } + + /** + * Hook for parser function {{NUMBEROFCOMMENTSPAGE:<page>}} + * @param Parser $parser + * @param string $pagename + * @return number + */ + static function getNumberOfCommentsPageParser( $parser, $pagename ) { + $page = Title::newFromText( $pagename ); + + if ( $page instanceof Title ) { + $id = $page->getArticleID(); + } else { + $id = $parser->getTitle()->getArticleID(); + } + + return NumberOfComments::getNumberOfCommentsPage( $id ); + } + + /** + * Get the actual number of comments + * @param int $pageId: ID of page to get number of comments for + * @return int + */ + static function getNumberOfCommentsPage( $pageId ) { + global $wgMemc; + + $key = wfMemcKey( 'comments', 'numberofcommentspage', $pageId ); + $cache = $wgMemc->get( $key ); + + if ( $cache ) { + $val = intval( $cache ); + } else { + $dbr = wfGetDB( DB_SLAVE ); + + $res = $dbr->selectField( + 'Comments', + 'COUNT(*)', + array( 'Comment_Page_ID' => $pageId ), + __METHOD__ + ); + + if ( !$res ) { + $val = 0; + } else { + $val = intval( $res ); + } + $wgMemc->set( $key, $val, 60 * 60 * 24 ); // cache for 24 hours + } + return $val; + } + +} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/98375 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibc66c0d1e26653136f59b72be6fa17650ac740a8 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Comments Gerrit-Branch: master Gerrit-Owner: UltrasonicNXT <adamr_car...@btinternet.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits