Yurik has uploaded a new change for review.
https://gerrit.wikimedia.org/r/109853
Change subject: Search result as a category page parser function
......................................................................
Search result as a category page parser function
* Allow {{catquery:search terms}} parser function on
category pages to allow dynamic category pages. This
will allow category intersections or any other complex
querying to be shown as a category page.
* Added $join_conds parameter to the database's
selectField() function.
Change-Id: I968bcdc08360f09b3a7a2792130af3c2e05c7b43
---
M includes/CategoryViewer.php
M includes/MagicWord.php
M includes/db/Database.php
M includes/parser/CoreParserFunctions.php
M languages/messages/MessagesEn.php
5 files changed, 82 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/53/109853/1
diff --git a/includes/CategoryViewer.php b/includes/CategoryViewer.php
index 4027bef..e442ab9 100644
--- a/includes/CategoryViewer.php
+++ b/includes/CategoryViewer.php
@@ -283,7 +283,64 @@
);
$this->flip = array( 'page' => false, 'subcat' => false, 'file'
=> false );
- foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
+ // Check if this is a dynamic category (has 'catquery' property set)
+ $catquery = $dbr->selectField(
+ array( 'page', 'page_props' ),
+ 'pp_value',
+ array(
+ 'page_namespace' => NS_CATEGORY,
+ 'page_title' => $this->title->getDBkey(),
+ 'pp_propname' => 'catquery' ),
+ __METHOD__,
+ array(),
+ array( 'page_props' => array( 'INNER JOIN', 'pp_page = page_id' ) )
+ );
+
+ if ( $catquery === false ) {
+ // classical category, iterate over all types
+ $types = array( 'page', 'subcat', 'file' );
+ } else {
+ $types = array( 'subcat', 'file' );
+ // dynamically supply pages from the search result
+ if ( isset( $this->from['page'] ) && $this->from['page'] !== null
) {
+ $offset = intval( $this->from['page'] );
+ } elseif ( isset( $this->until['page'] ) && $this->until['page']
!== null ) {
+ $offset = intval( $this->until['page'] );
+ if ( $offset > $this->limit ) {
+ $offset -= $this->limit;
+ } else {
+ $offset = 0;
+ }
+ } else {
+ $offset = 0;
+ }
+ $api = new ApiMain(
+ new DerivativeRequest(
+ $this->getRequest(),
+ array(
+ 'action' => 'query',
+ 'list' => 'search',
+ 'srsearch' => $catquery,
+ 'srwhat' => 'text',
+ 'srprop' => 'size',
+ 'sroffset' => $offset,
+ 'srlimit' => $this->limit,
+ )
+ )
+ );
+ $api->execute();
+ $data = $api->getResult()->getData();
+ if ( isset( $data['query']['search'] ) ) {
+ foreach ( $data['query']['search'] as $res ) {
+ $t = Title::makeTitle( $res['ns'], $res['title'] );
+ $this->addPage( $t, $t->getCategorySortkey(), $res['size']
);
+ }
+ if ( isset( $data['query-continue']['search']['sroffset'] ) ) {
+ $this->nextPage['page'] =
$data['query-continue']['search']['sroffset'];
+ }
+ }
+ }
+ foreach ( $types as $type ) {
# Get the sortkeys for start/end, if applicable. Note
that if
# the collation in the database differs from the one
# set in $wgCategoryCollation, pagination might go
totally haywire.
diff --git a/includes/MagicWord.php b/includes/MagicWord.php
index 377d406..e0d4244 100644
--- a/includes/MagicWord.php
+++ b/includes/MagicWord.php
@@ -151,6 +151,7 @@
'numberofadmins',
'numberofviews',
'cascadingsources',
+ 'catquery',
);
/* Array of caching hints for ParserCache */
diff --git a/includes/db/Database.php b/includes/db/Database.php
index 90e658f..b8098d0 100644
--- a/includes/db/Database.php
+++ b/includes/db/Database.php
@@ -1254,19 +1254,20 @@
* @param string|array $cond The condition array. See
DatabaseBase::select() for details.
* @param string $fname The function name of the caller.
* @param string|array $options The query options. See
DatabaseBase::select() for details.
+ * @param string|array $join_conds The query join conditions. See
DatabaseBase::select() for details.
*
* @return bool|mixed The value from the field, or false on failure.
*/
public function selectField( $table, $var, $cond = '', $fname =
__METHOD__,
- $options = array()
- ) {
+ $options = array(), $join_conds = array()
+ ) {
if ( !is_array( $options ) ) {
$options = array( $options );
}
$options['LIMIT'] = 1;
- $res = $this->select( $table, $var, $cond, $fname, $options );
+ $res = $this->select( $table, $var, $cond, $fname, $options,
$join_conds );
if ( $res === false || !$this->numRows( $res ) ) {
return false;
diff --git a/includes/parser/CoreParserFunctions.php
b/includes/parser/CoreParserFunctions.php
index 3966b9e..2ef921e 100644
--- a/includes/parser/CoreParserFunctions.php
+++ b/includes/parser/CoreParserFunctions.php
@@ -54,7 +54,7 @@
'talkpagename', 'talkpagenamee', 'subjectpagename',
'subjectpagenamee', 'pageid', 'revisionid',
'revisionday',
'revisionday2', 'revisionmonth', 'revisionmonth1',
'revisionyear',
- 'revisiontimestamp', 'revisionuser', 'cascadingsources',
+ 'revisiontimestamp', 'revisionuser',
'cascadingsources', 'catquery',
);
foreach ( $noHashFunctions as $func ) {
$parser->setFunctionHook( $func, array( __CLASS__,
$func ), SFH_NO_HASH );
@@ -1174,4 +1174,21 @@
return '';
}
+ /**
+ * For category pages, sets property 'catquery' that will later be used
to render search results
+ * instead of the regular pages.
+ *
+ * @param Parser $parser
+ * @param string $query
+ *
+ * @return string
+ * @since 1.23
+ */
+ public static function catquery( $parser, $query = '' ) {
+ if ( $query && $parser->getTitle()->inNamespace( NS_CATEGORY ) ) {
+ $parser->getOutput()->setProperty( 'catquery', $query );
+ }
+ return '';
+ }
+
}
diff --git a/languages/messages/MessagesEn.php
b/languages/messages/MessagesEn.php
index 300792a..8aad462 100644
--- a/languages/messages/MessagesEn.php
+++ b/languages/messages/MessagesEn.php
@@ -374,6 +374,7 @@
'pagesincategory_pages' => array( 0, 'pages' ),
'pagesincategory_subcats' => array( 0, 'subcats' ),
'pagesincategory_files' => array( 0, 'files' ),
+ 'catquery' => array( 0, 'catquery' ),
);
/**
--
To view, visit https://gerrit.wikimedia.org/r/109853
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I968bcdc08360f09b3a7a2792130af3c2e05c7b43
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits