diff --git a/Hooks.php b/Hooks.php index 66453d8..69d5710 100644 --- a/Hooks.php +++ b/Hooks.php @@ -27,6 +27,13 @@ public static function onPersonalUrls( array &$personal_urls ) { $numPending = $watchStats['num_pending']; $maxPendingDays = $watchStats['max_pending_days']; + // Get user's pending approvals + // Check that Approved Revs is installed + $numPendingApprovals = 0; + if ( class_exists( 'ApprovedRevs' ) ) { + $numPendingApprovals = count( PendingApproval::getUserPendingApprovals( $user ) ); + } + // Determine CSS class of Watchlist/PendingReviews link $personal_urls['watchlist']['class'] = [ 'mw-watchanalytics-watchlist-badge' ]; if ( $numPending != 0 ) { @@ -37,10 +44,19 @@ public static function onPersonalUrls( array &$personal_urls ) { global $egPendingReviewsEmphasizeDays; if ( $maxPendingDays > $egPendingReviewsEmphasizeDays ) { $personal_urls['watchlist']['class'][] = 'mw-watchanalytics-watchlist-pending-old'; - $text = wfMessage( 'watchanalytics-personal-url-old' )->params( $numPending, $maxPendingDays )->text(); + if ( $numPendingApprovals != 0 ) { + $text = wfMessage( 'watchanalytics-personal-url-approvals-old' )->params( $numPending, $maxPendingDays, $numPendingApprovals )->text(); + } else { + $text = wfMessage( 'watchanalytics-personal-url-old' )->params( $numPending, $maxPendingDays )->text(); + } } else { - // when $sk (third arg) available, replace wfMessage with $sk->msg() - $text = wfMessage( 'watchanalytics-personal-url' )->params( $numPending )->text(); + if ( $numPendingApprovals != 0 ) { + $text = wfMessage( 'watchanalytics-personal-url-approvals' )->params( $numPending, $numPendingApprovals )->text(); + } else { + // when $sk (third arg) available, replace wfMessage with $sk->msg() + $text = wfMessage( 'watchanalytics-personal-url' )->params( $numPending )->text(); + } + } $personal_urls['watchlist']['text'] = $text; @@ -55,7 +71,7 @@ public static function onPersonalUrls( array &$personal_urls ) { * * 1) Determine if user should see shaky pending reviews link * 2) Insert page scores on applicable pages - * 3) REMOVED FOR MW 1.27: If a page review has occured on this page view, display an unreview + * 3) REMOVED FOR MW 1.27: If a page review has occurred on this page view, display an unreview * option and record that the review happened. * * Also supports parameter: Skin $skin. @@ -243,8 +259,10 @@ public static function handleMagicWords( &$parser, &$text ) { * @return bool */ public static function onPageViewUpdates( WikiPage $wikiPage, User $user ) { + global $wgRequest; $title = $wikiPage->getTitle(); - $reviewHandler = ReviewHandler::setup( $user, $title ); + $isDiff = $wgRequest->getText( 'oldid' ); + $reviewHandler = ReviewHandler::setup( $user, $title, $isDiff ); if ( $reviewHandler::pageIsBeingReviewed() ) { diff --git a/extension.json b/extension.json index 5048cc2..f11b12f 100644 --- a/extension.json +++ b/extension.json @@ -1,6 +1,6 @@ { "name": "WatchAnalytics", - "version": "2.0.1", + "version": "3.1.0", "author": [ "[https://www.mediawiki.org/wiki/User:Jamesmontalvo3 James Montalvo]" ], @@ -41,6 +41,7 @@ "WatchAnalyticsUser": "WatchAnalyticsUser.php", "WatchAnalyticsUpdaterHooks": "schema/WatchAnalyticsUpdaterHooks.php", "PendingReview": "includes/PendingReview.php", + "PendingApproval": "includes/PendingApproval.php", "WatchSuggest": "includes/WatchSuggest.php", "ReviewHandler": "includes/ReviewHandler.php", "PageScore": "includes/PageScore.php", @@ -205,6 +206,9 @@ "config": { "_prefix": "eg", "WatchAnalyticsPageCounter": false, + "WatchAnalyticsShowUnreviewDiff": true, + "PendingReviewMaxDiffChar": 3500, + "PendingReviewMaxDiffRows": 15, "PendingReviewsEmphasizeDays": 7, "PendingReviewsRedPagesThreshold": 2, "PendingReviewsOrangePagesThreshold": 4, diff --git a/i18n/en.json b/i18n/en.json index 42d931f..ca04b2a 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -6,7 +6,9 @@ }, "watchanalytics-desc": "Encouraging good distribution of watchers", "watchanalytics-personal-url": "Pending reviews ($1)", + "watchanalytics-personal-url-approvals": "Pending reviews ($1)/approvals ($2)", "watchanalytics-personal-url-old": "{{PLURAL:$1|1 review|$1 reviews}} (oldest: {{PLURAL:$2|1 day|$2 days}})", + "watchanalytics-personal-url-approvals-old": "{{PLURAL:$1|1 review|$1 reviews}} (oldest: {{PLURAL:$2|1 day|$2 days}})/approvals ($3)", "watchanalytics-view": "View:", "pendingreviews": "Pending reviews", "watchanalytics": "Watch analytics", @@ -75,7 +77,8 @@ "watchanalytics-watch-forcegraph-description": "Orange dots represent users. Blue dots represent pages. Lines between the dots represent the user being a watcher of the page. Lines are gray if the latest page revision has been reviewed. Lines are red if reviews are pending. Move your mouse over a dot to see the user or page name.", "watchanalytics-pause-visualization": "Pause visualization", "watchanalytics-unpause-visualization": "Unpause visualization", - "watchanalytics-pendingreviews-diff-revisions": "Display {{PLURAL:$1|1 change|$1 changes}} since last visit", + "watchanalytics-pendingreviews-diff-revisions": "View {{PLURAL:$1|change|$1 changes}} on page", + "watchanalytics-view-and-approve": "View/Approve changes", "watchanalytics-pendingreviews-users-first-view": "New page - view latest", "watchanalytics-pendingreviews-history-link": "view page history", "watchanalytics-pendingreviews-prev-revisions": "< Previous", @@ -85,7 +88,10 @@ "pendingreviews-timediff-minutes": "Changed {{PLURAL:$1|1 minute|$1 minutes}} ago", "pendingreviews-timediff-just-now": "Changed just now", "pendingreviews-no-revisions": "No page content changes", - "pendingreviews-num-reviews": "You have $1 pending {{PLURAL:$1|review|reviews}}.", + "pendingreviews-num-reviews": "You have $1 pending {{PLURAL:$1|review|reviews}}", + "pendingreviews-num-reviews-complete": "Congrats! You completed your reviews.", + "pendingreviews-num-other-user-reviews": "$1 has $2 pending {{PLURAL:$2|review|reviews}}", + "pendingreviews-num-approvals": "/$1 pending {{PLURAL:$1|approval|approvals}}.", "pendingreviews-reviewer-criticality-danger": "Pages reviewed by 0 - {{PLURAL:$1|1 person|$1 people}}", "pendingreviews-reviewer-criticality-danger-zero": "Pages reviewed by 0 people", "pendingreviews-reviewer-criticality-generic": "Pages reviewed by $1 or more people", @@ -103,12 +109,15 @@ "watchanalytics-pagestats-chart-header": "Number of reviewers over time", "watchanalytics-unreview-button": "Defer review", + "watchanalytics-accept-change-close-banner": "Close banner", "watchanalytics-unreview-banner-text": "'''Page reviewed!''' By navigating to this page you have marked it reviewed. If you did not want to review the page you may un-review it to save it for another time.", "watchanalytics-unreview-complete": "'''This page has been un-reviewed''' and is again in your [[Special:PendingReviews|Pending Reviews]].", "watchanalytics-view-user-pendingreviews": "pending reviews", "pendingreviews-watch-suggestion-thanks": "Thanks for watching!", + "pendingreviews-pending-approvedrev": "Has revisions which require approval", + "pendingreviews-pending-approvedrev-title": "Revision approval required: $1", "pendingreviews-edited-by": "[[$1]] made an edit '''without a summary'''", "pendingreviews-with-comment": "[[$1]] made an edit with summary: ", "pendingreviews-page-deleted": "Deleted page: $1", @@ -117,6 +126,7 @@ "pendingreviews-accept-deletion": "Accept deletion", "pendingreviews-accept-move-without-redirect": "Accept move without redirect", "pendingreviews-accept-redirect": "Accept redirect", + "pendingreviews-accept-change": "Mark reviewed", "pendingreviews-clear-page-notification": "Page \"$1\" has been marked reviewed. Return to $2", "pendingreviews-log-approved": "[[$1]] approved a revision", "pendingreviews-log-unapproved": "[[$1]] removed all revision approvals", @@ -135,6 +145,7 @@ "log-description-pendingreviews": "Tracks actions taken using tools from the Watch Analytics extension related to Pending Reviews.", "logentry-pendingreviews-clearreivews": "$1 {{GENDER:$2|cleared $4 pending reviews}} with criteria -category$5 -title like$6 using $3", "pendingreviews-watch-suggestion-title": "This wiki needs your help watching pages", + "pendingreviews-approve-revs-title": "{{PLURAL:$1|Page|Pages}} needing your approval ($1)", "pendingreviews-watch-suggestion-description": "Few (if any) people are watching the pages below. They are related to other pages in your watchlist, and it'd be real swell if you could help by watching some of them.", "pendingreviews-watch-suggestion-watchlink": "Watch" diff --git a/includes/PendingApproval.php b/includes/PendingApproval.php new file mode 100644 index 0000000..7b0cf31 --- /dev/null +++ b/includes/PendingApproval.php @@ -0,0 +1,100 @@ +title = $title; + + $this->notificationTimestamp = $row['notificationtimestamp']; + $this->numReviewers = intval( $row['num_reviewed'] ); + + // Keep these just to be consistent with PendingReview class + $this->deletedTitle = false; + $this->deletedNS = false; + $this->deletionLog = false; + + // FIXME + // no log for now, maybe link to approval log + // no list of revisions for now + $this->log = []; + $this->newRevisions = []; + } + + /** + * Get an array of pages user can approve that require approvals + * @param User $user + * @return Array + */ + public static function getUserPendingApprovals( User $user ) { + $dbr = wfGetDB( DB_REPLICA ); + + $queryInfo = ApprovedRevs::getQueryInfoPageApprovals( 'notlatest' ); + $latestNotApproved = $dbr->select( + $queryInfo['tables'], + $queryInfo['fields'], + $queryInfo['conds'], + __METHOD__, + $queryInfo['options'], + $queryInfo['join_conds'] + ); + $pagesUserCanApprove = []; + + while ( $page = $latestNotApproved->fetchRow() ) { + + // $page with keys id, rev_id, latest_id + $title = Title::newFromID( $page['id'] ); + + if ( ApprovedRevs::userCanApprove( $user, $title ) ) { + + // FIXME: May want to get these in there so PendingReviews can + // show the list of revs in the approval. + // 'approved_rev_id' => $page['rev_id'] + // 'latest_rev_id' => $page['latest_id'] + $pagesUserCanApprove[] = new self( + [ + 'notificationtimestamp' => null, + 'num_reviewed' => 0, // if page has pending approval, zero people have approved + ], + $title + ); + + } + + } + + return $pagesUserCanApprove; + } + +} diff --git a/includes/PendingReview.php b/includes/PendingReview.php index f73802f..56d3cbf 100644 --- a/includes/PendingReview.php +++ b/includes/PendingReview.php @@ -1,5 +1,7 @@ notificationTimestamp = $notificationTimestamp; + $this->numReviewers = intval( $row['num_reviewed'] ); + + if ( $title ) { + $pageID = $title->getArticleID(); + $namespace = $title->getNamespace(); + $titleDBkey = $title->getDBkey(); } else { - $title = false; + $pageID = $row['page_id']; + $namespace = $row['namespace']; + $titleDBkey = $row['title']; + + if ( $pageID ) { + $title = Title::newFromID( $pageID ); + } else { + $title = false; + } } if ( $pageID && $title->exists() ) { $dbr = wfGetDB( DB_REPLICA ); + $revisionStore = MediaWikiServices::getInstance()->getRevisionStore(); + + $revQueryInfo = $revisionStore->getQueryInfo(); + $revResults = $dbr->select( - [ 'r' => 'revision' ], - Revision::getQueryInfo()['fields'], - // array( - // 'r.rev_id AS rev_id', - // 'r.rev_comment AS rev_comment', - // 'r.rev_user AS rev_user_id', - // 'r.rev_user_text AS rev_user_name', - // 'r.rev_timestamp AS rev_timestamp', - // 'r.rev_len AS rev_len', - // ), - "r.rev_page=$pageID AND r.rev_timestamp>=$notificationTimestamp", + $revQueryInfo['tables'], + $revQueryInfo['fields'], + "rev_page=$pageID AND rev_timestamp>=$notificationTimestamp", __METHOD__, [ 'ORDER BY' => 'rev_timestamp ASC' ], - null + $revQueryInfo['joins'] ); $revsPending = []; while ( $rev = $revResults->fetchObject() ) { @@ -85,14 +95,6 @@ public function __construct( $row ) { $logResults = $dbr->select( [ 'l' => 'logging' ], [ '*' ], - // array( - // 'l.log_id AS log_id', - // 'l.log_type AS log_type', - // 'l.log_action AS log_action', - // 'l.log_timestamp AS log_timestamp', - // 'l.log_user AS log_user_id', - // 'l.log_user_text AS log_user_name', - // ), "l.log_page=$pageID AND l.log_timestamp>=$notificationTimestamp AND l.log_type NOT IN ('interwiki','newusers','patrol','rights','upload')", __METHOD__, @@ -109,21 +111,19 @@ public function __construct( $row ) { $deletionLog = false; } else { - $deletedNS = $row['namespace']; - $deletedTitle = $row['title']; + $deletedNS = $namespace; + $deletedTitle = $titleDBkey; $deletionLog = $this->getDeletionLog( $deletedTitle, $deletedNS, $notificationTimestamp ); $logPending = false; $revsPending = false; } - $this->notificationTimestamp = $notificationTimestamp; $this->title = $title; $this->newRevisions = $revsPending; $this->deletedTitle = $deletedTitle; $this->deletedNS = $deletedNS; $this->deletionLog = $deletionLog; $this->log = $logPending; - $this->numReviewers = intval( $row['num_reviewed'] ); } public static function getPendingReviewsList( User $user, $limit, $offset ) { @@ -188,37 +188,104 @@ public static function getPendingReviewsList( User $user, $limit, $offset ) { } + // If ApprovedRevs is installed, append any pages in need of approvals + // to the front of the Pending Reviews list + if ( class_exists( 'ApprovedRevs' ) ) { + $pending = array_merge( PendingApproval::getUserPendingApprovals( $user ), $pending ); + } + return $pending; } - public function getDeletionLog( $title, $ns, $notificationTimestamp ) { + public static function getPendingReview( User $user, Title $title ) { + $tables = [ + 'w' => 'watchlist', + 'p' => 'page', + 'log' => 'logging', + ]; + + $fields = [ + 'p.page_id AS page_id', + 'log.log_action AS log_action', + 'w.wl_namespace AS namespace', + 'w.wl_title AS title', + 'w.wl_notificationtimestamp AS notificationtimestamp', + '(SELECT COUNT(*) FROM watchlist AS subwatch + WHERE + subwatch.wl_namespace = w.wl_namespace + AND subwatch.wl_title = w.wl_title + AND subwatch.wl_notificationtimestamp IS NULL + ) AS num_reviewed', + ]; + + $conds = [ 'w.wl_user' => $user->getId() , 'p.page_id' => $title->getArticleID() , 'w.wl_notificationtimestamp IS NOT NULL' ]; + + $options = []; + + $join_conds = [ + 'p' => [ + 'LEFT JOIN', 'p.page_namespace=w.wl_namespace AND p.page_title=w.wl_title' + ], + 'log' => [ + 'LEFT JOIN', + 'log.log_namespace = w.wl_namespace ' + . ' AND log.log_title = w.wl_title' + . ' AND p.page_namespace IS NULL' + . ' AND p.page_title IS NULL' + . ' AND log.log_action IN ("delete","move")' + ], + ]; + $dbr = wfGetDB( DB_REPLICA ); + $watchResult = $dbr->select( + $tables, + $fields, + $conds, + __METHOD__, + $options, + $join_conds + ); + + $pending = []; + + while ( $row = $dbr->fetchRow( $watchResult ) ) { + + $pending[] = new self( $row ); + + } + return $pending; + } + + public function getDeletionLog( $title, $ns, $notificationTimestamp ) { + $dbr = wfGetDB( DB_REPLICA ); $title = $dbr->addQuotes( $title ); // pages are deleted when (a) they are explicitly deleted or (b) they // are moved without leaving a redirect behind. $logResults = $dbr->select( - [ 'l' => 'logging' ], + [ 'l' => 'logging', 'c' => 'comment' ], [ - 'log_id', - 'log_type', - 'log_action', - 'log_timestamp', - 'log_user', - 'log_user_text', - 'log_namespace', - 'log_title', - 'log_page', - 'log_comment', - 'log_params', - 'log_deleted', + 'l.log_id', + 'l.log_type', + 'l.log_action', + 'l.log_timestamp', + 'l.log_user', + 'l.log_user_text', + 'l.log_namespace', + 'l.log_title', + 'l.log_page', + 'l.log_comment_id', + 'l.log_params', + 'l.log_deleted', + 'c.comment_id', + 'c.comment_text AS log_comment' ], "l.log_title=$title AND l.log_namespace=$ns AND l.log_timestamp>=$notificationTimestamp AND l.log_type IN ('delete','move')", __METHOD__, - [ 'ORDER BY' => 'log_timestamp ASC' ], - null + [ 'ORDER BY' => 'l.log_timestamp ASC' ], + [ 'c' => [ 'INNER JOIN', [ 'l.log_comment_id=c.comment_id' ] ] ] ); $logDeletes = []; while ( $log = $logResults->fetchObject() ) { diff --git a/includes/ReviewHandler.php b/includes/ReviewHandler.php index 0302ecb..4ad9483 100644 --- a/includes/ReviewHandler.php +++ b/includes/ReviewHandler.php @@ -30,17 +30,18 @@ class ReviewHandler { */ public $final = null; - public function __construct( User $user, Title $title ) { + public function __construct( User $user, Title $title, $isDiff ) { $this->user = $user; $this->title = $title; + $this->isDiff = $isDiff; } - public static function setup( User $user, Title $title ) { + public static function setup( User $user, Title $title, $isDiff ) { if ( ! $title->isWatchable() ) { self::$isReviewable = false; return false; } - self::$pageLoadHandler = new self ( $user, $title ); + self::$pageLoadHandler = new self ( $user, $title, $isDiff ); self::$pageLoadHandler->initial = self::$pageLoadHandler->getReviewStatus(); return self::$pageLoadHandler; } @@ -116,20 +117,75 @@ public static function pageIsBeingReviewed() { public function getTemplate() { // $msg = wfMessage( 'watch-analytics-page-score-tooltip' )->text(); - $unReviewLink = SpecialPage::getTitleFor( 'PageStatistics' )->getInternalURL( [ - 'page' => $this->title->getPrefixedText(), - 'unreview' => $this->initial - ] ); + $reviewLink = Xml::element( + 'a', + [ + 'href' => null, + 'id' => 'watch-analytics-unreview', + 'class' => 'pendingreviews-green-button pendingreviews-accept-change', + ], + wfMessage( 'watchanalytics-accept-change-close-banner' )->text() + ); + + $unReviewLink = Xml::element( + 'a', + [ + 'href' => null, + 'id' => 'watch-analytics-unreview', + 'class' => 'watch-analytics-unreview', + 'timestamp' => $this->initial, + 'pending-title' => $this->title->getPrefixedText(), + 'title' => wfMessage( 'watchanalytics-unreview-button' )->text(), + ], + wfMessage( 'watchanalytics-unreview-button' )->text() + ); - $linkText = wfMessage( 'watchanalytics-unreview-button' )->text(); $bannerText = wfMessage( 'watchanalytics-unreview-banner-text' )->parse(); - // when MW 1.25 is released (very soon) replace this with a mustache template + $this->pendingReview = PendingReview::getPendingReview( $this->user, $this->title ); + + foreach ( $this->pendingReview as $item ) { + if ( count( $item->newRevisions ) > 0 ) { + + // returns essentially the negative-oneth revision...the one before + // the wl_notificationtimestamp revision...or null/false if none exists? + $mostRecentReviewed = Revision::newFromRow( $item->newRevisions[0] )->getPrevious(); + } else { + $mostRecentReviewed = false; // no previous revision, the user has not reviewed the first! + } + + if ( $mostRecentReviewed ) { + + $lastSeenId = $mostRecentReviewed->getId(); + + } else { + + $latest = Revision::newFromTitle( $item->title ); + $lastSeenId = $latest->getId(); + + } + + } + + $diff = new DifferenceEngine( null, $lastSeenId, 0 ); + $template = "
$bannerText
-$bannerText
"; + + global $egWatchAnalyticsShowUnreviewDiff; + if ( $egWatchAnalyticsShowUnreviewDiff ) { + // Don't show diff on in header while viewing diff page + if ( !( $this->isDiff ) ) { + $template .= "' . wfMessage( 'pendingreviews-num-reviews', $numPendingReviews, $this->reviewLimit )->text(); - - // close out header - $html .= '
'; - $nextReviewSet = $this->reviewOffset + $this->reviewLimit; $prevReviewSet = max( [ 0, $this->reviewOffset - $this->reviewLimit ] ); $currentURL = $this->getPageTitle()->getLocalUrl(); - $viewingUser = ''; - // if ( $this->mUser ) { - // $viewingUser = '&user='.$this->mUser; - // } + $viewingUser = '&user=' . $this->mUser; $linkClass = "pendingreviews-nav-link"; if ( $this->reviewOffset == 0 ) { @@ -603,6 +734,22 @@ public function getPageHeader( User $user ) { wfMessage( 'watchanalytics-pendingreviews-next-revisions' )->text() ); + $html .= '