neue Version für Mediawiki ab Version 1.32
This commit is contained in:
121
includes/parser/DisplayComments.php
Normal file
121
includes/parser/DisplayComments.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
class DisplayComments {
|
||||
|
||||
/**
|
||||
* Callback function for onParserFirstCallInit(),
|
||||
* displays comments.
|
||||
*
|
||||
* @param $input
|
||||
* @param array $args
|
||||
* @param Parser $parser
|
||||
* @return string HTML
|
||||
*/
|
||||
public static function getParserHandler( $input, $args, $parser ) {
|
||||
global $wgCommentsSortDescending;
|
||||
|
||||
$po = $parser->getOutput();
|
||||
$po->updateCacheExpiry( 0 );
|
||||
// If an unclosed <comments> tag is added to a page, the extension will
|
||||
// go to an infinite loop...this protects against that condition.
|
||||
$parser->setHook( 'comments', [ __CLASS__, 'nonDisplayComments' ] );
|
||||
|
||||
$title = $parser->getTitle();
|
||||
if ( $title->getArticleID() == 0 && $title->getDBkey() == 'CommentListGet' ) {
|
||||
return self::nonDisplayComments( $input, $args, $parser );
|
||||
}
|
||||
|
||||
// Add required CSS & JS via ResourceLoader
|
||||
$po->addModuleStyles( 'ext.comments.css' );
|
||||
$po->addModules( 'ext.comments.js' );
|
||||
$po->addJsConfigVars( [ 'wgCommentsSortDescending' => $wgCommentsSortDescending ] );
|
||||
|
||||
// Parse arguments
|
||||
// The preg_match() lines here are to support the old-style way of
|
||||
// adding arguments:
|
||||
// <comments>
|
||||
// Allow=Foo,Bar
|
||||
// Voting=Plus
|
||||
// </comments>
|
||||
// whereas the normal, standard MediaWiki style, which this extension
|
||||
// also supports is: <comments allow="Foo,Bar" voting="Plus" />
|
||||
$allow = '';
|
||||
if ( preg_match( '/^\s*Allow\s*=\s*(.*)/mi', $input, $matches ) ) {
|
||||
$allow = htmlspecialchars( $matches[1] );
|
||||
} elseif ( !empty( $args['allow'] ) ) {
|
||||
$allow = $args['allow'];
|
||||
}
|
||||
|
||||
$voting = '';
|
||||
if ( preg_match( '/^\s*Voting\s*=\s*(.*)/mi', $input, $matches ) ) {
|
||||
$voting = htmlspecialchars( $matches[1] );
|
||||
} elseif (
|
||||
!empty( $args['voting'] ) &&
|
||||
in_array( strtoupper( $args['voting'] ), [ 'OFF', 'PLUS', 'MINUS' ] )
|
||||
) {
|
||||
$voting = $args['voting'];
|
||||
}
|
||||
|
||||
// Create a new context to execute the CommentsPage
|
||||
$context = new RequestContext;
|
||||
$context->setTitle( $title );
|
||||
$context->setRequest( new FauxRequest() );
|
||||
$context->setUser( $parser->getUser() );
|
||||
$context->setLanguage( $parser->getTargetLanguage() );
|
||||
|
||||
$commentsPage = new CommentsPage( $title->getArticleID(), $context );
|
||||
$commentsPage->allow = $allow;
|
||||
$commentsPage->setVoting( $voting );
|
||||
|
||||
$output = '<div class="comments-body">';
|
||||
|
||||
if ( $wgCommentsSortDescending ) { // form before comments
|
||||
$output .= '<a id="end" rel="nofollow"></a>';
|
||||
if ( !wfReadOnly() ) {
|
||||
$output .= $commentsPage->displayForm();
|
||||
} else {
|
||||
$output .= wfMessage( 'comments-db-locked' )->parse();
|
||||
}
|
||||
}
|
||||
|
||||
$output .= $commentsPage->displayOrderForm();
|
||||
|
||||
$output .= '<div id="allcomments">' . $commentsPage->display() . '</div>';
|
||||
|
||||
// If the database is in read-only mode, display a message informing the
|
||||
// user about that, otherwise allow them to comment
|
||||
if ( !$wgCommentsSortDescending ) { // form after comments
|
||||
if ( !wfReadOnly() ) {
|
||||
$output .= $commentsPage->displayForm();
|
||||
} else {
|
||||
$output .= wfMessage( 'comments-db-locked' )->parse();
|
||||
}
|
||||
$output .= '<a id="end" rel="nofollow"></a>';
|
||||
}
|
||||
|
||||
$output .= '</div>'; // div.comments-body
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public static function nonDisplayComments( $input, $args, $parser ) {
|
||||
$attr = [];
|
||||
|
||||
foreach ( $args as $name => $value ) {
|
||||
$attr[] = htmlspecialchars( $name ) . '="' . htmlspecialchars( $value ) . '"';
|
||||
}
|
||||
|
||||
$output = '<comments';
|
||||
if ( count( $attr ) > 0 ) {
|
||||
$output .= ' ' . implode( ' ', $attr );
|
||||
}
|
||||
|
||||
if ( !is_null( $input ) ) {
|
||||
$output .= '>' . htmlspecialchars( $input ) . '</comments>';
|
||||
} else {
|
||||
$output .= ' />';
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
125
includes/parser/NumberOfComments.php
Normal file
125
includes/parser/NumberOfComments.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
class NumberOfComments {
|
||||
/**
|
||||
* Registers NUMBEROFCOMMENTS and NUMPBEROFCOMMENTSPAGE as a valid magic word identifier.
|
||||
*
|
||||
* @param array $variableIds Array of valid magic word identifiers
|
||||
* @return bool true
|
||||
*/
|
||||
public static function onMagicWordwgVariableIDs( &$variableIds ) {
|
||||
$variableIds[] = 'NUMBEROFCOMMENTS';
|
||||
$variableIds[] = 'NUMBEROFCOMMENTSPAGE';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main backend logic for the {{NUMBEROFCOMMENTS}} and {{NUMBEROFCOMMENTSPAGE}}
|
||||
* 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.
|
||||
* If the {{NUMBEROFCOMMENTSPAGE}} magic word is found, uses
|
||||
* NumberOfComments::getNumberOfCommentsPage to get the number of comments
|
||||
* for this article.
|
||||
*
|
||||
* @param $parser Parser
|
||||
* @param $cache
|
||||
* @param string $magicWordId Magic word identifier
|
||||
* @param int $ret What to return to the user (in our case, the number of comments)
|
||||
* @return bool
|
||||
*/
|
||||
public static function onParserGetVariableValueSwitch( &$parser, &$cache, &$magicWordId, &$ret ) {
|
||||
global $wgMemc;
|
||||
|
||||
if ( $magicWordId == 'NUMBEROFCOMMENTS' ) {
|
||||
$key = $wgMemc->makeKey( '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_REPLICA );
|
||||
$commentCount = (int)$dbr->selectField(
|
||||
'Comments',
|
||||
'COUNT(*) AS count',
|
||||
[],
|
||||
__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;
|
||||
}
|
||||
} elseif ( $magicWordId == 'NUMBEROFCOMMENTSPAGE' ) {
|
||||
$id = $parser->getTitle()->getArticleID();
|
||||
$ret = self::getNumberOfCommentsPage( $id );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for parser function {{NUMBEROFCOMMENTSPAGE:<page>}}
|
||||
*
|
||||
* @param Parser $parser
|
||||
* @param string $pagename Page name
|
||||
* @return int Amount of comments on the given page
|
||||
*/
|
||||
static function getParserHandler( $parser, $pagename ) {
|
||||
$page = Title::newFromText( $pagename );
|
||||
|
||||
if ( $page instanceof Title ) {
|
||||
$id = $page->getArticleID();
|
||||
} else {
|
||||
$id = $parser->getTitle()->getArticleID();
|
||||
}
|
||||
|
||||
return self::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 = $wgMemc->makeKey( 'comments', 'numberofcommentspage', $pageId );
|
||||
$cache = $wgMemc->get( $key );
|
||||
|
||||
if ( $cache ) {
|
||||
$val = intval( $cache );
|
||||
} else {
|
||||
$dbr = wfGetDB( DB_REPLICA );
|
||||
|
||||
$res = $dbr->selectField(
|
||||
'Comments',
|
||||
'COUNT(*)',
|
||||
[ 'Comment_Page_ID' => $pageId ],
|
||||
__METHOD__
|
||||
);
|
||||
|
||||
if ( !$res ) {
|
||||
$val = 0;
|
||||
} else {
|
||||
$val = intval( $res );
|
||||
}
|
||||
$wgMemc->set( $key, $val, 60 * 60 ); // cache for an hour
|
||||
}
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user