- if( isset($from) )
- $where[] = 'page_title >= '.$dbr->addQuotes( $from );
- if( isset($to) )
- $where[] = 'page_title <= '.$dbr->addQuotes( $to );
-
- global $wgMemc;
- $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
- $lines = $wgMemc->get( $key );
-
- $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
- $maxPerSubpage = intval($count/$this->maxLineCount);
- $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage);
-
- if( !is_array( $lines ) ) {
- $options = array( 'LIMIT' => 1 );
- $options['ORDER BY'] = 'page_title ASC';
- $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
- $lastTitle = $firstTitle;
- # This array is going to hold the page_titles in order.
- $lines = array( $firstTitle );
- # If we are going to show n rows, we need n+1 queries to find the relevant titles.
- $done = false;
- while( !$done ) {
- // Fetch the last title of this chunk and the first of the next
- $chunk = ( $lastTitle === false )
- ? array()
- : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
- $res = $dbr->select( 'page', /* FROM */
- 'page_title', /* WHAT */
- array_merge($where,$chunk),
- __METHOD__,
- array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
- );
-
- $s = $dbr->fetchObject( $res );
- if( $s ) {
- array_push( $lines, $s->page_title );
- } else {
- // Final chunk, but ended prematurely. Go back and find the end.
- $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
- array_merge($where,$chunk),
- __METHOD__ );
- array_push( $lines, $endTitle );
- $done = true;
- }
- $s = $res->fetchObject();
- if( $s ) {
- array_push( $lines, $s->page_title );
- $lastTitle = $s->page_title;
- } else {
- // This was a final chunk and ended exactly at the limit.
- // Rare but convenient!
- $done = true;
- }
- $res->free();
- }
- $wgMemc->add( $key, $lines, 3600 );
- }
-
- // If there are only two or less sections, don't even display them.
- // Instead, display the first section directly.
- if( count( $lines ) <= 2 ) {
- if( !empty($lines) ) {
- $this->showChunk( $namespace, $from, $to );
- } else {
- $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
- }
- return;
- }
-
- # At this point, $lines should contain an even number of elements.
- $out .= Xml::openElement( 'table', array( 'class' => 'allpageslist' ) );
- while( count ( $lines ) > 0 ) {
- $inpoint = array_shift( $lines );
- $outpoint = array_shift( $lines );
- $out .= $this->showline( $inpoint, $outpoint, $namespace );
- }
- $out .= Xml::closeElement( 'table' );
- $nsForm = $this->namespaceForm( $namespace, $from, $to );
-
- # Is there more?
- if( $this->including() ) {
- $out2 = '';
- } else {
- if( isset($from) || isset($to) ) {
- global $wgUser;
- $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ).
- '<tr>
- <td>' .
- $nsForm .
- '</td>
- <td class="mw-allpages-nav">' .
- $wgUser->getSkin()->link( $this->getTitle(), wfMsgHtml ( 'allpages' ),
- array(), array(), 'known' ) .
- "</td>
- </tr>" .
- Xml::closeElement( 'table' );
- } else {
- $out2 = $nsForm;
- }
- }
- $wgOut->addHTML( $out2 . $out );