MediaWiki  1.28.0
SpecialAllPages.php
Go to the documentation of this file.
1 <?php
31 
37  protected $maxPerPage = 345;
38 
44  protected $nsfromMsg = 'allpagesfrom';
45 
51  function __construct( $name = 'Allpages' ) {
52  parent::__construct( $name );
53  }
54 
60  function execute( $par ) {
61  $request = $this->getRequest();
62  $out = $this->getOutput();
63 
64  $this->setHeaders();
65  $this->outputHeader();
66  $out->allowClickjacking();
67 
68  # GET values
69  $from = $request->getVal( 'from', null );
70  $to = $request->getVal( 'to', null );
71  $namespace = $request->getInt( 'namespace' );
72  $hideredirects = $request->getBool( 'hideredirects', false );
73 
74  $namespaces = $this->getLanguage()->getNamespaces();
75 
76  $out->setPageTitle(
77  ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
78  $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
79  $this->msg( 'allarticles' )
80  );
81  $out->addModuleStyles( 'mediawiki.special' );
82 
83  if ( $par !== null ) {
84  $this->showChunk( $namespace, $par, $to, $hideredirects );
85  } elseif ( $from !== null && $to === null ) {
86  $this->showChunk( $namespace, $from, $to, $hideredirects );
87  } else {
88  $this->showToplevel( $namespace, $from, $to, $hideredirects );
89  }
90  }
91 
100  protected function outputHTMLForm( $namespace = NS_MAIN,
101  $from = '', $to = '', $hideRedirects = false
102  ) {
103  $fields = [
104  'from' => [
105  'type' => 'text',
106  'name' => 'from',
107  'id' => 'nsfrom',
108  'size' => 30,
109  'label-message' => 'allpagesfrom',
110  'default' => str_replace( '_', ' ', $from ),
111  ],
112  'to' => [
113  'type' => 'text',
114  'name' => 'to',
115  'id' => 'nsto',
116  'size' => 30,
117  'label-message' => 'allpagesto',
118  'default' => str_replace( '_', ' ', $to ),
119  ],
120  'namespace' => [
121  'type' => 'namespaceselect',
122  'name' => 'namespace',
123  'id' => 'namespace',
124  'label-message' => 'namespace',
125  'all' => null,
126  'value' => $namespace,
127  ],
128  'hideredirects' => [
129  'type' => 'check',
130  'name' => 'hideredirects',
131  'id' => 'hidredirects',
132  'label-message' => 'allpages-hide-redirects',
133  'value' => $hideRedirects,
134  ],
135  ];
136  $form = HTMLForm::factory( 'table', $fields, $this->getContext() );
137  $form->setMethod( 'get' )
138  ->setWrapperLegendMsg( 'allpages' )
139  ->setSubmitTextMsg( 'allpagessubmit' )
140  ->prepareForm()
141  ->displayForm( false );
142  }
143 
150  function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
151  $from = Title::makeTitleSafe( $namespace, $from );
152  $to = Title::makeTitleSafe( $namespace, $to );
153  $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
154  $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
155 
156  $this->showChunk( $namespace, $from, $to, $hideredirects );
157  }
158 
165  function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
166  $output = $this->getOutput();
167 
168  $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
169  $toList = $this->getNamespaceKeyAndText( $namespace, $to );
170  $namespaces = $this->getContext()->getLanguage()->getNamespaces();
171  $n = 0;
172  $prevTitle = null;
173 
174  if ( !$fromList || !$toList ) {
175  $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
176  } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
177  // Show errormessage and reset to NS_MAIN
178  $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
179  $namespace = NS_MAIN;
180  } else {
181  list( $namespace, $fromKey, $from ) = $fromList;
182  list( , $toKey, $to ) = $toList;
183 
184  $dbr = wfGetDB( DB_REPLICA );
185  $filterConds = [ 'page_namespace' => $namespace ];
186  if ( $hideredirects ) {
187  $filterConds['page_is_redirect'] = 0;
188  }
189 
190  $conds = $filterConds;
191  $conds[] = 'page_title >= ' . $dbr->addQuotes( $fromKey );
192  if ( $toKey !== "" ) {
193  $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
194  }
195 
196  $res = $dbr->select( 'page',
197  [ 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ],
198  $conds,
199  __METHOD__,
200  [
201  'ORDER BY' => 'page_title',
202  'LIMIT' => $this->maxPerPage + 1,
203  'USE INDEX' => 'name_title',
204  ]
205  );
206 
207  if ( $res->numRows() > 0 ) {
208  $out = Html::openElement( 'ul', [ 'class' => 'mw-allpages-chunk' ] );
209 
210  while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
211  $t = Title::newFromRow( $s );
212  if ( $t ) {
213  $out .= '<li' .
214  ( $s->page_is_redirect ? ' class="allpagesredirect"' : '' ) .
215  '>' .
216  Linker::link( $t ) .
217  "</li>\n";
218  } else {
219  $out .= '<li>[[' . htmlspecialchars( $s->page_title ) . "]]</li>\n";
220  }
221  $n++;
222  }
223  $out .= Html::closeElement( 'ul' );
224 
225  if ( $res->numRows() > 2 ) {
226  // Only apply CSS column styles if there's more than 2 entries.
227  // Otherwise, rendering is broken as "mw-allpages-body"'s CSS column count is 3.
228  $out = Html::rawElement( 'div', [ 'class' => 'mw-allpages-body' ], $out );
229  }
230  } else {
231  $out = '';
232  }
233 
234  if ( $fromKey !== '' && !$this->including() ) {
235  # Get the first title from previous chunk
236  $prevConds = $filterConds;
237  $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
238  $prevKey = $dbr->selectField(
239  'page',
240  'page_title',
241  $prevConds,
242  __METHOD__,
243  [ 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage - 1 ]
244  );
245 
246  if ( $prevKey === false ) {
247  # The previous chunk is not complete, need to link to the very first title
248  # available in the database
249  $prevKey = $dbr->selectField(
250  'page',
251  'page_title',
252  $prevConds,
253  __METHOD__,
254  [ 'ORDER BY' => 'page_title' ]
255  );
256  }
257 
258  if ( $prevKey !== false ) {
259  $prevTitle = Title::makeTitle( $namespace, $prevKey );
260  }
261  }
262  }
263 
264  if ( $this->including() ) {
265  $output->addHTML( $out );
266  return;
267  }
268 
269  $navLinks = [];
270  $self = $this->getPageTitle();
271 
272  // Generate a "previous page" link if needed
273  if ( $prevTitle ) {
274  $query = [ 'from' => $prevTitle->getText() ];
275 
276  if ( $namespace ) {
277  $query['namespace'] = $namespace;
278  }
279 
280  if ( $hideredirects ) {
281  $query['hideredirects'] = $hideredirects;
282  }
283 
284  $navLinks[] = Linker::linkKnown(
285  $self,
286  $this->msg( 'prevpage', $prevTitle->getText() )->escaped(),
287  [],
288  $query
289  );
290 
291  }
292 
293  // Generate a "next page" link if needed
294  if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
295  # $s is the first link of the next chunk
296  $t = Title::makeTitle( $namespace, $s->page_title );
297  $query = [ 'from' => $t->getText() ];
298 
299  if ( $namespace ) {
300  $query['namespace'] = $namespace;
301  }
302 
303  if ( $hideredirects ) {
304  $query['hideredirects'] = $hideredirects;
305  }
306 
307  $navLinks[] = Linker::linkKnown(
308  $self,
309  $this->msg( 'nextpage', $t->getText() )->escaped(),
310  [],
311  $query
312  );
313  }
314 
315  $this->outputHTMLForm( $namespace, $from, $to, $hideredirects );
316 
317  if ( count( $navLinks ) ) {
318  // Add pagination links
319  $pagination = Html::rawElement( 'div',
320  [ 'class' => 'mw-allpages-nav' ],
321  $this->getLanguage()->pipeList( $navLinks )
322  );
323 
324  $output->addHTML( $pagination );
325  $out .= Html::element( 'hr' ) . $pagination; // Footer
326  }
327 
328  $output->addHTML( $out );
329  }
330 
336  protected function getNamespaceKeyAndText( $ns, $text ) {
337  if ( $text == '' ) {
338  # shortcut for common case
339  return [ $ns, '', '' ];
340  }
341 
342  $t = Title::makeTitleSafe( $ns, $text );
343  if ( $t && $t->isLocal() ) {
344  return [ $t->getNamespace(), $t->getDBkey(), $t->getText() ];
345  } elseif ( $t ) {
346  return null;
347  }
348 
349  # try again, in case the problem was an empty pagename
350  $text = preg_replace( '/(#|$)/', 'X$1', $text );
351  $t = Title::makeTitleSafe( $ns, $text );
352  if ( $t && $t->isLocal() ) {
353  return [ $t->getNamespace(), '', '' ];
354  } else {
355  return null;
356  }
357  }
358 
367  public function prefixSearchSubpages( $search, $limit, $offset ) {
368  return $this->prefixSearchString( $search, $limit, $offset );
369  }
370 
371  protected function getGroupName() {
372  return 'pages';
373  }
374 }
static newFromRow($row)
Make a Title object from a DB row.
Definition: Title.php:450
static closeElement($element)
Returns "".
Definition: Html.php:305
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:802
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1555
getContext()
Gets the context this SpecialPage is executed in.
const NS_MAIN
Definition: Defines.php:56
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:209
prefixSearchSubpages($search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Implements Special:Allpages.
static factory($displayFormat)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:275
getNamespaceKeyAndText($ns, $text)
msg()
Wrapper around wfMessage that sets the current context.
including($x=null)
Whether the special page is being evaluated via transclusion.
getOutput()
Get the OutputPage being used for this instance.
__construct($name= 'Allpages')
Constructor.
outputHeader($summaryMessageKey= '')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
static openElement($element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:247
Shortcut to construct an includable special page.
$self
$res
Definition: database.txt:21
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:535
static linkKnown($target, $html=null, $customAttribs=[], $query=[], $options=[ 'known'])
Identical to link(), except $options defaults to 'known'.
Definition: Linker.php:255
$maxPerPage
Maximum number of pages to show on single subpage.
showChunk($namespace=NS_MAIN, $from=false, $to=false, $hideredirects=false)
showToplevel($namespace=NS_MAIN, $from= '', $to= '', $hideredirects=false)
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:953
static link($target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition: Linker.php:203
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition: hooks.txt:1046
$from
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2573
execute($par)
Entry point : initialise variables and call subfunctions.
getLanguage()
Shortcut to get user's language.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition: hooks.txt:1046
$nsfromMsg
Determines, which message describes the input field 'nsfrom'.
const DB_REPLICA
Definition: defines.php:22
getRequest()
Get the WebRequest being used for this instance.
prefixSearchString($search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
static element($element, $attribs=[], $contents= '')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:229
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
outputHTMLForm($namespace=NS_MAIN, $from= '', $to= '', $hideRedirects=false)
Outputs the HTMLForm used on this page.
getPageTitle($subpage=false)
Get a self-referential title object.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300