Jump to content

Manual:Page props table: Difference between revisions

From mediawiki.org
Content deleted Content added
m {{Languages}} - delete old page name
add an extension example of using this table, since people seem to ask for it on the talk page. Not sure if this is best place for it.
Line 2: Line 2:
{{MW 1.13|and after}}
{{MW 1.13|and after}}


Contains properties about pages set by the parser via ParserOutput::setProperty(), such as the [[Manual:$wgAllowDisplayTitle|display title]] and the default category sortkey. In particular, all [[Help:Magic_words#Behavior_switches|double underscore magic words]] are automatically recorded here. Also, many [[Manual:Extensions|extensions]] use this table to store their own data. Note that reparsing a page causes all of its properties to be purged from this table and replaced with the new ones, so this table is ''not'' suitable for storing data that can't be regenerated during a reparse.
Contains properties about pages set by the parser via ParserOutput::setProperty(), such as the [[Manual:$wgAllowDisplayTitle|display title]] and the default category sortkey. In particular, all [[Help:Magic_words#Behavior_switches|double underscore magic words]] are automatically recorded here. Also, many [[Manual:Extensions|extensions]] use this table to store their own data. Note that reparsing a page causes all of its properties to be purged from this table and replaced with the new ones, so this table is ''not'' suitable for storing data that can't be regenerated during a reparse.




<pre>
<pre>
Line 37: Line 39:
13 rows in set (1.97 sec)
13 rows in set (1.97 sec)
</pre>
</pre>

==Example of a simple extension that uses this table==

Here is an example of an extension that uses the page_props table. It defines two parser tags (hooks) - &lt;getprop&gt; and &lt;setprop&gt;. <nowiki>A prop can be set by <setprop>Property to set</setprop>, and retrieved via <getprop/> (for the current page) or <getprop page="some page"/> to get it for some other page.</nowiki>
<source lang="php">
<?php
$wgHooks['ParserFirstCallInit'][] = 'wfSampleParserInit';

function wfSampleParserInit( Parser &$parser ) {
// This does <setprop>Some random text</setprop>
// And then <getprop/> to retrieve a prop
// Or <getprop page="somepage"> to retrieve for
// something other than the current page.

$parser->setHook( 'getprop', 'wfSampleGetProp' );
$parser->setHook( 'setprop', 'wfSampleSetProp' );
// Always return true from this function. The return value does not denote
// success or otherwise have meaning - it just must always be true.
return true;
}

function wfSampleSetProp( $input, array $args, Parser $parser, PPFrame $frame ) {
$parsed = $parser->recursiveTagParse( $input, $frame );
// Since this can span different parses, we need to take account of
// the fact recursiveTagParse only half parses the text. or strip tags
// (UNIQ's) will be exposed. (Alternative would be to just call
// $parser->replaceLinkHolders() and $parser->mStripState->unstripBoth()
// right here right now.
$serialized = serialize( $parser->serializeHalfParsedText( $parsed ) );
$parser->getOutput()->setProperty( 'SimpleSetPropExtension', $serialized );

// Note if other pages change based on a property, you should see $wgPagePropLinkInvalidations
// to automatically invalidate dependant page. In this example that would be pages that
// use <getprop page="something>. However that would require adding a linking table
// (since none of the standard ones work for this example) which is a bit beyond the
// scope of this simple example.

return '';
}
function wfSampleGetProp( $input, array $args, Parser $parser, PPFrame $frame ) {
$pageId = $parser->getTitle()->getArticleId();
if ( isset( $args['page'] ) ) {
$title = Title::newFromText( $args['page'] );
if ( !$title || $title->getArticleId() === 0 ) {
// In a real extension, this would be i18n-ized.
return '<span class="error">Invalid page ' . htmlspecialchars( $args['page'] ) . ' specified.</span>';
}
// Do for some page other then current one.
$dbr = wfGetDB( DB_SLAVE );
$propValue = $dbr->selectField( 'page_props', // table to use
'pp_value', // Field to select
array( 'pp_page' => $title->getArticleId(), 'pp_propname' => "SimpleSetPropExtension" ), // where conditions
__METHOD__
);
if ( $propValue === false ) {
// No prop stored for this page
// In a real extension, this would be i18n-ized.
return '<span class="error">No prop set for page ' . htmlspecialchars( $args['page'] ) . ' specified.</span>';
}
// We found the prop. Unserialize (First level of serialization)
$prop = unserialize( $propValue );

if ( !$parser->isValidHalfParsedText( $prop ) ) {
// Probably won't ever happen.
return '<span class="error">Error retrieving prop</span>';
} else {
// Everything should be good.
return $parser->unserializeHalfParsedText( $prop );
}
} else {
// Second case, current page.
// Can't query db, because could be set earlier in the page and not saved yet.
// So have to use the parserOutput object.

$prop = unserialize( $parser->getOutput()->getProperty( 'SimpleSetPropExtension' ) );

if ( !$parser->isValidHalfParsedText( $prop ) ) {
// Probably won't ever happen.
return '<span class="error">Error retrieving prop</span>';
} else {
// Everything should be good.
return $parser->unserializeHalfParsedText( $prop );
}
}
}
</source>

==See also==
*[[manual:$wgPagePropLinkInvalidations]]
*[[API:Properties#pageprops_.2F_pp]]




{{languages}}
{{languages}}

Revision as of 23:19, 11 February 2012

Manual:Contents MediaWiki database layout page_props table
MediaWiki version:
1.13

Contains properties about pages set by the parser via ParserOutput::setProperty(), such as the display title and the default category sortkey. In particular, all double underscore magic words are automatically recorded here. Also, many extensions use this table to store their own data. Note that reparsing a page causes all of its properties to be purged from this table and replaced with the new ones, so this table is not suitable for storing data that can't be regenerated during a reparse.


mysql> describe page_props;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| pp_page     | int(11)       | NO   | PRI | NULL    |       | 
| pp_propname | varbinary(60) | NO   | PRI | NULL    |       | 
| pp_value    | blob          | NO   |     | NULL    |       | 
+-------------+---------------+------+-----+---------+-------+
3 rows in set (0,00 sec)
mysql> SELECT DISTINCT pp_propname FROM page_props;
+------------------+
| pp_propname      |
+------------------+
| defaultsort      |
| notoc            |
| displaytitle     |
| nonewsectionlink |
| noindex          |
| forcetoc         |
| noeditsection    |
| newsectionlink   |
| notitleconvert   |
| nogallery        |
| index            |
| hiddencat        |
| staticredirect   |
+------------------+
13 rows in set (1.97 sec)

Example of a simple extension that uses this table

Here is an example of an extension that uses the page_props table. It defines two parser tags (hooks) - <getprop> and <setprop>. A prop can be set by <setprop>Property to set</setprop>, and retrieved via <getprop/> (for the current page) or <getprop page="some page"/> to get it for some other page.

<?php
 
$wgHooks['ParserFirstCallInit'][] = 'wfSampleParserInit';

function wfSampleParserInit( Parser &$parser ) {
        // This does <setprop>Some random text</setprop>
        // And then <getprop/> to retrieve a prop
        // Or <getprop page="somepage"> to retrieve for
        // something other than the current page.

        $parser->setHook( 'getprop', 'wfSampleGetProp' );
        $parser->setHook( 'setprop', 'wfSampleSetProp' );
        // Always return true from this function. The return value does not denote
        // success or otherwise have meaning - it just must always be true.
        return true;
}

function wfSampleSetProp( $input, array $args, Parser $parser, PPFrame $frame ) {
        $parsed = $parser->recursiveTagParse( $input, $frame );
        // Since this can span different parses, we need to take account of
        // the fact recursiveTagParse only half parses the text. or strip tags
        // (UNIQ's) will be exposed. (Alternative would be to just call
        // $parser->replaceLinkHolders() and $parser->mStripState->unstripBoth()
        // right here right now.
        $serialized = serialize( $parser->serializeHalfParsedText( $parsed ) );
        $parser->getOutput()->setProperty( 'SimpleSetPropExtension', $serialized );

        // Note if other pages change based on a property, you should see $wgPagePropLinkInvalidations
        // to automatically invalidate dependant page. In this example that would be pages that
        // use <getprop page="something>. However that would require adding a linking table
        // (since none of the standard ones work for this example) which is a bit beyond the
        // scope of this simple example.

        return '';
}
function wfSampleGetProp( $input, array $args, Parser $parser, PPFrame $frame ) {
        $pageId = $parser->getTitle()->getArticleId();
        if ( isset( $args['page'] ) ) {
              $title = Title::newFromText( $args['page'] );
              if ( !$title || $title->getArticleId() === 0 ) {
                          // In a real extension, this would be i18n-ized.
                          return '<span class="error">Invalid page ' . htmlspecialchars( $args['page'] ) . ' specified.</span>';
              }
              
              // Do for some page other then current one.
              $dbr = wfGetDB( DB_SLAVE );
              $propValue = $dbr->selectField( 'page_props', // table to use
                          'pp_value', // Field to select
                          array( 'pp_page' => $title->getArticleId(), 'pp_propname' => "SimpleSetPropExtension" ), // where conditions
                          __METHOD__
              );
              if ( $propValue === false ) {
                          // No prop stored for this page
                          // In a real extension, this would be i18n-ized.
                          return '<span class="error">No prop set for page ' . htmlspecialchars( $args['page'] ) . ' specified.</span>';
              }
              // We found the prop. Unserialize (First level of serialization)
              $prop = unserialize( $propValue );

              if ( !$parser->isValidHalfParsedText( $prop ) ) {
                          // Probably won't ever happen.
                          return '<span class="error">Error retrieving prop</span>';
              } else {
                          // Everything should be good.
                          return $parser->unserializeHalfParsedText( $prop );
              }
        } else {
              // Second case, current page.
              // Can't query db, because could be set earlier in the page and not saved yet.
              // So have to use the parserOutput object.

              $prop = unserialize( $parser->getOutput()->getProperty( 'SimpleSetPropExtension' ) );

              if ( !$parser->isValidHalfParsedText( $prop ) ) {
                          // Probably won't ever happen.
                          return '<span class="error">Error retrieving prop</span>';
              } else {
                          // Everything should be good.
                          return $parser->unserializeHalfParsedText( $prop );
              }
        }
}

See also