jenkins-bot has submitted this change and it was merged. Change subject: Make BoardHistory a Formatter instance ......................................................................
Make BoardHistory a Formatter instance Change-Id: I9719b8a911b497ddb59a149d59063f8c1ce6d7e5 --- M Flow.php M Resources.php M container.php M includes/Block/BoardHistory.php M includes/Data/BoardHistoryIndex.php M includes/Data/BoardHistoryStorage.php A includes/Formatter/BoardHistory.php A includes/Formatter/BoardHistoryQuery.php A modules/history/styles/board-history.less M templates/board-history.html.php 10 files changed, 180 insertions(+), 91 deletions(-) Approvals: Matthias Mullie: Looks good to me, approved jenkins-bot: Verified diff --git a/Flow.php b/Flow.php index 7522e50..023db6e 100755 --- a/Flow.php +++ b/Flow.php @@ -157,6 +157,8 @@ $wgAutoloadClasses['Flow\Formatter\CheckUser'] = $dir . 'includes/Formatter/CheckUser.php'; $wgAutoloadClasses['Flow\Formatter\Contributions'] = $dir . 'includes/Formatter/Contributions.php'; $wgAutoloadClasses['Flow\Formatter\ContributionsQuery'] = $dir . 'includes/Formatter/ContributionsQuery.php'; +$wgAutoloadClasses['Flow\Formatter\BoardHistory'] = $dir . 'includes/Formatter/BoardHistory.php'; +$wgAutoloadClasses['Flow\Formatter\BoardHistoryQuery'] = $dir . 'includes/Formatter/BoardHistoryQuery.php'; $wgAutoloadClasses['Flow\Formatter\RecentChanges'] = $dir . 'includes/Formatter/RecentChanges.php'; $wgAutoloadClasses['Flow\Formatter\RecentChangesQuery'] = $dir . 'includes/Formatter/RecentChangesQuery.php'; $wgAutoloadClasses['Flow\Formatter\RecentChangesRow'] = $dir . 'includes/Formatter/RecentChangesQuery.php'; diff --git a/Resources.php b/Resources.php index 6bcf365..e87947a 100644 --- a/Resources.php +++ b/Resources.php @@ -109,6 +109,7 @@ ), 'ext.flow.history' => $flowResourceTemplate + array( 'styles' => array( + 'history/styles/board-history.less', // @todo should be independant? 'history/styles/history.less', 'history/styles/diff.less', ), diff --git a/container.php b/container.php index 003601e..edad729 100644 --- a/container.php +++ b/container.php @@ -506,6 +506,18 @@ $c['templating'] ); } ); +$c['board-history.query'] = $c->share( function( $c ) { + return new Flow\Formatter\BoardHistoryQuery( + $c['storage'], + $c['repository.tree'] + ); +} ); +$c['board-history.formatter'] = $c->share( function( $c ) { + return new Flow\Formatter\BoardHistory( + $c['permissions'], + $c['templating'] + ); +} ); $c['logger'] = $c->share( function( $c ) { return new Flow\Log\Logger( $c['flow_actions'], diff --git a/includes/Block/BoardHistory.php b/includes/Block/BoardHistory.php index 94c990c..a86ed6d 100644 --- a/includes/Block/BoardHistory.php +++ b/includes/Block/BoardHistory.php @@ -2,11 +2,7 @@ namespace Flow\Block; -use Flow\Model\Header; -use Flow\Model\PostRevision; use Flow\RevisionActionPermissions; -use Flow\View\History\History; -use Flow\View\History\HistoryRenderer; use Flow\Container; use Flow\Templating; use Flow\Exception\DataModelException; @@ -40,60 +36,54 @@ } public function render( Templating $templating, array $options ) { - $templating->getOutput()->addModuleStyles( array( 'ext.flow.history' ) ); - $templating->getOutput()->addModules( array( 'ext.flow.history' ) ); - $tplVars = array( - 'title' => wfMessage( 'flow-board-history', $this->workflow->getArticleTitle() )->escaped(), - 'historyExists' => false, - ); + $output = $templating->getOutput(); + $output->addModuleStyles( array( 'ext.flow.history' ) ); + $output->addModules( array( 'ext.flow.history' ) ); + $title = wfMessage( 'flow-board-history', $this->workflow->getArticleTitle() )->escaped(); + $output->setHtmlTitle( $title ); + $output->setPageTitle( $title ); + + // @todo To turn this into a reasonable json api we need the query + // results to be more directly serializable. + $lines = array(); $history = $this->loadBoardHistory(); - - if ( $history ) { - $tplVars['historyExists'] = true; - $tplVars['history'] = new History( $history ); - $tplVars['historyRenderer'] = new HistoryRenderer( $this->permissions, $templating, $this ); - } - - $templating->render( "flow:board-history.html.php", $tplVars ); - } - - public function renderAPI( Templating $templating, array $options ) { - $output = array( - 'type' => 'board-history', - '*' => $this->loadBoardHistory(), - ); - - $output = array( - '_element' => 'board-history', - 0 => $output, - ); - - return $output; - } - - protected function loadBoardHistory() { - $history = $this->storage->find( - 'BoardHistoryEntry', - array( 'topic_list_id' => $this->workflow->getId() ), - array( 'sort' => 'rev_id', 'order' => 'DESC', 'limit' => 300 ) - ); - - if ( !$history ) { - return array(); - } - - // get rid of history entries user doesn't have sufficient permissions for - foreach ( $history as $i => $revision ) { - /** @var PostRevision|Header $revision */ - - // only check against the specific revision, ignoring the most recent - if ( !$this->permissions->isAllowed( $revision, 'post-history' ) ) { - unset( $history[$i] ); + $formatter = Container::get( 'board-history.formatter' ); + $ctx = \RequestContext::getMain(); + foreach ( $history as $row ) { + $res = $formatter->format( $row, $ctx ); + if ( $res !== false ) { + $lines[] = $res; } } - return $history; + $templating->render( "flow:board-history.html.php", array( + 'lines' => $lines, + 'historyExists' => count( $lines ) > 0 + ) ); + } + + public function renderAPI( Templating $templating, array $options ) { + $history = $this->loadBoardHistory(); + $formatter = Container::get( 'formatter.revision' ); + $ctx = \RequestContext::getMain(); + + $result = array(); + foreach ( $history as $row ) { + $result[] = $formatter->formatApi( $row, $ctx ); + } + + return array( + '_element' => 'board-history', + 0 => array( + 'type' => 'board-history', + '*' => $result, + ), + ); + } + + protected function loadBoardHistory() { + return Container::get( 'board-history.query' )->getResults( $this->workflow ); } public function getName() { diff --git a/includes/Data/BoardHistoryIndex.php b/includes/Data/BoardHistoryIndex.php index 5a6c112..57507bd 100644 --- a/includes/Data/BoardHistoryIndex.php +++ b/includes/Data/BoardHistoryIndex.php @@ -41,11 +41,11 @@ * @param string[] $new */ public function onAfterInsert( $object, array $new ) { - if ( $object->getRevisionType() === 'header' ) { + if ( $object instanceof Header ) { $new['topic_list_id'] = $new['header_workflow_id']; parent::onAfterInsert( $object, $new ); - } elseif ( $object->getRevisionType() === 'post' ) { - $topicListId = $this->findTopicListIdForRootPost( $object ); + } elseif ( $object instanceof PostRevision ) { + $topicListId = $this->findTopicListId( $object ); if ( $topicListId ) { $new['topic_list_id'] = $topicListId; parent::onAfterInsert( $object, $new ); @@ -59,11 +59,11 @@ * @param string[] $new */ public function onAfterUpdate( $object, array $old, array $new ) { - if ( $object->getRevisionType() === 'header' ) { + if ( $object instanceof Header ) { $new['topic_list_id'] = $old['topic_list_id'] = $new['header_workflow_id']; parent::onAfterUpdate( $object, $old, $new ); - } elseif ( $object->getRevisionType() === 'post' ) { - $topicListId = $this->findTopicListIdForRootPost( $object ); + } elseif ( $object instanceof PostRevision ) { + $topicListId = $this->findTopicListId( $object ); if ( $topicListId ) { $new['topic_list_id'] = $old['topic_list_id'] = $topicListId; parent::onAfterUpdate( $object, $old, $new ); @@ -76,11 +76,11 @@ * @param string[] $old */ public function onAfterRemove( $object, array $old ) { - if ( $object->getRevisionType() === 'header' ) { + if ( $object instanceof Header ) { $old['topic_list_id'] = $old['header_workflow_id']; parent::onAfterRemove( $object, $old ); - } elseif ( $object->getRevisionType() === 'post' ) { - $topicListId = $this->findTopicListIdForRootPost( $object ); + } elseif ( $object instanceof PostRevision ) { + $topicListId = $this->findTopicListId( $object ); if ( $topicListId ) { $old['topic_list_id'] = $topicListId; parent::onAfterRemove( $object, $old ); @@ -94,20 +94,14 @@ * @param PostRevision $object * @return string|boolean False when object is not root post or topic is not found */ - protected function findTopicListIdForRootPost( $object ) { - if ( !$object->isTopicTitle() ) { - return false; - } - - /** @var ManagerGroup $storage */ - $storage = Container::get( 'storage' ); - /** @var TopicListEntry[] $found */ - $found = $storage->find( + protected function findTopicListId( $object ) { + $found = Container::get( 'storage' )->find( 'TopicListEntry', - array( 'topic_id' => $object->getPostId() ) + array( 'topic_id' => $object->getRootPost()->getPostId() ) ); if ( $found ) { + /** @var TopicListEntry $var */ $topicListEntry = reset( $found ); return $topicListEntry->getListId()->getBinary(); } else { diff --git a/includes/Data/BoardHistoryStorage.php b/includes/Data/BoardHistoryStorage.php index b23602e..7cac161 100644 --- a/includes/Data/BoardHistoryStorage.php +++ b/includes/Data/BoardHistoryStorage.php @@ -57,9 +57,13 @@ $queries = $this->preprocessSqlArray( reset( $queries ) ); $res = $this->dbFactory->getDB( DB_SLAVE )->select( - array( 'flow_topic_list', 'flow_tree_revision', 'flow_revision' ), + array( 'flow_topic_list', 'flow_tree_node', 'flow_tree_revision', 'flow_revision' ), array( '*' ), - array( 'tree_rev_id = rev_id', 'tree_rev_descendant_id = topic_id' ) + $queries, + array( + 'topic_id = tree_ancestor_id', + 'tree_descendant_id = tree_rev_descendant_id', + 'tree_rev_id = rev_id', + ) + $queries, __METHOD__, $options ); diff --git a/includes/Formatter/BoardHistory.php b/includes/Formatter/BoardHistory.php new file mode 100644 index 0000000..d0ebec2 --- /dev/null +++ b/includes/Formatter/BoardHistory.php @@ -0,0 +1,52 @@ +<?php + +namespace Flow\Formatter; + +use Flow\Exception\FlowException; +use Flow\Model\PostRevision; +use ChangesList; +use IContextSource; + +class BoardHistory extends AbstractFormatter { + + /** + * @param \stdClass $row A result row from BoardHistoryQuery + * @param IContextSource $ctx + * @return string|false HTML string representing $row + */ + public function format( $row, IContextSource $ctx ) { + try { + if ( !$this->permissions->isAllowed( $row->revision, 'history' ) ) { + return false; + } + if ( $row->revision instanceof PostRevision && + !$this->permissions->isAllowed( $row->rootPost, 'history' ) ) { + return false; + } + return $this->formatHtml( $row, $ctx ); + } catch ( FlowException $e ) { + \MWExceptionHandler::logException( $e ); + return false; + } + } + + protected function formatHtml( $row, IContextSource $ctx ) { + $data = $this->serializer->formatApi( $row, $ctx ); + + $charDiff = ChangesList::showCharacterDifference( + $data['size']['old'], + $data['size']['new'] + ); + + $separator = $this->changeSeparator(); + + // timestamp needs to render before links list to not duplicate the link + $formattedTimestamp = $this->formatTimestamp( $data ); + + return $this->formatLinksAsPipeList( $data['links'], $ctx ) . ' ' + . $formattedTimestamp + . $separator + . $this->formatDescription( $data, $ctx ) + . ( $charDiff ? $separator . $charDiff : '' ); + } +} diff --git a/includes/Formatter/BoardHistoryQuery.php b/includes/Formatter/BoardHistoryQuery.php new file mode 100644 index 0000000..07fa250 --- /dev/null +++ b/includes/Formatter/BoardHistoryQuery.php @@ -0,0 +1,43 @@ +<?php + +namespace Flow\Formatter; + +use Flow\Model\Workflow; +use Flow\Exception\FlowException; +use Flow\Exception\InvalidDataException; +use MWExceptionHandler; + +class BoardHistoryQuery extends AbstractQuery { + public function getResults( Workflow $workflow ) { + // Load the history + $history = $this->storage->find( + 'BoardHistoryEntry', + array( 'topic_list_id' => $workflow->getId() ), + array( 'sort' => 'rev_id', 'order' => 'DESC', 'limit' => 300 ) + ); + + if ( !$history ) { + throw new InvalidDataException( 'Unable to load topic list history for ' . $workflow->getId()->getAlphadecimal(), 'fail-load-history' ); + } + + // pre-load our workflow + $this->workflowCache[$workflow->getId()->getAlphadecimal()] = $workflow; + // fetch any necessary metadata + $this->loadMetadataBatch( $history ); + // build rows with the extra metadata + $results = array(); + foreach ( $history as $revision ) { + try { + $result = $this->buildResult( $revision, 'rev_id' ); + } catch ( FlowException $e ) { + $result = false; + MWExceptionHandler::logException( $e ); + } + if ( $result ) { + $results[] = $result; + } + } + + return $results; + } +} diff --git a/modules/history/styles/board-history.less b/modules/history/styles/board-history.less new file mode 100644 index 0000000..a31f9a6 --- /dev/null +++ b/modules/history/styles/board-history.less @@ -0,0 +1,4 @@ + +.flow-board-history-container { + font-size: 14px; +} diff --git a/templates/board-history.html.php b/templates/board-history.html.php index 3f123d7..807f4fb 100644 --- a/templates/board-history.html.php +++ b/templates/board-history.html.php @@ -1,20 +1,7 @@ -<?php -$this->getOutput()->setHtmlTitle( $title ); -$this->getOutput()->setPageTitle( $title ); -?> -<div class="flow-history-container"> +<div class="flow-board-history-container"> <div class="flow-history-log"> - <?php - if ( $historyExists ) { - $timespans = $historyRenderer->getTimespans( $history ); - foreach ( $timespans as $text => $timespan ) { - $timespan = $history->getTimespan( $timespan['from'], $timespan['to'] ); - if ( $timespan->numRows() ) { - echo "<h2>$text</h2>"; - echo $historyRenderer->render( $timespan ); - } - } - } - ?> + <ul> + <li><?php echo implode( "</li>\n<li>", $lines ) ?></li> + </ul> </div> </div> -- To view, visit https://gerrit.wikimedia.org/r/115532 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I9719b8a911b497ddb59a149d59063f8c1ce6d7e5 Gerrit-PatchSet: 19 Gerrit-Project: mediawiki/extensions/Flow Gerrit-Branch: master Gerrit-Owner: EBernhardson <ebernhard...@wikimedia.org> Gerrit-Reviewer: Bsitu <bs...@wikimedia.org> Gerrit-Reviewer: Matthias Mullie <mmul...@wikimedia.org> Gerrit-Reviewer: Werdna <agarr...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits