+/** @package MediaWiki */
+class contribs_finder {
+ var $username, $offset, $limit, $namespace;
+ var $dbr;
+
+ function contribs_finder($username) {
+ $this->username = $username;
+ $this->namespace = false;
+ $this->dbr =& wfGetDB(DB_SLAVE);
+ }
+
+ function set_namespace($ns) {
+ $this->namespace = $ns;
+ }
+
+ function set_limit($limit) {
+ $this->limit = $limit;
+ }
+
+ function set_offset($offset) {
+ $this->offset = $offset;
+ }
+
+ function get_edit_limit($dir) {
+ list($index, $usercond) = $this->get_user_cond();
+ $nscond = $this->get_namespace_cond();
+ $use_index = $this->dbr->useIndexClause($index);
+ extract($this->dbr->tableNames('revision', 'page'));
+ $sql = "SELECT rev_timestamp " .
+ " FROM $page,$revision $use_index " .
+ " WHERE rev_page=page_id AND $usercond $nscond" .
+ " ORDER BY rev_timestamp $dir LIMIT 1";
+
+ $res = $this->dbr->query($sql, "contribs_finder::get_edit_limit");
+ while ($o = $this->dbr->fetchObject($res))
+ $row = $o;
+ return $row->rev_timestamp;
+ }
+
+ function get_edit_limits() {
+ return array(
+ $this->get_edit_limit("ASC"),
+ $this->get_edit_limit("DESC")
+ );
+ }
+
+ function get_user_cond() {
+ $condition = '';
+
+ if ($this->username == 'newbies') {
+ $max = $this->dbr->selectField('user', 'max(user_id)', false, 'make_sql');
+ $condition = '>' . (int)($max - $max / 100);
+ }
+
+ if ($condition == '') {
+ $condition = ' rev_user_text=' . $this->dbr->addQuotes($this->username);
+ $index = 'usertext_timestamp';
+ } else {
+ $condition = ' rev_user '.$condition ;
+ $index = 'user_timestamp';
+ }
+ return array($index, $condition);
+ }
+
+ function get_namespace_cond() {
+ if ($this->namespace !== false)
+ return ' AND page_namespace = ' . (int)$this->namespace;
+ return '';
+ }
+
+ function get_previous_offset_for_paging() {
+ list($index, $usercond) = $this->get_user_cond();
+ $nscond = $this->get_namespace_cond();
+
+ $use_index = $this->dbr->useIndexClause($index);
+ extract($this->dbr->tableNames('page', 'revision'));
+
+ $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
+ "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset . "' AND " .
+ "rev_user_text = " . $this->dbr->addQuotes($this->username)
+ . $nscond;
+ $sql .= " ORDER BY rev_timestamp ASC";
+ $sql = $this->dbr->limitResult($sql, $this->limit, 0);
+ $res = $this->dbr->query($sql);
+ $rows = array();
+ while ($obj = $this->dbr->fetchObject($res))
+ $rows[] = $obj;
+ $this->dbr->freeResult($res);
+ return $rows[count($rows) - 1]->rev_timestamp;
+ }
+
+ function get_first_offset_for_paging() {
+ list($index, $usercond) = $this->get_user_cond();
+ $use_index = $this->dbr->useIndexClause($index);
+ extract($this->dbr->tableNames('page', 'revision'));
+ $nscond = $this->get_namespace_cond();
+ $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
+ "WHERE page_id = rev_page AND " .
+ "rev_user_text = " . $this->dbr->addQuotes($this->username)
+ . $nscond;
+ $sql .= " ORDER BY rev_timestamp ASC";
+ $sql = $this->dbr->limitResult($sql, $this->limit, 0);
+ $res = $this->dbr->query($sql);
+ $rows = array();
+ while ($obj = $this->dbr->fetchObject($res))
+ $rows[] = $obj;
+ $this->dbr->freeResult($res);
+ return $rows[count($rows) - 1]->rev_timestamp;
+ }
+
+ /* private */ function make_sql() {
+ $userCond = $condition = $index = $offsetQuery = $limitQuery = "";
+
+ extract($this->dbr->tableNames('page', 'revision'));
+ list($index, $userCond) = $this->get_user_cond();
+
+ $limitQuery = 'LIMIT '.$this->limit;
+ if ($this->offset)
+ $offsetQuery = "AND rev_timestamp <= '{$this->offset}'";
+
+ $nscond = $this->get_namespace_cond();
+ $use_index = $this->dbr->useIndexClause($index);
+ $sql = "SELECT
+ page_namespace,page_title,page_is_new,page_latest,
+ rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
+ rev_deleted
+ FROM $page,$revision $use_index
+ WHERE page_id=rev_page AND $userCond $nscond $offsetQuery
+ ORDER BY rev_timestamp DESC";
+ $sql = $this->dbr->limitResult($sql, $this->limit, 0);
+ return $sql;
+ }
+
+ function find() {
+ $contribs = array();
+ $res = $this->dbr->query($this->make_sql(), 'contribs_finder::find');
+ while ($c = $this->dbr->fetchObject($res))
+ $contribs[] = $c;
+ $this->dbr->freeResult($res);
+ return $contribs;
+ }
+};
+