r20184 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r20183‎ | r20184 | r20185 >
Date:23:52, 6 March 2007
Author:daniel
Status:old
Tags:
Comment:
PageBy extension
Modified paths:
  • /trunk/extensions/PageBy (added) (history)
  • /trunk/extensions/PageBy/PageBy.i18n.de.php (added) (history)
  • /trunk/extensions/PageBy/PageBy.i18n.php (added) (history)
  • /trunk/extensions/PageBy/PageBy.php (added) (history)
  • /trunk/extensions/PageBy/PageByRenderer.php (added) (history)
  • /trunk/extensions/PageBy/README (added) (history)
  • /trunk/extensions/PageBy/install.settings (added) (history)

Diff [purge]

Index: trunk/extensions/PageBy/PageBy.i18n.de.php
@@ -0,0 +1,17 @@
 2+<?php
 3+
 4+/**
 5+ * Internationalisation file for the ContactPage extension
 6+ *
 7+ * @package MediaWiki
 8+ * @subpackage Extensions
 9+ * @author Daniel Kinzler, brightbyte.de
 10+ * @copyright © 2007 Daniel Kinzler
 11+ * @licence GNU General Public Licence 2.0 or later
 12+ */
 13+
 14+$messages['pageby-first']= 'Seite angelegt von $1, $2';
 15+$messages['pageby-last']= 'Zuletzt <a href="$3">geändert</a> von $1, $2';
 16+$messages['pageby-contributors']= 'Beiträge:';
 17+$messages['pageby-anon']= '<i>anonym</i>';
 18+?>
Index: trunk/extensions/PageBy/PageByRenderer.php
@@ -0,0 +1,222 @@
 2+<?php
 3+/**
 4+ * PageBy renderer for PageBy extension.
 5+ *
 6+ * @package MediaWiki
 7+ * @subpackage Extensions
 8+ * @author Daniel Kinzler, brightbyte.de
 9+ * @copyright © 2007 Daniel Kinzler
 10+ * @licence GNU General Public Licence 2.0 or later
 11+ */
 12+
 13+if( !defined( 'MEDIAWIKI' ) ) {
 14+ echo( "Not a valid entry point.\n" );
 15+ die( 1 );
 16+}
 17+
 18+class PageByRenderer {
 19+ var $nominor;
 20+ var $noanon;
 21+ var $nobot;
 22+
 23+ var $parser;
 24+ var $title;
 25+ var $otherpage;
 26+
 27+ var $showtime;
 28+ var $showfirst;
 29+ var $showcomments;
 30+
 31+ function __construct( $page, $argv, &$parser ) {
 32+ global $wgTitle;
 33+
 34+ $this->parser = $parser;
 35+
 36+ if ($page===NULL || $page===false || trim($page)==='') {
 37+ $this->title = $wgTitle;
 38+ $this->otherpage = false;
 39+ }
 40+ else {
 41+ $this->title = Title::newFromText( trim($page) );
 42+ $this->otherpage = true; #TODO: set to false if this is the current page
 43+ }
 44+
 45+ $this->nominor = isset($argv['nominor']) ? $argv['nominor'] : true;
 46+ if ( $this->nominor === 'false' || $this->nominor === 'no' || $this->nominor === '0' )
 47+ $this->nominor = false;
 48+
 49+ $this->nobot = isset($argv['nobot']) ? $argv['nobot'] : true;
 50+ if ( $this->nobot === 'false' || $this->nobot === 'no' || $this->nobot === '0' )
 51+ $this->nobot = false;
 52+
 53+ $this->noanon = isset($argv['noanon']) ? $argv['noanon'] : false;
 54+ if ( $this->noanon === 'false' || $this->noanon === 'no' || $this->noanon === '0' )
 55+ $this->noanon = false;
 56+
 57+ $this->showfirst = isset($argv['creation']) ? $argv['creation'] : true;
 58+ if ( $this->showfirst === 'false' || $this->showfirst === 'no' || $this->showfirst === '0' )
 59+ $this->showfirst = false;
 60+
 61+ $this->showcomments = isset($argv['comments']) ? $argv['comments'] : true;
 62+ if ( $this->showcomments === 'false' || $this->showcomments === 'no' || $this->showcomments === '0' )
 63+ $this->showcomments = false;
 64+
 65+ $this->showtime = isset($argv['time']) ? $argv['time'] : false;
 66+ if ( $this->showtime === 'false' || $this->showtime === 'no' || $this->showtime === '0' )
 67+ $this->showtime = false;
 68+ }
 69+
 70+ function collectInfo( ) {
 71+ $dbr = wfGetDB( DB_SLAVE );
 72+ list( $trevision, $tuser, $tuser_groups ) = $dbr->tableNamesN( 'revision', '$tuser', 'user_groups' );
 73+
 74+ #TODO: use query cache, check against page-timestamp
 75+ #NOTE: if $this->otherpage == false, the parser cache already takes care of this...
 76+
 77+ $sql = "SELECT $trevision.* FROM $trevision ";
 78+ $sql .= "WHERE rev_deleted = 0 AND rev_page = " . (int)$this->title->getArticleID() . " ";
 79+ $sql .= "ORDER BY rev_id ASC";
 80+
 81+ #TODO: limit?
 82+
 83+ $res = $dbr->query( $sql, 'PageByRenderer::collectInfo' );
 84+
 85+ $users = array();
 86+ $first = NULL;
 87+ $last = NULL;
 88+ $edits = 0;
 89+ while ($row = $dbr->fetchObject( $res )) {
 90+ $edits += 1;
 91+
 92+ if ($first===NULL) $first = $row;
 93+ $last = $row;
 94+
 95+ if ($this->nominor && $row->rev_minor_edit) continue;
 96+ if ($this->noanon && !$row->rev_user) continue; //FIXE: this also ignores imported revisions!
 97+
 98+ if (!isset($users[$row->rev_user])) {
 99+ $users[$row->rev_user] = array(
 100+ 'name' => $row->rev_user ? $row->rev_user_text : NULL,
 101+ 'id' => $row->rev_user,
 102+ 'count' => 0,
 103+ );
 104+ }
 105+
 106+ $users[$row->rev_user]['count'] += 1;
 107+ }
 108+ $dbr->freeResult($res);
 109+
 110+ if (!$edits) return false; #no revision -> no page
 111+
 112+ if ($this->nobot) {
 113+ $userids = array_keys($users);
 114+ $userids = array_diff($userids, array( 0 )); //ignore anon
 115+
 116+ if ($userids) {
 117+ $sql = "SELECT $tuser_groups.* FROM $tuser_groups ";
 118+ $sql .= "WHERE ug_user IN ( " . $dbr->makeList( $userids ) . " ) ";
 119+ $sql .= "AND ug_group = 'bot' ";
 120+
 121+ $res = $dbr->query( $sql, 'PageByRenderer::collectInfo#bots' );
 122+ while ($row = $dbr->fetchObject( $res )) {
 123+ unset($users[$row->ug_user]); #strip bots
 124+ }
 125+ $dbr->freeResult($res);
 126+ }
 127+ }
 128+
 129+ $info = array();
 130+ $info['edits'] = $edits;
 131+ $info['first'] = $first;
 132+ $info['last'] = $last;
 133+ $info['users'] = $users;
 134+
 135+ return $info;
 136+ }
 137+
 138+ function renderPageBy( ) {
 139+ global $wgContLang, $wgUser;
 140+ $sk = $wgUser->getSkin();
 141+
 142+ loadPageByI18n();
 143+
 144+ if ($this->otherpage) {
 145+ #TODO: if we can't use the parser cache, we should use the query cache
 146+ $this->parser->disableCache();
 147+ }
 148+
 149+ $info = $this->collectInfo();
 150+ if (!$info) return false; #TODO: report error!
 151+
 152+ extract($info);
 153+
 154+ $html = '<ul class="pageby">';
 155+
 156+ #TODO: somehere link the page history. And mention the page name, if it's not the local page.
 157+
 158+ if ($this->showfirst) {
 159+ $firstuser = Title::makeTitle(NS_USER, $first->rev_user_text);
 160+ $ulink = $sk->makeLinkObj($firstuser, $first->rev_user_text);
 161+ $date = $this->showtime ? $wgContLang->timeanddate($first->rev_timestamp) : $wgContLang->date($first->rev_timestamp);
 162+ $diff = $this->title->getLocalURL('diff=' . $first->rev_id);
 163+ $comment = htmlspecialchars( $first->rev_comment );
 164+
 165+ $html .= '<li class="pageby-first">';
 166+ $html .= wfMsg('pageby-first', $ulink, $date, $diff);
 167+ if ($this->showcomments) $html .= '<span class="pageby-comment">: <i>' . $comment . '</i></span>';
 168+ $html .= '</li>';
 169+ $html .= "\n";
 170+ }
 171+
 172+ if ( sizeof($users) > 1 && ( (!$this->showfirst && $edits>1) || $edits>2) ) {
 173+ $contributors = '';
 174+
 175+ foreach ($users as $u) {
 176+ if (!$u['id']) {
 177+ $ulink = wfMsg('pageby-anon');
 178+ }
 179+ else {
 180+ $cuser = Title::makeTitle(NS_USER, $u['name']);
 181+ $ulink = $sk->makeLinkObj($cuser, $u['name']);
 182+ }
 183+
 184+ if ($contributors !== '') $contributors .= ', ';
 185+
 186+ $contributors .= '<span class="pageby-contributor">';
 187+ $contributors .= $ulink;
 188+ $contributors .= ' <span class="pageby-contribcount">x';
 189+ $contributors .= $u['count'];
 190+ $contributors .= '</span>';
 191+ $contributors .= '</span>';
 192+ }
 193+
 194+ $html .= '<li class="pageby-contribs">';
 195+ $html .= wfMsg('pageby-contributors');
 196+ $html .= ' ';
 197+ $html .= $contributors;
 198+ $html .= '</li>';
 199+ $html .= "\n";
 200+ }
 201+
 202+ if (!$this->showfirst || $edits > 1) {
 203+ $lastuser = Title::makeTitle(NS_USER, $last->rev_user_text);
 204+ $ulink = $sk->makeLinkObj($lastuser, $last->rev_user_text);
 205+ $date = $this->showtime ? $wgContLang->timeanddate($last->rev_timestamp) : $wgContLang->date($last->rev_timestamp);
 206+ $diff = $this->title->getLocalURL('diff=' . $last->rev_id);
 207+ $comment = htmlspecialchars( $last->rev_comment );
 208+
 209+ $html .= '<li class="pageby-last">';
 210+ $html .= wfMsg('pageby-last', $ulink, $date, $diff);
 211+ if ($this->showcomments) $html .= '<span class="pageby-comment">: <i>' . $comment . '</i></span>';
 212+ $html .= '</li>';
 213+ }
 214+
 215+ $html .= '</ul>';
 216+ $html .= "\n";
 217+
 218+ return $html;
 219+ }
 220+
 221+}
 222+
 223+?>
\ No newline at end of file
Index: trunk/extensions/PageBy/install.settings
@@ -0,0 +1 @@
 2+require_once( "{{path}}/PageBy.php" );
Index: trunk/extensions/PageBy/PageBy.i18n.php
@@ -0,0 +1,17 @@
 2+<?php
 3+
 4+/**
 5+ * Internationalisation file for the ContactPage extension
 6+ *
 7+ * @package MediaWiki
 8+ * @subpackage Extensions
 9+ * @author Daniel Kinzler, brightbyte.de
 10+ * @copyright © 2007 Daniel Kinzler
 11+ * @licence GNU General Public Licence 2.0 or later
 12+ */
 13+
 14+$messages['pageby-first']= 'Page created by $1, $2';
 15+$messages['pageby-last']= 'Last <a href="$3">modified</a> by $1, $2';
 16+$messages['pageby-contributors']= 'Contributors:';
 17+$messages['pageby-anon']= '<i>anonymous</i>';
 18+?>
Index: trunk/extensions/PageBy/PageBy.php
@@ -0,0 +1,66 @@
 2+<?php
 3+/**
 4+ * PageBy extension - shows recent changes on a wiki page.
 5+ *
 6+ * @package MediaWiki
 7+ * @subpackage Extensions
 8+ * @author Daniel Kinzler, brightbyte.de
 9+ * @copyright © 2007 Daniel Kinzler
 10+ * @licence GNU General Public Licence 2.0 or later
 11+ */
 12+
 13+
 14+if( !defined( 'MEDIAWIKI' ) ) {
 15+ echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
 16+ die( 1 );
 17+}
 18+
 19+$wgExtensionCredits['other'][] = array(
 20+ 'name' => 'PageBy',
 21+ 'author' => 'Daniel Kinzler, brightbyte.de',
 22+ 'url' => 'http://mediawiki.org/wiki/Extension:PageBy',
 23+ 'description' => 'shows contributors inline on a wiki page',
 24+);
 25+
 26+$wgExtensionFunctions[] = "wfPageByExtension";
 27+
 28+$wgAutoloadClasses['PageByRenderer'] = dirname( __FILE__ ) . '/PageByRenderer.php';
 29+
 30+function wfPageByExtension() {
 31+ global $wgParser;
 32+ $wgParser->setHook( "pageby", "newsxRenderPageBy" );
 33+}
 34+
 35+
 36+function newsxRenderPageBy( $page, $argv, &$parser ) {
 37+ $renderer = new PageByRenderer($page, $argv, $parser);
 38+ return $renderer->renderPageBy();
 39+}
 40+
 41+/**
 42+* load the PageBy internationalization file
 43+*/
 44+function loadPageByI18n() {
 45+ global $wgContLang, $wgMessageCache;
 46+
 47+ #TODO: optionally disable caching and use user language?
 48+ # or always use user language, because it's in the parser-cache-key? is it?
 49+
 50+ static $initialized = false;
 51+
 52+ if ( $initialized ) return;
 53+
 54+ $messages= array();
 55+
 56+ $f= dirname( __FILE__ ) . '/PageBy.i18n.php';
 57+ include( $f );
 58+
 59+ $f= dirname( __FILE__ ) . '/PageBy.i18n.' . $wgContLang->getCode() . '.php';
 60+ if ( file_exists( $f ) ) include( $f );
 61+
 62+ $initialized = true;
 63+ $wgMessageCache->addMessages( $messages );
 64+}
 65+
 66+
 67+?>
\ No newline at end of file
Index: trunk/extensions/PageBy/README
@@ -0,0 +1,105 @@
 2+--------------------------------------------------------------------------
 3+README for the PageBy extension
 4+Copyright © 2007 Daniel Kinzler
 5+Licenses: GNU General Public Licence (GPL)
 6+ GNU Free Documentation License (GFDL)
 7+--------------------------------------------------------------------------
 8+
 9+The PageBy extension provides a custom tag, <pageby>, that renders as a
 10+summary of the pages edit history.
 11+
 12+<http://mediawiki.org/wiki/Extension:PageBy>
 13+
 14+The PageBy extension was originally written by Daniel Kinzler in 2007
 15+and is released under the GNU General Public Licence (GPL).
 16+
 17+
 18+The <pageby> tag shows up to three lines of information:
 19+
 20+* page creation (not shown if creation="false" is given)
 21+* contributor summary (lists all editors with the number of edits they made;
 22+ not shown if there is only one eligible editor)
 23+* last edit (not shown if there is only one edit - unless page creation is
 24+ not being shown)
 25+
 26+
 27+== Installing ==
 28+
 29+Copy the PageBy directory into the extensions folder of your
 30+MediaWiki installation. Then add the following line to your
 31+LocalSettings.php file (near the end):
 32+
 33+ require_once( "$IP/extensions/PageBy/PageBy.php" );
 34+
 35+
 36+== Usage ==
 37+
 38+To get a short summary of the edit hostory:
 39+
 40+ <pageby/>
 41+
 42+You can provide options to controll the summary - for example, include minor
 43+edits and hide edit comments:
 44+
 45+ <pageby nominor="false" comments="false"/>
 46+
 47+You can also specify another page than the one that contains the pageby tag:
 48+
 49+ <pageby>Foo</pageby>
 50+
 51+Note that using the pageby tag for *another* page will disable the parser cache
 52+for the page the tag is used on. So perhaps don't do that on the main page.
 53+
 54+For a full list of options, see below.
 55+
 56+
 57+=== Options ===
 58+The following options (tag attributes) can be used to controll the output of the
 59+<pageby> tags:
 60+
 61+* nominor ignore minor edits ("true" or "false", default is "true").
 62+ Applies only to to the contributors summary, does not effect
 63+ creation or last-edit line.
 64+
 65+* nobot hide bots ("true" or "false", default is "true").
 66+ Applies only to to the contributors summary, does not effect
 67+ creation or last-edit line.
 68+
 69+* noanon hide anonymous users ("true" or "false", default is "false").
 70+ Applies only to to the contributors summary, does not effect
 71+ creation or last-edit line.
 72+
 73+* comments show edit comments ("true" or "false", default is "true").
 74+
 75+* creation mention page creation ("true" or "false", default is "true").
 76+
 77+* time show exact time in addition to the date ("true" or "false",
 78+ default is "false").
 79+
 80+== Styling ==
 81+The HTML generated by a <pageby> tag is a plain <ul>-list, but it assigns
 82+different CSS classes to the individual elements, so you can style it
 83+conveniently.
 84+
 85+CSS classes used:
 86+
 87+* pageby - used on the <ul>-tag that contains all output
 88+* pageby-contribs - for the <li>-tag for the contribution summary
 89+* pageby-last - for the <li>-tag for the latest edit
 90+* pageby-contributor - for the <span>-tag of each contributor
 91+* pageby-comment - for the <span>-tag containing the edit comment
 92+
 93+An example CSS style to put into MediaWiki:common.css or a simmilar place:
 94+
 95+ ul.pageby {
 96+ float:right;
 97+ clear:right;
 98+ border:1px solid #60606F;
 99+ background-color:#E0E0EF;
 100+ font-size: 80%;
 101+ margin:1ex;
 102+ padding:1ex;
 103+ list-style-type:none;
 104+ }
 105+
 106+