Krinkle has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/323494

Change subject: internal: Create guc_Wiki class
......................................................................

internal: Create guc_Wiki class

Change-Id: Ie240cf42a2e740e78dba5117d80102d3abe3d604
---
M lb/guc.php
A lb/wiki.php
M lb/wikicontribs.php
M settings.php
4 files changed, 147 insertions(+), 74 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/labs/tools/guc 
refs/changes/94/323494/1

diff --git a/lb/guc.php b/lb/guc.php
index 305562f..b357719 100644
--- a/lb/guc.php
+++ b/lb/guc.php
@@ -77,12 +77,8 @@
         $wikisWithEditcount = $this->_getWikisWithContribs($wikis);
 
         $datas = new stdClass();
-        foreach ($wikisWithEditcount as $dbname => $wiki) {
-            $wiki->canonical_server = $wiki->url;
-            $wiki->domain = preg_replace('#^https?://#', '', 
$wiki->canonical_server);
-            // Convert "http://"; to "//".
-            // Keep https:// as-is since we should not override that. 
(phabricator:T94351)
-            $wiki->url = preg_replace('#^http://#', '//', 
$wiki->canonical_server);
+        foreach ($wikisWithEditcount as $dbname => $wikiRow) {
+            $wiki = guc_Wiki::newFromRow($wikiRow);
 
             $data = new stdClass();
             $data->wiki = $wiki;
@@ -95,7 +91,8 @@
                     $this->user,
                     $this->isIP,
                     $wiki,
-                    $this->_getCentralauthData($wiki->dbname),
+                    $wikiRow->_editcount,
+                    $this->_getCentralauthData($wikiRow->dbname),
                     $options
                 );
                 if ($this->options['isPrefixPattern'] && 
!$contribs->getRegisteredUsers()) {
@@ -108,7 +105,7 @@
                 }
                 $data->contribs = $contribs;
             } catch (Exception $e) {
-                $wiki->error = $e;
+                $data->error = $e;
             }
             unset($contribs);
             $datas->$dbname = $data;
@@ -161,7 +158,7 @@
 
     /**
      * return the wikis with contribs
-     * @param array $wikis
+     * @param array $wikis List of meta_p rows
      * @return array
      */
     private function _getWikisWithContribs(array $wikis) {
diff --git a/lb/wiki.php b/lb/wiki.php
new file mode 100644
index 0000000..dfae4a0
--- /dev/null
+++ b/lb/wiki.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Copyright 2016 by Timo Tijhof
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class guc_Wiki {
+    public $dbname;
+    public $slice;
+    public $family;
+
+    /**
+     * @var string
+     */
+    public $domain;
+
+    /**
+     * HTTP or HTTPS
+     * @var string
+     */
+    public $canonicalServer;
+
+    /**
+     * May be protocol-relative, or HTTPS
+     * @var string
+     */
+    public $url;
+
+    private function __construct() {
+    }
+
+    /**
+     * See guc::_getWikis().
+     *
+     * @param stdClass $metaRow Row from meta_p table
+     * @return guc_Wiki
+     */
+    public static function newFromRow(stdClass $row) {
+        $wiki = new self();
+
+        $wiki->dbname = $row->dbname;
+        $wiki->slice = $row->slice;
+        $wiki->family = $row->family;
+
+        $wiki->domain = preg_replace('#^https?://#', '', $row->url);
+
+        $wiki->canonicalServer = $row->url;
+
+        // Convert "http://"; to "//".
+        // Keep https:// as-is since we should not override that. 
(phabricator:T94351)
+        $wiki->url = preg_replace('#^http://#', '//', $row->url);
+
+        return $wiki;
+
+    }
+
+    public function getUrl($pageName) {
+        return $this->url . '/wiki/' . _wpurlencode($pageName);
+    }
+
+    public function getLongUrl($query) {
+        return $this->url . '/w/index.php?' . $query;
+    }
+}
diff --git a/lb/wikicontribs.php b/lb/wikicontribs.php
index 491edbb..fc02df9 100644
--- a/lb/wikicontribs.php
+++ b/lb/wikicontribs.php
@@ -29,6 +29,7 @@
     private $isIp;
     private $options;
 
+    private $editcount;
     private $centralAuth;
     private $hasManyMatches = false;
     private $contribs = null;
@@ -39,21 +40,17 @@
      * @param lb_app $app
      * @param string $user Search query for wiki users
      * @param boolean $isIP
-     * @param object $wiki
-     *  - dbname
-     *  - slice
-     *  - family
-     *  - domain
-     *  - url (may be protocol-relative, or HTTPS)
-     *  - canonical_server (HTTP or HTTPS)
+     * @param guc_Wiki $wiki
+     * @param int $editcount
      * @param null|false|object $centralAuth
      * @param array $options
      */
-    public function __construct(lb_app $app, $user, $isIP, $wiki, 
$centralAuth, $options = array()) {
+    public function __construct(lb_app $app, $user, $isIP, $wiki, $editcount, 
$centralAuth, $options = array()) {
         if (!$user) {
             throw new Exception('No username or IP');
         }
         $this->app = $app;
+        $this->wiki = $wiki;
 
         $this->user = $user;
         $this->isIp = $isIP;
@@ -62,7 +59,7 @@
             'src' => 'all',
         );
 
-        $this->wiki = $wiki;
+        $this->editcount = $editcount;
         $this->centralAuth = $centralAuth;
 
         if ($this->isIp !== true) {
@@ -248,7 +245,7 @@
             $rc->guc_namespace_name = $this->app->getNamespaceName(
                 $rc->page_namespace,
                 $this->wiki->dbname,
-                $this->wiki->canonical_server
+                $this->wiki->canonicalServer
             );
             // Full page name
             $rc->guc_pagename = $rc->guc_namespace_name
@@ -349,22 +346,14 @@
         return $res;
     }
 
-    private function getUrl($pageName) {
-        return $this->wiki->url . '/wiki/' . _wpurlencode($pageName);
-    }
-
-    private function getLongUrl($query) {
-        return $this->wiki->url . '/w/index.php?' . $query;
-    }
-
     private function getUserTools($userName) {
-        return 'For <a 
href="'.htmlspecialchars($this->getUrl("User:$userName")).'">'.htmlspecialchars($userName).'</a>
 ('
-            . '<a 
href="'.htmlspecialchars($this->getUrl("Special:Contributions/$userName")).'" 
title="Special:Contributions">contribs</a>&nbsp;| '
-            . '<a 
href="'.htmlspecialchars($this->getUrl("User_talk:$userName")).'">talk</a>&nbsp;|
 '
-            . '<a 
href="'.htmlspecialchars($this->getUrl("Special:Log/block").'?page=User:'._wpurlencode($userName)).'"
 title="Special:Log/block">block log</a>&nbsp;| '
-            . '<a 
href="'.htmlspecialchars($this->getUrl("Special:ListFiles/$userName")).'" 
title="Special:ListFiles">uploads</a>&nbsp;| '
-            . '<a 
href="'.htmlspecialchars($this->getUrl("Special:Log/$userName")).'" 
title="Special:Log">logs</a>&nbsp;| '
-            . '<a 
href="'.htmlspecialchars($this->getUrl("Special:AbuseLog").'?wpSearchUser='._wpurlencode($userName)).'"
 title="Edit Filter log for this user">filter log</a>'
+        return 'For <a 
href="'.htmlspecialchars($this->wiki->getUrl("User:$userName")).'">'.htmlspecialchars($userName).'</a>
 ('
+            . '<a 
href="'.htmlspecialchars($this->wiki->getUrl("Special:Contributions/$userName")).'"
 title="Special:Contributions">contribs</a>&nbsp;| '
+            . '<a 
href="'.htmlspecialchars($this->wiki->getUrl("User_talk:$userName")).'">talk</a>&nbsp;|
 '
+            . '<a 
href="'.htmlspecialchars($this->wiki->getUrl("Special:Log/block").'?page=User:'._wpurlencode($userName)).'"
 title="Special:Log/block">block log</a>&nbsp;| '
+            . '<a 
href="'.htmlspecialchars($this->wiki->getUrl("Special:ListFiles/$userName")).'" 
title="Special:ListFiles">uploads</a>&nbsp;| '
+            . '<a 
href="'.htmlspecialchars($this->wiki->getUrl("Special:Log/$userName")).'" 
title="Special:Log">logs</a>&nbsp;| '
+            . '<a 
href="'.htmlspecialchars($this->wiki->getUrl("Special:AbuseLog").'?wpSearchUser='._wpurlencode($userName)).'"
 title="Edit Filter log for this user">filter log</a>'
             . ')';
     }
 
@@ -385,7 +374,7 @@
                 }
             }
         }
-        $userinfo[] = $this->wiki->_editcount . ' edits';
+        $userinfo[] = $this->editcount . ' edits';
         if ($this->centralAuth) {
             $userinfo[] = 'SUL: Account attached at 
'.$this->app->formatMwDate($this->centralAuth->lu_attached_timestamp);
         }
@@ -397,50 +386,61 @@
         }
         $return .= '<ul>';
         foreach ($this->getContribs() as $rc) {
-            $item = array();
-            // Diff and history
-            $item[] =
-                '(<a 
href="'.htmlspecialchars($this->getLongUrl('title='._wpurlencode($rc->guc_pagename).'&diff=prev&oldid='.urlencode($rc->rev_id))).'">diff</a>'
-                . '&nbsp;|&nbsp;'
-                . '<a 
href="'.htmlspecialchars($this->getLongUrl('title='._wpurlencode($rc->guc_pagename).'&action=history')).'">hist</a>)'
-                ;
-
-            // Date
-            $item[] = $this->app->formatMwDate($rc->rev_timestamp);
-
-            // Patterns may yield different users for different edits,
-            // provide basic tools for each entry.
-            if ($this->options['isPrefixPattern']) {
-                $item[] = '<a 
href="'.htmlspecialchars($this->getUrl("User:{$rc->rev_user_text}")).'">'
-                    . htmlspecialchars($rc->rev_user_text).'</a>'
-                    . '&nbsp;(<a 
href="'.htmlspecialchars($this->getUrl("User_talk:{$rc->rev_user_text}")).'">talk</a>&nbsp;|
 '
-                    . '<a 
href="'.htmlspecialchars($this->getUrl("Special:Contributions/{$rc->rev_user_text}")).'"
 title="Special:Contributions">contribs</a>)';
-                $item[] = '. .';
-            }
-            // Minor edit
-            if ($rc->rev_minor_edit) {
-                $item[] = '<span class="minor">M</span>';
-            }
-
-            // Link to the page
-            $item[] = '<a 
href="'.htmlspecialchars($this->getUrl($rc->guc_pagename)).'">'
-                . htmlspecialchars($rc->guc_pagename)."</a>";
-
-            // Edit summary
-            if ($rc->rev_comment) {
-                $item[] = '<span 
class="comment">('.$this->app->wikiparser($rc->rev_comment, $rc->guc_pagename, 
$this->wiki->url).')</span>';
-            }
-
-            // Cur revision
-            if ($rc->guc_is_cur) {
-                $item[] = '<span class="rev_cur">(current)</span>';
-            }
-            $return .= '<li>' . join('&nbsp;', $item) . '</li>';
+            $return .= $this->formatChangeLine($rc);
         }
         $return .= '</ul>';
         return $return;
     }
 
+    protected function formatChangeLine($rc) {
+        $chunks = self::formatChange($this->app, $this->wiki, $rc);
+        if (!$this->options['isPrefixPattern']) {
+            unset($chunks['user']);
+        }
+        return '<li>' . join('&nbsp;', $chunks) . '</li>';
+    }
+
+    public static function formatChange(lb_app $app, guc_Wiki $wiki, stdClass 
$rc) {
+        $item = array();
+        // Diff and history
+        $item[] =
+            '(<a 
href="'.htmlspecialchars($wiki->getLongUrl('title='._wpurlencode($rc->guc_pagename).'&diff=prev&oldid='.urlencode($rc->rev_id))).'">diff</a>'
+            . '&nbsp;|&nbsp;'
+            . '<a 
href="'.htmlspecialchars($wiki->getLongUrl('title='._wpurlencode($rc->guc_pagename).'&action=history')).'">hist</a>)'
+            ;
+
+        // Date
+        $item[] = $app->formatMwDate($rc->rev_timestamp);
+
+        // When using isPrefixPattern, different edits may be from different 
users.
+        // Show user name and basic tools for each entry.
+        $item['user'] = '<a 
href="'.htmlspecialchars($wiki->getUrl("User:{$rc->rev_user_text}")).'">'
+            . htmlspecialchars($rc->rev_user_text).'</a>'
+            . '&nbsp;(<a 
href="'.htmlspecialchars($wiki->getUrl("User_talk:{$rc->rev_user_text}")).'">talk</a>&nbsp;|
 '
+            . '<a 
href="'.htmlspecialchars($wiki->getUrl("Special:Contributions/{$rc->rev_user_text}")).'"
 title="Special:Contributions">contribs</a>)&nbsp;. .&nbsp;';
+
+        // Minor edit
+        if ($rc->rev_minor_edit) {
+            $item[] = '<span class="minor">M</span>';
+        }
+
+        // Link to the page
+        $item[] = '<a 
href="'.htmlspecialchars($wiki->getUrl($rc->guc_pagename)).'">'
+            . htmlspecialchars($rc->guc_pagename)."</a>";
+
+        // Edit summary
+        if ($rc->rev_comment) {
+            $item[] = '<span 
class="comment">('.$app->wikiparser($rc->rev_comment, $rc->guc_pagename, 
$wiki->url).')</span>';
+        }
+
+        // Cur revision
+        if ($rc->guc_is_cur) {
+            $item[] = '<span class="rev_cur">(current)</span>';
+        }
+
+        return $item;
+    }
+
     /**
      * Whether the user should be considered to be ununified.
      *
diff --git a/settings.php b/settings.php
index 19816f9..25ebf03 100644
--- a/settings.php
+++ b/settings.php
@@ -21,6 +21,7 @@
                     // Components
                     'components' => array(
                         'guc',
+                        'wiki',
                         'wikicontribs',
                         'exception',
                     ),

-- 
To view, visit https://gerrit.wikimedia.org/r/323494
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie240cf42a2e740e78dba5117d80102d3abe3d604
Gerrit-PatchSet: 1
Gerrit-Project: labs/tools/guc
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to