Krinkle has submitted this change and it was merged. Change subject: Implement "Recent only" feature ......................................................................
Implement "Recent only" feature * Add dropdown to choose between revisions, recentchanges and last hour of recentchanges. * Add prepareRecentchangesQuery() implementation. * Add prepareLastHourQuery() implementation. * Update _getWikisWithContribs() with logic for recentchanges to make sure the summary is still accurate. Bug: T64914 Change-Id: Ic4ebf3716a92a12daabc292c6ff739b44ced35bd --- M index.php M lb/guc.php M lb/wikicontribs.php 3 files changed, 110 insertions(+), 10 deletions(-) Approvals: Krinkle: Verified; Looks good to me, approved diff --git a/index.php b/index.php index e5818dd..86505c5 100644 --- a/index.php +++ b/index.php @@ -36,6 +36,7 @@ $data->Username = @$_REQUEST['user'] ?: null; $data->options = array( 'isPrefixPattern' => @$_REQUEST['isPrefixPattern'] === '1', + 'src' => @$_REQUEST['src'] ?: 'all', ); // Create app @@ -101,6 +102,16 @@ print ' checked'; } ?>></label></p> + <p><label>Results from: <?php + $resultSelect = new HtmlSelect([ + 'all' => 'All contributions', + 'rc' => 'Recent changes (last 30 days)', + 'hr' => 'Last hour only' + ]); + $resultSelect->setDefault( $data->options['src'] ); + $resultSelect->setName( 'src' ); + print $resultSelect->getHTML(); + ?></label></p> <input type="submit" value="Search" class="submitbutton" id="submitButton"> <div id="loadLine" style="display: none;"> </div> </form> diff --git a/lb/guc.php b/lb/guc.php index 3ac0f51..f79e025 100644 --- a/lb/guc.php +++ b/lb/guc.php @@ -30,6 +30,7 @@ public static function getDefaultOptions() { return array( 'isPrefixPattern' => false, + 'src' => 'all', 'includeClosedWikis' => false, ); } @@ -167,19 +168,40 @@ $this->app->aTP('Query all wikis for matching revisions'); $wikisWithEditcount = array(); + // Copied from lb_wikicontribs::prepareLastHourQuery + // (TODO: Refactor somehow) + $cutoff = gmdate(lb_wikicontribs::MW_DATE_FORMAT, time() - 3600); + $slices = array(); $wikisByDbname = array(); foreach ($wikis as $wiki) { $wikisByDbname[$wiki->dbname] = $wiki; - $slices[$wiki->slice][] = 'SELECT - COUNT(rev_id) AS counter, - \''.$wiki->dbname.'\' AS dbname - FROM '.$wiki->dbname.'_p.revision_userindex - WHERE '.( - ($this->options['isPrefixPattern']) - ? 'rev_user_text LIKE :userlike' - : 'rev_user_text = :user' - ); + if ($this->options['src'] === 'rc' || $this->options['src'] === 'hr') { + $sql = 'SELECT + COUNT(*) AS counter, + \''.$wiki->dbname.'\' AS dbname + FROM '.$wiki->dbname.'_p.recentchanges_userindex + WHERE '.( + ($this->options['isPrefixPattern']) + ? 'rc_user_text LIKE :userlike' + : 'rc_user_text = :user' + ).( + ($this->options['src'] === 'hr') + ? ' AND rc_timestamp >= :hrcutoff' + : '' + ); + } else { + $sql = 'SELECT + COUNT(rev_id) AS counter, + \''.$wiki->dbname.'\' AS dbname + FROM '.$wiki->dbname.'_p.revision_userindex + WHERE '.( + ($this->options['isPrefixPattern']) + ? 'rev_user_text LIKE :userlike' + : 'rev_user_text = :user' + ); + } + $slices[$wiki->slice][] = $sql; } $globalEditCount = 0; @@ -193,6 +215,9 @@ } else { $statement->bindParam(':user', $this->user); } + if ($this->options['src'] === 'hr') { + $statement->bindValue(':hrcutoff', $cutoff); + } $statement->execute(); $rows = $statement->fetchAll(PDO::FETCH_OBJ); diff --git a/lb/wikicontribs.php b/lb/wikicontribs.php index a65a7f3..826209b 100644 --- a/lb/wikicontribs.php +++ b/lb/wikicontribs.php @@ -17,6 +17,7 @@ class lb_wikicontribs { const CONTRIB_LIMIT = 20; + const MW_DATE_FORMAT = 'YmdHis'; private $app; private $wiki; @@ -55,6 +56,7 @@ $this->isIp = $isIP; $this->options = $options += array( 'isPrefixPattern' => false, + 'src' => 'all', ); $this->wiki = $wiki; @@ -156,13 +158,75 @@ } /** + * @param PDO $pdo + * @param array $extraConds + * @return PDOStatement + */ + private function prepareRecentchangesQuery(PDO $pdo, $extraConds = []) { + // Avoid use of rc_user. Contrary to revision table, it has no index. + // Simply use rc_user_text instead, which has a good index. + $conds = [ + '`rc_deleted` = 0', + ($this->options['isPrefixPattern']) + ? 'rc_user_text LIKE :userlike' + : 'rc_user_text = :user' + ]; + $conds = array_merge($conds, $extraConds); + $sqlCond = implode(' AND ', $conds); + $sql = 'SELECT + `rc_comment` as `rev_comment`, + `rc_timestamp` as `rev_timestamp`, + `rc_minor` as `rev_minor_edit`, + `rc_new_len` as `rev_len`, + `rc_this_oldid` as `rev_id`, + `rc_last_oldid` as `rev_parent_id`, + `rc_user_text` as `rev_user_text`, + `rc_title` as `page_title`, + `rc_namespace` as `page_namespace`, + "0" AS `guc_is_cur` + FROM + `recentchanges_userindex` + WHERE + ' . $sqlCond . ' + ORDER BY `recentchanges_userindex`.`rc_timestamp` DESC + LIMIT 0, ' . intval(self::CONTRIB_LIMIT) . + ';'; + $statement = $pdo->prepare($sql); + if ($this->options['isPrefixPattern']) { + $statement->bindParam(':userlike', $this->user); + } else { + $statement->bindParam(':user', $this->user); + } + return $statement; + } + + /** + * @param PDO $pdo + * @return PDOStatement + */ + private function prepareLastHourQuery(PDO $pdo) { + // UTC timestamp of 1 hour ago + $cutoff = gmdate(self::MW_DATE_FORMAT, time() - 3600); + $conds = [ + 'rc_timestamp >= ' . $pdo->quote($cutoff) + ]; + return $this->prepareRecentchangesQuery($pdo, $conds); + } + + /** * Fetch contributions from the database */ private function fetchContribs() { $this->app->aTP('Query contributions on ' . $this->wiki->domain); $pdo = $this->app->getDB($this->wiki->dbname, $this->wiki->slice); - $statement = $this->prepareRevisionQuery($pdo); + if ($this->options['src'] === 'rc') { + $statement = $this->prepareRecentchangesQuery($pdo); + } elseif ($this->options['src'] === 'hr') { + $statement = $this->prepareLastHourQuery($pdo); + } else { + $statement = $this->prepareRevisionQuery($pdo); + } $statement->execute(); $contribs = $statement->fetchAll(PDO::FETCH_OBJ); -- To view, visit https://gerrit.wikimedia.org/r/307050 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic4ebf3716a92a12daabc292c6ff739b44ced35bd Gerrit-PatchSet: 18 Gerrit-Project: labs/tools/guc Gerrit-Branch: master Gerrit-Owner: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits