r10199 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r10198‎ | r10199 | r10200 >
Date:22:15, 18 July 2005
Author:vibber
Status:old
Tags:
Comment:
* Cache the top-level index list in memcached
* Removed inverse checkbox that looked index-unfriendly
* Use relative offsets in toplevel index generation to
avoid retracing all previous steps on each line
* Bump up page size from 480 to 960 to make the
toplevels more manageable
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/SpecialAllpages.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialAllpages.php
@@ -11,36 +11,28 @@
1212 function wfSpecialAllpages( $par=NULL, $specialPage ) {
1313 global $indexMaxperpage, $toplevelMaxperpage, $wgRequest, $wgOut, $wgContLang;
1414 # Config
15 - $indexMaxperpage = 480;
 15+ $indexMaxperpage = 960;
1616 $toplevelMaxperpage = 50;
1717 # GET values
1818 $from = $wgRequest->getVal( 'from' );
1919 $namespace = $wgRequest->getInt( 'namespace' );
20 - $invert = $wgRequest->getBool( 'invert' );
2120
2221 $namespaces = $wgContLang->getNamespaces();
2322
2423 if( !in_array($namespace, array_keys($namespaces)) )
2524 $namespace = 0;
2625
27 - if ($invert) {
28 - $wgOut->setPagetitle( $namespace > 0 ?
29 - wfMsg( 'allnotinnamespace', $namespaces[$namespace] ) :
30 - wfMsg( 'allnonarticles' )
31 - );
32 - } else {
33 - $wgOut->setPagetitle( $namespace > 0 ?
34 - wfMsg( 'allinnamespace', $namespaces[$namespace] ) :
35 - wfMsg( 'allarticles' )
36 - );
37 - }
 26+ $wgOut->setPagetitle( $namespace > 0 ?
 27+ wfMsg( 'allinnamespace', $namespaces[$namespace] ) :
 28+ wfMsg( 'allarticles' )
 29+ );
3830
3931 if ( isset($par) ) {
40 - indexShowChunk( $namespace, $par, $invert, $specialPage->including() );
 32+ indexShowChunk( $namespace, $par, $specialPage->including() );
4133 } elseif ( isset($from) ) {
42 - indexShowChunk( $namespace, $from, $invert, $specialPage->including() );
 34+ indexShowChunk( $namespace, $from, $specialPage->including() );
4335 } else {
44 - indexShowToplevel ( $namespace, $invert, $specialPage->including() );
 36+ indexShowToplevel ( $namespace, $specialPage->including() );
4537 }
4638 }
4739
@@ -48,9 +40,8 @@
4941 * HTML for the top form
5042 * @param integer $namespace A namespace constant (default NS_MAIN).
5143 * @param string $from Article name we are starting listing at.
52 - * @param bool $invert true if we want the namespaces inverted (default false)
5344 */
54 -function indexNamespaceForm ( $namespace = NS_MAIN, $from = '', $invert = false ) {
 45+function indexNamespaceForm ( $namespace = NS_MAIN, $from = '' ) {
5546 global $wgContLang, $wgScript;
5647 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
5748
@@ -69,8 +60,6 @@
7061 . htmlspecialchars ( $from ) . '"/>';
7162 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
7263
73 - $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
74 -
7564 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
7665 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
7766 $out .= "
@@ -82,8 +71,7 @@
8372 <tr>
8473 <td align='right'><label for='nsselectbox'>" . wfMsg('namespace') . "</label></td>
8574 <td align='left'>
86 - $namespaceselect $submitbutton $invertbox
87 - <label for='nsinvert'>" . wfMsg('invert') . "</label>
 75+ $namespaceselect $submitbutton
8876 </td>
8977 </tr>
9078 </table>
@@ -94,9 +82,8 @@
9583
9684 /**
9785 * @param integer $namespace (default NS_MAIN)
98 - * @param bool $invert true if we want the namespaces inverted (default false)
9986 */
100 -function indexShowToplevel ( $namespace = NS_MAIN, $invert = false, $including = false ) {
 87+function indexShowToplevel ( $namespace = NS_MAIN, $including = false ) {
10188 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgContLang, $wgRequest, $wgUser;
10289 $sk = $wgUser->getSkin();
10390 $fname = "indexShowToplevel";
@@ -106,89 +93,81 @@
10794
10895 $dbr =& wfGetDB( DB_SLAVE );
10996 $page = $dbr->tableName( 'page' );
110 - $fromwhere = "FROM $page WHERE page_namespace" .
111 - ($invert ? '!' : '') . "=$namespace";
 97+ $fromwhere = "FROM $page WHERE page_namespace=$namespace";
11298 $order_arr = array ( 'ORDER BY' => 'page_title' );
11399 $order_str = 'ORDER BY page_title';
114100 $out = "";
115101 $where = array( 'page_namespace' => $namespace );
116102
117 - $count = $dbr->selectField( 'page', 'COUNT(*)', $where, $fname );
118 - $sections = ceil( $count / $indexMaxperpage );
 103+ global $wgMemc, $wgDBname;
 104+ $key = "$wgDBname:allpages:ns:$namespace";
 105+ $lines = $wgMemc->get( $key );
119106
120 - if ( $sections < 3 ) {
121 - # If there are only two or less sections, don't even display them.
122 - # Instead, display the first section directly.
123 - indexShowChunk( $namespace, '', $invert, $including );
124 - return;
125 - }
126 -
127 - # We want to display $toplevelMaxperpage lines starting at $offset.
128 - # NOTICE: $offset starts at 0
129 - $offset = intval ( $wgRequest->getVal( 'offset' ) );
130 - if ( $offset < 0 ) { $offset = 0; }
131 - if ( $offset >= $sections ) { $offset = $sections - 1; }
132 -
133 - # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
134 - # we're displaying only the very last section, we still need two DB queries to find the titles
135 - $stopat = ( $offset + $toplevelMaxperpage < $sections )
136 - ? $offset + $toplevelMaxperpage : $sections ;
137 -
138 - # This array is going to hold the page_titles in order.
139 - $lines = array();
140 -
141 - # If we are going to show n rows, we need n+1 queries to find the relevant titles.
142 - for ( $i = $offset; $i <= $stopat; ++$i ) {
143 - if ( $i == $sections ) # if we're displaying the last section, we need to
144 - $from = $count-1; # find the last page_title in the DB
145 - else if ( $i > $offset )
146 - $from = $i * $indexMaxperpage - 1;
147 - else
148 - $from = $i * $indexMaxperpage;
149 - $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
150 - $sql = "SELECT page_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
151 - $res = $dbr->query( $sql, $fname );
152 - if ( $s = $dbr->fetchObject( $res ) ) {
153 - array_push ( $lines, $s->page_title );
 107+ if( !is_array( $lines ) ) {
 108+ $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
 109+ $lastTitle = $firstTitle;
 110+
 111+ # This array is going to hold the page_titles in order.
 112+ $lines = array( $firstTitle );
 113+
 114+ # If we are going to show n rows, we need n+1 queries to find the relevant titles.
 115+ $done = false;
 116+ for( $i = 0; !$done; ++$i ) {
 117+ // Fetch the last title of this chunk and the first of the next
 118+ $chunk = is_null( $lastTitle )
 119+ ? '1=1'
 120+ : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
 121+ $sql = "SELECT page_title $fromwhere AND $chunk $order_str " .
 122+ $dbr->limitResult( 2, $indexMaxperpage - 1 );
 123+ $res = $dbr->query( $sql, $fname );
154124 if ( $s = $dbr->fetchObject( $res ) ) {
155 - array_push ( $lines, $s->page_title );
 125+ array_push( $lines, $s->page_title );
 126+ } else {
 127+ // Final chunk, but ended prematurely. Go back and find the end.
 128+ $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
 129+ array(
 130+ 'page_namespace' => $namespace,
 131+ $chunk
 132+ ), $fname );
 133+ array_push( $lines, $endTitle );
 134+ $done = true;
156135 }
 136+ if( $s = $dbr->fetchObject( $res ) ) {
 137+ array_push( $lines, $s->page_title );
 138+ $lastTitle = $s->page_title;
 139+ } else {
 140+ // This was a final chunk and ended exactly at the limit.
 141+ // Rare but convenient!
 142+ $done = true;
 143+ }
 144+ $dbr->freeResult( $res );
157145 }
158 - $dbr->freeResult( $res );
 146+ $wgMemc->add( $key, $lines, 3600 );
159147 }
 148+
 149+ // If there are only two or less sections, don't even display them.
 150+ // Instead, display the first section directly.
 151+ if( count( $lines ) <= 2 ) {
 152+ indexShowChunk( $namespace, '', false, $including );
 153+ return;
 154+ }
160155
161156 # At this point, $lines should contain an even number of elements.
162157 $out .= "<table style='background: inherit;'>";
163158 while ( count ( $lines ) > 0 ) {
164159 $inpoint = array_shift ( $lines );
165160 $outpoint = array_shift ( $lines );
166 - $out .= indexShowline ( $inpoint, $outpoint, $namespace, $invert );
 161+ $out .= indexShowline ( $inpoint, $outpoint, $namespace, false );
167162 }
168163 $out .= '</table>';
169164
170 - $nsForm = indexNamespaceForm ( $namespace, '', $invert );
 165+ $nsForm = indexNamespaceForm ( $namespace, '', false );
171166
172167 # Is there more?
173168 if ( $including ) {
174169 $out2 = '';
175170 } else {
176171 $morelinks = '';
177 - if ( $offset > 0 ) {
178 - $morelinks = $sk->makeKnownLink (
179 - $wgContLang->specialPage ( 'Allpages' ),
180 - wfMsg ( 'allpagesprev' ),
181 - ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
182 - );
183 - }
184 - if ( $stopat < $sections-1 ) {
185 - if ( $morelinks != '' ) { $morelinks .= " | "; }
186 - $morelinks .= $sk->makeKnownLink (
187 - $wgContLang->specialPage ( 'Allpages' ),
188 - wfMsg ( 'allpagesnext' ),
189 - 'offset=' . ($offset + $toplevelMaxperpage)
190 - );
191 - }
192 -
193172 if ( $morelinks != '' ) {
194173 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
195174 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
@@ -206,16 +185,15 @@
207186 * @todo Document
208187 * @param string $from
209188 * @param integer $namespace (Default NS_MAIN)
210 - * @param bool $invert true if we want the namespaces inverted (default false)
211189 */
212 -function indexShowline( $inpoint, $outpoint, $namespace = NS_MAIN, $invert ) {
 190+function indexShowline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
213191 global $wgOut, $wgLang, $wgUser;
214192 $sk = $wgUser->getSkin();
215193 $dbr =& wfGetDB( DB_SLAVE );
216194
217195 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
218196 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
219 - $queryparams = ($namespace ? "namespace=$namespace" : '') . ($invert ? "&invert=$invert" : '');
 197+ $queryparams = ($namespace ? "namespace=$namespace" : '');
220198 $special = Title::makeTitle( NS_SPECIAL, 'Allpages/' . $inpoint );
221199 $link = $special->escapeLocalUrl( $queryparams );
222200
@@ -230,9 +208,8 @@
231209 /**
232210 * @param integer $namespace (Default NS_MAIN)
233211 * @param string $from list all pages from this name (default FALSE)
234 - * @param bool $invert true if we want the namespaces inverted (default false)
235212 */
236 -function indexShowChunk( $namespace = NS_MAIN, $from, $invert = false, $including = false ) {
 213+function indexShowChunk( $namespace = NS_MAIN, $from, $including = false ) {
237214 global $wgOut, $wgUser, $indexMaxperpage, $wgContLang;
238215 $sk = $wgUser->getSkin();
239216 $maxPlusOne = $indexMaxperpage + 1;
@@ -244,8 +221,8 @@
245222 $fromTitle = Title::newFromURL( $from );
246223 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
247224
248 - $sql = "SELECT page_namespace,page_title FROM $page WHERE page_namespace" .
249 - ($invert ? '!' : '') . "=$namespace" .
 225+ $sql = "SELECT page_namespace,page_title FROM $page" .
 226+ " WHERE page_namespace=$namespace" .
250227 " AND page_title >= ". $dbr->addQuotes( $fromKey ) .
251228 " ORDER BY page_title LIMIT " . $maxPlusOne;
252229 $res = $dbr->query( $sql, 'indexShowChunk' );
@@ -259,10 +236,7 @@
260237 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
261238 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
262239 if( $t ) {
263 - $ns = $s->page_namespace;
264 - $prefix = $invert ? $namespaces[$ns] : '';
265 - $prefix .= $invert && $namespaces[$ns] != $wgContLang->getNsText(NS_MAIN) ? ':' : '';
266 - $link = $sk->makeKnownLinkObj( $t, $t->getText(), false, false, $prefix );
 240+ $link = $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false );
267241 } else {
268242 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
269243 }
@@ -283,7 +257,7 @@
284258 if ( $including ) {
285259 $out2 = '';
286260 } else {
287 - $nsForm = indexNamespaceForm ( $namespace, $from, $invert );
 261+ $nsForm = indexNamespaceForm ( $namespace, $from );
288262 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
289263 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
290264 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
@@ -291,11 +265,10 @@
292266 wfMsg ( 'allpages' ) );
293267 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
294268 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
295 - $invertparam = $invert ? "&invert=$invert" : '';
296269 $out2 .= " | " . $sk->makeKnownLink(
297270 $wgContLang->specialPage( "Allpages" ),
298271 wfMsg ( 'nextpage', $s->page_title ),
299 - "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam . $invertparam );
 272+ "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam );
300273 }
301274 $out2 .= "</td></tr></table><hr />";
302275 }
Index: trunk/phase3/RELEASE-NOTES
@@ -580,7 +580,10 @@
581581 * (bug 2866) Revert experimental, non-cross-platform sortable table hack
582582 * PHP 4.1.2 compatibility fix: define floatval() equivalent if missing
583583 * (bug 2901) Number format for Catalan
 584+* Special:Allpages performance hacks: index memcached caching, removed
 585+ inverse checkbox, use friendlier relative offsets in index build
584586
 587+
585588 === Caveats ===
586589
587590 Some output, particularly involving user-supplied inline HTML, may not

Status & tagging log