jenkins-bot has submitted this change and it was merged.
Change subject: Refactor information collection for i18n to dedicated class
......................................................................
Refactor information collection for i18n to dedicated class
Attempts to refactor many of the longer and multi-line php statements
out of post.html.php and into a helper class. For the most part these
are i18n messages rendering.
The primary goal of moving the i18n messages from post.html.php into
View\Post is that I think they are a primary source of the 'php mess'
that is infiltrating the html markup we want to focus on in template
files. The various i18n messages need to collect information from
the data models before they can output the information, so this patch
moves that information collection into a class dedicated to that
aspect of rendering.
Change-Id: I6024f66becb8b858b824f9e9f5ca04e07d0f91f3
---
M Flow.php
M includes/Templating.php
A includes/View/Post.php
M modules/base/ext.flow.base.js
M templates/post.html.php
5 files changed, 237 insertions(+), 63 deletions(-)
Approvals:
Matthias Mullie: Looks good to me, approved
jenkins-bot: Verified
diff --git a/Flow.php b/Flow.php
index bcda687..b8bf4a7 100755
--- a/Flow.php
+++ b/Flow.php
@@ -91,6 +91,7 @@
$wgAutoloadClasses['Flow\View\History\HistoryRecord'] =
"$dir/includes/View/History/HistoryRecord.php";
$wgAutoloadClasses['Flow\View\History\HistoryBundle'] =
"$dir/includes/View/History/HistoryBundle.php";
$wgAutoloadClasses['Flow\View\History\HistoryRenderer'] =
"$dir/includes/View/History/HistoryRenderer.php";
+$wgAutoloadClasses['Flow\View\Post'] = "$dir/includes/View/Post.php";
// Classes that deal with database interaction between database and the models
$wgAutoloadClasses['Flow\Repository\TreeRepository'] = $dir .
'includes/Repository/TreeRepository.php';
diff --git a/includes/Templating.php b/includes/Templating.php
index d22ee68..c657e78 100644
--- a/includes/Templating.php
+++ b/includes/Templating.php
@@ -15,7 +15,6 @@
use RequestContext;
use Title;
use User;
-use Flow\View\PostActionMenu;
class Templating {
public $urlGenerator;
@@ -93,21 +92,27 @@
// @todo: I don't like container being pulled in here, improve
this some day
$container = Container::getContainer();
+ // An ideal world may pull this from the container, but for now
this is fine. This templating
+ // class has too many responsibilities to keep receiving all
required objects in the constructor.
+ $view = new View\Post(
+ $container['user'],
+ $post,
+ new View\PostActionMenu(
+ $this->urlGenerator,
+ $container['flow_actions'],
+ new PostActionPermissions(
$container['flow_actions'], $container['user'] ),
+ $block,
+ $post,
+ $container['user']->getEditToken(
$wgFlowTokenSalt )
+ )
+ );
+
return $this->render(
'flow:post.html.php',
array(
'block' => $block,
'post' => $post,
- // An ideal world may pull this from the
container, but for now this is fine. This templating
- // class has too many responsibilities to keep
receiving all required objects in the constructor.
- 'postActionMenu' => new PostActionMenu(
- $this->urlGenerator,
- $container['flow_actions'],
- new PostActionPermissions(
$container['flow_actions'], $container['user'] ),
- $block,
- $post,
- $container['user']->getEditToken(
$wgFlowTokenSalt )
- ),
+ 'postView' => $view,
),
$return
);
@@ -157,12 +162,19 @@
}
public function userToolLinks( $userId, $userText ) {
- if ( $userText instanceof MWMessage ) {
- // username was moderated away, we don't know who this
is
- return '';
+ global $wgLang;
+ static $cache = array();
+ if ( isset( $cache[$userId][$userText] ) ) {
+ return $cache[$userId][$userText];
}
- return Linker::userLink( $userId, $userText ) .
Linker::userToolLinks( $userId, $userText );
+ if ( $userText instanceof MWMessage ) {
+ // username was moderated away, we dont know who this is
+ $res = '';
+ } else {
+ $res = Linker::userLink( $userId, $userText ) .
Linker::userToolLinks( $userId, $userText );
+ }
+ return $cache[$userId][$userText] = $res;
}
/**
@@ -197,7 +209,7 @@
$haveAnon = true;
}
- $text = $this->getUserText( $participant );
+ $text = self::getUserText( $participant );
if ( !$text || !$participant ) {
continue;
@@ -224,12 +236,12 @@
* Usually the user's name, but it can also return "an anonymous user",
* or information about an item's moderation state.
*
- * @param User $user The User object to get a
description for.
+ * @param User $user The User object to get a
description of.
* @param AbstractRevision $rev An AbstractRevision object to
retrieve moderation state from.
* @param bool $showIPs Whether or not to show IP
addresses for anonymous users
* @return String A human-readable identifier for
the given User.
*/
- public function getUserText( $user, $rev = null, $showIPs = false ) {
+ static public function getUserText( $user, $rev = null, $showIPs =
false ) {
if ( $user === false && $rev instanceof AbstractRevision ) {
$state = $rev->getModerationState();
@@ -243,7 +255,7 @@
return wfMessage(
AbstractRevision::$perms[$state]['content'],
- $this->getUserText( $user ),
+ self::getUserText( $user ),
$moderatedAt->getHumanTimestamp()
);
} elseif ( $user === false ) {
diff --git a/includes/View/Post.php b/includes/View/Post.php
new file mode 100644
index 0000000..0546624
--- /dev/null
+++ b/includes/View/Post.php
@@ -0,0 +1,168 @@
+<?php
+
+namespace Flow\View;
+
+use Flow\Model\PostRevision;
+use Flow\Templating;
+use Linker;
+use User;
+
+class Post {
+ protected $user;
+ protected $post;
+ protected $actions;
+ protected $creatorUserText;
+
+ /**
+ * @param User $user The User viewing posts
+ */
+ public function __construct( User $user, PostRevision $post,
PostActionMenu $actions ) {
+ $this->user = $user;
+ $this->post = $post;
+ $this->actions = $actions;
+
+ $this->creatorUserText = Templating::getUserText(
+ $post->getCreator( $this->user ),
+ $post
+ );
+
+ }
+
+ public function replyPlaceholder() {
+ return wfMessage( 'flow-reply-placeholder',
$this->creatorUserText )->text();
+ }
+
+ public function replySubmit() {
+ return wfMessage( 'flow-reply-submit', $this->creatorUserText
)->text();
+ }
+
+ public function replyLink() {
+ return wfMessage( 'flow-reply-link', $this->creatorUserText
)->escaped();
+ }
+
+ public function thankLink() {
+ return wfMessage( 'flow-thank-link',
$this->creatorUserText)->escaped();
+ }
+
+ public function moderatedTalkLink() {
+ $user = User::newFromId( $this->post->getModeratedByUserId() );
+ $title = $user->getTalkPage();
+
+ return array(
+ $title->getLinkUrl(),
+ wfMessage(
+ 'flow-talk-link',
+ $this->post->getModeratedByUserText()
+ )->escaped()
+ );
+ }
+
+ public function creator() {
+ return $this->creatorUserText;
+ }
+
+ public function userToolLinks( $userId, $userText ) {
+ if ( $userText instanceof MWMessage ) {
+ // username was moderated away, we dont know who this is
+ return '';
+ } else {
+ return Linker::userLink( $userId, $userText ) .
Linker::userToolLinks( $userId, $userText );
+ }
+ }
+
+ public function creatorToolLinks() {
+ return $this->userToolLinks(
+ $this->post->getCreatorId( $this->user ),
+ $this->post->getCreatorName( $this->user )
+ );
+ }
+ public function editPostButton( PostRevision $post, $buttonClass ) {
+ if ( !$this->actions->isAllowed( 'edit-post' ) ) {
+ return '';
+ }
+ return $this->actions->getButton(
+ 'edit-post',
+ wfMessage( 'flow-post-action-edit-post' )->plain(),
+ $buttonClass
+ );
+ }
+
+ public function postHistoryButton( PostRevision $post, $content ) {
+ if ( $this->actions->isAllowed( 'post-history' ) ) {
+ return $this->actions->getButton(
+ 'post-history',
+ $content,
+ 'flow-action-history-link'
+ );
+ } else {
+ return $content;
+ }
+ }
+
+ public function hidePostButton( PostRevision $post, $buttonClass ) {
+ if ( !$this->actions->isAllowed( 'hide-post' ) ) {
+ return '';
+ }
+ return $this->actions->getButton(
+ 'hide-post',
+ wfMessage( 'flow-post-action-hide-post' )->plain(),
+ $buttonClass
+ );
+ }
+
+ public function deletePostButton( PostRevision $post, $buttonClass ) {
+ if ( !$this->actions->isAllowed( 'delete-post' ) ) {
+ return '';
+ }
+ return $this->actions->getButton(
+ 'delete-post',
+ wfMessage( 'flow-post-action-delete-post' )->plain(),
+ $buttonClass
+ );
+ }
+
+ public function suppressPostButton( PostRevision $post, $buttonClass ) {
+ if ( !$this->actions->isAllowed( 'censor-post' ) ) {
+ return '';
+ }
+ return $this->actions->getButton(
+ 'censor-post',
+ wfMessage( 'flow-post-action-censor-post' )->plain(),
+ $buttonClass
+ );
+ }
+
+ public function restorePostButton( PostRevision $post, $buttonClass ) {
+ if ( !$this->actions->isAllowed( 'restore-post' ) ) {
+ return '';
+ }
+ return $this->actions->getButton(
+ 'restore-post',
+ wfMessage( 'flow-post-action-restore-post' )->plain(),
+ $buttonClass
+ );
+ }
+
+ public function actions() {
+ return $this->actions;
+ }
+
+ public function allowedAnyActions() {
+ // This will need to change, but not sure best way
+ return $this->actions->isAllowedAny( 'hide-post',
'delete-post', 'censor-post', 'restore-post' );
+ }
+
+ /**
+ * Gets a Flow-formatted plaintext human-readable identifier for a user.
+ * Usually the user's name, but it can also return "an anonymous user",
+ * or information about an item's moderation state.
+ *
+ * @param User $user The User object to get a
description of.
+ * @param AbstractRevision $rev An AbstractRevision object to
retrieve moderation state from.
+ * @param bool $showIPs Whether or not to show IP addresses
for anonymous users
+ * @return String A human-readable identifier for the
given User.
+ */
+ public function getUserText( $user, $rev = null, $showIPs = false ) {
+ return Templating::getUserText( $user, $rev, $showIPs );
+ }
+}
diff --git a/modules/base/ext.flow.base.js b/modules/base/ext.flow.base.js
index ca1c172..9489bef 100644
--- a/modules/base/ext.flow.base.js
+++ b/modules/base/ext.flow.base.js
@@ -276,4 +276,10 @@
'reason'
]
);
+
+// random onclick handler moved from html template
+mw.flow.notImplemented = function() {
+ alert( '@todo: Not yet implemented!' );
+ return false;
+};
} )( jQuery, mediaWiki );
diff --git a/templates/post.html.php b/templates/post.html.php
index 25d0caa..7e611d8 100644
--- a/templates/post.html.php
+++ b/templates/post.html.php
@@ -22,8 +22,7 @@
'value' => $post->getPostId()->getHex(),
) ) .
Html::textarea( $block->getName() . '[content]', '', array(
- 'placeholder' => wfMessage( 'flow-reply-placeholder',
- $this->getUserText( $post->getCreator( $user ),
$post ) )->text(),
+ 'placeholder' => $postView->replyPlaceholder( $post ),
'class' => 'flow-reply-content flow-input mw-ui-input',
) ) .
// NOTE: cancel button will be added via JS, makes no sense in
non-JS context
@@ -31,26 +30,19 @@
Html::openElement( 'div', array( 'class' =>
'flow-post-form-controls' ) ) .
Html::element( 'input', array(
'type' => 'submit',
- 'value' => wfMessage( 'flow-reply-submit',
$this->getUserText( $post->getCreator( $user ), $post ) )->text(),
+ 'value' => $postView->replySubmit( $post ),
'class' => 'mw-ui-button mw-ui-constructive
flow-reply-submit',
) ) .
Html::closeElement( 'div' ) .
Html::closeElement( 'form' );
}
-
-// The actual output
-echo Html::openElement( 'div', array(
- 'class' => 'flow-post-container',
- 'data-post-id' => $post->getPostId()->getHex(),
- 'data-creator-name' => $post->getCreatorName(),
-) );
- echo Html::openElement( 'div', array(
- 'class' => 'flow-post flow-element-container ' . (
$post->isModerated() ? 'flow-post-moderated' : 'flow-post-unmoderated' ),
- 'data-post-id' => $post->getPostId()->getHex(),
- 'id' => 'flow-post-' . $post->getPostId()->getHex(),
- ) ); ?>
-
+?>
+<div class='flow-post-container'
+ data-revision-id='<?php echo $post->getRevisionId()->getHex() ?>'
+ data-post-id='<?php echo $post->getPostId()->getHex() ?>'
+ data-creator-name='<?php echo $post->getCreatorName() ?>'>
+ <div class='flow-post flow-element-container <?php echo
$post->isModerated() ? 'flow-post-moderated' : 'flow-post-unmoderated' ?>' >
<?php if ( $post->isModerated() ): ?>
<p class="flow-post-moderated-message
flow-post-moderated-<?php echo $post->getModerationState(); ?>
flow-post-content-<?php echo $post->isAllowed( $user ) ? 'allowed' :
'disallowed'; ?>">
<?php
@@ -64,21 +56,21 @@
<div class="flow-post-title">
<span class="flow-creator">
<span class="flow-creator-simple"
style="display: inline">
- <?php echo $this->getUserText(
$post->getCreator( $user ), $post ); ?>
+ <?php echo $postView->creator(
$post ) ?>
</span>
<span class="flow-creator-full"
style="display: none">
- <?php echo
$this->userToolLinks( $post->getCreatorId( $user ), $post->getCreatorName(
$user ) ); ?>
+ <?php echo
$postView->creatorToolLinks( $post ) ?>
</span>
</span>
- </div>
+ </div>
<div class="flow-post-content">
<?php echo $post->getContent( $user, 'html' );
?>
</div>
+
+ <?php echo $postView->editPostButton( $post,
'flow-edit-post-link flow-icon flow-icon-bottom-aligned' ); ?>
+
<?php
- if ( $postActionMenu->isAllowed( 'edit-post' ) ) {
- echo $postActionMenu->getButton( 'edit-post',
wfMessage( 'flow-post-action-edit-post' )->plain(), 'flow-edit-post-link
flow-icon flow-icon-bottom-aligned' );
- }
echo Html::element(
'a',
array(
@@ -98,48 +90,43 @@
<span class="flow-utctime"
style="display: none">' . htmlspecialchars(
$post->getPostId()->getTimestampObj()->getTimestamp( TS_RFC2822 ) ) . '</span>';
// build history button with timestamp
html as content
- if ( $postActionMenu->isAllowed(
'post-history' ) ) {
- echo
$postActionMenu->getButton( 'post-history', $content,
'flow-action-history-link' );
- } else {
- echo $content;
- }
+ echo $postView->postHistoryButton(
$post, $content );
?>
</p>
<div class="flow-post-interaction">
<?php if ( !$post->isModerated() ): ?>
- <a class="flow-reply-link mw-ui-button"
href="#"><span><?php echo wfMessage( 'flow-reply-link', $this->getUserText(
$post->getCreator( $user ), $post ) )->escaped(); ?></span></a>
- <a class="flow-thank-link mw-ui-button"
href="#" onclick="alert( '@todo: Not yet implemented!' ); return
false;"><span><?php echo wfMessage( 'flow-thank-link', $this->getUserText(
$post->getCreator( $user ), $post ) )->escaped(); ?></span></a>
+ <a class="flow-reply-link mw-ui-button"
href="#"><span><?php echo $postView->replyLink( $post ); ?></span></a>
+ <a class="flow-thank-link mw-ui-button"
href="#" onclick="return mw.flow.notImplemented()">
+ <span><?php echo
$postView->thankLink( $post ); ?></span>
+ </a>
<?php else: ?>
- <?php
- $user = User::newFromId(
$post->getModeratedByUserId() );
- $title = $user->getTalkPage();
- ?>
- <a class="flow-talk-link mw-ui-button"
href="<?php echo $title->getLinkURL(); ?>">
- <span><?php echo wfMessage(
'flow-talk-link', $post->getModeratedByUserText() )->escaped(); ?></span>
+ <?php list( $talkUrl, $talkLink ) =
$postView->moderatedTalkLink( $post ); ?>
+ <a class="flow-talk-link mw-ui-button"
href="<?php echo $talkUrl; ?>">
+ <span><?php echo $talkLink;
?></span>
</a>
<?php endif; ?>
</div>
</div>
- <?php if ( $postActionMenu->isAllowedAny( 'hide-post',
'delete-post', 'censor-post', 'restore-post' ) ): ?>
+ <?php if ( $postView->actions()->isAllowedAny( 'hide-post',
'delete-post', 'censor-post', 'restore-post' ) ): ?>
<div class="flow-actions">
<a class="flow-actions-link flow-icon
flow-icon-bottom-aligned" href="#"><?php echo wfMessage( 'flow-post-actions'
)->escaped(); ?></a>
<div class="flow-actions-flyout">
<ul>
<?php
- if ( $postActionMenu->isAllowed(
'hide-post' ) ) {
- echo '<li
class="flow-action-hide">'. $postActionMenu->getButton( 'hide-post', wfMessage(
'flow-post-action-hide-post' )->plain(), 'flow-hide-post-link mw-ui-button' )
.'</li>';
+ if ( $hidePost =
$postView->hidePostButton( $post, 'flow-hide-post-link mw-ui-button' ) ) {
+ echo "<li
class='flow-action-hide'>$hidePost</li>";
}
- if ( $postActionMenu->isAllowed(
'delete-post' ) ) {
- echo '<li
class="flow-action-delete">'. $postActionMenu->getButton( 'delete-post',
wfMessage( 'flow-post-action-delete-post' )->plain(), 'flow-delete-post-link
mw-ui-button' ) .'</li>';
+ if ( $deletePost =
$postView->deletePostButton( $post, 'flow-delete-post-link mw-ui-button' ) ) {
+ echo "<li
class='flow-action-delete'>$deletePost</li>";
}
- if ( $postActionMenu->isAllowed(
'censor-post' ) ) {
- echo '<li
class="flow-action-censor">'. $postActionMenu->getButton( 'censor-post',
wfMessage( 'flow-post-action-censor-post' )->plain(), 'flow-censor-post-link
mw-ui-button' ) .'</li>';
+ if ( $suppressPost =
$postView->suppressPostButton( $post, 'flow-censor-post-link mw-ui-button' ) ) {
+ echo "<li
class='flow-action-censor'>$suppressPost</li>";
}
// @todo restore button will probably
be moved somewhere else, some day
- if ( $postActionMenu->isAllowed(
'restore-post' ) ) {
- echo '<li
class="flow-action-restore">'. $postActionMenu->getButton( 'restore-post',
wfMessage( 'flow-post-action-restore-post' )->plain(), 'flow-restore-post-link
mw-ui-button mw-ui-constructive' ) .'</li>';
+ if ( $restorePost =
$postView->restorePostButton( $post, 'flow-restore-post-link mw-ui-button
mw-ui-constructive' ) ) {
+ echo "<li
class='flow-action-restore'>$restorePost</li>";
}
?>
</ul>
--
To view, visit https://gerrit.wikimedia.org/r/91306
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I6024f66becb8b858b824f9e9f5ca04e07d0f91f3
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: EBernhardson <[email protected]>
Gerrit-Reviewer: EBernhardson <[email protected]>
Gerrit-Reviewer: Matthias Mullie <[email protected]>
Gerrit-Reviewer: Werdna <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits