r4726 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r4725‎ | r4726 | r4727 >
Date:07:46, 13 August 2004
Author:timwi
Status:old
Tags:
Comment:
A major reworking of [[Special:Allmessages]].

So far, the index page has generated 1020 (one thousand and twenty!) SQL
queries. Let's limit this to a maximum of 51. This means that the user is
presented with only 50 rows at a time; however, I have provided "previous" and
"next" links so they can navigate through it. Additionally, the user can now
enter a "from" term in a text box rather than having to fiddle with the URL.

Furthermore, the user can now choose a namespace using a drop down box. This
works both on the index page and on a "chunk" page.

Lastly, I think I have made the code for the index page more readable -- not
only by adding comments.

HINT: since this is such a major reworking, the patch is pretty useless. It is
probably better for anyone interested to view the new file.
Modified paths:
  • /trunk/phase3/includes/SpecialAllpages.php (modified) (history)
  • /trunk/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialAllpages.php
@@ -2,25 +2,58 @@
33
44 function wfSpecialAllpages( $par=NULL )
55 {
6 - global $indexMaxperpage, $wgRequest;
 6+ global $indexMaxperpage, $toplevelMaxperpage, $wgRequest, $wgOut, $wgLang;
77 $indexMaxperpage = 480;
 8+ $toplevelMaxperpage = 50;
89 $from = $wgRequest->getVal( 'from' );
910 $namespace = $wgRequest->getVal( 'namespace' );
1011 if ( is_null($namespace) ) { $namespace = 0; }
 12+ $arr = $wgLang->getNamespaces();
 13+ $wgOut->setPagetitle ( $namespace > 0 ? wfMsg ( 'allpagesnamespace', $arr[$namespace] )
 14+ : wfMsg ( 'allarticles' ) );
1115
12 - if( $par ) {
 16+ if ( $par ) {
1317 indexShowChunk( $par, $namespace );
14 - } elseif( !is_null( $from ) ) {
 18+ } elseif ( $from ) {
1519 indexShowChunk( $from, $namespace );
1620 } else {
17 - indexShowToplevel();
 21+ indexShowToplevel ( $namespace );
1822 }
1923 }
2024
21 -function indexShowToplevel()
 25+function namespaceForm ( $namespace = 0, $from = "" )
2226 {
23 - global $wgOut, $indexMaxperpage, $wgLang;
 27+ global $wgLang, $wgScript;
 28+
 29+ $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
 30+
 31+ $namespaceselect = '<select name="namespace">';
 32+ $arr = $wgLang->getNamespaces();
 33+ for ( $i = 0; $i < 14; $i++ ) {
 34+ $namespacename = str_replace ( "_", " ", $arr[$i] );
 35+ $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
 36+ $sel = ($i == $namespace) ? ' selected="selected"' : '';
 37+ $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
 38+ }
 39+ $namespaceselect .= '</select>';
 40+
 41+ $frombox = '<input type="text" size="20" name="from" value="'
 42+ . htmlspecialchars ( $from ) . '"/>';
 43+ $submitbutton = '<input type="submit" value="' . wfMsg( 'go' ) . '" />';
 44+
 45+ $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
 46+ $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
 47+ $out .= wfMsg ( 'allpagesformtext', $frombox, $namespaceselect, $submitbutton );
 48+ $out .= '</form></div>';
 49+ return $out;
 50+}
 51+
 52+function indexShowToplevel ( $namespace = 0 )
 53+{
 54+ global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgLang, $wgRequest, $wgUser;
 55+ $sk = $wgUser->getSkin();
2456 $fname = "indexShowToplevel";
 57+ $namespace = intval ($namespace);
2558
2659 # Cache
2760 $vsp = $wgLang->getValidSpecialPages();
@@ -35,62 +68,109 @@
3669
3770 $dbr =& wfGetDB( DB_SLAVE );
3871 $cur = $dbr->tableName( 'cur' );
39 - $fromwhere = "FROM $cur WHERE cur_namespace=0";
40 - $order = 'ORDER BY cur_title';
 72+ $fromwhere = "FROM $cur WHERE cur_namespace=$namespace";
 73+ $order_arr = array ( 'ORDER BY' => 'cur_title' );
 74+ $order_str = 'ORDER BY cur_title';
4175 $out = "";
42 - $where = array( 'cur_namespace' => 0 );
 76+ $where = array( 'cur_namespace' => $namespace );
4377
4478 $count = $dbr->selectField( 'cur', 'COUNT(*)', $where, $fname );
4579 $sections = ceil( $count / $indexMaxperpage );
46 - $inpoint = $dbr->selectField( 'cur', 'cur_title', $where, $fname, $order );
4780
48 - $out .= "<table>\n";
49 - # There's got to be a cleaner way to do this!
50 - for( $i = 1; $i < $sections; $i++ ) {
51 - $from = $i * $indexMaxperpage;
52 - $sql = "SELECT cur_title $fromwhere $order ".$dbr->limitResult(2,$from);
53 - $res = $dbr->query( $sql, $fname );
 81+ # We want to display $toplevelMaxperpage lines starting at $offset.
 82+ # NOTICE: $offset starts at 0
 83+ $offset = intval ( $wgRequest->getVal( 'offset' ) );
 84+ if ( $offset < 0 ) { $offset = 0; }
 85+ if ( $offset >= $sections ) { $offset = $sections - 1; }
5486
55 - $s = $dbr->fetchObject( $res );
56 - $outpoint = $s->cur_title;
57 - $out .= indexShowline( $inpoint, $outpoint );
 87+ # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
 88+ # we're displaying only the very last section, we still need two DB queries to find the titles
 89+ $stopat = ( $offset + $toplevelMaxperpage < $sections )
 90+ ? $offset + $toplevelMaxperpage : $sections ;
5891
 92+ # This array is going to hold the cur_titles in order.
 93+ $lines = array();
 94+
 95+ # If we are going to show n rows, we need n+1 queries to find the relevant titles.
 96+ for ( $i = $offset; $i <= $stopat; $i++ ) {
 97+ if ( $i == $sections ) # if we're displaying the last section, we need to
 98+ $from = $count-1; # find the last cur_title in the DB
 99+ else if ( $i > $offset )
 100+ $from = $i * $indexMaxperpage - 1;
 101+ else
 102+ $from = $i * $indexMaxperpage;
 103+ $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
 104+ $sql = "SELECT cur_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
 105+ echo "($offset/$stopat) $sql<br>\n";
 106+ $res = $dbr->query( $sql, $fname );
59107 $s = $dbr->fetchObject( $res );
60 - $inpoint = $s->cur_title;
61 -
 108+ array_push ( $lines, $s->cur_title );
 109+ if ( $s = $dbr->fetchObject( $res ) ) {
 110+ array_push ( $lines, $s->cur_title );
 111+ }
62112 $dbr->freeResult( $res );
63113 }
64114
65 - $from = $i * $indexMaxperpage;
66 - $sql = "SELECT cur_title $fromwhere $order ".wfLimitResult(1,$count-1);
67 - $res = $dbr->query( $sql, $fname );
68 - $s = $dbr->fetchObject( $res );
69 - $outpoint = $s->cur_title;
70 - $out .= indexShowline( $inpoint, $outpoint );
71 - $out .= "</table>\n";
 115+ # At this point, $lines should contain an even number of elements.
 116+ $out .= "<table style='background: inherit;'>";
 117+ while ( count ( $lines ) > 0 ) {
 118+ $inpoint = array_shift ( $lines );
 119+ $outpoint = array_shift ( $lines );
 120+ $out .= indexShowline ( $inpoint, $outpoint, $namespace );
 121+ }
 122+ $out .= "</table>";
72123
 124+ $nsForm = namespaceForm ( $namespace );
 125+
 126+ # Is there more?
 127+ $morelinks = "";
 128+ if ( $offset > 0 ) {
 129+ $morelinks = $sk->makeKnownLink (
 130+ $wgLang->specialPage ( "Allpages" ),
 131+ wfMsg ( 'allpagesprev' ),
 132+ ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
 133+ );
 134+ }
 135+ if ( $stopat < $sections-1 ) {
 136+ if ( $morelinks != "" ) { $morelinks .= " | "; }
 137+ $morelinks .= $sk->makeKnownLink (
 138+ $wgLang->specialPage ( "Allpages" ),
 139+ wfMsg ( 'allpagesnext' ),
 140+ 'offset=' . ($offset + $toplevelMaxperpage)
 141+ );
 142+ }
 143+
 144+ if ( $morelinks != "" ) {
 145+ $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
 146+ $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
 147+ $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
 148+ $out2 .= $morelinks . '</td></tr></table><hr />';
 149+ } else {
 150+ $out2 = $nsForm . '<hr />';
 151+ }
 152+
73153 # Saving cache
74154 $log->replaceContent( $out );
75155
76 - $wgOut->addHtml( $out );
 156+ $wgOut->addHtml( $out2 . $out );
77157 }
78158
79 -function indexShowline( $inpoint, $outpoint )
 159+function indexShowline( $inpoint, $outpoint, $namespace = 0 )
80160 {
81161 global $wgOut, $wgLang, $wgUser;
82162 $sk = $wgUser->getSkin();
83163 $dbr =& wfGetDB( DB_SLAVE );
84164
85 - # Fixme: this is ugly
 165+ $inpointf = str_replace( "_", " ", $inpoint );
 166+ $outpointf = str_replace( "_", " ", $outpoint );
 167+ $queryparams = 'from=' . $dbr->strencode( $inpoint );
 168+ if ( $namespace > 0 ) { $queryparams .= '&namespace='.intval($namespace); }
86169 $out = wfMsg(
87 - "alphaindexline",
88 - $sk->makeKnownLink( $wgLang->specialPage( "Allpages" ),
89 - str_replace( "_", " ", $inpoint ),
90 - "from=" . $dbr->strencode( $inpoint ) ) . "</td><td>",
91 - "</td><td align=\"left\">" .
92 - str_replace( "_", " ", $outpoint )
93 - );
94 - return "<tr><td align=\"right\">{$out}</td></tr>\n";
 170+ 'alphaindexline',
 171+ $sk->makeKnownLink( $wgLang->specialPage( "Allpages" ), $inpointf, $queryparams ) . '</td><td>',
 172+ '</td><td align="left">' . $outpointf
 173+ );
 174+ return '<tr><td align="right">'.$out.'</td></tr>';
95175 }
96176
97177 function indexShowChunk( $from, $namespace = 0 )
@@ -110,38 +190,41 @@
111191 ### FIXME: side link to previous
112192
113193 $n = 0;
114 - $out = "<table border=\"0\" width=\"100%\">\n";
 194+ $out = '<table style="background: inherit;" border="0" width="100%">';
115195 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
116196 $t = Title::makeTitle( $namespacee, $s->cur_title );
117197 if( $t ) {
118 - $link = $sk->makeKnownLinkObj( $t );
 198+ $link = $sk->makeKnownLinkObj( $t, $t->getText() );
119199 } else {
120 - $link = "[[" . htmlspecialchars( $s->cur_title ) . "]]";
 200+ $link = '[[' . htmlspecialchars( $s->cur_title ) . ']]';
121201 }
122202 if( $n % 3 == 0 ) {
123 - $out .= "<tr>\n";
 203+ $out .= '<tr>';
124204 }
125205 $out .= "<td>$link</td>";
126206 $n++;
127207 if( $n % 3 == 0 ) {
128 - $out .= "</tr>\n";
 208+ $out .= '</tr>';
129209 }
130210 }
131211 if( ($n % 3) != 0 ) {
132 - $out .= "</tr>\n";
 212+ $out .= '</tr>';
133213 }
134 - $out .= "</table>";
 214+ $out .= '</table>';
135215
136 - $out2 = "<div style='text-align: right; font-size: smaller; margin-bottom: 1em;'>" .
 216+ $nsForm = namespaceForm ( $namespace, $from );
 217+ $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
 218+ $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
 219+ $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
137220 $sk->makeKnownLink( $wgLang->specialPage( "Allpages" ),
138221 wfMsg ( 'allpages' ) );
139222 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
140223 $out2 .= " | " . $sk->makeKnownLink(
141224 $wgLang->specialPage( "Allpages" ),
142225 wfMsg ( 'nextpage', $s->cur_title ),
143 - "from=" . $dbr->strencode( $s->cur_title ) );
 226+ "from=" . wfUrlEncode ( $s->cur_title ) );
144227 }
145 - $out2 .= "</div>";
 228+ $out2 .= "</td></tr></table><hr />";
146229
147230 $wgOut->addHtml( $out2 . $out );
148231 }
Index: trunk/phase3/languages/Language.php
@@ -971,7 +971,6 @@
972972 'wantedpages' => 'Wanted pages',
973973 'nlinks' => '$1 links',
974974 'allpages' => 'All pages',
975 -'nextpage' => 'Next page ($1)',
976975 'randompage' => 'Random page',
977976 'shortpages' => 'Short pages',
978977 'longpages' => 'Long pages',
@@ -1006,6 +1005,15 @@
10071006 'alphaindexline' => "$1 to $2",
10081007 'version' => 'Version',
10091008
 1009+# Specific to All pages
 1010+'nextpage' => 'Next page ($1)',
 1011+'articlenamespace' => '(articles)',
 1012+'allpagesformtext' => 'Display pages starting at: $1 Choose namespace: $2 $3',
 1013+'allarticles' => 'All articles',
 1014+'allpagesprev' => 'Previous',
 1015+'allpagesnext' => 'Next',
 1016+'allpagesnamespace' => 'All pages ($1 namespace)',
 1017+
10101018 # Email this user
10111019 #
10121020 'mailnologin' => 'No send address',

Status & tagging log